@powersync/service-module-mongodb 0.22.9 → 0.23.0-dev-20261205085725

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.
Files changed (27) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/replication/MongoReplicationQueryProvider.d.ts +50 -5
  3. package/dist/replication/MongoReplicationQueryProvider.js +24 -1
  4. package/dist/replication/MongoReplicationQueryProvider.js.map +1 -1
  5. package/dist/replication/MongoSnapshotPartitionResume.d.ts +36 -0
  6. package/dist/replication/MongoSnapshotPartitionResume.js +95 -0
  7. package/dist/replication/MongoSnapshotPartitionResume.js.map +1 -0
  8. package/dist/replication/MongoSnapshotQuery.d.ts +5 -0
  9. package/dist/replication/MongoSnapshotQuery.js +15 -0
  10. package/dist/replication/MongoSnapshotQuery.js.map +1 -1
  11. package/dist/replication/MongoSnapshotter.js +144 -84
  12. package/dist/replication/MongoSnapshotter.js.map +1 -1
  13. package/dist/replication/estimateFilteredSnapshotCount.d.ts +12 -0
  14. package/dist/replication/estimateFilteredSnapshotCount.js +37 -0
  15. package/dist/replication/estimateFilteredSnapshotCount.js.map +1 -1
  16. package/package.json +5 -4
  17. package/src/replication/MongoReplicationQueryProvider.ts +73 -6
  18. package/src/replication/MongoSnapshotPartitionResume.ts +133 -0
  19. package/src/replication/MongoSnapshotQuery.ts +15 -0
  20. package/src/replication/MongoSnapshotter.ts +157 -93
  21. package/src/replication/estimateFilteredSnapshotCount.ts +43 -0
  22. package/test/src/filtering_example.test.ts +4 -2
  23. package/test/src/filtering_lifecycle.test.ts +3 -2
  24. package/test/src/partitioned_snapshot.test.ts +205 -0
  25. package/test/src/snapshot_query_provider.test.ts +26 -19
  26. package/test/src/snapshot_resume_key.test.ts +108 -0
  27. package/tsconfig.tsbuildinfo +1 -1
@@ -1,4 +1,7 @@
1
- import { DEFAULT_MONGO_REPLICATION_QUERY_PROVIDER } from '@module/replication/MongoReplicationQueryProvider.js';
1
+ import {
2
+ DEFAULT_MONGO_REPLICATION_QUERY_PROVIDER,
3
+ singleSnapshotPartition
4
+ } from '@module/replication/MongoReplicationQueryProvider.js';
2
5
  import { mongo } from '@powersync/lib-service-mongodb';
3
6
  import { describe, expect, test, vi } from 'vitest';
4
7
  import { openChangeStreamTestContext } from './change_stream_test_setup.js';
