@powersync/service-module-mongodb 0.15.3 → 0.16.0

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 (69) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/dist/api/MongoRouteAPIAdapter.js +2 -2
  3. package/dist/api/MongoRouteAPIAdapter.js.map +1 -1
  4. package/dist/replication/ChangeStream.d.ts +6 -6
  5. package/dist/replication/ChangeStream.js +300 -322
  6. package/dist/replication/ChangeStream.js.map +1 -1
  7. package/dist/replication/ChangeStreamReplicationJob.js +2 -2
  8. package/dist/replication/ChangeStreamReplicationJob.js.map +1 -1
  9. package/dist/replication/ChangeStreamReplicator.d.ts +1 -1
  10. package/dist/replication/ChangeStreamReplicator.js +1 -1
  11. package/dist/replication/ChangeStreamReplicator.js.map +1 -1
  12. package/dist/replication/JsonBufferWriter.d.ts +80 -0
  13. package/dist/replication/JsonBufferWriter.js +342 -0
  14. package/dist/replication/JsonBufferWriter.js.map +1 -0
  15. package/dist/replication/MongoManager.d.ts +1 -1
  16. package/dist/replication/MongoManager.js +1 -1
  17. package/dist/replication/MongoManager.js.map +1 -1
  18. package/dist/replication/MongoRelation.js +4 -0
  19. package/dist/replication/MongoRelation.js.map +1 -1
  20. package/dist/replication/MongoSnapshotQuery.d.ts +1 -1
  21. package/dist/replication/MongoSnapshotQuery.js +6 -3
  22. package/dist/replication/MongoSnapshotQuery.js.map +1 -1
  23. package/dist/replication/RawChangeStream.d.ts +55 -0
  24. package/dist/replication/RawChangeStream.js +322 -0
  25. package/dist/replication/RawChangeStream.js.map +1 -0
  26. package/dist/replication/SourceRowConverter.d.ts +46 -0
  27. package/dist/replication/SourceRowConverter.js +42 -0
  28. package/dist/replication/SourceRowConverter.js.map +1 -0
  29. package/dist/replication/bufferToSqlite.d.ts +43 -0
  30. package/dist/replication/bufferToSqlite.js +740 -0
  31. package/dist/replication/bufferToSqlite.js.map +1 -0
  32. package/dist/replication/internal-mongodb-utils.d.ts +0 -12
  33. package/dist/replication/internal-mongodb-utils.js +0 -54
  34. package/dist/replication/internal-mongodb-utils.js.map +1 -1
  35. package/dist/replication/replication-index.d.ts +4 -2
  36. package/dist/replication/replication-index.js +4 -2
  37. package/dist/replication/replication-index.js.map +1 -1
  38. package/dist/replication/replication-utils.d.ts +1 -1
  39. package/dist/types/types.js.map +1 -1
  40. package/package.json +11 -11
  41. package/scripts/benchmark-change-document-json.mts +358 -0
  42. package/scripts/benchmark-change-document.mts +370 -0
  43. package/src/api/MongoRouteAPIAdapter.ts +2 -2
  44. package/src/replication/ChangeStream.ts +348 -371
  45. package/src/replication/ChangeStreamReplicationJob.ts +2 -2
  46. package/src/replication/ChangeStreamReplicator.ts +2 -5
  47. package/src/replication/JsonBufferWriter.ts +390 -0
  48. package/src/replication/MongoManager.ts +2 -2
  49. package/src/replication/MongoRelation.ts +5 -2
  50. package/src/replication/MongoSnapshotQuery.ts +8 -5
  51. package/src/replication/RawChangeStream.ts +460 -0
  52. package/src/replication/SourceRowConverter.ts +65 -0
  53. package/src/replication/bufferToSqlite.ts +944 -0
  54. package/src/replication/internal-mongodb-utils.ts +0 -66
  55. package/src/replication/replication-index.ts +4 -2
  56. package/src/replication/replication-utils.ts +2 -2
  57. package/src/types/types.ts +1 -1
  58. package/test/src/buffer_to_sqlite.test.ts +1146 -0
  59. package/test/src/change_stream.test.ts +49 -3
  60. package/test/src/change_stream_utils.ts +4 -10
  61. package/test/src/mongo_test.test.ts +66 -64
  62. package/test/src/parse_document_id.test.ts +54 -0
  63. package/test/src/raw_change_stream.test.ts +547 -0
  64. package/test/src/resume.test.ts +12 -2
  65. package/test/src/util.ts +56 -3
  66. package/test/tsconfig.json +0 -1
  67. package/tsconfig.scripts.json +13 -0
  68. package/tsconfig.tsbuildinfo +1 -1
  69. package/test/src/internal_mongodb_utils.test.ts +0 -103
