Skip to content
5 changes: 5 additions & 0 deletions .changeset/five-turtles-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/powersync-db-collection': patch
---

Added checks to not flush records when a `diffTrigger` isn't setup yet for `on-demand` mode. This fixes a non-critical error that was logged on startup.
19 changes: 13 additions & 6 deletions packages/powersync-db-collection/src/powersync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,14 @@ export function powerSyncCollectionOptions<
onUnloadSubset = await restConfig.onLoadSubset?.(options)
}

// Nothing to flush or dispose if no tracking table has been created yet.
if (activeWhereExpressions.length === 0) {
await database.writeLock(async (ctx) => {
await flushDiffRecordsWithContext(ctx)
await disposeTracking?.({ context: ctx })
})
if (disposeTracking) {
await database.writeLock(async (ctx) => {
await flushDiffRecordsWithContext(ctx)
await disposeTracking?.({ context: ctx })
})
}
return
}

Expand Down Expand Up @@ -563,8 +566,12 @@ export function powerSyncCollectionOptions<
const viewWhereClause = toInlinedWhereClause(compiledView)

await database.writeLock(async (ctx) => {
await flushDiffRecordsWithContext(ctx)
await disposeTracking?.({ context: ctx })
// On the first loadSubset there is no tracking table yet, so there
// is nothing to flush or dispose.
if (disposeTracking) {
await flushDiffRecordsWithContext(ctx)
await disposeTracking({ context: ctx })
}

disposeTracking = await createDiffTrigger({
setupContext: ctx,
Expand Down