@powersync/service-module-mongodb 0.22.8-dev-20261127125127 → 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 +23 -6
- 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 +13 -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 -7
- package/src/replication/ChangeStream.ts +37 -12
- package/src/replication/MongoSnapshotter.ts +14 -1
- package/src/replication/SourceRowConverter.ts +15 -1
- package/test/src/buffer_to_sqlite.test.ts +95 -46
- package/test/src/rust_evaluation.test.ts +103 -0
- package/test/tsconfig.json +3 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -538,6 +538,7 @@ export class MongoSnapshotter {
|
|
|
538
538
|
if (upperBound.kind === 'empty') {
|
|
539
539
|
// The lookup saw no matches in a majority read at least as recent as the resume LSN.
|
|
540
540
|
// The caller still marks the snapshot complete, and the change stream handles later matches.
|
|
541
|
+
this.logger.info(`Skipping replication of ${table.qualifiedName} - no documents match the snapshot filter`);
|
|
541
542
|
return;
|
|
542
543
|
}
|
|
543
544
|
|
|
@@ -548,6 +549,8 @@ export class MongoSnapshotter {
|
|
|
548
549
|
const totalEstimatedCount =
|
|
549
550
|
filter != null && previousEstimate >= 0 ? previousEstimate : await this.estimatedCountNumber(table, filter);
|
|
550
551
|
let at = table.snapshotStatus?.replicatedCount ?? 0;
|
|
552
|
+
// When supported by the storage batch, pass raw BSON instead of decoding rows here.
|
|
553
|
+
const saveBson = batch.prepareUndecodedSave?.('bson');
|
|
551
554
|
await using query = new ChunkedSnapshotQuery({
|
|
552
555
|
collection,
|
|
553
556
|
key: table.snapshotStatus?.lastKey,
|
|
@@ -606,8 +609,18 @@ export class MongoSnapshotter {
|
|
|
606
609
|
// Pre-fetch next batch, so that we can read and write concurrently
|
|
607
610
|
nextChunkPromise = query.nextChunk();
|
|
608
611
|
for (const buffer of docBatch) {
|
|
612
|
+
// These auto-flush when the batch reaches its size limit
|
|
613
|
+
if (saveBson != null) {
|
|
614
|
+
await saveBson({
|
|
615
|
+
tag: SaveOperationTag.INSERT,
|
|
616
|
+
sourceTable: table,
|
|
617
|
+
after: buffer,
|
|
618
|
+
afterReplicaId: this.sourceRowConverter.rawToReplica(buffer)
|
|
619
|
+
});
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
|
|
609
623
|
const { row, replicaId } = this.sourceRowConverter.rawToSqliteRow(buffer);
|
|
610
|
-
// This auto-flushes when the batch reaches its size limit
|
|
611
624
|
await batch.save({
|
|
612
625
|
tag: SaveOperationTag.INSERT,
|
|
613
626
|
sourceTable: table,
|
|
@@ -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;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { resolveRustAddonPath } from '@powersync/service-rust-types/testing';
|
|
1
2
|
import {
|
|
2
3
|
CompatibilityContext,
|
|
3
4
|
CompatibilityEdition,
|
|
4
5
|
CompatibilityOption,
|
|
6
|
+
SqliteRow,
|
|
5
7
|
TimeValuePrecision
|
|
6
8
|
} from '@powersync/service-sync-rules';
|
|
7
9
|
import {
|
|
@@ -28,6 +30,7 @@ import {
|
|
|
28
30
|
LegacySourceRowConverter,
|
|
29
31
|
SourceRowConverter
|
|
30
32
|
} from '@module/replication/SourceRowConverter.js';
|
|
33
|
+
import { loadRustAddon, RustAddon } from '@powersync/service-rust-types';
|
|
31
34
|
|
|
32
35
|
type Placement = 'top' | 'array' | 'nested';
|
|
33
36
|
type ExpectedPlacements = Record<Placement, unknown>;
|
|
@@ -56,6 +59,12 @@ const uuidBytes = Buffer.from('00112233445566778899aabbccddeeff', 'hex');
|
|
|
56
59
|
const depth21Expected =
|
|
57
60
|
'{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":1}}}}}}}}}}}}}}}}}}}}}';
|
|
58
61
|
|
|
62
|
+
const rustModuleTest = test.extend('rust', { scope: 'worker' }, async ({}, { onCleanup }) => {
|
|
63
|
+
const [path, cleanup] = await resolveRustAddonPath();
|
|
64
|
+
onCleanup(cleanup);
|
|
65
|
+
return loadRustAddon(path);
|
|
66
|
+
});
|
|
67
|
+
|
|
59
68
|
describe('JsonBufferWriter', () => {
|
|
60
69
|
test('writeQuotedUtf8Slice escapes large control-heavy payloads without corruption', () => {
|
|
61
70
|
const writer = new JsonBufferWriter(32);
|
|
@@ -479,35 +488,27 @@ describe('SourceRowConverter.rawToSqliteRow expected output', () => {
|
|
|
479
488
|
value: parityCase.expected[placement]
|
|
480
489
|
});
|
|
481
490
|
});
|
|
491
|
+
|
|
492
|
+
rustModuleTest(`rust output for ${parityCase.name} as ${placementLabel(placement)}`, ({ rust }) => {
|
|
493
|
+
expectNormalizedRow(rustConverter(rust, context), parityCase.buildBuffer(placement), {
|
|
494
|
+
_id: parityCase.expectedId?.(placement) ?? `${parityCase.name}:${placement}`,
|
|
495
|
+
value: parityCase.expected[placement]
|
|
496
|
+
});
|
|
497
|
+
});
|
|
482
498
|
}
|
|
483
499
|
}
|
|
484
500
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
);
|
|
497
|
-
});
|
|
498
|
-
|
|
499
|
-
test('custom output for 21 nested object levels', () => {
|
|
500
|
-
expectNormalizedRow(
|
|
501
|
-
directConverter,
|
|
502
|
-
BSON.serialize({
|
|
503
|
-
_id: 'depth-21',
|
|
504
|
-
value: deepNestedObject(21)
|
|
505
|
-
}) as Buffer,
|
|
506
|
-
{
|
|
507
|
-
_id: 'depth-21',
|
|
508
|
-
value: depth21Expected
|
|
509
|
-
}
|
|
510
|
-
);
|
|
501
|
+
rustModuleTest('output for 21 nested object levels', ({ rust }) => {
|
|
502
|
+
const serialized = BSON.serialize({
|
|
503
|
+
_id: 'depth-21',
|
|
504
|
+
value: deepNestedObject(21)
|
|
505
|
+
}) as Buffer;
|
|
506
|
+
const expected = {
|
|
507
|
+
_id: 'depth-21',
|
|
508
|
+
value: depth21Expected
|
|
509
|
+
};
|
|
510
|
+
expectNormalizedRow(legacyConverter, serialized, expected);
|
|
511
|
+
expectNormalizedDirectRow(rust, serialized, expected);
|
|
511
512
|
});
|
|
512
513
|
});
|
|
513
514
|
|
|
@@ -668,7 +669,7 @@ describe('SourceRowConverter.rawToSqliteRow regex option preservation', () => {
|
|
|
668
669
|
|
|
669
670
|
for (const regexCase of regexDivergenceCases) {
|
|
670
671
|
for (const placement of PLACEMENTS) {
|
|
671
|
-
|
|
672
|
+
rustModuleTest(`${regexCase.name} ${regexCase.testDescription} as ${placementLabel(placement)}`, ({ rust }) => {
|
|
672
673
|
const source = regexCase.buildBuffer(placement);
|
|
673
674
|
|
|
674
675
|
// Parity is intentionally not expected here. The default path converts BSON regexes
|
|
@@ -678,7 +679,7 @@ describe('SourceRowConverter.rawToSqliteRow regex option preservation', () => {
|
|
|
678
679
|
_id: `${regexCase.name}:${placement}`,
|
|
679
680
|
value: regexCase.defaultExpected[placement]
|
|
680
681
|
});
|
|
681
|
-
|
|
682
|
+
expectNormalizedDirectRow(rust, source, {
|
|
682
683
|
_id: `${regexCase.name}:${placement}`,
|
|
683
684
|
value: regexCase.customExpected[placement]
|
|
684
685
|
});
|
|
@@ -686,25 +687,25 @@ describe('SourceRowConverter.rawToSqliteRow regex option preservation', () => {
|
|
|
686
687
|
}
|
|
687
688
|
}
|
|
688
689
|
|
|
689
|
-
|
|
690
|
+
rustModuleTest('unsupported BSON regex flag is preserved only on the custom raw converter', ({ rust }) => {
|
|
690
691
|
const source = rawCaseDocument('regex:invalid:z', 'top', 0x0b, Buffer.concat([cstring('a'), cstring('z')]));
|
|
691
692
|
|
|
692
693
|
expectNormalizedRow(legacyConverter, source, {
|
|
693
694
|
_id: 'regex:invalid:z',
|
|
694
695
|
value: '{"pattern":"a","options":""}'
|
|
695
696
|
});
|
|
696
|
-
|
|
697
|
+
expectNormalizedDirectRow(rust, source, {
|
|
697
698
|
_id: 'regex:invalid:z',
|
|
698
699
|
value: '{"pattern":"a","options":"z"}'
|
|
699
700
|
});
|
|
700
701
|
});
|
|
701
702
|
|
|
702
|
-
|
|
703
|
+
rustModuleTest('duplicate BSON regex flags are preserved only on the custom raw converter', ({ rust }) => {
|
|
703
704
|
const source = rawCaseDocument('regex:invalid:ii', 'top', 0x0b, Buffer.concat([cstring('a'), cstring('ii')]));
|
|
704
705
|
|
|
705
706
|
expectRowFailure(legacyConverter, source, "Invalid flags supplied to RegExp constructor 'ii'");
|
|
706
707
|
// The raw converter preserves the BSON option string even when it is not valid JS RegExp flags.
|
|
707
|
-
|
|
708
|
+
expectNormalizedDirectRow(rust, source, {
|
|
708
709
|
_id: 'regex:invalid:ii',
|
|
709
710
|
value: '{"pattern":"a","options":"ii"}'
|
|
710
711
|
});
|
|
@@ -714,6 +715,7 @@ describe('SourceRowConverter.rawToSqliteRow regex option preservation', () => {
|
|
|
714
715
|
describe('SourceRowConverter.rawToSqliteRow invalid UTF-8', () => {
|
|
715
716
|
// The upstream bson parser validates UTF-8 strings by default.
|
|
716
717
|
// Our custom parser accepts invalid UTF-8 strings, using the replacement character in JSON.
|
|
718
|
+
// TODO: Also support invalid UTF-8 in our Rust parser (blocked by https://jira.mongodb.org/browse/RUST-2541).
|
|
717
719
|
|
|
718
720
|
test('invalid UTF-8 in top-level string is accepted only on the custom raw converter', () => {
|
|
719
721
|
const source = bsonDocument([
|
|
@@ -747,7 +749,7 @@ describe('SourceRowConverter.rawToSqliteRow invalid UTF-8', () => {
|
|
|
747
749
|
});
|
|
748
750
|
|
|
749
751
|
describe('SourceRowConverter.rawToSqliteRow malformed BSON lengths', () => {
|
|
750
|
-
|
|
752
|
+
rustModuleTest('overlong top-level string length fails instead of hanging', ({ rust }) => {
|
|
751
753
|
const source = bsonDocument([
|
|
752
754
|
bsonElement(0x02, '_id', bsonString('malformed-length:top-string')),
|
|
753
755
|
// Declares far more string bytes than exist in the document.
|
|
@@ -756,11 +758,29 @@ describe('SourceRowConverter.rawToSqliteRow malformed BSON lengths', () => {
|
|
|
756
758
|
|
|
757
759
|
expect(captureRow(legacyConverter, source).ok).toBe(false);
|
|
758
760
|
expectRowFailure(directConverter, source, 'Invalid BSON string length');
|
|
761
|
+
expectRowFailure(
|
|
762
|
+
rustConverter(rust, CONTEXT),
|
|
763
|
+
source,
|
|
764
|
+
'BSON error at key "value". Kind: Malformed BSON bytes. Message: expected buffer to contain at least 1004 bytes, but it only has 7.'
|
|
765
|
+
);
|
|
759
766
|
});
|
|
760
767
|
});
|
|
761
768
|
|
|
769
|
+
const parityTest = rustModuleTest.extend('expectRowParity', ({ rust }) => {
|
|
770
|
+
const convertRust = rustConverter(rust, CONTEXT);
|
|
771
|
+
|
|
772
|
+
return function expectRowParity(source: Buffer) {
|
|
773
|
+
const direct = captureRow(directConverter, source);
|
|
774
|
+
const legacy = captureRow(legacyConverter, source);
|
|
775
|
+
const rust = captureRow(convertRust, source);
|
|
776
|
+
|
|
777
|
+
expect(direct).toEqual(legacy);
|
|
778
|
+
expect(rust).toEqual(legacy);
|
|
779
|
+
};
|
|
780
|
+
});
|
|
781
|
+
|
|
762
782
|
describe('SourceRowConverter.rawToSqliteRow fuzz', () => {
|
|
763
|
-
|
|
783
|
+
parityTest('matches across randomized supported documents', ({ expectRowParity }) => {
|
|
764
784
|
const rng = makeRng(0x5eedc0de);
|
|
765
785
|
|
|
766
786
|
for (let i = 0; i < 150; i++) {
|
|
@@ -777,7 +797,7 @@ describe('SourceRowConverter.rawToSqliteRow fuzz', () => {
|
|
|
777
797
|
}
|
|
778
798
|
});
|
|
779
799
|
|
|
780
|
-
|
|
800
|
+
parityTest('matches on a large nested string that grows the JSON writer buffer', ({ expectRowParity }) => {
|
|
781
801
|
const source = BSON.serialize({
|
|
782
802
|
_id: 'large-string',
|
|
783
803
|
value: {
|
|
@@ -788,7 +808,7 @@ describe('SourceRowConverter.rawToSqliteRow fuzz', () => {
|
|
|
788
808
|
expectRowParity(source);
|
|
789
809
|
});
|
|
790
810
|
|
|
791
|
-
|
|
811
|
+
parityTest('matches on escape-heavy keys and values', ({ expectRowParity }) => {
|
|
792
812
|
const source = BSON.serialize({
|
|
793
813
|
_id: 'escapes',
|
|
794
814
|
'quote"slash\\newline\n': {
|
|
@@ -802,7 +822,7 @@ describe('SourceRowConverter.rawToSqliteRow fuzz', () => {
|
|
|
802
822
|
expectRowParity(source);
|
|
803
823
|
});
|
|
804
824
|
|
|
805
|
-
|
|
825
|
+
parityTest('matches on large escape-heavy nested values serialized from BSON', ({ expectRowParity }) => {
|
|
806
826
|
// For this test to work, this must be larger than the default buffer size
|
|
807
827
|
const escapeHeavy = '\u0001'.repeat(1024 * 400);
|
|
808
828
|
const source = BSON.serialize({
|
|
@@ -815,7 +835,7 @@ describe('SourceRowConverter.rawToSqliteRow fuzz', () => {
|
|
|
815
835
|
expectRowParity(source);
|
|
816
836
|
});
|
|
817
837
|
|
|
818
|
-
|
|
838
|
+
parityTest('matches 21 nested object levels', ({ expectRowParity }) => {
|
|
819
839
|
expectRowParity(
|
|
820
840
|
BSON.serialize({
|
|
821
841
|
_id: 'depth-21',
|
|
@@ -826,12 +846,13 @@ describe('SourceRowConverter.rawToSqliteRow fuzz', () => {
|
|
|
826
846
|
});
|
|
827
847
|
|
|
828
848
|
describe('SourceRowConverter.rawToSqliteRow full output parity', () => {
|
|
829
|
-
|
|
849
|
+
parityTest('matches replicaId when row parity succeeds', ({ expectRowParity }) => {
|
|
830
850
|
const source = BSON.serialize({
|
|
831
851
|
_id: 'replica-id',
|
|
832
852
|
value: new Int32(7)
|
|
833
853
|
}) as Buffer;
|
|
834
854
|
|
|
855
|
+
expectRowParity(source);
|
|
835
856
|
expect(captureOutput(directConverter, source)).toEqual(captureOutput(legacyConverter, source));
|
|
836
857
|
});
|
|
837
858
|
|
|
@@ -873,7 +894,7 @@ describe('SourceRowConverter.rawToSqliteRow full output parity', () => {
|
|
|
873
894
|
});
|
|
874
895
|
|
|
875
896
|
describe('SourceRowConverter.rawToSqliteRow DBRef handling', () => {
|
|
876
|
-
|
|
897
|
+
rustModuleTest('documents why DBRef differs from deprecated DBPointer handling', ({ rust }) => {
|
|
877
898
|
// These are _not_ serialized as a DBPointer value. It is a normal document,
|
|
878
899
|
// with $ref, $id, $db keys.
|
|
879
900
|
const source = BSON.serialize({
|
|
@@ -887,9 +908,9 @@ describe('SourceRowConverter.rawToSqliteRow DBRef handling', () => {
|
|
|
887
908
|
value: '{"collection":"mycollection","oid":"66e834cc91d805df11fa0ecb","db":"mydb","fields":{"foo":"bar"}}'
|
|
888
909
|
});
|
|
889
910
|
|
|
890
|
-
// DirectSourceRowConverter
|
|
911
|
+
// DirectSourceRowConverter and the Rust converter serialize the raw BSON document shape for DBRef.
|
|
891
912
|
// We intentionally do not attempt to convert this type.
|
|
892
|
-
|
|
913
|
+
expectNormalizedDirectRow(rust, source, {
|
|
893
914
|
_id: 'dbref',
|
|
894
915
|
value: '{"$ref":"mycollection","$id":"66e834cc91d805df11fa0ecb","$db":"mydb","foo":"bar"}'
|
|
895
916
|
});
|
|
@@ -1029,10 +1050,6 @@ function captureOutput(converter: SourceRowConverter, source: Buffer): OutputCap
|
|
|
1029
1050
|
}
|
|
1030
1051
|
}
|
|
1031
1052
|
|
|
1032
|
-
function expectRowParity(source: Buffer) {
|
|
1033
|
-
expect(captureRow(directConverter, source)).toEqual(captureRow(legacyConverter, source));
|
|
1034
|
-
}
|
|
1035
|
-
|
|
1036
1053
|
function expectNormalizedRow(converter: SourceRowConverter, source: Buffer, expected: Record<string, unknown>) {
|
|
1037
1054
|
expect(captureRow(converter, source)).toEqual({
|
|
1038
1055
|
ok: true,
|
|
@@ -1040,6 +1057,21 @@ function expectNormalizedRow(converter: SourceRowConverter, source: Buffer, expe
|
|
|
1040
1057
|
});
|
|
1041
1058
|
}
|
|
1042
1059
|
|
|
1060
|
+
function expectNormalizedDirectRow(addon: typeof RustAddon, source: Buffer, expected: Record<string, unknown>) {
|
|
1061
|
+
const direct = directConverter;
|
|
1062
|
+
const rust = rustConverter(addon, CONTEXT);
|
|
1063
|
+
const normalized = normalize(expected);
|
|
1064
|
+
|
|
1065
|
+
expect(captureRow(direct, source)).toEqual({
|
|
1066
|
+
ok: true,
|
|
1067
|
+
row: normalized
|
|
1068
|
+
});
|
|
1069
|
+
expect(captureRow(rust, source)).toEqual({
|
|
1070
|
+
ok: true,
|
|
1071
|
+
row: normalized
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1043
1075
|
function placementLabel(placement: Placement): string {
|
|
1044
1076
|
switch (placement) {
|
|
1045
1077
|
case 'top':
|
|
@@ -1153,3 +1185,20 @@ function deepNestedObject(depth: number): unknown {
|
|
|
1153
1185
|
}
|
|
1154
1186
|
return value;
|
|
1155
1187
|
}
|
|
1188
|
+
|
|
1189
|
+
function rustConverter(rust: typeof RustAddon, options: CompatibilityContext): SourceRowConverter {
|
|
1190
|
+
const rustOptions = options.toRust(rust);
|
|
1191
|
+
|
|
1192
|
+
return {
|
|
1193
|
+
rawToSqliteRow: function (source: Buffer): { row: SqliteRow; replicaId: any } {
|
|
1194
|
+
const decoded = rust.testDecodeBson(source, rustOptions) as SqliteRow;
|
|
1195
|
+
return { row: decoded, replicaId: '' };
|
|
1196
|
+
},
|
|
1197
|
+
documentToSqliteRow: function (): SqliteRow {
|
|
1198
|
+
throw new Error('Function not implemented.');
|
|
1199
|
+
},
|
|
1200
|
+
rawToReplica(source) {
|
|
1201
|
+
throw new Error('Function not implemented');
|
|
1202
|
+
}
|
|
1203
|
+
} satisfies SourceRowConverter;
|
|
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
|
+
}
|