perf(database): hoist prepared statement out of favorites reorder loop

The inner loop in reorderGlobalFavorites() rebuilt the same
update().set().where() AST per row — up to thousands of times for large
favorite lists. Hoist a prepared statement using sql.placeholder() so
Drizzle generates the SQL string once and SQLite caches the parsed plan.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Entire-Checkpoint: 3d6901072049
This commit is contained in:
4grayandClaude Opus 4.7 committed 2026-05-01 21:15:03 +02:00
1 parent 9ecf4d44d3
commit 313230ab16
1 file changed
+11 -5
@@ -168,15 +168,21 @@ export async function reorderGlobalFavorites(
let current = 0;
const total = updates.length;
const updateFavoritePosition = db
.update(schema.favorites)
.set({ position: sql.placeholder('position') })
.where(eq(schema.favorites.contentId, sql.placeholder('contentId')))
.prepare();
for (const chunk of chunkValues(updates, DEFAULT_BATCH_SIZE)) {
await checkpointOperation(control);
await db.transaction(async (tx) => {
await db.transaction(async () => {
for (const { content_id, position } of chunk) {
await tx
.update(schema.favorites)
.set({ position })
.where(eq(schema.favorites.contentId, content_id));
await updateFavoritePosition.execute({
position,
contentId: content_id,
});
}
});