@powersync/service-module-mongodb-storage 0.0.0-dev-20250901073220 → 0.0.0-dev-20250903064005

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 (39) hide show
  1. package/CHANGELOG.md +14 -5
  2. package/dist/migrations/db/migrations/1741697235857-bucket-state-index.js +1 -4
  3. package/dist/migrations/db/migrations/1741697235857-bucket-state-index.js.map +1 -1
  4. package/dist/storage/MongoBucketStorage.d.ts +3 -2
  5. package/dist/storage/MongoBucketStorage.js +4 -2
  6. package/dist/storage/MongoBucketStorage.js.map +1 -1
  7. package/dist/storage/MongoReportStorage.d.ts +0 -1
  8. package/dist/storage/MongoReportStorage.js +0 -2
  9. package/dist/storage/MongoReportStorage.js.map +1 -1
  10. package/dist/storage/implementation/MongoChecksums.d.ts +45 -13
  11. package/dist/storage/implementation/MongoChecksums.js +148 -135
  12. package/dist/storage/implementation/MongoChecksums.js.map +1 -1
  13. package/dist/storage/implementation/MongoCompactor.js +23 -21
  14. package/dist/storage/implementation/MongoCompactor.js.map +1 -1
  15. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +5 -2
  16. package/dist/storage/implementation/MongoSyncBucketStorage.js +3 -3
  17. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  18. package/dist/storage/implementation/db.d.ts +4 -0
  19. package/dist/storage/implementation/db.js +10 -1
  20. package/dist/storage/implementation/db.js.map +1 -1
  21. package/dist/storage/implementation/models.d.ts +4 -0
  22. package/dist/utils/test-utils.d.ts +2 -0
  23. package/dist/utils/test-utils.js +1 -1
  24. package/dist/utils/test-utils.js.map +1 -1
  25. package/package.json +6 -6
  26. package/src/migrations/db/migrations/1741697235857-bucket-state-index.ts +1 -7
  27. package/src/storage/MongoBucketStorage.ts +4 -3
  28. package/src/storage/MongoReportStorage.ts +0 -3
  29. package/src/storage/implementation/MongoChecksums.ts +172 -150
  30. package/src/storage/implementation/MongoCompactor.ts +23 -21
  31. package/src/storage/implementation/MongoSyncBucketStorage.ts +9 -4
  32. package/src/storage/implementation/db.ts +14 -1
  33. package/src/storage/implementation/models.ts +4 -0
  34. package/src/utils/test-utils.ts +3 -1
  35. package/test/src/__snapshots__/storage.test.ts.snap +17 -1
  36. package/test/src/connection-report-storage.test.ts +17 -17
  37. package/test/src/storage.test.ts +38 -1
  38. package/test/src/storage_compacting.test.ts +120 -5
  39. package/tsconfig.tsbuildinfo +1 -1
@@ -1,7 +1,44 @@
1
1
  import { register } from '@powersync/service-core-tests';
2
2
  import { describe } from 'vitest';
3
3
  import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
4
+ import { env } from './env.js';
5
+ import { mongoTestStorageFactoryGenerator } from '@module/utils/test-utils.js';
4
6
 
5
- describe('Mongo Sync Bucket Storage', () => register.registerDataStorageTests(INITIALIZED_MONGO_STORAGE_FACTORY));
7
+ describe('Mongo Sync Bucket Storage - Parameters', () =>
8
+ register.registerDataStorageParameterTests(INITIALIZED_MONGO_STORAGE_FACTORY));
9
+
10
+ describe('Mongo Sync Bucket Storage - Data', () =>
11
+ register.registerDataStorageDataTests(INITIALIZED_MONGO_STORAGE_FACTORY));
12
+
13
+ describe('Mongo Sync Bucket Storage - Checkpoints', () =>
14
+ register.registerDataStorageCheckpointTests(INITIALIZED_MONGO_STORAGE_FACTORY));
6
15
 
7
16
  describe('Sync Bucket Validation', register.registerBucketValidationTests);
17
+
18
+ describe('Mongo Sync Bucket Storage - split operations', () =>
19
+ register.registerDataStorageDataTests(
20
+ mongoTestStorageFactoryGenerator({
21
+ url: env.MONGO_TEST_URL,
22
+ isCI: env.CI,
23
+ internalOptions: {
24
+ checksumOptions: {
25
+ bucketBatchLimit: 100,
26
+ operationBatchLimit: 1
27
+ }
28
+ }
29
+ })
30
+ ));
31
+
32
+ describe('Mongo Sync Bucket Storage - split buckets', () =>
33
+ register.registerDataStorageDataTests(
34
+ mongoTestStorageFactoryGenerator({
35
+ url: env.MONGO_TEST_URL,
36
+ isCI: env.CI,
37
+ internalOptions: {
38
+ checksumOptions: {
39
+ bucketBatchLimit: 1,
40
+ operationBatchLimit: 100
41
+ }
42
+ }
43
+ })
44
+ ));
@@ -1,7 +1,122 @@
1
- import { register } from '@powersync/service-core-tests';
2
- import { describe } from 'vitest';
1
+ import { register, TEST_TABLE, test_utils } from '@powersync/service-core-tests';
2
+ import { describe, expect, test } from 'vitest';
3
3
  import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