@@ -14,19 +17,21 @@ describe.skipIf(DATABASE_TYPE == DatabaseType.DOCUMENTDB)('MongoDB snapshot quer
14
17
  ])('pages snapshots: %j', async ({ filtered, retryUnknown }) => {
15
18
  // Run the same snapshot with and without a provider predicate. The filtered case selects even
16
19
  // ids between 2 and 12 whose label is exactly 'live'; null must preserve the full snapshot.
17
- const getSnapshotFilter = vi.fn(() =>
18
- filtered
19
- ? {
20
- $expr: {
21
- $and: [
22
- { $gt: ['$_id', 2] },
23
- { $lt: ['$_id', 12] },
24
- { $eq: [{ $mod: ['$_id', 2] }, 0] },
25
- { $eq: ['$label', 'live'] }
26
- ]
20
+ const getSnapshotPartitions = vi.fn(async () =>
21
+ singleSnapshotPartition(
22
+ filtered
23
+ ? {
24
+ $expr: {
25
+ $and: [
26
+ { $gt: ['$_id', 2] },
27
+ { $lt: ['$_id', 12] },
28
+ { $eq: [{ $mod: ['$_id', 2] }, 0] },
29
+ { $eq: ['$label', 'live'] }
30
+ ]
31
+ }
27
32
  }
28
- }
29
- : null
33
+ : null
34
+ )
30
35
  );
31
36
  await using context = await openChangeStreamTestContext(factory, {
32
37
  storageVersion,
@@ -38,7 +43,7 @@ describe.skipIf(DATABASE_TYPE == DatabaseType.DOCUMENTDB)('MongoDB snapshot quer
38
43
  expect(connectionTag).toBe(context.connectionTag);
39
44
  expect(defaultSchema).toBe(context.db.databaseName);
40
45
  expect(syncConfig.sourceTableConfig).toEqual({});
41
- return { ...DEFAULT_MONGO_REPLICATION_QUERY_PROVIDER, getSnapshotFilter };
46
+ return { ...DEFAULT_MONGO_REPLICATION_QUERY_PROVIDER, getSnapshotPartitions };
42
47
  }
43
48
  }
44
49
  });
@@ -82,8 +87,10 @@ describe.skipIf(DATABASE_TYPE == DatabaseType.DOCUMENTDB)('MongoDB snapshot quer
82
87
  expect(rows.map((op) => op.object_id).sort()).toEqual(
83
88
  (filtered ? [4, 8, 10] : Array.from({ length: 14 }, (_, i) => i + 1)).map(String).sort()
84
89
  );
85
- expect(getSnapshotFilter).toHaveBeenCalledWith(
86
- expect.objectContaining({ schema: context.db.databaseName, name: 'documents' })
90
+ expect(getSnapshotPartitions).toHaveBeenCalledWith(
91
+ expect.objectContaining({
92
+ table: expect.objectContaining({ schema: context.db.databaseName, name: 'documents' })
93
+ })
87
94
  );
88
95
  });
89
96
 
@@ -94,11 +101,11 @@ describe.skipIf(DATABASE_TYPE == DatabaseType.DOCUMENTDB)('MongoDB snapshot quer
94
101
  throw new Error('unsupported test source');
95
102
  });
96
103
  const openChangeStream = vi.fn(DEFAULT_MONGO_REPLICATION_QUERY_PROVIDER.openChangeStream);
97
- const getSnapshotFilter = vi.fn(DEFAULT_MONGO_REPLICATION_QUERY_PROVIDER.getSnapshotFilter);
104
+ const getSnapshotPartitions = vi.fn(DEFAULT_MONGO_REPLICATION_QUERY_PROVIDER.getSnapshotPartitions);
98
105
  await using context = await openChangeStreamTestContext(factory, {
99
106
  storageVersion,
100
107
  streamOptions: {
101
- createReplicationQueryProvider: () => ({ validateSource, openChangeStream, getSnapshotFilter })
108
+ createReplicationQueryProvider: () => ({ validateSource, openChangeStream, getSnapshotPartitions })
102
109
  }
103
110
  });
104
111
  await context.updateSyncRules(/* yaml */ `
@@ -116,7 +123,7 @@ describe.skipIf(DATABASE_TYPE == DatabaseType.DOCUMENTDB)('MongoDB snapshot quer
116
123
  });
117
124
  // Spies delegate to the real default hooks if invoked, but validation should prevent either call.
118
125
  expect(openChangeStream).not.toHaveBeenCalled();
119
- expect(getSnapshotFilter).not.toHaveBeenCalled();
126
+ expect(getSnapshotPartitions).not.toHaveBeenCalled();
120
127
  });
121
128
  });
122
129
  });
@@ -0,0 +1,108 @@
1
+ import {
2
+ assertSnapshotPartitionPlan,
3
+ singleSnapshotPartition,
4
+ type MongoSnapshotPartitionPlan
5
+ } from '@module/replication/MongoReplicationQueryProvider.js';
6
+ import {
7
+ encodeSnapshotResumeKey,
8
+ resolveSnapshotResumeState,
9
+ type SnapshotPartitionProgress
10
+ } from '@module/replication/MongoSnapshotPartitionResume.js';
11
+ import { bson } from '@powersync/service-core';
12
+ import { describe, expect, test } from 'vitest';
13
+
14
+ const PLAN: MongoSnapshotPartitionPlan = {
15
+ fingerprint: 'plan-v1',
16
+ partitions: ['a', 'b', 'c'].map((id) => ({ id, filter: { store: id } }))
17
+ };
18
+ // A whole-number double, which a default round trip would turn into an int32.
19
+ const ID_BUFFER = bson.serialize({ _id: new bson.Double(7) });
20
+
21
+ describe('snapshot resume keys', () => {
22
+ test('saves partition progress without a top-level _id and resumes from it', () => {
23
+ const progress = new Map<string, SnapshotPartitionProgress>([
24
+ ['a', { done: true }],
25
+ ['b', { lastKey: ID_BUFFER }]
26
+ ]);
27
+ const key = encodeSnapshotResumeKey(PLAN, progress);
28
+ const saved = bson.deserialize(key);
29
+ // Versions before partitions read `lastKey._id`; without it they restart the table.
30
+ expect(saved).not.toHaveProperty('_id');
31
+ expect(saved).toEqual({ fingerprint: 'plan-v1', partitions: { a: { done: true }, b: { _id: 7 } } });
32
+
33
+ const resume = resolveSnapshotResumeState(PLAN, key);
34
+ expect(resume.discardedReason).toBeUndefined();
35
+ expect([...resume.progress.keys()]).toEqual(['a', 'b']);
36
+ expect(resume.progress.get('a')).toEqual({ done: true });
37
+ const resumed = resume.progress.get('b') as { lastKey: Uint8Array };
38
+ expect(Buffer.from(resumed.lastKey).equals(Buffer.from(ID_BUFFER))).toBe(true);
39
+ });
40
+
41
+ test('resumes several in-progress partitions and partitions finished out of order', () => {
42
+ const key = bson.serialize({
43
+ fingerprint: 'plan-v1',
44
+ partitions: { a: { _id: 1 }, b: { done: true }, c: { _id: 3 } }
45
+ });
46
+ const { progress, discardedReason } = resolveSnapshotResumeState(PLAN, key);
47
+ expect(discardedReason).toBeUndefined();
48
+ expect(progress.get('b')).toEqual({ done: true });
49
+ expect(['a', 'c'].map((id) => bson.deserialize((progress.get(id) as { lastKey: Uint8Array }).lastKey))).toEqual([
50
+ { _id: 1 },
51
+ { _id: 3 }
52
+ ]);
53
+ });
54
+
55
+ test('keeps single-partition keys as { _id }', () => {
56
+ const plan = singleSnapshotPartition(null);
57
+ expect(encodeSnapshotResumeKey(plan, new Map([['all', { lastKey: ID_BUFFER }]]))).toBe(ID_BUFFER);
58
+ expect(resolveSnapshotResumeState(plan, ID_BUFFER)).toEqual({
59
+ progress: new Map([['all', { lastKey: ID_BUFFER }]])
60
+ });
61
+ });
62
+
63
+ test.each([
64
+ { label: 'a plain _id key', plan: PLAN, key: { _id: 7 } },
65
+ {
66
+ label: 'a partitioned key on a single-partition plan',
67
+ plan: singleSnapshotPartition(null),
68
+ key: { fingerprint: 'plan-v1', partitions: {} }
69
+ },
70
+ { label: 'a different fingerprint', plan: PLAN, key: { fingerprint: 'plan-v2', partitions: {} } },
71
+ {
72
+ label: 'a partition the plan does not have',
73
+ plan: PLAN,
74
+ key: { fingerprint: 'plan-v1', partitions: { z: { _id: 7 } } }
75
+ },
76
+ { label: 'an entry with neither _id nor done', plan: PLAN, key: { fingerprint: 'plan-v1', partitions: { a: {} } } },
77
+ {
78
+ label: 'an entry with both _id and done',
79
+ plan: PLAN,
80
+ key: { fingerprint: 'plan-v1', partitions: { a: { _id: 7, done: true } } }
81
+ },
82
+ { label: 'a list of partitions', plan: PLAN, key: { fingerprint: 'plan-v1', partitions: [{ _id: 7 }] } },
83
+ {
84
+ label: 'a done entry that is not true',
85
+ plan: PLAN,
86
+ key: { fingerprint: 'plan-v1', partitions: { a: { done: false } } }
87
+ },
88
+ {
89
+ label: 'the earlier { fingerprint, partition, _id } key',
90
+ plan: PLAN,
91
+ key: { fingerprint: 'plan-v1', partition: 'b', _id: 7 }
92
+ }
93
+ ])('restarts the table for $label', ({ plan, key }) => {
94
+ expect(resolveSnapshotResumeState(plan, bson.serialize(key))).toMatchObject({
95
+ progress: new Map(),
96
+ discardedReason: expect.any(String)
97
+ });
98
+ });
99
+
100
+ test.each([
101
+ { label: 'no partitions', ids: [] },
102
+ { label: 'duplicate partition IDs', ids: ['a', 'a'] },
103
+ { label: 'a partition ID containing a NUL character', ids: ['a', 'b\0c'] }
104
+ ])('rejects plans with $label', ({ ids }) => {
105
+ const plan = { fingerprint: 'plan-v1', partitions: ids.map((id) => ({ id, filter: null })) };
106
+ expect(() => assertSnapshotPartitionPlan(plan)).toThrow();
107
+ });
108
+ });