@terreno/syncdb 56.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +419 -0
- package/dist/auth/betterAuthAdapter.d.ts +22 -0
- package/dist/auth/betterAuthAdapter.d.ts.map +1 -0
- package/dist/auth/betterAuthAdapter.js +181 -0
- package/dist/auth/betterAuthAdapter.js.map +1 -0
- package/dist/auth/betterAuthAdapter.test.d.ts +2 -0
- package/dist/auth/betterAuthAdapter.test.d.ts.map +1 -0
- package/dist/auth/betterAuthAdapter.test.js +350 -0
- package/dist/auth/betterAuthAdapter.test.js.map +1 -0
- package/dist/auth/types.d.ts +42 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +9 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/client.d.ts +246 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1415 -0
- package/dist/client.js.map +1 -0
- package/dist/client.test.d.ts +2 -0
- package/dist/client.test.d.ts.map +1 -0
- package/dist/client.test.js +2310 -0
- package/dist/client.test.js.map +1 -0
- package/dist/crypto/aesGcmCodec.d.ts +33 -0
- package/dist/crypto/aesGcmCodec.d.ts.map +1 -0
- package/dist/crypto/aesGcmCodec.js +80 -0
- package/dist/crypto/aesGcmCodec.js.map +1 -0
- package/dist/crypto/aesGcmCodec.test.d.ts +2 -0
- package/dist/crypto/aesGcmCodec.test.d.ts.map +1 -0
- package/dist/crypto/aesGcmCodec.test.js +83 -0
- package/dist/crypto/aesGcmCodec.test.js.map +1 -0
- package/dist/crypto/identityCodec.d.ts +7 -0
- package/dist/crypto/identityCodec.d.ts.map +1 -0
- package/dist/crypto/identityCodec.js +9 -0
- package/dist/crypto/identityCodec.js.map +1 -0
- package/dist/crypto/keyProviders.d.ts +34 -0
- package/dist/crypto/keyProviders.d.ts.map +1 -0
- package/dist/crypto/keyProviders.js +102 -0
- package/dist/crypto/keyProviders.js.map +1 -0
- package/dist/crypto/keyProviders.test.d.ts +2 -0
- package/dist/crypto/keyProviders.test.d.ts.map +1 -0
- package/dist/crypto/keyProviders.test.js +189 -0
- package/dist/crypto/keyProviders.test.js.map +1 -0
- package/dist/crypto/types.d.ts +24 -0
- package/dist/crypto/types.d.ts.map +1 -0
- package/dist/crypto/types.js +8 -0
- package/dist/crypto/types.js.map +1 -0
- package/dist/debug/debugChannel.d.ts +52 -0
- package/dist/debug/debugChannel.d.ts.map +1 -0
- package/dist/debug/debugChannel.js +73 -0
- package/dist/debug/debugChannel.js.map +1 -0
- package/dist/debug/debugChannel.test.d.ts +2 -0
- package/dist/debug/debugChannel.test.d.ts.map +1 -0
- package/dist/debug/debugChannel.test.js +105 -0
- package/dist/debug/debugChannel.test.js.map +1 -0
- package/dist/debug/debugLog.d.ts +111 -0
- package/dist/debug/debugLog.d.ts.map +1 -0
- package/dist/debug/debugLog.js +150 -0
- package/dist/debug/debugLog.js.map +1 -0
- package/dist/debug/debugLog.test.d.ts +2 -0
- package/dist/debug/debugLog.test.d.ts.map +1 -0
- package/dist/debug/debugLog.test.js +121 -0
- package/dist/debug/debugLog.test.js.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.d.ts +2 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.d.ts.map +1 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.js +38 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.js.map +1 -0
- package/dist/mutations/conflicts.d.ts +31 -0
- package/dist/mutations/conflicts.d.ts.map +1 -0
- package/dist/mutations/conflicts.js +73 -0
- package/dist/mutations/conflicts.js.map +1 -0
- package/dist/mutations/outbox.d.ts +174 -0
- package/dist/mutations/outbox.d.ts.map +1 -0
- package/dist/mutations/outbox.js +385 -0
- package/dist/mutations/outbox.js.map +1 -0
- package/dist/mutations/outbox.test.d.ts +2 -0
- package/dist/mutations/outbox.test.d.ts.map +1 -0
- package/dist/mutations/outbox.test.js +643 -0
- package/dist/mutations/outbox.test.js.map +1 -0
- package/dist/mutations/resolveConflict.d.ts +32 -0
- package/dist/mutations/resolveConflict.d.ts.map +1 -0
- package/dist/mutations/resolveConflict.js +101 -0
- package/dist/mutations/resolveConflict.js.map +1 -0
- package/dist/mutations/resolveConflict.test.d.ts +2 -0
- package/dist/mutations/resolveConflict.test.d.ts.map +1 -0
- package/dist/mutations/resolveConflict.test.js +282 -0
- package/dist/mutations/resolveConflict.test.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.d.ts +9 -0
- package/dist/persisters/defaultPersisterFactory.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.js +9 -0
- package/dist/persisters/defaultPersisterFactory.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.native.d.ts +10 -0
- package/dist/persisters/defaultPersisterFactory.native.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.native.js +44 -0
- package/dist/persisters/defaultPersisterFactory.native.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.test.d.ts +2 -0
- package/dist/persisters/defaultPersisterFactory.test.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.test.js +177 -0
- package/dist/persisters/defaultPersisterFactory.test.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.web.d.ts +17 -0
- package/dist/persisters/defaultPersisterFactory.web.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.web.js +65 -0
- package/dist/persisters/defaultPersisterFactory.web.js.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.d.ts +46 -0
- package/dist/persisters/encryptedIndexedDbPersister.d.ts.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.js +171 -0
- package/dist/persisters/encryptedIndexedDbPersister.js.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.d.ts +2 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.d.ts.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.js +352 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.js.map +1 -0
- package/dist/persisters/memoryPersister.d.ts +19 -0
- package/dist/persisters/memoryPersister.d.ts.map +1 -0
- package/dist/persisters/memoryPersister.js +31 -0
- package/dist/persisters/memoryPersister.js.map +1 -0
- package/dist/persisters/memoryPersister.test.d.ts +2 -0
- package/dist/persisters/memoryPersister.test.d.ts.map +1 -0
- package/dist/persisters/memoryPersister.test.js +113 -0
- package/dist/persisters/memoryPersister.test.js.map +1 -0
- package/dist/persisters/types.d.ts +65 -0
- package/dist/persisters/types.d.ts.map +1 -0
- package/dist/persisters/types.js +2 -0
- package/dist/persisters/types.js.map +1 -0
- package/dist/react/hooks.d.ts +132 -0
- package/dist/react/hooks.d.ts.map +1 -0
- package/dist/react/hooks.js +269 -0
- package/dist/react/hooks.js.map +1 -0
- package/dist/react/hooks.test.d.ts +2 -0
- package/dist/react/hooks.test.d.ts.map +1 -0
- package/dist/react/hooks.test.js +645 -0
- package/dist/react/hooks.test.js.map +1 -0
- package/dist/react/index.d.ts +11 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +11 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/provider.d.ts +11 -0
- package/dist/react/provider.d.ts.map +1 -0
- package/dist/react/provider.js +17 -0
- package/dist/react/provider.js.map +1 -0
- package/dist/storage/idb.d.ts +27 -0
- package/dist/storage/idb.d.ts.map +1 -0
- package/dist/storage/idb.js +72 -0
- package/dist/storage/idb.js.map +1 -0
- package/dist/storage/idb.test.d.ts +2 -0
- package/dist/storage/idb.test.d.ts.map +1 -0
- package/dist/storage/idb.test.js +100 -0
- package/dist/storage/idb.test.js.map +1 -0
- package/dist/storage/purgeUnknownStream.test.d.ts +2 -0
- package/dist/storage/purgeUnknownStream.test.d.ts.map +1 -0
- package/dist/storage/purgeUnknownStream.test.js +64 -0
- package/dist/storage/purgeUnknownStream.test.js.map +1 -0
- package/dist/storage/schema.d.ts +33 -0
- package/dist/storage/schema.d.ts.map +1 -0
- package/dist/storage/schema.js +92 -0
- package/dist/storage/schema.js.map +1 -0
- package/dist/storage/store.d.ts +136 -0
- package/dist/storage/store.d.ts.map +1 -0
- package/dist/storage/store.js +275 -0
- package/dist/storage/store.js.map +1 -0
- package/dist/storage/store.test.d.ts +2 -0
- package/dist/storage/store.test.d.ts.map +1 -0
- package/dist/storage/store.test.js +357 -0
- package/dist/storage/store.test.js.map +1 -0
- package/dist/storage/types.d.ts +146 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/storage/types.js +32 -0
- package/dist/storage/types.js.map +1 -0
- package/dist/storage/wipe.d.ts +22 -0
- package/dist/storage/wipe.d.ts.map +1 -0
- package/dist/storage/wipe.js +43 -0
- package/dist/storage/wipe.js.map +1 -0
- package/dist/storage/wipe.test.d.ts +2 -0
- package/dist/storage/wipe.test.d.ts.map +1 -0
- package/dist/storage/wipe.test.js +95 -0
- package/dist/storage/wipe.test.js.map +1 -0
- package/dist/sync/bootstrap.d.ts +64 -0
- package/dist/sync/bootstrap.d.ts.map +1 -0
- package/dist/sync/bootstrap.js +160 -0
- package/dist/sync/bootstrap.js.map +1 -0
- package/dist/sync/bootstrap.test.d.ts +2 -0
- package/dist/sync/bootstrap.test.d.ts.map +1 -0
- package/dist/sync/bootstrap.test.js +447 -0
- package/dist/sync/bootstrap.test.js.map +1 -0
- package/dist/sync/cursor.d.ts +67 -0
- package/dist/sync/cursor.d.ts.map +1 -0
- package/dist/sync/cursor.js +74 -0
- package/dist/sync/cursor.js.map +1 -0
- package/dist/sync/cursor.test.d.ts +2 -0
- package/dist/sync/cursor.test.d.ts.map +1 -0
- package/dist/sync/cursor.test.js +73 -0
- package/dist/sync/cursor.test.js.map +1 -0
- package/dist/sync/deltaApplier.d.ts +30 -0
- package/dist/sync/deltaApplier.d.ts.map +1 -0
- package/dist/sync/deltaApplier.js +61 -0
- package/dist/sync/deltaApplier.js.map +1 -0
- package/dist/sync/deltaApplier.test.d.ts +2 -0
- package/dist/sync/deltaApplier.test.d.ts.map +1 -0
- package/dist/sync/deltaApplier.test.js +212 -0
- package/dist/sync/deltaApplier.test.js.map +1 -0
- package/dist/sync/entityRepair.d.ts +45 -0
- package/dist/sync/entityRepair.d.ts.map +1 -0
- package/dist/sync/entityRepair.js +91 -0
- package/dist/sync/entityRepair.js.map +1 -0
- package/dist/sync/entityRepair.test.d.ts +2 -0
- package/dist/sync/entityRepair.test.d.ts.map +1 -0
- package/dist/sync/entityRepair.test.js +146 -0
- package/dist/sync/entityRepair.test.js.map +1 -0
- package/dist/sync/httpChannel.d.ts +74 -0
- package/dist/sync/httpChannel.d.ts.map +1 -0
- package/dist/sync/httpChannel.js +123 -0
- package/dist/sync/httpChannel.js.map +1 -0
- package/dist/sync/httpChannel.test.d.ts +2 -0
- package/dist/sync/httpChannel.test.d.ts.map +1 -0
- package/dist/sync/httpChannel.test.js +215 -0
- package/dist/sync/httpChannel.test.js.map +1 -0
- package/dist/sync/replayCoordinator.d.ts +212 -0
- package/dist/sync/replayCoordinator.d.ts.map +1 -0
- package/dist/sync/replayCoordinator.js +1110 -0
- package/dist/sync/replayCoordinator.js.map +1 -0
- package/dist/sync/replayCoordinator.test.d.ts +2 -0
- package/dist/sync/replayCoordinator.test.d.ts.map +1 -0
- package/dist/sync/replayCoordinator.test.js +1496 -0
- package/dist/sync/replayCoordinator.test.js.map +1 -0
- package/dist/sync/socketTransport.d.ts +56 -0
- package/dist/sync/socketTransport.d.ts.map +1 -0
- package/dist/sync/socketTransport.js +292 -0
- package/dist/sync/socketTransport.js.map +1 -0
- package/dist/sync/socketTransport.test.d.ts +2 -0
- package/dist/sync/socketTransport.test.d.ts.map +1 -0
- package/dist/sync/socketTransport.test.js +346 -0
- package/dist/sync/socketTransport.test.js.map +1 -0
- package/dist/sync/transport.d.ts +80 -0
- package/dist/sync/transport.d.ts.map +1 -0
- package/dist/sync/transport.js +5 -0
- package/dist/sync/transport.js.map +1 -0
- package/dist/testing/fakeTransport.d.ts +55 -0
- package/dist/testing/fakeTransport.d.ts.map +1 -0
- package/dist/testing/fakeTransport.js +139 -0
- package/dist/testing/fakeTransport.js.map +1 -0
- package/dist/testing/fakeTransport.test.d.ts +2 -0
- package/dist/testing/fakeTransport.test.d.ts.map +1 -0
- package/dist/testing/fakeTransport.test.js +112 -0
- package/dist/testing/fakeTransport.test.js.map +1 -0
- package/dist/testing/index.d.ts +13 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +13 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/types.d.ts +271 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +71 -0
- package/src/auth/betterAuthAdapter.test.ts +455 -0
- package/src/auth/betterAuthAdapter.ts +214 -0
- package/src/auth/types.ts +49 -0
- package/src/client.test.ts +2670 -0
- package/src/client.ts +1748 -0
- package/src/crypto/aesGcmCodec.test.ts +103 -0
- package/src/crypto/aesGcmCodec.ts +98 -0
- package/src/crypto/identityCodec.ts +10 -0
- package/src/crypto/keyProviders.test.ts +228 -0
- package/src/crypto/keyProviders.ts +153 -0
- package/src/crypto/types.ts +23 -0
- package/src/debug/debugChannel.test.ts +121 -0
- package/src/debug/debugChannel.ts +112 -0
- package/src/debug/debugLog.test.ts +139 -0
- package/src/debug/debugLog.ts +272 -0
- package/src/index.ts +157 -0
- package/src/isolated/defaultPersisterFactoryNative.isolated.ts +45 -0
- package/src/mutations/conflicts.ts +108 -0
- package/src/mutations/outbox.test.ts +754 -0
- package/src/mutations/outbox.ts +585 -0
- package/src/mutations/resolveConflict.test.ts +338 -0
- package/src/mutations/resolveConflict.ts +115 -0
- package/src/persisters/defaultPersisterFactory.native.ts +53 -0
- package/src/persisters/defaultPersisterFactory.test.ts +207 -0
- package/src/persisters/defaultPersisterFactory.ts +12 -0
- package/src/persisters/defaultPersisterFactory.web.ts +78 -0
- package/src/persisters/encryptedIndexedDbPersister.test.ts +381 -0
- package/src/persisters/encryptedIndexedDbPersister.ts +218 -0
- package/src/persisters/memoryPersister.test.ts +136 -0
- package/src/persisters/memoryPersister.ts +52 -0
- package/src/persisters/types.ts +71 -0
- package/src/react/hooks.test.tsx +761 -0
- package/src/react/hooks.ts +409 -0
- package/src/react/index.ts +11 -0
- package/src/react/provider.tsx +27 -0
- package/src/storage/idb.test.ts +116 -0
- package/src/storage/idb.ts +112 -0
- package/src/storage/purgeUnknownStream.test.ts +74 -0
- package/src/storage/schema.ts +103 -0
- package/src/storage/store.test.ts +424 -0
- package/src/storage/store.ts +461 -0
- package/src/storage/types.ts +159 -0
- package/src/storage/wipe.test.ts +111 -0
- package/src/storage/wipe.ts +58 -0
- package/src/sync/bootstrap.test.ts +486 -0
- package/src/sync/bootstrap.ts +217 -0
- package/src/sync/cursor.test.ts +84 -0
- package/src/sync/cursor.ts +113 -0
- package/src/sync/deltaApplier.test.ts +237 -0
- package/src/sync/deltaApplier.ts +84 -0
- package/src/sync/entityRepair.test.ts +161 -0
- package/src/sync/entityRepair.ts +124 -0
- package/src/sync/httpChannel.test.ts +265 -0
- package/src/sync/httpChannel.ts +220 -0
- package/src/sync/replayCoordinator.test.ts +1709 -0
- package/src/sync/replayCoordinator.ts +1370 -0
- package/src/sync/socketTransport.test.ts +413 -0
- package/src/sync/socketTransport.ts +379 -0
- package/src/sync/transport.ts +76 -0
- package/src/testing/fakeTransport.test.ts +125 -0
- package/src/testing/fakeTransport.ts +206 -0
- package/src/testing/index.ts +18 -0
- package/src/types.ts +284 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {createSyncStore, type SyncStore} from "./store";
|
|
4
|
+
|
|
5
|
+
const STREAM = "todos|owner:u1";
|
|
6
|
+
|
|
7
|
+
const makeStore = (): SyncStore => createSyncStore({collections: ["todos"]});
|
|
8
|
+
|
|
9
|
+
describe("purgeUnknownStreamEntities", () => {
|
|
10
|
+
it("removes phantom rows that never synced and have no pending mutation", () => {
|
|
11
|
+
const store = makeStore();
|
|
12
|
+
store.upsertEntity({
|
|
13
|
+
collection: "todos",
|
|
14
|
+
data: {title: "Phantom from a failed create"},
|
|
15
|
+
id: "phantom",
|
|
16
|
+
pendingMutationId: "",
|
|
17
|
+
seq: 0,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
expect(store.purgeUnknownStreamEntities({collection: "todos"})).toBe(1);
|
|
21
|
+
expect(store.getEntity({collection: "todos", id: "phantom"})).toBeUndefined();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("keeps rows still protected by a pending outbox mutation", () => {
|
|
25
|
+
const store = makeStore();
|
|
26
|
+
store.upsertEntity({
|
|
27
|
+
collection: "todos",
|
|
28
|
+
data: {title: "Queued local create"},
|
|
29
|
+
id: "queued",
|
|
30
|
+
pendingMutationId: "m1",
|
|
31
|
+
seq: 0,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(store.purgeUnknownStreamEntities({collection: "todos"})).toBe(0);
|
|
35
|
+
expect(store.getEntity({collection: "todos", id: "queued"})?.data).toEqual({
|
|
36
|
+
title: "Queued local create",
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("keeps server-synced rows that carry stream provenance", () => {
|
|
41
|
+
const store = makeStore();
|
|
42
|
+
store.upsertEntity({
|
|
43
|
+
collection: "todos",
|
|
44
|
+
data: {title: "From server"},
|
|
45
|
+
id: "synced",
|
|
46
|
+
pendingMutationId: "",
|
|
47
|
+
seq: 3,
|
|
48
|
+
stream: STREAM,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
expect(store.purgeUnknownStreamEntities({collection: "todos"})).toBe(0);
|
|
52
|
+
expect(store.getEntity({collection: "todos", id: "synced"})?.seq).toBe(3);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("clears any repair marker for a purged phantom", () => {
|
|
56
|
+
const store = makeStore();
|
|
57
|
+
store.upsertEntity({
|
|
58
|
+
collection: "todos",
|
|
59
|
+
data: {title: "Phantom"},
|
|
60
|
+
id: "phantom",
|
|
61
|
+
pendingMutationId: "",
|
|
62
|
+
seq: 0,
|
|
63
|
+
});
|
|
64
|
+
store.markNeedsRepair({
|
|
65
|
+
collection: "todos",
|
|
66
|
+
entityId: "phantom",
|
|
67
|
+
missedSeq: 2,
|
|
68
|
+
stream: STREAM,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
store.purgeUnknownStreamEntities({collection: "todos"});
|
|
72
|
+
expect(store.hasNeedsRepair({collection: "todos", entityId: "phantom"})).toBe(false);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type {CellSchema, TablesSchema, ValuesSchema} from "tinybase";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
CONFLICTS_TABLE,
|
|
5
|
+
CURSORS_TABLE,
|
|
6
|
+
KNOWN_STREAMS_TABLE,
|
|
7
|
+
NEEDS_REPAIR_TABLE,
|
|
8
|
+
OUTBOX_TABLE,
|
|
9
|
+
} from "./types";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Current local schema version. Bump when the table shapes change.
|
|
13
|
+
* v2 (C2): entity rows gained a `stream` column; added the `_knownStreams` table.
|
|
14
|
+
* v3: cursor rows gained `snapshotSeq` + `bootstrapped`. The bump is also the
|
|
15
|
+
* repair path for stores written by v2, where an interrupted bootstrap could
|
|
16
|
+
* leave the cursor at the stream head with only part of the snapshot applied —
|
|
17
|
+
* unreachable by any later reconcile, so a wipe + full re-bootstrap is the only
|
|
18
|
+
* way to make those stores whole.
|
|
19
|
+
*/
|
|
20
|
+
export const SYNC_SCHEMA_VERSION = 3;
|
|
21
|
+
|
|
22
|
+
const ENTITY_TABLE_SCHEMA: Record<string, CellSchema> = {
|
|
23
|
+
data: {type: "string"},
|
|
24
|
+
deleted: {type: "boolean"},
|
|
25
|
+
/** E5: stamped when a tombstone is first applied; "" otherwise. */
|
|
26
|
+
deletedAt: {type: "string"},
|
|
27
|
+
pendingMutationId: {type: "string"},
|
|
28
|
+
seq: {type: "number"},
|
|
29
|
+
stream: {type: "string"},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Build the TinyBase tables schema for a store configured with the given
|
|
34
|
+
* collections. All reserved tables are defined up front (even where operations
|
|
35
|
+
* land in later phases) so the persisted serialization format stays stable and
|
|
36
|
+
* avoids client-side schema migrations.
|
|
37
|
+
*
|
|
38
|
+
* No cell declares a `default`. A MergeableStore keeps a CRDT tombstone for
|
|
39
|
+
* every deleted row, and loading that tombstone back re-applies the schema:
|
|
40
|
+
* any cell with a default is re-created, resurrecting the deleted row as a
|
|
41
|
+
* husk holding only its defaults. That turned every resolved conflict into a
|
|
42
|
+
* phantom `_conflicts` row after a reload (`collection: ""`, which threw
|
|
43
|
+
* `Unknown collection ""` when resolved) and left ghost rows in `_outbox`,
|
|
44
|
+
* `_cursors`, and the entity tables. Writers always set every cell explicitly
|
|
45
|
+
* and readers already substitute their own fallbacks for an absent cell, so
|
|
46
|
+
* dropping the defaults costs nothing and keeps deleted rows deleted.
|
|
47
|
+
*/
|
|
48
|
+
export const buildTablesSchema = ({collections}: {collections: string[]}): TablesSchema => {
|
|
49
|
+
const schema: TablesSchema = {
|
|
50
|
+
[CONFLICTS_TABLE]: {
|
|
51
|
+
collection: {type: "string"},
|
|
52
|
+
dismissed: {type: "boolean"},
|
|
53
|
+
entityId: {type: "string"},
|
|
54
|
+
localData: {type: "string"},
|
|
55
|
+
serverData: {type: "string"},
|
|
56
|
+
/** Task 9.12: server side of the conflict is a tombstone. */
|
|
57
|
+
serverDeleted: {type: "boolean"},
|
|
58
|
+
serverSeq: {type: "number"},
|
|
59
|
+
},
|
|
60
|
+
[CURSORS_TABLE]: {
|
|
61
|
+
bootstrapped: {type: "boolean"},
|
|
62
|
+
seq: {type: "number"},
|
|
63
|
+
snapshotSeq: {type: "number"},
|
|
64
|
+
updatedAt: {type: "string"},
|
|
65
|
+
},
|
|
66
|
+
[KNOWN_STREAMS_TABLE]: {
|
|
67
|
+
addedAt: {type: "string"},
|
|
68
|
+
collection: {type: "string"},
|
|
69
|
+
},
|
|
70
|
+
[NEEDS_REPAIR_TABLE]: {
|
|
71
|
+
collection: {type: "string"},
|
|
72
|
+
entityId: {type: "string"},
|
|
73
|
+
markedAt: {type: "string"},
|
|
74
|
+
missedSeq: {type: "number"},
|
|
75
|
+
stream: {type: "string"},
|
|
76
|
+
},
|
|
77
|
+
[OUTBOX_TABLE]: {
|
|
78
|
+
args: {type: "string"},
|
|
79
|
+
attemptCount: {type: "number"},
|
|
80
|
+
baseVersion: {type: "number"},
|
|
81
|
+
collection: {type: "string"},
|
|
82
|
+
createdAt: {type: "string"},
|
|
83
|
+
enqueueOrder: {type: "number"},
|
|
84
|
+
entityId: {type: "string"},
|
|
85
|
+
errorNackCount: {type: "number"},
|
|
86
|
+
operation: {type: "string"},
|
|
87
|
+
status: {type: "string"},
|
|
88
|
+
userId: {type: "string"},
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
for (const collection of collections) {
|
|
92
|
+
schema[collection] = {...ENTITY_TABLE_SCHEMA};
|
|
93
|
+
}
|
|
94
|
+
return schema;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** Store-level values schema: schema version + last authenticated user. */
|
|
98
|
+
export const SYNC_VALUES_SCHEMA: ValuesSchema = {
|
|
99
|
+
lastUserId: {default: "", type: "string"},
|
|
100
|
+
/** O(1) FIFO ordering cell: the highest `enqueueOrder` handed out so far. */
|
|
101
|
+
outboxMaxEnqueueOrder: {default: 0, type: "number"},
|
|
102
|
+
schemaVersion: {default: SYNC_SCHEMA_VERSION, type: "number"},
|
|
103
|
+
};
|
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {createOutbox} from "../mutations/outbox";
|
|
4
|
+
import {createMemoryPersister} from "../persisters/memoryPersister";
|
|
5
|
+
import {SYNC_SCHEMA_VERSION} from "./schema";
|
|
6
|
+
import {createSyncStore, type SyncStore} from "./store";
|
|
7
|
+
import {CONFLICTS_TABLE, CURSORS_TABLE, OUTBOX_TABLE} from "./types";
|
|
8
|
+
|
|
9
|
+
interface Todo {
|
|
10
|
+
completed: boolean;
|
|
11
|
+
title: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const makeStore = () => createSyncStore({collections: ["todos", "notes"]});
|
|
15
|
+
|
|
16
|
+
describe("createSyncStore", () => {
|
|
17
|
+
it("throws when a collection name starts with the reserved prefix", () => {
|
|
18
|
+
expect(() => createSyncStore({collections: ["_outbox"]})).toThrow(/must not start with "_"/);
|
|
19
|
+
expect(() => createSyncStore({collections: ["todos", "_shadow"]})).toThrow(/_shadow/);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("throws on duplicate collection names", () => {
|
|
23
|
+
expect(() => createSyncStore({collections: ["todos", "todos"]})).toThrow(/Duplicate/);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("exposes the configured collections and a raw MergeableStore", () => {
|
|
27
|
+
const store = makeStore();
|
|
28
|
+
expect(store.collections).toEqual(["todos", "notes"]);
|
|
29
|
+
expect(typeof store.raw.getMergeableContent).toBe("function");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("initializes schemaVersion and an empty lastUserId", () => {
|
|
33
|
+
const store = makeStore();
|
|
34
|
+
expect(store.getSchemaVersion()).toBe(SYNC_SCHEMA_VERSION);
|
|
35
|
+
expect(store.getLastUserId()).toBeUndefined();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("round-trips lastUserId", () => {
|
|
39
|
+
const store = makeStore();
|
|
40
|
+
store.setLastUserId({userId: "user-1"});
|
|
41
|
+
expect(store.getLastUserId()).toBe("user-1");
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("upsertEntity / getEntity", () => {
|
|
46
|
+
it("round-trips entity data through the JSON data cell", () => {
|
|
47
|
+
const store = makeStore();
|
|
48
|
+
store.upsertEntity({
|
|
49
|
+
collection: "todos",
|
|
50
|
+
data: {completed: false, title: "Buy milk"},
|
|
51
|
+
id: "t1",
|
|
52
|
+
seq: 3,
|
|
53
|
+
});
|
|
54
|
+
const entity = store.getEntity<Todo>({collection: "todos", id: "t1"});
|
|
55
|
+
expect(entity).toEqual({
|
|
56
|
+
data: {completed: false, title: "Buy milk"},
|
|
57
|
+
deleted: false,
|
|
58
|
+
id: "t1",
|
|
59
|
+
pendingMutationId: undefined,
|
|
60
|
+
seq: 3,
|
|
61
|
+
stream: undefined,
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("returns undefined for a missing entity", () => {
|
|
66
|
+
const store = makeStore();
|
|
67
|
+
expect(store.getEntity({collection: "todos", id: "nope"})).toBeUndefined();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("defaults seq to 0 and deleted to false for new local entities", () => {
|
|
71
|
+
const store = makeStore();
|
|
72
|
+
const entity = store.upsertEntity({collection: "todos", data: {title: "Local"}, id: "t1"});
|
|
73
|
+
expect(entity.seq).toBe(0);
|
|
74
|
+
expect(entity.deleted).toBe(false);
|
|
75
|
+
expect(entity.pendingMutationId).toBeUndefined();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("preserves existing seq, deleted, and pendingMutationId when omitted", () => {
|
|
79
|
+
const store = makeStore();
|
|
80
|
+
store.upsertEntity({
|
|
81
|
+
collection: "todos",
|
|
82
|
+
data: {title: "v1"},
|
|
83
|
+
deleted: true,
|
|
84
|
+
id: "t1",
|
|
85
|
+
pendingMutationId: "m1",
|
|
86
|
+
seq: 7,
|
|
87
|
+
});
|
|
88
|
+
const updated = store.upsertEntity({collection: "todos", data: {title: "v2"}, id: "t1"});
|
|
89
|
+
expect(updated.seq).toBe(7);
|
|
90
|
+
expect(updated.deleted).toBe(true);
|
|
91
|
+
expect(updated.pendingMutationId).toBe("m1");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("clears pendingMutationId when explicitly set to empty", () => {
|
|
95
|
+
const store = makeStore();
|
|
96
|
+
store.upsertEntity({
|
|
97
|
+
collection: "todos",
|
|
98
|
+
data: {title: "v1"},
|
|
99
|
+
id: "t1",
|
|
100
|
+
pendingMutationId: "m1",
|
|
101
|
+
});
|
|
102
|
+
store.upsertEntity({collection: "todos", data: {title: "v2"}, id: "t1", pendingMutationId: ""});
|
|
103
|
+
expect(store.getEntity({collection: "todos", id: "t1"})?.pendingMutationId).toBeUndefined();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("stores null data payloads", () => {
|
|
107
|
+
const store = makeStore();
|
|
108
|
+
store.upsertEntity({collection: "todos", data: null, id: "t1"});
|
|
109
|
+
expect(store.getEntity({collection: "todos", id: "t1"})?.data).toBeNull();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("returns null data for a corrupt (non-JSON) data cell instead of throwing", () => {
|
|
113
|
+
const store = makeStore();
|
|
114
|
+
store.upsertEntity({collection: "todos", data: {title: "ok"}, id: "t1"});
|
|
115
|
+
store.raw.setCell("todos", "t1", "data", "{not json");
|
|
116
|
+
expect(store.getEntity({collection: "todos", id: "t1"})?.data).toBeNull();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("throws for an unknown collection", () => {
|
|
120
|
+
const store = makeStore();
|
|
121
|
+
expect(() => store.upsertEntity({collection: "nope", data: {}, id: "x"})).toThrow(
|
|
122
|
+
/Unknown collection "nope"/
|
|
123
|
+
);
|
|
124
|
+
expect(() => store.getEntity({collection: "nope", id: "x"})).toThrow(/Unknown collection/);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("listEntities", () => {
|
|
129
|
+
it("lists entities in a collection", () => {
|
|
130
|
+
const store = makeStore();
|
|
131
|
+
store.upsertEntity({collection: "todos", data: {title: "a"}, id: "t1"});
|
|
132
|
+
store.upsertEntity({collection: "todos", data: {title: "b"}, id: "t2"});
|
|
133
|
+
const entities = store.listEntities<Todo>({collection: "todos"});
|
|
134
|
+
expect(entities.map((entity) => entity.id).sort()).toEqual(["t1", "t2"]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("excludes tombstones by default and includes them on request", () => {
|
|
138
|
+
const store = makeStore();
|
|
139
|
+
store.upsertEntity({collection: "todos", data: {title: "live"}, id: "t1"});
|
|
140
|
+
store.upsertEntity({collection: "todos", data: {title: "dead"}, deleted: true, id: "t2"});
|
|
141
|
+
expect(store.listEntities({collection: "todos"}).map((entity) => entity.id)).toEqual(["t1"]);
|
|
142
|
+
expect(
|
|
143
|
+
store
|
|
144
|
+
.listEntities({collection: "todos", includeDeleted: true})
|
|
145
|
+
.map((entity) => entity.id)
|
|
146
|
+
.sort()
|
|
147
|
+
).toEqual(["t1", "t2"]);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("isolates collections from each other, even with the same entity id", () => {
|
|
151
|
+
const store = makeStore();
|
|
152
|
+
store.upsertEntity({collection: "todos", data: {title: "todo"}, id: "shared"});
|
|
153
|
+
store.upsertEntity({collection: "notes", data: {body: "note"}, id: "shared"});
|
|
154
|
+
expect(store.listEntities({collection: "todos"})).toHaveLength(1);
|
|
155
|
+
expect(store.getEntity({collection: "todos", id: "shared"})?.data).toEqual({title: "todo"});
|
|
156
|
+
expect(store.getEntity({collection: "notes", id: "shared"})?.data).toEqual({body: "note"});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("throws for an unknown collection", () => {
|
|
160
|
+
const store = makeStore();
|
|
161
|
+
expect(() => store.listEntities({collection: "nope"})).toThrow(/Unknown collection/);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe("softDeleteEntity", () => {
|
|
166
|
+
it("marks the entity deleted while keeping its data and seq", () => {
|
|
167
|
+
const store = makeStore();
|
|
168
|
+
store.upsertEntity({collection: "todos", data: {title: "keep me"}, id: "t1", seq: 4});
|
|
169
|
+
store.softDeleteEntity({collection: "todos", id: "t1"});
|
|
170
|
+
const entity = store.getEntity<Todo>({collection: "todos", id: "t1"});
|
|
171
|
+
expect(entity?.deleted).toBe(true);
|
|
172
|
+
expect(entity?.data).toEqual({title: "keep me"} as Todo);
|
|
173
|
+
expect(entity?.seq).toBe(4);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("is a no-op for a missing entity", () => {
|
|
177
|
+
const store = makeStore();
|
|
178
|
+
store.softDeleteEntity({collection: "todos", id: "nope"});
|
|
179
|
+
expect(store.getEntity({collection: "todos", id: "nope"})).toBeUndefined();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("throws for an unknown collection", () => {
|
|
183
|
+
const store = makeStore();
|
|
184
|
+
expect(() => store.softDeleteEntity({collection: "nope", id: "x"})).toThrow(
|
|
185
|
+
/Unknown collection/
|
|
186
|
+
);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("clearCollection", () => {
|
|
191
|
+
it("removes every entity in the collection but leaves other tables alone", () => {
|
|
192
|
+
const store = makeStore();
|
|
193
|
+
store.upsertEntity({collection: "todos", data: {title: "a"}, id: "t1"});
|
|
194
|
+
store.upsertEntity({collection: "notes", data: {body: "b"}, id: "n1"});
|
|
195
|
+
store.clearCollection({collection: "todos"});
|
|
196
|
+
expect(store.listEntities({collection: "todos", includeDeleted: true})).toHaveLength(0);
|
|
197
|
+
expect(store.listEntities({collection: "notes"})).toHaveLength(1);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("throws for an unknown collection", () => {
|
|
201
|
+
const store = makeStore();
|
|
202
|
+
expect(() => store.clearCollection({collection: "nope"})).toThrow(/Unknown collection/);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("cannot be used to clear reserved tables", () => {
|
|
206
|
+
const store = makeStore();
|
|
207
|
+
expect(() => store.clearCollection({collection: OUTBOX_TABLE})).toThrow(/Unknown collection/);
|
|
208
|
+
expect(() => store.clearCollection({collection: CURSORS_TABLE})).toThrow(/Unknown collection/);
|
|
209
|
+
expect(() => store.clearCollection({collection: CONFLICTS_TABLE})).toThrow(
|
|
210
|
+
/Unknown collection/
|
|
211
|
+
);
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
describe("deleted rows stay deleted across a persister reload", () => {
|
|
216
|
+
/**
|
|
217
|
+
* A MergeableStore keeps a CRDT tombstone for every deleted row. Loading that
|
|
218
|
+
* tombstone re-applies the tables schema, so any cell carrying a `default`
|
|
219
|
+
* used to be re-created — resurrecting the row as a husk of its defaults. For
|
|
220
|
+
* `_conflicts` that meant an already-resolved conflict reappeared with
|
|
221
|
+
* `collection: ""` and threw `Unknown collection ""` when resolved again.
|
|
222
|
+
*/
|
|
223
|
+
const reloadAfterDelete = async ({
|
|
224
|
+
table,
|
|
225
|
+
rowId,
|
|
226
|
+
seed,
|
|
227
|
+
}: {
|
|
228
|
+
table: string;
|
|
229
|
+
rowId: string;
|
|
230
|
+
seed: () => SyncStore;
|
|
231
|
+
}): Promise<Record<string, unknown>> => {
|
|
232
|
+
const databaseName = `resurrection-${table}-${Date.now()}-${Math.random()}`;
|
|
233
|
+
const store = seed();
|
|
234
|
+
const persister = createMemoryPersister({databaseName, store: store.raw});
|
|
235
|
+
await persister.save();
|
|
236
|
+
store.raw.delRow(table, rowId);
|
|
237
|
+
await persister.save();
|
|
238
|
+
|
|
239
|
+
const reloaded = createSyncStore({collections: ["todos", "notes"]});
|
|
240
|
+
const reloadPersister = createMemoryPersister({databaseName, store: reloaded.raw});
|
|
241
|
+
await reloadPersister.load();
|
|
242
|
+
return reloaded.raw.getTable(table);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
it("does not resurrect a deleted conflict row", async () => {
|
|
246
|
+
const table = await reloadAfterDelete({
|
|
247
|
+
rowId: "m1",
|
|
248
|
+
seed: (): SyncStore => {
|
|
249
|
+
const store = makeStore();
|
|
250
|
+
store.raw.setRow(CONFLICTS_TABLE, "m1", {
|
|
251
|
+
collection: "todos",
|
|
252
|
+
dismissed: false,
|
|
253
|
+
entityId: "t1",
|
|
254
|
+
localData: "{}",
|
|
255
|
+
serverData: "{}",
|
|
256
|
+
serverSeq: 9,
|
|
257
|
+
});
|
|
258
|
+
return store;
|
|
259
|
+
},
|
|
260
|
+
table: CONFLICTS_TABLE,
|
|
261
|
+
});
|
|
262
|
+
expect(table).toEqual({});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("does not resurrect a deleted outbox row", async () => {
|
|
266
|
+
const table = await reloadAfterDelete({
|
|
267
|
+
rowId: "m1",
|
|
268
|
+
seed: (): SyncStore => {
|
|
269
|
+
const store = makeStore();
|
|
270
|
+
createOutbox({store}).enqueue({
|
|
271
|
+
args: {title: "a"},
|
|
272
|
+
collection: "todos",
|
|
273
|
+
entityId: "t1",
|
|
274
|
+
mutationId: "m1",
|
|
275
|
+
operation: "update",
|
|
276
|
+
userId: "u1",
|
|
277
|
+
});
|
|
278
|
+
return store;
|
|
279
|
+
},
|
|
280
|
+
table: OUTBOX_TABLE,
|
|
281
|
+
});
|
|
282
|
+
expect(table).toEqual({});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("does not resurrect a compacted entity tombstone", async () => {
|
|
286
|
+
const table = await reloadAfterDelete({
|
|
287
|
+
rowId: "t1",
|
|
288
|
+
seed: (): SyncStore => {
|
|
289
|
+
const store = makeStore();
|
|
290
|
+
store.upsertEntity({collection: "todos", data: {title: "a"}, id: "t1"});
|
|
291
|
+
return store;
|
|
292
|
+
},
|
|
293
|
+
table: "todos",
|
|
294
|
+
});
|
|
295
|
+
expect(table).toEqual({});
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
describe("deletedAt stamping (E5)", () => {
|
|
300
|
+
it("upsertEntity stamps deletedAt on the transition into a tombstone, and only then", () => {
|
|
301
|
+
const store = makeStore();
|
|
302
|
+
store.upsertEntity({collection: "todos", data: {title: "a"}, id: "t1"});
|
|
303
|
+
expect(store.getEntity({collection: "todos", id: "t1"})?.deletedAt).toBeUndefined();
|
|
304
|
+
|
|
305
|
+
store.softDeleteEntity({collection: "todos", id: "t1"});
|
|
306
|
+
const firstStampedAt = store.getEntity({collection: "todos", id: "t1"})?.deletedAt;
|
|
307
|
+
expect(firstStampedAt).toBeTruthy();
|
|
308
|
+
|
|
309
|
+
// A second upsert that keeps deleted: true must NOT re-stamp deletedAt.
|
|
310
|
+
store.upsertEntity({collection: "todos", data: {title: "a"}, deleted: true, id: "t1"});
|
|
311
|
+
expect(store.getEntity({collection: "todos", id: "t1"})?.deletedAt).toBe(firstStampedAt);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("clears deletedAt on a resurrection (upsert back to deleted: false)", () => {
|
|
315
|
+
const store = makeStore();
|
|
316
|
+
store.upsertEntity({collection: "todos", data: {title: "a"}, id: "t1"});
|
|
317
|
+
store.softDeleteEntity({collection: "todos", id: "t1"});
|
|
318
|
+
expect(store.getEntity({collection: "todos", id: "t1"})?.deletedAt).toBeTruthy();
|
|
319
|
+
|
|
320
|
+
store.upsertEntity({collection: "todos", data: {title: "a"}, deleted: false, id: "t1"});
|
|
321
|
+
expect(store.getEntity({collection: "todos", id: "t1"})?.deletedAt).toBeUndefined();
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
describe("compactTombstones (E5)", () => {
|
|
326
|
+
it("removes tombstones older than the retention window, across all collections", () => {
|
|
327
|
+
const clock = {value: 1_000_000};
|
|
328
|
+
const store = createSyncStore({
|
|
329
|
+
collections: ["todos", "notes"],
|
|
330
|
+
now: () => new Date(clock.value).toISOString(),
|
|
331
|
+
});
|
|
332
|
+
store.upsertEntity({collection: "todos", data: {title: "old"}, id: "t1"});
|
|
333
|
+
store.upsertEntity({collection: "todos", data: {title: "recent"}, id: "t2"});
|
|
334
|
+
store.upsertEntity({collection: "notes", data: {body: "old note"}, id: "n1"});
|
|
335
|
+
store.upsertEntity({collection: "todos", data: {title: "kept, not deleted"}, id: "t3"});
|
|
336
|
+
|
|
337
|
+
// t1/n1 tombstoned "long ago"; t2 tombstoned "recently", relative to the
|
|
338
|
+
// clock the compaction call itself uses below.
|
|
339
|
+
store.upsertEntity({collection: "todos", data: null, deleted: true, id: "t1"});
|
|
340
|
+
store.upsertEntity({collection: "notes", data: null, deleted: true, id: "n1"});
|
|
341
|
+
clock.value += 100 * 24 * 60 * 60 * 1_000; // +100 days
|
|
342
|
+
store.upsertEntity({collection: "todos", data: null, deleted: true, id: "t2"});
|
|
343
|
+
|
|
344
|
+
// Advance the clock another 10 days: t1/n1 are now ~110 days old, t2 is
|
|
345
|
+
// ~10 days old. A 90-day retention window compacts t1/n1 but keeps t2.
|
|
346
|
+
clock.value += 10 * 24 * 60 * 60 * 1_000;
|
|
347
|
+
const result = store.compactTombstones({olderThanMs: 90 * 24 * 60 * 60 * 1_000});
|
|
348
|
+
|
|
349
|
+
expect(result.removed).toBe(2);
|
|
350
|
+
expect(store.getEntity({collection: "todos", id: "t1"})).toBeUndefined();
|
|
351
|
+
expect(store.getEntity({collection: "notes", id: "n1"})).toBeUndefined();
|
|
352
|
+
expect(store.getEntity({collection: "todos", id: "t2"})?.deleted).toBe(true);
|
|
353
|
+
expect(store.getEntity({collection: "todos", id: "t3"})?.deleted).toBe(false);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("leaves tombstones with no deletedAt (pre-E5 rows) untouched", () => {
|
|
357
|
+
const store = makeStore();
|
|
358
|
+
store.upsertEntity({collection: "todos", data: {title: "legacy"}, id: "t1"});
|
|
359
|
+
// Simulate a tombstone written before deletedAt existed: set the cell
|
|
360
|
+
// directly, bypassing the stamping logic in upsertEntity/softDeleteEntity.
|
|
361
|
+
store.raw.setCell("todos", "t1", "deleted", true);
|
|
362
|
+
expect(store.getEntity({collection: "todos", id: "t1"})?.deletedAt).toBeUndefined();
|
|
363
|
+
|
|
364
|
+
const result = store.compactTombstones({olderThanMs: 0});
|
|
365
|
+
expect(result.removed).toBe(0);
|
|
366
|
+
expect(store.getEntity({collection: "todos", id: "t1"})).toBeDefined();
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("never removes non-tombstone rows regardless of age", () => {
|
|
370
|
+
const store = makeStore();
|
|
371
|
+
const clock = {value: 1_000_000};
|
|
372
|
+
store.upsertEntity({collection: "todos", data: {title: "alive"}, id: "t1"});
|
|
373
|
+
const result = store.compactTombstones({
|
|
374
|
+
now: () => new Date(clock.value + 1_000 * 60 * 60 * 24 * 365).toISOString(),
|
|
375
|
+
olderThanMs: 0,
|
|
376
|
+
});
|
|
377
|
+
expect(result.removed).toBe(0);
|
|
378
|
+
expect(store.getEntity({collection: "todos", id: "t1"})).toBeDefined();
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
describe("known streams + purgeStream (C2)", () => {
|
|
383
|
+
it("round-trips known streams (add / get / remove)", () => {
|
|
384
|
+
const store = makeStore();
|
|
385
|
+
expect(store.getKnownStreams()).toEqual([]);
|
|
386
|
+
store.addKnownStream({collection: "todos", stream: "todos|owner:u1"});
|
|
387
|
+
store.addKnownStream({collection: "todos", stream: "todos|tenant:org1"});
|
|
388
|
+
expect(store.getKnownStreams().sort()).toEqual(["todos|owner:u1", "todos|tenant:org1"]);
|
|
389
|
+
store.removeKnownStream({stream: "todos|owner:u1"});
|
|
390
|
+
expect(store.getKnownStreams()).toEqual(["todos|tenant:org1"]);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("purgeStream deletes only the matching stream's entities, its cursor, and its known-stream row", () => {
|
|
394
|
+
const store = makeStore();
|
|
395
|
+
const leaving = "todos|tenant:org1";
|
|
396
|
+
const staying = "todos|owner:u1";
|
|
397
|
+
// Two entities on the leaving stream (one in each collection), one on the staying stream.
|
|
398
|
+
store.upsertEntity({collection: "todos", data: {t: 1}, id: "a", seq: 3, stream: leaving});
|
|
399
|
+
store.upsertEntity({collection: "notes", data: {n: 1}, id: "b", seq: 4, stream: leaving});
|
|
400
|
+
store.upsertEntity({collection: "todos", data: {t: 2}, id: "c", seq: 5, stream: staying});
|
|
401
|
+
store.addKnownStream({collection: "todos", stream: leaving});
|
|
402
|
+
store.addKnownStream({collection: "todos", stream: staying});
|
|
403
|
+
store.raw.setRow(CURSORS_TABLE, leaving, {seq: 3, updatedAt: "x"});
|
|
404
|
+
store.raw.setRow(CURSORS_TABLE, staying, {seq: 5, updatedAt: "y"});
|
|
405
|
+
|
|
406
|
+
const purged = store.purgeStream({stream: leaving});
|
|
407
|
+
|
|
408
|
+
expect(purged).toBe(2);
|
|
409
|
+
expect(store.getEntity({collection: "todos", id: "a"})).toBeUndefined();
|
|
410
|
+
expect(store.getEntity({collection: "notes", id: "b"})).toBeUndefined();
|
|
411
|
+
// The staying stream's entity and cursor and known-stream row are untouched.
|
|
412
|
+
expect(store.getEntity({collection: "todos", id: "c"})?.data).toEqual({t: 2});
|
|
413
|
+
expect(store.raw.getCell(CURSORS_TABLE, staying, "seq")).toBe(5);
|
|
414
|
+
expect(store.raw.hasRow(CURSORS_TABLE, leaving)).toBe(false);
|
|
415
|
+
expect(store.getKnownStreams()).toEqual([staying]);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it("purgeStream returns 0 and is a no-op for a stream with no entities", () => {
|
|
419
|
+
const store = makeStore();
|
|
420
|
+
store.addKnownStream({collection: "todos", stream: "todos|owner:ghost"});
|
|
421
|
+
expect(store.purgeStream({stream: "todos|owner:ghost"})).toBe(0);
|
|
422
|
+
expect(store.getKnownStreams()).toEqual([]);
|
|
423
|
+
});
|
|
424
|
+
});
|