Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2023-10-27 - [Fix N+1 query fetching last session messages]
**Learning:** When retrieving the latest associated row (e.g., max index) for a list of parent IDs in Drizzle, avoid N+1 queries with loops using `.limit(1)`. This causes significant latency. Instead, use a batched approach by first querying `max(index)` grouped by the parent ID in a subquery, and then joining that subquery back to the main table. Be aware that simple syntax strings like `$1` might conflict with variables in tools like esbuild during testing but not Drizzle syntax when done correctly.
**Action:** Always batch aggregate child queries when enriching list-type responses by utilizing SQL `max()` or equivalent within a subquery joined on the original table to minimize round-trips.
## 2026-05-17 - [Drizzle Subquery Issue]
**Learning:** Adding grouping logic inside batched queries may be necessary to resolve the N+1 issue securely in our wrapper. But it is important to remember our Drizzle instance uses `database-wrapper.ts` to abstract differences in db dialects between Postgres/Sqlite. Because of this, certain Drizzle functionalities that may be used in the codebase but missing from the generic `DrizzleQuery` interface (e.g., `.groupBy()` or `.as()`) could cause TypeScript or runtime failures if not passed through inside `wrapQuery()`.
**Action:** Always check `wrapQuery` in `packages/core/src/db/database-wrapper.ts` to ensure any new ORM method used (e.g. `groupBy`, `as`) is explicitly exposed, maintaining compatibility across the monorepo test runners and execution paths.
4 changes: 4 additions & 0 deletions packages/core/src/db/database-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ function wrapQuery(query: DrizzleQuery, db: Database): any {
innerJoin: (...args: unknown[]) => wrapQuery((query as any).innerJoin(...args), db),
// biome-ignore lint/suspicious/noExplicitAny: These methods accept varying argument types from Drizzle
leftJoin: (...args: unknown[]) => wrapQuery((query as any).leftJoin(...args), db),
// biome-ignore lint/suspicious/noExplicitAny: These methods accept varying argument types from Drizzle
groupBy: (...args: unknown[]) => wrapQuery((query as any).groupBy(...args), db),
// biome-ignore lint/suspicious/noExplicitAny: These methods accept varying argument types from Drizzle
as: (...args: unknown[]) => wrapQuery((query as any).as(...args), db),
onConflictDoNothing: (...args: unknown[]) =>
// biome-ignore lint/suspicious/noExplicitAny: Drizzle exposes onConflictDoNothing on both dialects after .values()
wrapQuery((query as any).onConflictDoNothing(...args), db),
Expand Down