@powersync/service-module-mongodb 0.22.8 → 0.22.9-dev-20261204084200
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 +14 -0
- package/dist/replication/ChangeStream.d.ts +2 -2
- package/dist/replication/ChangeStream.js +28 -12
- package/dist/replication/ChangeStream.js.map +1 -1
- package/dist/replication/MongoSnapshotter.js +12 -1
- package/dist/replication/MongoSnapshotter.js.map +1 -1
- package/dist/replication/SourceRowConverter.d.ts +6 -1
- package/dist/replication/SourceRowConverter.js +8 -0
- package/dist/replication/SourceRowConverter.js.map +1 -1
- package/package.json +8 -8
- package/src/replication/ChangeStream.ts +37 -12
- package/src/replication/MongoSnapshotter.ts +13 -1
- package/src/replication/SourceRowConverter.ts +15 -1
- package/test/src/buffer_to_sqlite.test.ts +3 -0
- package/test/src/rust_evaluation.test.ts +103 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
|
+
import { ReplicaId } from '@powersync/service-core';
|
|
2
3
|
import { applyRowContext, CompatibilityContext, CompatibilityOption, SqliteRow } from '@powersync/service-sync-rules';
|
|
3
4
|
import { bufferToSqlite, DateRenderMode, getDateRenderMode, parseDocumentId } from './bufferToSqlite.js';
|
|
4
5
|
import { constructAfterRecord } from './MongoRelation.js';
|
|
@@ -10,7 +11,10 @@ export interface SourceRowConverter {
|
|
|
10
11
|
*
|
|
11
12
|
* The returned replicaId is the plain parsed bson _id value, to preserve the source id as-is.
|
|
12
13
|
*/
|
|
13
|
-
rawToSqliteRow(source: Buffer): { row: SqliteRow; replicaId:
|
|
14
|
+
rawToSqliteRow(source: Buffer): { row: SqliteRow; replicaId: ReplicaId };
|
|
15
|
+
|
|
16
|
+
/** Parse the replica id from a buffer, without decoding the rest if possible. */
|
|
17
|
+
rawToReplica(source: Buffer): ReplicaId;
|
|
14
18
|
|
|
15
19
|
/**
|
|
16
20
|
* Convert a document, as received from MongoDB query or change stream, to
|
|
@@ -38,6 +42,12 @@ export class LegacySourceRowConverter implements SourceRowConverter {
|
|
|
38
42
|
return { row, replicaId };
|
|
39
43
|
}
|
|
40
44
|
|
|
45
|
+
rawToReplica(source: Buffer) {
|
|
46
|
+
const parsed = mongo.BSON.deserialize(source, { useBigInt64: true });
|
|
47
|
+
const replicaId = parsed._id;
|
|
48
|
+
return replicaId;
|
|
49
|
+
}
|
|
50
|
+
|
|
41
51
|
documentToSqliteRow(document: mongo.Document): SqliteRow {
|
|
42
52
|
const input = constructAfterRecord(document);
|
|
43
53
|
return applyRowContext<never>(input, this.compatibilityContext);
|
|
@@ -59,6 +69,10 @@ export class DirectSourceRowConverter implements SourceRowConverter {
|
|
|
59
69
|
return { row, replicaId };
|
|
60
70
|
}
|
|
61
71
|
|
|
72
|
+
rawToReplica(source: Buffer) {
|
|
73
|
+
return parseDocumentId(source).id;
|
|
74
|
+
}
|
|
75
|
+
|
|
62
76
|
documentToSqliteRow(document: mongo.Document): SqliteRow {
|
|
63
77
|
// This is slow, but should not be used other than in tests
|
|
64
78
|
const buffer = mongo.BSON.serialize(document) as Buffer;
|
|
@@ -1196,6 +1196,9 @@ function rustConverter(rust: typeof RustAddon, options: CompatibilityContext): S
|
|
|
1196
1196
|
},
|
|
1197
1197
|
documentToSqliteRow: function (): SqliteRow {
|
|
1198
1198
|
throw new Error('Function not implemented.');
|
|
1199
|
+
},
|
|
1200
|
+
rawToReplica(source) {
|
|
1201
|
+
throw new Error('Function not implemented');
|
|
1199
1202
|
}
|
|
1200
1203
|
} satisfies SourceRowConverter;
|
|
1201
1204
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
|
+
import { container } from '@powersync/lib-services-framework';
|
|
3
|
+
import { OplogEntry, RustModule, storage, TestStorageFactory } from '@powersync/service-core';
|
|
4
|
+
import * as mongo_storage from '@powersync/service-module-mongodb-storage';
|
|
5
|
+
import { loadRustAddon } from '@powersync/service-rust-types';
|
|
6
|
+
import { resolveRustAddonPath } from '@powersync/service-rust-types/testing';
|
|
7
|
+
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
|
|
8
|
+
|
|
9
|
+
import { openChangeStreamTestContext } from './change_stream_test_setup.js';
|
|
10
|
+
import { env } from './env.js';
|
|
11
|
+
import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
|
|
12
|
+
|
|
13
|
+
const SYNC_CONFIG = /* yaml */ `
|
|
14
|
+
config:
|
|
15
|
+
edition: 3
|
|
16
|
+
streams:
|
|
17
|
+
global:
|
|
18
|
+
auto_subscribe: true
|
|
19
|
+
query: SELECT _id AS id, description, num FROM test_data
|
|
20
|
+
by_list:
|
|
21
|
+
query: SELECT _id AS id, description FROM test_data WHERE list_id IN (SELECT _id FROM lists WHERE owner = auth.user_id())
|
|
22
|
+
`;
|
|
23
|
+
|
|
24
|
+
const RUST_MONGO_STORAGE_FACTORY = mongo_storage.test_utils.mongoTestStorageFactoryGenerator({
|
|
25
|
+
url: env.MONGO_TEST_URL,
|
|
26
|
+
isCI: env.CI,
|
|
27
|
+
supportsMultipleSyncConfigs: true,
|
|
28
|
+
rustEvaluation: 'force'
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe.runIf(env.TEST_MONGO_STORAGE)('Rust evaluation', { timeout: 60_000 }, () => {
|
|
32
|
+
let cleanupAddon: (() => void) | undefined;
|
|
33
|
+
|
|
34
|
+
beforeAll(async () => {
|
|
35
|
+
const [path, cleanup] = await resolveRustAddonPath();
|
|
36
|
+
cleanupAddon = cleanup;
|
|
37
|
+
container.register(RustModule.id, loadRustAddon(path));
|
|
38
|
+
}, 600_000);
|
|
39
|
+
|
|
40
|
+
afterAll(() => {
|
|
41
|
+
cleanupAddon?.();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('stores the same data and parameters as JavaScript evaluation', async () => {
|
|
45
|
+
const js = await replicate(INITIALIZED_MONGO_STORAGE_FACTORY.factory);
|
|
46
|
+
const rust = await replicate(RUST_MONGO_STORAGE_FACTORY.factory);
|
|
47
|
+
|
|
48
|
+
expect(rust.global.length).toBeGreaterThan(0);
|
|
49
|
+
expect(rust.byList.length).toBeGreaterThan(0);
|
|
50
|
+
expect(rust.parameters.length).toBeGreaterThan(0);
|
|
51
|
+
expect(rust).toEqual(js);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Snapshots and then streams the same changes, returning bucket data and persisted parameter lookups.
|
|
57
|
+
*/
|
|
58
|
+
async function replicate(factory: TestStorageFactory) {
|
|
59
|
+
await using context = await openChangeStreamTestContext(factory, { storageVersion: storage.STORAGE_VERSION_3 });
|
|
60
|
+
const { db } = context;
|
|
61
|
+
await context.updateSyncRules(SYNC_CONFIG);
|
|
62
|
+
|
|
63
|
+
const listId = new mongo.ObjectId('66e834cc91d805df11fa0ecb');
|
|
64
|
+
const snapshotId = new mongo.ObjectId('66e834cc91d805df11fa0ecc');
|
|
65
|
+
const streamedId = new mongo.ObjectId('66e834cc91d805df11fa0ecd');
|
|
66
|
+
const deletedId = new mongo.ObjectId('66e834cc91d805df11fa0ece');
|
|
67
|
+
|
|
68
|
+
await db.collection('lists').insertOne({ _id: listId, owner: 'user1' });
|
|
69
|
+
await db.collection('test_data').insertMany([
|
|
70
|
+
{ _id: snapshotId, description: 'snapshot', num: 1152921504606846976n, list_id: listId },
|
|
71
|
+
{ _id: deletedId, description: 'deleted later', list_id: listId }
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
await context.replicateSnapshot();
|
|
75
|
+
context.startStreaming();
|
|
76
|
+
|
|
77
|
+
await db.collection('test_data').insertOne({ _id: streamedId, description: 'streamed', num: 3, list_id: listId });
|
|
78
|
+
await db.collection('test_data').updateOne({ _id: snapshotId }, { $set: { description: 'updated' } });
|
|
79
|
+
await db.collection('test_data').replaceOne({ _id: streamedId }, { description: 'replaced', list_id: listId });
|
|
80
|
+
await db.collection('test_data').deleteOne({ _id: deletedId });
|
|
81
|
+
await db.collection('lists').insertOne({ _id: new mongo.ObjectId('66e834cc91d805df11fa0ecf'), owner: 'user2' });
|
|
82
|
+
|
|
83
|
+
const global = normalizeOps(await context.getBucketData('global|0[]'));
|
|
84
|
+
const byList = normalizeOps(await context.getBucketData(`by_list|0["${listId.toHexString()}"]`));
|
|
85
|
+
|
|
86
|
+
const storageDb = (context.factory as mongo_storage.storage.MongoBucketStorage).db.db;
|
|
87
|
+
const parameters: mongo.Document[] = [];
|
|
88
|
+
const collections = await storageDb.listCollections({ name: /^parameter_index_/ }, { nameOnly: true }).toArray();
|
|
89
|
+
for (const { name } of collections) {
|
|
90
|
+
for (const doc of await storageDb.collection(name).find().toArray()) {
|
|
91
|
+
// Op ids and source keys may differ between runs, compare lookups and their outputs.
|
|
92
|
+
parameters.push({ lookup: doc.lookup, bucket_parameters: doc.bucket_parameters });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
parameters.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
|
|
96
|
+
|
|
97
|
+
return { global, byList, parameters };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function normalizeOps(ops: OplogEntry[]) {
|
|
101
|
+
// remove subkeys and checksums for remove: they include the unstable source table id, so we can't compare them
|
|
102
|
+
return ops.map(({ subkey: _, ...op }) => (op.op == 'REMOVE' ? { ...op, checksum: undefined } : op));
|
|
103
|
+
}
|