@powersync/service-module-mongodb 0.0.0-dev-20260602073133 → 0.0.0-dev-20260909133214
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 +153 -5
- package/dist/api/MongoRouteAPIAdapter.d.ts +4 -2
- package/dist/api/MongoRouteAPIAdapter.js +23 -32
- package/dist/api/MongoRouteAPIAdapter.js.map +1 -1
- package/dist/common/SentinelLSN.d.ts +37 -0
- package/dist/common/SentinelLSN.js +59 -0
- package/dist/common/SentinelLSN.js.map +1 -0
- package/dist/module/MongoModule.js +2 -1
- package/dist/module/MongoModule.js.map +1 -1
- package/dist/replication/ChangeStream.d.ts +24 -0
- package/dist/replication/ChangeStream.js +203 -67
- package/dist/replication/ChangeStream.js.map +1 -1
- package/dist/replication/ChangeStreamReplicationJob.js +3 -2
- package/dist/replication/ChangeStreamReplicationJob.js.map +1 -1
- package/dist/replication/ChangeStreamReplicator.js +1 -1
- package/dist/replication/ChangeStreamReplicator.js.map +1 -1
- package/dist/replication/MongoRelation.d.ts +36 -1
- package/dist/replication/MongoRelation.js +133 -18
- package/dist/replication/MongoRelation.js.map +1 -1
- package/dist/replication/MongoSnapshotter.d.ts +9 -1
- package/dist/replication/MongoSnapshotter.js +129 -67
- package/dist/replication/MongoSnapshotter.js.map +1 -1
- package/dist/replication/RawChangeStream.d.ts +4 -1
- package/dist/replication/RawChangeStream.js +6 -10
- package/dist/replication/RawChangeStream.js.map +1 -1
- package/dist/replication/checkpoints/CheckpointImplementation.d.ts +135 -0
- package/dist/replication/checkpoints/CheckpointImplementation.js +22 -0
- package/dist/replication/checkpoints/CheckpointImplementation.js.map +1 -0
- package/dist/replication/checkpoints/SentinelCheckpointImplementation.d.ts +58 -0
- package/dist/replication/checkpoints/SentinelCheckpointImplementation.js +224 -0
- package/dist/replication/checkpoints/SentinelCheckpointImplementation.js.map +1 -0
- package/dist/replication/checkpoints/TimestampCheckpointImplementation.d.ts +27 -0
- package/dist/replication/checkpoints/TimestampCheckpointImplementation.js +116 -0
- package/dist/replication/checkpoints/TimestampCheckpointImplementation.js.map +1 -0
- package/dist/replication/checkpoints/create-checkpoint-implementation.d.ts +6 -0
- package/dist/replication/checkpoints/create-checkpoint-implementation.js +10 -0
- package/dist/replication/checkpoints/create-checkpoint-implementation.js.map +1 -0
- package/dist/replication/replication-utils.d.ts +6 -0
- package/dist/replication/replication-utils.js +19 -2
- package/dist/replication/replication-utils.js.map +1 -1
- package/dist/types/types.d.ts +7 -2
- package/dist/types/types.js +20 -2
- package/dist/types/types.js.map +1 -1
- package/package.json +11 -21
- package/src/api/MongoRouteAPIAdapter.ts +28 -39
- package/src/common/SentinelLSN.ts +78 -0
- package/src/module/MongoModule.ts +2 -1
- package/src/replication/ChangeStream.ts +223 -78
- package/src/replication/ChangeStreamReplicationJob.ts +3 -2
- package/src/replication/ChangeStreamReplicator.ts +1 -1
- package/src/replication/MongoRelation.ts +153 -21
- package/src/replication/MongoSnapshotter.ts +148 -73
- package/src/replication/RawChangeStream.ts +27 -32
- package/src/replication/checkpoints/CheckpointImplementation.ts +167 -0
- package/src/replication/checkpoints/SentinelCheckpointImplementation.ts +269 -0
- package/src/replication/checkpoints/TimestampCheckpointImplementation.ts +145 -0
- package/src/replication/checkpoints/create-checkpoint-implementation.ts +14 -0
- package/src/replication/replication-utils.ts +23 -2
- package/src/types/types.ts +27 -2
- package/test/DOCUMENTDB_TESTING.md +115 -0
- package/test/src/DatabaseType.ts +25 -0
- package/test/src/change_stream.test.ts +180 -71
- package/test/src/change_stream_utils.ts +117 -29
- package/test/src/checkpoint_retry.test.ts +134 -0
- package/test/src/config.test.ts +34 -0
- package/test/src/documentdb_helpers.test.ts +124 -0
- package/test/src/documentdb_mode.test.ts +1039 -0
- package/test/src/mongo_test.test.ts +15 -5
- package/test/src/raw_change_stream.test.ts +147 -126
- package/test/src/resume.test.ts +2 -2
- package/test/src/resume_token.test.ts +30 -0
- package/test/src/slow_tests.test.ts +4 -1
- package/test/src/test-timeouts.ts +23 -0
- package/test/src/util.ts +14 -32
- package/tsconfig.json +1 -4
- package/tsconfig.scripts.json +1 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CheckpointImplementation, CheckpointImplementationContext } from './CheckpointImplementation.js';
|
|
2
|
+
import { SentinelCheckpointImplementation } from './SentinelCheckpointImplementation.js';
|
|
3
|
+
import { TimestampCheckpointImplementation } from './TimestampCheckpointImplementation.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Select the checkpoint implementation for a source: sentinel-based for DocumentDB
|
|
7
|
+
* DB, clusterTime-based for standard MongoDB.
|
|
8
|
+
*/
|
|
9
|
+
export function createCheckpointImplementation(
|
|
10
|
+
isDocumentDb: boolean,
|
|
11
|
+
context: CheckpointImplementationContext
|
|
12
|
+
): CheckpointImplementation {
|
|
13
|
+
return isDocumentDb ? new SentinelCheckpointImplementation(context) : new TimestampCheckpointImplementation(context);
|
|
14
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { mongo } from '@powersync/lib-service-mongodb';
|
|
1
2
|
import { ErrorCode, ServiceError } from '@powersync/lib-services-framework';
|
|
2
3
|
import * as bson from 'bson';
|
|
3
4
|
import { PostImagesOption } from '../types/types.js';
|
|
@@ -7,16 +8,36 @@ export const CHECKPOINTS_COLLECTION = '_powersync_checkpoints';
|
|
|
7
8
|
|
|
8
9
|
const REQUIRED_CHECKPOINT_PERMISSIONS = ['find', 'insert', 'update', 'remove', 'changeStream', 'createCollection'];
|
|
9
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Whether a `hello` response indicates Azure DocumentDB (formerly Azure Cosmos
|
|
13
|
+
* DB for MongoDB vCore), which reports `documentdb_versions` in the `internal`
|
|
14
|
+
* section.
|
|
15
|
+
*/
|
|
16
|
+
function isDocumentDbHello(hello: mongo.Document): boolean {
|
|
17
|
+
return hello.internal?.documentdb_versions != null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Detect whether the connected server is DocumentDB. DocumentDB lacks usable
|
|
22
|
+
* clusterTime/operationTime and uses the sentinel checkpoint implementation.
|
|
23
|
+
*/
|
|
24
|
+
export async function detectDocumentDb(db: mongo.Db): Promise<boolean> {
|
|
25
|
+
const hello = await db.command({ hello: 1 });
|
|
26
|
+
return isDocumentDbHello(hello);
|
|
27
|
+
}
|
|
28
|
+
|
|
10
29
|
export async function checkSourceConfiguration(connectionManager: MongoManager): Promise<void> {
|
|
11
30
|
const db = connectionManager.db;
|
|
12
31
|
|
|
13
32
|
const hello = await db.command({ hello: 1 });
|
|
14
|
-
|
|
33
|
+
const isDocumentDb = isDocumentDbHello(hello);
|
|
34
|
+
|
|
35
|
+
if (hello.msg == 'isdbgrid' && !isDocumentDb) {
|
|
15
36
|
throw new ServiceError(
|
|
16
37
|
ErrorCode.PSYNC_S1341,
|
|
17
38
|
'Sharded MongoDB Clusters are not supported yet (including MongoDB Serverless instances).'
|
|
18
39
|
);
|
|
19
|
-
} else if (hello.setName == null) {
|
|
40
|
+
} else if (hello.setName == null && !isDocumentDb) {
|
|
20
41
|
throw new ServiceError(ErrorCode.PSYNC_S1342, 'Standalone MongoDB instances are not supported - use a replicaset.');
|
|
21
42
|
}
|
|
22
43
|
|
package/src/types/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { MongoConnectionParams } from '@powersync/lib-service-mongodb/types';
|
|
2
2
|
import * as lib_mongo from '@powersync/lib-service-mongodb/types';
|
|
3
|
+
import { ErrorCode, ServiceError } from '@powersync/lib-services-framework';
|
|
3
4
|
import * as service_types from '@powersync/service-types';
|
|
4
5
|
import { LookupFunction } from 'node:net';
|
|
5
6
|
import * as t from 'ts-codec';
|
|
@@ -40,6 +41,10 @@ export enum PostImagesOption {
|
|
|
40
41
|
READ_ONLY = 'read_only'
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
const DEFAULT_PING_INTERVAL_SECONDS = 60;
|
|
45
|
+
const MIN_PING_INTERVAL_SECONDS = 5;
|
|
46
|
+
const MAX_PING_INTERVAL_SECONDS = 60;
|
|
47
|
+
|
|
43
48
|
export interface NormalizedMongoConnectionConfig {
|
|
44
49
|
id: string;
|
|
45
50
|
tag: string;
|
|
@@ -54,13 +59,19 @@ export interface NormalizedMongoConnectionConfig {
|
|
|
54
59
|
|
|
55
60
|
postImages: PostImagesOption;
|
|
56
61
|
|
|
62
|
+
heartbeat_interval_seconds: number;
|
|
63
|
+
|
|
57
64
|
connectionParams: MongoConnectionParams;
|
|
58
65
|
}
|
|
59
66
|
|
|
60
67
|
export const MongoConnectionConfig = service_types.configFile.DataSourceConfig.and(lib_mongo.BaseMongoConfig).and(
|
|
61
68
|
t.object({
|
|
62
69
|
// Replication specific settings
|
|
63
|
-
post_images:
|
|
70
|
+
post_images: service_types.enumLiteral('off', 'auto_configure', 'read_only').optional(),
|
|
71
|
+
/**
|
|
72
|
+
* Interval in seconds between source connection heartbeats. Null or omitted defaults to 60 seconds.
|
|
73
|
+
*/
|
|
74
|
+
heartbeat_interval_seconds: service_types.orNull(t.number).optional()
|
|
64
75
|
})
|
|
65
76
|
);
|
|
66
77
|
|
|
@@ -87,6 +98,20 @@ export function normalizeConnectionConfig(options: MongoConnectionConfigDecoded)
|
|
|
87
98
|
...base,
|
|
88
99
|
id: options.id ?? 'default',
|
|
89
100
|
tag: options.tag ?? 'default',
|
|
90
|
-
postImages: (options.post_images as PostImagesOption | undefined) ?? PostImagesOption.OFF
|
|
101
|
+
postImages: (options.post_images as PostImagesOption | undefined) ?? PostImagesOption.OFF,
|
|
102
|
+
heartbeat_interval_seconds: normalizeHeartbeatInterval(options.heartbeat_interval_seconds)
|
|
91
103
|
};
|
|
92
104
|
}
|
|
105
|
+
|
|
106
|
+
function normalizeHeartbeatInterval(value: number | null | undefined): number {
|
|
107
|
+
if (value == null) {
|
|
108
|
+
return DEFAULT_PING_INTERVAL_SECONDS;
|
|
109
|
+
}
|
|
110
|
+
if (!Number.isFinite(value) || value < MIN_PING_INTERVAL_SECONDS || value > MAX_PING_INTERVAL_SECONDS) {
|
|
111
|
+
throw new ServiceError(
|
|
112
|
+
ErrorCode.PSYNC_S1109,
|
|
113
|
+
`MongoDB connection: heartbeat_interval_seconds must be between ${MIN_PING_INTERVAL_SECONDS} and ${MAX_PING_INTERVAL_SECONDS} seconds; null or omitted defaults to ${DEFAULT_PING_INTERVAL_SECONDS}`
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# Running Tests Against DocumentDB
|
|
2
|
+
|
|
3
|
+
These instructions cover running the `module-mongodb` test suite against an Azure DocumentDB (formerly Cosmos DB for MongoDB vCore) cluster.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- An Azure DocumentDB (Cosmos DB for MongoDB vCore) cluster with change stream support
|
|
8
|
+
- Local PostgreSQL for PowerSync's internal storage (not the source database)
|
|
9
|
+
- The connection URI for the DocumentDB cluster
|
|
10
|
+
|
|
11
|
+
> **The open-source `documentdb-local` Docker image cannot be used for these tests.** The
|
|
12
|
+
> open-source DocumentDB engine does not implement change streams (`$changeStream is not
|
|
13
|
+
supported yet in native pipeline`), does not report the `documentdb_versions` `hello` field
|
|
14
|
+
> the suite uses for detection, and presents as `msg: isdbgrid`. An Azure-managed DocumentDB
|
|
15
|
+
> (vCore) cluster is required.
|
|
16
|
+
|
|
17
|
+
## Environment Variables
|
|
18
|
+
|
|
19
|
+
DocumentDB is detected automatically from the server: the test suite runs
|
|
20
|
+
`detectDocumentDb()` once at startup (see `DatabaseType.ts`) and gates the
|
|
21
|
+
DocumentDB-specific tests on the result. There is no separate enable flag — pointing
|
|
22
|
+
`MONGO_TEST_DATA_URL` at a DocumentDB cluster is what activates the DocumentDB tests.
|
|
23
|
+
|
|
24
|
+
| Variable | Required | Description |
|
|
25
|
+
| ------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
26
|
+
| `MONGO_TEST_DATA_URL` | Yes | DocumentDB connection URI. Must include a database name in the path (see below). Pointing this at a DocumentDB cluster enables the DocumentDB tests. |
|
|
27
|
+
| `PG_STORAGE_TEST_URL` | No | PostgreSQL connection for PowerSync storage. Defaults to `postgres://postgres:postgres@localhost:5432/powersync_storage_test`. |
|
|
28
|
+
| `TEST_MONGO_STORAGE` | No | Set to `false` to skip MongoDB storage tests. Recommended when testing against DocumentDB to avoid using it as a storage backend. |
|
|
29
|
+
| `TEST_TIMEOUT_MULTIPLIER` | No | Scale factor `testTimeout()` applies to a default timeout on DocumentDB when no explicit cloud override is given. Defaults to **6**. Increase for a slow/remote cluster. |
|
|
30
|
+
|
|
31
|
+
### Connection URI format
|
|
32
|
+
|
|
33
|
+
The `MONGO_TEST_DATA_URL` must include a database name in the path. DocumentDB URIs typically don't have one, so you need to add it before the query string:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
# Original URI (no database):
|
|
37
|
+
mongodb+srv://user:pass@cluster.mongocluster.cosmos.azure.com/
|
|
38
|
+
|
|
39
|
+
# With database added:
|
|
40
|
+
mongodb+srv://user:pass@cluster.mongocluster.cosmos.azure.com/powersync_test
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
If your password contains special characters (`=`, `@`, `+`, `/`), they must be URL-encoded in the URI (e.g., `=` becomes `%3D`). DocumentDB auto-generated passwords often contain `=` (base64).
|
|
44
|
+
|
|
45
|
+
## Commands
|
|
46
|
+
|
|
47
|
+
All commands run from the module directory: `modules/module-mongodb/`
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# Run all DocumentDB tests (integration + unit helpers):
|
|
51
|
+
MONGO_TEST_DATA_URL="mongodb+srv://user:pass@cluster.mongocluster.cosmos.azure.com/powersync_test" \
|
|
52
|
+
TEST_MONGO_STORAGE=false \
|
|
53
|
+
npx vitest run documentdb --reporter=verbose
|
|
54
|
+
|
|
55
|
+
# Run only integration tests:
|
|
56
|
+
MONGO_TEST_DATA_URL="<uri>" \
|
|
57
|
+
TEST_MONGO_STORAGE=false \
|
|
58
|
+
npx vitest run documentdb_mode --reporter=verbose
|
|
59
|
+
|
|
60
|
+
# Run only unit helper tests (no DocumentDB cluster needed):
|
|
61
|
+
npx vitest run documentdb_helpers --reporter=verbose
|
|
62
|
+
|
|
63
|
+
# Run a specific test by name:
|
|
64
|
+
MONGO_TEST_DATA_URL="<uri>" \
|
|
65
|
+
TEST_MONGO_STORAGE=false \
|
|
66
|
+
npx vitest run documentdb_mode -t "resume after restart" --reporter=verbose
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
If you have the URI in an environment variable (e.g., `$DOCUMENTDB_URI`), you can construct the test URL inline:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
DOCUMENTDB_TEST_URL=$(echo "$DOCUMENTDB_URI" | sed 's|\?|powersync_test?|')
|
|
73
|
+
MONGO_TEST_DATA_URL="$DOCUMENTDB_TEST_URL" \
|
|
74
|
+
TEST_MONGO_STORAGE=false \
|
|
75
|
+
npx vitest run documentdb --reporter=verbose
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## GitHub Actions
|
|
79
|
+
|
|
80
|
+
The `.github/workflows/documentdb-integration.yml` workflow runs these tests manually via `workflow_dispatch` only. Add a repository or organization secret named `AZURE_DOCUMENTDB_TEST_DATA_URL`, then dispatch the workflow. The workflow maps that secret to the test suite's `MONGO_TEST_DATA_URL` environment variable.
|
|
81
|
+
|
|
82
|
+
The URI must include a database name in the path. The tests clear/drop this database as part of setup, so use a dedicated test database and cluster.
|
|
83
|
+
|
|
84
|
+
The workflow starts a local MongoDB storage service, then runs the complete `modules/module-mongodb` `test` script against that storage backend. `MONGO_TEST_DATA_URL` is the remote DocumentDB source; `MONGO_TEST_URL` points at the local MongoDB storage test instance.
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
TEST_MONGO_STORAGE=true
|
|
88
|
+
TEST_POSTGRES_STORAGE=false
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Test Files
|
|
92
|
+
|
|
93
|
+
| File | Requires DocumentDB | Description |
|
|
94
|
+
| ---------------------------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
95
|
+
| `documentdb_mode.test.ts` | Yes | Integration tests: replication, sentinel checkpoints, write checkpoints, keepalive, resume. Skipped automatically unless `MONGO_TEST_DATA_URL` points at a DocumentDB cluster. |
|
|
96
|
+
| `documentdb_helpers.test.ts` | No (1 test needs MongoDB) | Unit tests: `getEventTimestamp`, sentinel parsing/matching, detection logic. Runs against any MongoDB or standalone. |
|
|
97
|
+
|
|
98
|
+
## What the Integration Tests Cover
|
|
99
|
+
|
|
100
|
+
Each integration test runs against 3 storage versions (v1, v2, v3) = 15 integration tests. Plus 15 unit tests in helpers = 30 total.
|
|
101
|
+
|
|
102
|
+
| Test | What it validates |
|
|
103
|
+
| ----------------------- | ----------------------------------------------------------------------------------------------- |
|
|
104
|
+
| basic replication | Insert, update, delete through change stream with wallTime timestamps |
|
|
105
|
+
| sentinel checkpoint | Checkpoint created with `mode: 'sentinel'`, resolved by matching document content in the stream |
|
|
106
|
+
| keepalive | Stream idles past the keepalive interval without crashing on DocumentDB resume tokens |
|
|
107
|
+
| write checkpoint | Full `createReplicationHead` → sentinel → polling flow for client write consistency |
|
|
108
|
+
| data events not dropped | Verifies `.lte()` dedup guard is skipped — events in the same wall-clock second are not lost |
|
|
109
|
+
| resume after restart | Stop streaming, create new context, resume from stored token |
|
|
110
|
+
|
|
111
|
+
There is also a **characterization test**, `does not report collection drop and rename events`,
|
|
112
|
+
that documents the current limitation: it writes DDL plus a post-DDL marker, waits for the marker
|
|
113
|
+
(proving the stream caught up), then asserts no `drop` / `rename` events were delivered. It passes
|
|
114
|
+
today and **fails if a future DocumentDB engine starts delivering DDL events** — a signal to add
|
|
115
|
+
real drop/rename replication support.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { detectDocumentDb } from '@module/replication/replication-utils.js';
|
|
2
|
+
import { logger } from '@powersync/lib-services-framework';
|
|
3
|
+
import { connectMongoData } from './util.js';
|
|
4
|
+
|
|
5
|
+
export enum DatabaseType {
|
|
6
|
+
DOCUMENTDB = 'DOCUMENTDB',
|
|
7
|
+
MONGODB = 'MONGODB'
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let _databaseType: DatabaseType = DatabaseType.MONGODB;
|
|
11
|
+
|
|
12
|
+
// Detected once at import time. Close the client afterwards so this detection
|
|
13
|
+
// does not leak a connection: this module is imported by many test files, and
|
|
14
|
+
// the client created here is otherwise never closed, accumulating open handles
|
|
15
|
+
// in Vitest.
|
|
16
|
+
const { client, db } = await connectMongoData();
|
|
17
|
+
try {
|
|
18
|
+
_databaseType = (await detectDocumentDb(db)) ? DatabaseType.DOCUMENTDB : DatabaseType.MONGODB;
|
|
19
|
+
} catch (ex) {
|
|
20
|
+
logger.warn(`Could not determine MongoDB database type`, ex);
|
|
21
|
+
} finally {
|
|
22
|
+
await client.close().catch(() => {});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const DATABASE_TYPE = _databaseType;
|