@@ -1,103 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
-
3
- import { getCursorBatchBytes, trackChangeStreamBsonBytes } from '@module/replication/replication-index.js';
4
- import { mongo } from '@powersync/lib-service-mongodb';
5
- import { clearTestDb, connectMongoData } from './util.js';
6
-
7
- describe('internal mongodb utils', () => {
8
- // The implementation relies on internal APIs, so we verify this works as expected for various types of change streams.
9
- test('collection change stream size tracking', async () => {
10
- await testChangeStreamBsonBytes('collection');
11
- });
12
-
13
- test('db change stream size tracking', async () => {
14
- await testChangeStreamBsonBytes('db');
15
- });
16
-
17
- test('cluster change stream size tracking', async () => {
18
- await testChangeStreamBsonBytes('cluster');
19
- });
20
-
21
- test('cursor batch size tracking', async () => {
22
- const { db, client } = await connectMongoData();
23
- await using _ = { [Symbol.asyncDispose]: async () => await client.close() };
24
- await clearTestDb(db);
25
- const collection = db.collection('test_data');
26
- await collection.insertMany([{ test: 1 }, { test: 2 }, { test: 3 }, { test: 4 }, { test: 5 }]);
27
-
28
- const cursor = collection.find({}, { batchSize: 2 });
29
- let batchBytes: number[] = [];
30
- let totalBytes = 0;
31
- // We use this in the same way as ChunkedSnapshotQuery
32
- while (await cursor.hasNext()) {
33
- batchBytes.push(getCursorBatchBytes(cursor));
34
- totalBytes += batchBytes[batchBytes.length - 1];
35
- cursor.readBufferedDocuments();
36
- }
37
-
38
- // 3 batches: [2, 2, 1] documents. Should not change
39
- expect(batchBytes.length).toEqual(3);
40
- // Current tests show 839, but this may change depending on the MongoDB version and other conditions.
41
- expect(totalBytes).toBeGreaterThan(400);
42
- expect(totalBytes).toBeLessThan(1200);
43
- });
44
-
45
- async function testChangeStreamBsonBytes(type: 'db' | 'collection' | 'cluster') {
46
- // With MongoDB, replication uses the exact same document format
47
- // as normal queries. We test it anyway.
48
- const { db, client } = await connectMongoData();
49
- await using _ = { [Symbol.asyncDispose]: async () => await client.close() };
50
- await clearTestDb(db);
51
- const collection = db.collection('test_data');
52
-
53
- let stream: mongo.ChangeStream;
54
- if (type === 'collection') {
55
- stream = collection.watch([], {
56
- maxAwaitTimeMS: 5,
57
- fullDocument: 'updateLookup'
58
- });
59
- } else if (type === 'db') {
60
- stream = db.watch([], {
61
- maxAwaitTimeMS: 5,
62
- fullDocument: 'updateLookup'
63
- });
64
- } else {
65
- stream = client.watch([], {
66
- maxAwaitTimeMS: 5,
67
- fullDocument: 'updateLookup'
68
- });
69
- }
70
-
71
- let batchBytes: number[] = [];
72
- let totalBytes = 0;
73
- trackChangeStreamBsonBytes(stream, (bytes) => {
74
- batchBytes.push(bytes);
75
- totalBytes += bytes;
76
- });
77
-
78
- const readAll = async () => {
79
- while ((await stream.tryNext()) != null) {}
80
- };
81
-
82
- await readAll();
83
-
84
- await collection.insertOne({ test: 1 });
85
- await readAll();
86
- await collection.insertOne({ test: 2 });
87
- await readAll();
88
- await collection.insertOne({ test: 3 });
89
- await readAll();
90
-
91
- await stream.close();
92
-
93
- // The exact length by vary based on exact batching logic, but we do want to know when it changes.
94
- // Note: If this causes unstable tests, we can relax this check.
95
- expect(batchBytes.length).toEqual(8);
96
-
97
- // Current tests show 4464-4576 bytes for the size, depending on the type of change stream.
98
- // This can easily vary based on the mongodb version and general conditions, so we just check the general range.
99
- // For the most part, if any bytes are reported, the tracking is working.
100
- expect(totalBytes).toBeGreaterThan(2000);
101
- expect(totalBytes).toBeLessThan(8000);
102
- }
103
- });