@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.
- package/CHANGELOG.md +52 -0
- package/dist/api/MongoRouteAPIAdapter.js +2 -2
- package/dist/api/MongoRouteAPIAdapter.js.map +1 -1
- package/dist/replication/ChangeStream.d.ts +6 -6
- package/dist/replication/ChangeStream.js +300 -322
- package/dist/replication/ChangeStream.js.map +1 -1
- package/dist/replication/ChangeStreamReplicationJob.js +2 -2
- package/dist/replication/ChangeStreamReplicationJob.js.map +1 -1
- package/dist/replication/ChangeStreamReplicator.d.ts +1 -1
- package/dist/replication/ChangeStreamReplicator.js +1 -1
- package/dist/replication/ChangeStreamReplicator.js.map +1 -1
- package/dist/replication/JsonBufferWriter.d.ts +80 -0
- package/dist/replication/JsonBufferWriter.js +342 -0
- package/dist/replication/JsonBufferWriter.js.map +1 -0
- package/dist/replication/MongoManager.d.ts +1 -1
- package/dist/replication/MongoManager.js +1 -1
- package/dist/replication/MongoManager.js.map +1 -1
- package/dist/replication/MongoRelation.js +4 -0
- package/dist/replication/MongoRelation.js.map +1 -1
- package/dist/replication/MongoSnapshotQuery.d.ts +1 -1
- package/dist/replication/MongoSnapshotQuery.js +6 -3
- package/dist/replication/MongoSnapshotQuery.js.map +1 -1
- package/dist/replication/RawChangeStream.d.ts +55 -0
- package/dist/replication/RawChangeStream.js +322 -0
- package/dist/replication/RawChangeStream.js.map +1 -0
- package/dist/replication/SourceRowConverter.d.ts +46 -0
- package/dist/replication/SourceRowConverter.js +42 -0
- package/dist/replication/SourceRowConverter.js.map +1 -0
- package/dist/replication/bufferToSqlite.d.ts +43 -0
- package/dist/replication/bufferToSqlite.js +740 -0
- package/dist/replication/bufferToSqlite.js.map +1 -0
- package/dist/replication/internal-mongodb-utils.d.ts +0 -12
- package/dist/replication/internal-mongodb-utils.js +0 -54
- package/dist/replication/internal-mongodb-utils.js.map +1 -1
- package/dist/replication/replication-index.d.ts +4 -2
- package/dist/replication/replication-index.js +4 -2
- package/dist/replication/replication-index.js.map +1 -1
- package/dist/replication/replication-utils.d.ts +1 -1
- package/dist/types/types.js.map +1 -1
- package/package.json +11 -11
- package/scripts/benchmark-change-document-json.mts +358 -0
- package/scripts/benchmark-change-document.mts +370 -0
- package/src/api/MongoRouteAPIAdapter.ts +2 -2
- package/src/replication/ChangeStream.ts +348 -371
- package/src/replication/ChangeStreamReplicationJob.ts +2 -2
- package/src/replication/ChangeStreamReplicator.ts +2 -5
- package/src/replication/JsonBufferWriter.ts +390 -0
- package/src/replication/MongoManager.ts +2 -2
- package/src/replication/MongoRelation.ts +5 -2
- package/src/replication/MongoSnapshotQuery.ts +8 -5
- package/src/replication/RawChangeStream.ts +460 -0
- package/src/replication/SourceRowConverter.ts +65 -0
- package/src/replication/bufferToSqlite.ts +944 -0
- package/src/replication/internal-mongodb-utils.ts +0 -66
- package/src/replication/replication-index.ts +4 -2
- package/src/replication/replication-utils.ts +2 -2
- package/src/types/types.ts +1 -1
- package/test/src/buffer_to_sqlite.test.ts +1146 -0
- package/test/src/change_stream.test.ts +49 -3
- package/test/src/change_stream_utils.ts +4 -10
- package/test/src/mongo_test.test.ts +66 -64
- package/test/src/parse_document_id.test.ts +54 -0
- package/test/src/raw_change_stream.test.ts +547 -0
- package/test/src/resume.test.ts +12 -2
- package/test/src/util.ts +56 -3
- package/test/tsconfig.json +0 -1
- package/tsconfig.scripts.json +13 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/test/src/internal_mongodb_utils.test.ts +0 -103
|
@@ -1,45 +1,4 @@
|
|
|
1
1
|
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
|
-
import { get } from 'http';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Track bytes read on a change stream.
|
|
6
|
-
*
|
|
7
|
-
* This is after decompression, and without TLS overhead.
|
|
8
|
-
*
|
|
9
|
-
* This excludes some protocol overhead, but does include per-batch overhead.
|
|
10
|
-
*
|
|
11
|
-
* This is built on internal APIs, and may stop working in future driver versions.
|
|
12
|
-
*
|
|
13
|
-
* @param add Called once for each batch of data.
|
|
14
|
-
*/
|
|
15
|
-
export function trackChangeStreamBsonBytes(changeStream: mongo.ChangeStream, add: (bytes: number) => void) {
|
|
16
|
-
let internalChangeStream = changeStream as ChangeStreamWithCursor;
|
|
17
|
-
let current = internalChangeStream.cursor;
|
|
18
|
-
let degisterCursor = trackCursor(current, add);
|
|
19
|
-
|
|
20
|
-
const refresh = () => {
|
|
21
|
-
// The cursor may be replaced closed and re-opened (replaced) in various scenarios, such as
|
|
22
|
-
// after a primary fail-over event.
|
|
23
|
-
// There is no direct even to track that, but the `resumeTokenChanged` event is a good proxy.
|
|
24
|
-
// It may be called more often than the cursor is replaced, so we just check whether the cursor changed.
|
|
25
|
-
// This might miss the init batch, so we may under-count slightly in that case. It is a rare event
|
|
26
|
-
// and typically a small number of bytes, so it's fine to ignore.
|
|
27
|
-
const next = internalChangeStream.cursor;
|
|
28
|
-
if (next !== current) {
|
|
29
|
-
degisterCursor();
|
|
30
|
-
current = next;
|
|
31
|
-
degisterCursor = trackCursor(current, add);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
changeStream.on('resumeTokenChanged', refresh);
|
|
36
|
-
|
|
37
|
-
// We return this to allow de-registration of the event listeners.
|
|
38
|
-
// However, these are garbage collected automatically when the stream is closed, so it's not strictly necessary to call this.
|
|
39
|
-
return () => {
|
|
40
|
-
changeStream.off('resumeTokenChanged', refresh);
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
2
|
|
|
44
3
|
/**
|
|
45
4
|
* Get the byte size of the current batch on a cursor.
|
|
@@ -60,31 +19,6 @@ interface CursorResponse {
|
|
|
60
19
|
toBytes?(): Uint8Array;
|
|
61
20
|
}
|
|
62
21
|
|
|
63
|
-
interface ChangeStreamWithCursor extends mongo.ChangeStream {
|
|
64
|
-
cursor?: mongo.AbstractCursor;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function trackCursor(cursor: mongo.AbstractCursor | undefined, add: (bytes: number) => void) {
|
|
68
|
-
if (cursor == null) {
|
|
69
|
-
return () => {};
|
|
70
|
-
}
|
|
71
|
-
const countBatch = (response: CursorResponse | undefined) => {
|
|
72
|
-
const bytes = getResponseBytes(response);
|
|
73
|
-
if (bytes > 0) {
|
|
74
|
-
add(bytes);
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
// The `init` event is emitted for the first batch, and the `more` event is emitted for subsequent batches.
|
|
79
|
-
cursor.on('init', countBatch);
|
|
80
|
-
cursor.on('more', countBatch);
|
|
81
|
-
|
|
82
|
-
return () => {
|
|
83
|
-
cursor.off('init', countBatch);
|
|
84
|
-
cursor.off('more', countBatch);
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
22
|
function getResponseBytes(response: CursorResponse | undefined): number {
|
|
89
23
|
const buffer = response?.toBytes?.();
|
|
90
24
|
return buffer?.byteLength ?? 0;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
export * from './MongoRelation.js';
|
|
2
1
|
export * from './ChangeStream.js';
|
|
3
|
-
export * from './ChangeStreamReplicator.js';
|
|
4
2
|
export * from './ChangeStreamReplicationJob.js';
|
|
3
|
+
export * from './ChangeStreamReplicator.js';
|
|
5
4
|
export * from './internal-mongodb-utils.js';
|
|
5
|
+
export * from './MongoRelation.js';
|
|
6
|
+
export * from './RawChangeStream.js';
|
|
7
|
+
export * from './SourceRowConverter.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ErrorCode, ServiceError } from '@powersync/lib-services-framework';
|
|
2
|
-
import { MongoManager } from './MongoManager.js';
|
|
3
|
-
import { PostImagesOption } from '../types/types.js';
|
|
4
2
|
import * as bson from 'bson';
|
|
3
|
+
import { PostImagesOption } from '../types/types.js';
|
|
4
|
+
import { MongoManager } from './MongoManager.js';
|
|
5
5
|
|
|
6
6
|
export const CHECKPOINTS_COLLECTION = '_powersync_checkpoints';
|
|
7
7
|
|
package/src/types/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as lib_mongo from '@powersync/lib-service-mongodb/types';
|
|
2
1
|
import type { MongoConnectionParams } from '@powersync/lib-service-mongodb/types';
|
|
2
|
+
import * as lib_mongo from '@powersync/lib-service-mongodb/types';
|
|
3
3
|
import * as service_types from '@powersync/service-types';
|
|
4
4
|
import { LookupFunction } from 'node:net';
|
|
5
5
|
import * as t from 'ts-codec';
|