diff --git a/web/src/lib/db.svelte.ts b/web/src/lib/db.svelte.ts index 562e613..85aa363 100644 --- a/web/src/lib/db.svelte.ts +++ b/web/src/lib/db.svelte.ts @@ -104,7 +104,7 @@ function toServerData(name: CollectionName, item: AnyDoc): Record(name: K, data: PayloadMap[K]): Record { +function pick(name: K, data: Partial): Record { const out: Record = {}; for (const [f, v] of Object.entries(data)) { if (DATA_FIELDS[name].includes(f)) out[f] = v; @@ -538,14 +538,26 @@ export async function save(name: K, id: string | null, return rid; } +/** Applies a patch once the sync applied the server state it was waiting for, so the fields the patch leaves out keep a newer remote value. */ +async function merge(name: K, id: string, patch: Partial, deleted?: boolean): Promise { + if (!cols) throw new Error('database is not open'); + await waitForApply(name); + const c = cols?.[name]; + if (!c) throw new Error('database is not open'); + const prev = readOnce(c, { id }, (cursor) => cursor.fetch()[0]); + if (!prev) return; + await c.replaceOne({ id }, { + ...prev, + ...pick(name, patch), + client_updated_at: Math.max(Date.now(), prev.client_updated_at + 1), + deleted: deleted ?? prev.deleted + }); +} + export async function update(name: K, id: string, patch: Partial): Promise { - const cur = get(name, id); - if (!cur) return; - await save(name, id, { ...cur, ...patch } as PayloadMap[K], cur.deleted); + await merge(name, id, patch); } export async function remove(name: K, id: string): Promise { - const cur = get(name, id); - if (!cur) return; - await save(name, id, cur, true); + await merge(name, id, {}, true); } diff --git a/web/src/lib/db.test.ts b/web/src/lib/db.test.ts index e925197..0c57701 100644 --- a/web/src/lib/db.test.ts +++ b/web/src/lib/db.test.ts @@ -364,3 +364,31 @@ describe('session expiry', () => { expect(mocks.pushes).toHaveLength(0); }); }); + +describe('merge after apply', () => { + it('an update waiting for the sync keeps the remote fields the patch does not touch', async () => { + status.online = true; + const id = await save('entries', null, { start_time: 1000, end_time: 2000, auto_stopped: false, note: 'mine', day_key: '2026-01-01' }); + await syncNow(); + const server = mocks.server.get('entries'); + if (!server?.get(id)) throw new Error('server record missing'); + server.set(id, { ...server.get(id)!, start_time: 3000, updated: mocks.stamp() }); + const snapshots = Collection.getCollections().find((c) => c.name === 'wotra-snapshots'); + if (!snapshots) throw new Error('snapshots collection missing'); + let waiting: Promise | undefined; + const onFind = (selector: unknown) => { + if ((selector as { collectionName?: unknown } | null)?.collectionName !== 'entries' || waiting) return; + waiting = update('entries', id, { note: 'edited here' }); + }; + snapshots.on('find', onFind); + try { + await syncNow(); + } finally { + snapshots.off('find', onFind); + } + expect(waiting).toBeDefined(); + await waiting; + expect(get('entries', id)).toMatchObject({ start_time: 3000, end_time: 2000, note: 'edited here', auto_stopped: false }); + expect(await update('entries', 'missing', { note: 'ignored' })).toBeUndefined(); + }); +});