4
+ import { storage, SyncRulesBucketStorage } from '@powersync/service-core';
4
5
 
5
- describe('Mongo Sync Bucket Storage Compact', () => register.registerCompactTests(INITIALIZED_MONGO_STORAGE_FACTORY));
6
- describe('Mongo Sync Parameter Storage Compact', () =>
7
- register.registerParameterCompactTests(INITIALIZED_MONGO_STORAGE_FACTORY));
6
+ describe('Mongo Sync Bucket Storage Compact', () => {
7
+ register.registerCompactTests(INITIALIZED_MONGO_STORAGE_FACTORY);
8
+
9
+ describe('with blank bucket_state', () => {
10
+ // This can happen when migrating from older service versions, that did not populate bucket_state yet.
11
+ const populate = async (bucketStorage: SyncRulesBucketStorage) => {
12
+ await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
13
+ await batch.save({
14
+ sourceTable: TEST_TABLE,
15
+ tag: storage.SaveOperationTag.INSERT,
16
+ after: {
17
+ id: 't1',
18
+ owner_id: 'u1'
19
+ },
20
+ afterReplicaId: test_utils.rid('t1')
21
+ });
22
+
23
+ await batch.save({
24
+ sourceTable: TEST_TABLE,
25
+ tag: storage.SaveOperationTag.INSERT,
26
+ after: {
27
+ id: 't2',
28
+ owner_id: 'u2'
29
+ },
30
+ afterReplicaId: test_utils.rid('t2')
31
+ });
32
+
33
+ await batch.commit('1/1');
34
+ });
35
+
36
+ return bucketStorage.getCheckpoint();
37
+ };
38
+
39
+ const setup = async () => {
40
+ await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY();
41
+ const syncRules = await factory.updateSyncRules({
42
+ content: `
43
+ bucket_definitions:
44
+ by_user:
45
+ parameters: select request.user_id() as user_id
46
+ data: [select * from test where owner_id = bucket.user_id]
47
+ `
48
+ });
49
+ const bucketStorage = factory.getInstance(syncRules);
50
+ const { checkpoint } = await populate(bucketStorage);
51
+
52
+ return { bucketStorage, checkpoint, factory };
53
+ };
54
+
55
+ test('full compact', async () => {
56
+ const { bucketStorage, checkpoint, factory } = await setup();
57
+
58
+ // Simulate bucket_state from old version not being available
59
+ await factory.db.bucket_state.deleteMany({});
60
+
61
+ await bucketStorage.compact({
62
+ clearBatchLimit: 200,
63
+ moveBatchLimit: 10,
64
+ moveBatchQueryLimit: 10,
65
+ maxOpId: checkpoint,
66
+ signal: null as any
67
+ });
68
+
69
+ const checksumAfter = await bucketStorage.getChecksums(checkpoint, ['by_user["u1"]', 'by_user["u2"]']);
70
+ expect(checksumAfter.get('by_user["u1"]')).toEqual({
71
+ bucket: 'by_user["u1"]',
72
+ checksum: -659469718,
73
+ count: 1
74
+ });
75
+ expect(checksumAfter.get('by_user["u2"]')).toEqual({
76
+ bucket: 'by_user["u2"]',
77
+ checksum: 430217650,
78
+ count: 1
79
+ });
80
+ });
81
+
82
+ test('populatePersistentChecksumCache', async () => {
83
+ // Populate old sync rules version
84
+ const { factory } = await setup();
85
+
86
+ // Not populate another version (bucket definition name changed)
87
+ const syncRules = await factory.updateSyncRules({
88
+ content: `
89
+ bucket_definitions:
90
+ by_user2:
91
+ parameters: select request.user_id() as user_id
92
+ data: [select * from test where owner_id = bucket.user_id]
93
+ `
94
+ });
95
+ const bucketStorage = factory.getInstance(syncRules);
96
+
97
+ await populate(bucketStorage);
98
+ const { checkpoint } = await bucketStorage.getCheckpoint();
99
+
100
+ await bucketStorage.populatePersistentChecksumCache({
101
+ maxOpId: checkpoint,
102
+ signal: new AbortController().signal
103
+ });
104
+
105
+ const checksumAfter = await bucketStorage.getChecksums(checkpoint, ['by_user2["u1"]', 'by_user2["u2"]']);
106
+ expect(checksumAfter.get('by_user2["u1"]')).toEqual({
107
+ bucket: 'by_user2["u1"]',
108
+ checksum: -659469718,
109
+ count: 1
110
+ });
111
+ expect(checksumAfter.get('by_user2["u2"]')).toEqual({
112
+ bucket: 'by_user2["u2"]',
113
+ checksum: 430217650,
114
+ count: 1
115
+ });
116
+ });
117
+ });
118
+ });
119
+
120
+ describe('Mongo Sync Parameter Storage Compact', () => {
121
+ register.registerParameterCompactTests(INITIALIZED_MONGO_STORAGE_FACTORY);
122
+ });