@powersync/service-module-mongodb 0.0.0-dev-20241128134723 → 0.0.0-dev-20241219110735
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/CHANGELOG.md +69 -4
- package/dist/db/db-index.d.ts +1 -0
- package/dist/db/db-index.js +2 -0
- package/dist/db/db-index.js.map +1 -0
- package/dist/db/mongo.d.ts +35 -0
- package/dist/db/mongo.js +73 -0
- package/dist/db/mongo.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/locks/MonogLocks.d.ts +36 -0
- package/dist/locks/MonogLocks.js +83 -0
- package/dist/locks/MonogLocks.js.map +1 -0
- package/dist/migrations/MonogMigrationAgent.d.ts +12 -0
- package/dist/migrations/MonogMigrationAgent.js +25 -0
- package/dist/migrations/MonogMigrationAgent.js.map +1 -0
- package/dist/migrations/db/migrations/1684951997326-init.d.ts +3 -0
- package/dist/migrations/db/migrations/1684951997326-init.js +30 -0
- package/dist/migrations/db/migrations/1684951997326-init.js.map +1 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +2 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +5 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +3 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +54 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +3 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +26 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +1 -0
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +3 -0
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +28 -0
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +1 -0
- package/dist/migrations/mongo-migration-store.d.ts +7 -0
- package/dist/migrations/mongo-migration-store.js +49 -0
- package/dist/migrations/mongo-migration-store.js.map +1 -0
- package/dist/module/MongoModule.js +15 -4
- package/dist/module/MongoModule.js.map +1 -1
- package/dist/replication/MongoManager.d.ts +1 -1
- package/dist/replication/MongoManager.js +3 -2
- package/dist/replication/MongoManager.js.map +1 -1
- package/dist/storage/MongoBucketStorage.d.ts +48 -0
- package/dist/storage/MongoBucketStorage.js +425 -0
- package/dist/storage/MongoBucketStorage.js.map +1 -0
- package/dist/storage/implementation/MongoBucketBatch.d.ts +72 -0
- package/dist/storage/implementation/MongoBucketBatch.js +681 -0
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -0
- package/dist/storage/implementation/MongoCompactor.d.ts +40 -0
- package/dist/storage/implementation/MongoCompactor.js +310 -0
- package/dist/storage/implementation/MongoCompactor.js.map +1 -0
- package/dist/storage/implementation/MongoIdSequence.d.ts +12 -0
- package/dist/storage/implementation/MongoIdSequence.js +21 -0
- package/dist/storage/implementation/MongoIdSequence.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +9 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.js +9 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +20 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +26 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +1 -0
- package/dist/storage/implementation/MongoStorageProvider.d.ts +6 -0
- package/dist/storage/implementation/MongoStorageProvider.js +34 -0
- package/dist/storage/implementation/MongoStorageProvider.js.map +1 -0
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +36 -0
- package/dist/storage/implementation/MongoSyncBucketStorage.js +529 -0
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -0
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +16 -0
- package/dist/storage/implementation/MongoSyncRulesLock.js +65 -0
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -0
- package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +20 -0
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js +104 -0
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -0
- package/dist/storage/implementation/OperationBatch.d.ts +34 -0
- package/dist/storage/implementation/OperationBatch.js +119 -0
- package/dist/storage/implementation/OperationBatch.js.map +1 -0
- package/dist/storage/implementation/PersistedBatch.d.ts +46 -0
- package/dist/storage/implementation/PersistedBatch.js +223 -0
- package/dist/storage/implementation/PersistedBatch.js.map +1 -0
- package/dist/storage/implementation/config.d.ts +19 -0
- package/dist/storage/implementation/config.js +26 -0
- package/dist/storage/implementation/config.js.map +1 -0
- package/dist/storage/implementation/db.d.ts +36 -0
- package/dist/storage/implementation/db.js +47 -0
- package/dist/storage/implementation/db.js.map +1 -0
- package/dist/storage/implementation/models.d.ts +139 -0
- package/dist/storage/implementation/models.js +2 -0
- package/dist/storage/implementation/models.js.map +1 -0
- package/dist/storage/implementation/util.d.ts +58 -0
- package/dist/storage/implementation/util.js +196 -0
- package/dist/storage/implementation/util.js.map +1 -0
- package/dist/storage/storage-index.d.ts +14 -0
- package/dist/storage/storage-index.js +15 -0
- package/dist/storage/storage-index.js.map +1 -0
- package/dist/types/types.d.ts +3 -0
- package/dist/types/types.js +4 -1
- package/dist/types/types.js.map +1 -1
- package/package.json +11 -8
- package/src/db/db-index.ts +1 -0
- package/src/db/mongo.ts +81 -0
- package/src/index.ts +4 -0
- package/src/locks/MonogLocks.ts +147 -0
- package/src/migrations/MonogMigrationAgent.ts +39 -0
- package/src/migrations/db/migrations/1684951997326-init.ts +39 -0
- package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +5 -0
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +105 -0
- package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +38 -0
- package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +40 -0
- package/src/migrations/mongo-migration-store.ts +62 -0
- package/src/module/MongoModule.ts +18 -4
- package/src/replication/MongoManager.ts +6 -2
- package/src/storage/MongoBucketStorage.ts +530 -0
- package/src/storage/implementation/MongoBucketBatch.ts +893 -0
- package/src/storage/implementation/MongoCompactor.ts +392 -0
- package/src/storage/implementation/MongoIdSequence.ts +24 -0
- package/src/storage/implementation/MongoPersistedSyncRules.ts +16 -0
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +49 -0
- package/src/storage/implementation/MongoStorageProvider.ts +42 -0
- package/src/storage/implementation/MongoSyncBucketStorage.ts +612 -0
- package/src/storage/implementation/MongoSyncRulesLock.ts +88 -0
- package/src/storage/implementation/MongoWriteCheckpointAPI.ts +146 -0
- package/src/storage/implementation/OperationBatch.ts +130 -0
- package/src/storage/implementation/PersistedBatch.ts +283 -0
- package/src/storage/implementation/config.ts +40 -0
- package/src/storage/implementation/db.ts +88 -0
- package/src/storage/implementation/models.ts +160 -0
- package/src/storage/implementation/util.ts +209 -0
- package/src/storage/storage-index.ts +14 -0
- package/src/types/types.ts +8 -1
- package/test/src/__snapshots__/storage_sync.test.ts.snap +332 -0
- package/test/src/change_stream.test.ts +34 -33
- package/test/src/change_stream_utils.ts +6 -6
- package/test/src/env.ts +1 -0
- package/test/src/slow_tests.test.ts +4 -4
- package/test/src/storage.test.ts +7 -0
- package/test/src/storage_compacting.test.ts +6 -0
- package/test/src/storage_sync.test.ts +113 -0
- package/test/src/util.ts +20 -7
- package/test/tsconfig.json +4 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +1 -1
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { storage } from '@powersync/service-core';
|
|
2
|
+
import { register, TEST_TABLE, test_utils } from '@powersync/service-core-tests';
|
|
3
|
+
import { describe, expect, test } from 'vitest';
|
|
4
|
+
import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
|
|
5
|
+
|
|
6
|
+
describe('sync - mongodb', () => {
|
|
7
|
+
register.registerSyncTests(INITIALIZED_MONGO_STORAGE_FACTORY);
|
|
8
|
+
|
|
9
|
+
// The split of returned results can vary depending on storage drivers
|
|
10
|
+
test('large batch (2)', async () => {
|
|
11
|
+
// Test syncing a batch of data that is small in count,
|
|
12
|
+
// but large enough in size to be split over multiple returned chunks.
|
|
13
|
+
// Similar to the above test, but splits over 1MB chunks.
|
|
14
|
+
const sync_rules = test_utils.testRules(
|
|
15
|
+
`
|
|
16
|
+
bucket_definitions:
|
|
17
|
+
global:
|
|
18
|
+
data:
|
|
19
|
+
- SELECT id, description FROM "%"
|
|
20
|
+
`
|
|
21
|
+
);
|
|
22
|
+
using factory = await INITIALIZED_MONGO_STORAGE_FACTORY();
|
|
23
|
+
const bucketStorage = factory.getInstance(sync_rules);
|
|
24
|
+
|
|
25
|
+
const result = await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
|
|
26
|
+
const sourceTable = TEST_TABLE;
|
|
27
|
+
|
|
28
|
+
const largeDescription = '0123456789'.repeat(2_000_00);
|
|
29
|
+
|
|
30
|
+
await batch.save({
|
|
31
|
+
sourceTable,
|
|
32
|
+
tag: storage.SaveOperationTag.INSERT,
|
|
33
|
+
after: {
|
|
34
|
+
id: 'test1',
|
|
35
|
+
description: 'test1'
|
|
36
|
+
},
|
|
37
|
+
afterReplicaId: test_utils.rid('test1')
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
await batch.save({
|
|
41
|
+
sourceTable,
|
|
42
|
+
tag: storage.SaveOperationTag.INSERT,
|
|
43
|
+
after: {
|
|
44
|
+
id: 'large1',
|
|
45
|
+
description: largeDescription
|
|
46
|
+
},
|
|
47
|
+
afterReplicaId: test_utils.rid('large1')
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Large enough to split the returned batch
|
|
51
|
+
await batch.save({
|
|
52
|
+
sourceTable,
|
|
53
|
+
tag: storage.SaveOperationTag.INSERT,
|
|
54
|
+
after: {
|
|
55
|
+
id: 'large2',
|
|
56
|
+
description: largeDescription
|
|
57
|
+
},
|
|
58
|
+
afterReplicaId: test_utils.rid('large2')
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
await batch.save({
|
|
62
|
+
sourceTable,
|
|
63
|
+
tag: storage.SaveOperationTag.INSERT,
|
|
64
|
+
after: {
|
|
65
|
+
id: 'test3',
|
|
66
|
+
description: 'test3'
|
|
67
|
+
},
|
|
68
|
+
afterReplicaId: test_utils.rid('test3')
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const checkpoint = result!.flushed_op;
|
|
73
|
+
|
|
74
|
+
const options: storage.BucketDataBatchOptions = {};
|
|
75
|
+
|
|
76
|
+
const batch1 = await test_utils.fromAsync(
|
|
77
|
+
bucketStorage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']]), options)
|
|
78
|
+
);
|
|
79
|
+
expect(test_utils.getBatchData(batch1)).toEqual([
|
|
80
|
+
{ op_id: '1', op: 'PUT', object_id: 'test1', checksum: 2871785649 },
|
|
81
|
+
{ op_id: '2', op: 'PUT', object_id: 'large1', checksum: 1178768505 }
|
|
82
|
+
]);
|
|
83
|
+
expect(test_utils.getBatchMeta(batch1)).toEqual({
|
|
84
|
+
after: '0',
|
|
85
|
+
has_more: true,
|
|
86
|
+
next_after: '2'
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const batch2 = await test_utils.fromAsync(
|
|
90
|
+
bucketStorage.getBucketDataBatch(checkpoint, new Map([['global[]', batch1[0].batch.next_after]]), options)
|
|
91
|
+
);
|
|
92
|
+
expect(test_utils.getBatchData(batch2)).toEqual([
|
|
93
|
+
{ op_id: '3', op: 'PUT', object_id: 'large2', checksum: 1607205872 }
|
|
94
|
+
]);
|
|
95
|
+
expect(test_utils.getBatchMeta(batch2)).toEqual({
|
|
96
|
+
after: '2',
|
|
97
|
+
has_more: true,
|
|
98
|
+
next_after: '3'
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const batch3 = await test_utils.fromAsync(
|
|
102
|
+
bucketStorage.getBucketDataBatch(checkpoint, new Map([['global[]', batch2[0].batch.next_after]]), options)
|
|
103
|
+
);
|
|
104
|
+
expect(test_utils.getBatchData(batch3)).toEqual([
|
|
105
|
+
{ op_id: '4', op: 'PUT', object_id: 'test3', checksum: 1359888332 }
|
|
106
|
+
]);
|
|
107
|
+
expect(test_utils.getBatchMeta(batch3)).toEqual({
|
|
108
|
+
after: '3',
|
|
109
|
+
has_more: false,
|
|
110
|
+
next_after: '4'
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
package/test/src/util.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import * as types from '@module/types/types.js';
|
|
2
|
-
import { BucketStorageFactory, Metrics
|
|
2
|
+
import { BucketStorageFactory, Metrics } from '@powersync/service-core';
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { MongoBucketStorage } from '@module/storage/MongoBucketStorage.js';
|
|
5
|
+
import { PowerSyncMongo } from '@module/storage/storage-index.js';
|
|
6
|
+
import { test_utils } from '@powersync/service-core-tests';
|
|
7
7
|
import * as mongo from 'mongodb';
|
|
8
|
+
import { env } from './env.js';
|
|
8
9
|
|
|
9
10
|
// The metrics need to be initialized before they can be used
|
|
10
11
|
await Metrics.initialise({
|
|
@@ -23,15 +24,16 @@ export const TEST_CONNECTION_OPTIONS = types.normalizeConnectionConfig({
|
|
|
23
24
|
|
|
24
25
|
export type StorageFactory = () => Promise<BucketStorageFactory>;
|
|
25
26
|
|
|
26
|
-
export const INITIALIZED_MONGO_STORAGE_FACTORY: StorageFactory = async () => {
|
|
27
|
+
export const INITIALIZED_MONGO_STORAGE_FACTORY: StorageFactory = async (options?: test_utils.StorageOptions) => {
|
|
27
28
|
const db = await connectMongo();
|
|
28
29
|
|
|
29
30
|
// None of the PG tests insert data into this collection, so it was never created
|
|
30
31
|
if (!(await db.db.listCollections({ name: db.bucket_parameters.collectionName }).hasNext())) {
|
|
31
32
|
await db.db.createCollection('bucket_parameters');
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
if (!options?.doNotClear) {
|
|
35
|
+
await db.clear();
|
|
36
|
+
}
|
|
35
37
|
|
|
36
38
|
return new MongoBucketStorage(db, { slot_name_prefix: 'test_' });
|
|
37
39
|
};
|
|
@@ -50,3 +52,14 @@ export async function connectMongoData() {
|
|
|
50
52
|
const dbname = new URL(env.MONGO_TEST_DATA_URL).pathname.substring(1);
|
|
51
53
|
return { client, db: client.db(dbname) };
|
|
52
54
|
}
|
|
55
|
+
|
|
56
|
+
export async function connectMongo() {
|
|
57
|
+
// Short timeout for tests, to fail fast when the server is not available.
|
|
58
|
+
// Slightly longer timeouts for CI, to avoid arbitrary test failures
|
|
59
|
+
const client = new mongo.MongoClient(env.MONGO_TEST_URL, {
|
|
60
|
+
connectTimeoutMS: env.CI ? 15_000 : 5_000,
|
|
61
|
+
socketTimeoutMS: env.CI ? 15_000 : 5_000,
|
|
62
|
+
serverSelectionTimeoutMS: env.CI ? 15_000 : 2_500
|
|
63
|
+
});
|
|
64
|
+
return new PowerSyncMongo(client);
|
|
65
|
+
}
|