@powersync/service-module-mongodb 0.0.0-dev-20250102111825 → 0.0.0-dev-20250108073049
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 +8 -5
- package/dist/api/MongoRouteAPIAdapter.d.ts +1 -1
- package/dist/api/MongoRouteAPIAdapter.js +4 -3
- package/dist/api/MongoRouteAPIAdapter.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/module/MongoModule.d.ts +0 -1
- package/dist/module/MongoModule.js +2 -10
- package/dist/module/MongoModule.js.map +1 -1
- package/dist/replication/MongoRelation.js +15 -5
- package/dist/replication/MongoRelation.js.map +1 -1
- package/dist/types/types.d.ts +16 -22
- package/dist/types/types.js +4 -24
- package/dist/types/types.js.map +1 -1
- package/package.json +9 -10
- package/src/api/MongoRouteAPIAdapter.ts +4 -4
- package/src/index.ts +3 -4
- package/src/module/MongoModule.ts +2 -14
- package/src/replication/MongoRelation.ts +13 -5
- package/src/types/types.ts +8 -34
- package/test/src/change_stream.test.ts +2 -4
- package/test/src/env.ts +1 -1
- package/test/src/mongo_test.test.ts +69 -10
- package/test/src/setup.ts +4 -1
- package/test/src/slow_tests.test.ts +5 -12
- package/test/src/util.ts +5 -38
- package/test/tsconfig.json +1 -2
- package/tsconfig.json +6 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/db/db-index.d.ts +0 -1
- package/dist/db/db-index.js +0 -2
- package/dist/db/db-index.js.map +0 -1
- package/dist/db/mongo.d.ts +0 -35
- package/dist/db/mongo.js +0 -73
- package/dist/db/mongo.js.map +0 -1
- package/dist/locks/MongoLockManager.d.ts +0 -25
- package/dist/locks/MongoLockManager.js +0 -79
- package/dist/locks/MongoLockManager.js.map +0 -1
- package/dist/migrations/MongoMigrationAgent.d.ts +0 -12
- package/dist/migrations/MongoMigrationAgent.js +0 -25
- package/dist/migrations/MongoMigrationAgent.js.map +0 -1
- package/dist/migrations/db/migrations/1684951997326-init.d.ts +0 -3
- package/dist/migrations/db/migrations/1684951997326-init.js +0 -30
- package/dist/migrations/db/migrations/1684951997326-init.js.map +0 -1
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +0 -2
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +0 -5
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +0 -1
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +0 -3
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +0 -54
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +0 -1
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +0 -3
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +0 -26
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +0 -1
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +0 -3
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +0 -28
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +0 -1
- package/dist/migrations/mongo-migration-store.d.ts +0 -7
- package/dist/migrations/mongo-migration-store.js +0 -49
- package/dist/migrations/mongo-migration-store.js.map +0 -1
- package/dist/storage/MongoBucketStorage.d.ts +0 -48
- package/dist/storage/MongoBucketStorage.js +0 -426
- package/dist/storage/MongoBucketStorage.js.map +0 -1
- package/dist/storage/implementation/MongoBucketBatch.d.ts +0 -72
- package/dist/storage/implementation/MongoBucketBatch.js +0 -681
- package/dist/storage/implementation/MongoBucketBatch.js.map +0 -1
- package/dist/storage/implementation/MongoCompactor.d.ts +0 -40
- package/dist/storage/implementation/MongoCompactor.js +0 -300
- package/dist/storage/implementation/MongoCompactor.js.map +0 -1
- package/dist/storage/implementation/MongoIdSequence.d.ts +0 -12
- package/dist/storage/implementation/MongoIdSequence.js +0 -21
- package/dist/storage/implementation/MongoIdSequence.js.map +0 -1
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +0 -9
- package/dist/storage/implementation/MongoPersistedSyncRules.js +0 -9
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +0 -1
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +0 -20
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +0 -26
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +0 -1
- package/dist/storage/implementation/MongoStorageProvider.d.ts +0 -6
- package/dist/storage/implementation/MongoStorageProvider.js +0 -34
- package/dist/storage/implementation/MongoStorageProvider.js.map +0 -1
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +0 -36
- package/dist/storage/implementation/MongoSyncBucketStorage.js +0 -529
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +0 -1
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +0 -16
- package/dist/storage/implementation/MongoSyncRulesLock.js +0 -65
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +0 -1
- package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +0 -20
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js +0 -104
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +0 -1
- package/dist/storage/implementation/OperationBatch.d.ts +0 -34
- package/dist/storage/implementation/OperationBatch.js +0 -119
- package/dist/storage/implementation/OperationBatch.js.map +0 -1
- package/dist/storage/implementation/PersistedBatch.d.ts +0 -46
- package/dist/storage/implementation/PersistedBatch.js +0 -223
- package/dist/storage/implementation/PersistedBatch.js.map +0 -1
- package/dist/storage/implementation/config.d.ts +0 -19
- package/dist/storage/implementation/config.js +0 -26
- package/dist/storage/implementation/config.js.map +0 -1
- package/dist/storage/implementation/db.d.ts +0 -36
- package/dist/storage/implementation/db.js +0 -47
- package/dist/storage/implementation/db.js.map +0 -1
- package/dist/storage/implementation/models.d.ts +0 -139
- package/dist/storage/implementation/models.js +0 -2
- package/dist/storage/implementation/models.js.map +0 -1
- package/dist/storage/implementation/util.d.ts +0 -41
- package/dist/storage/implementation/util.js +0 -141
- package/dist/storage/implementation/util.js.map +0 -1
- package/dist/storage/storage-index.d.ts +0 -14
- package/dist/storage/storage-index.js +0 -15
- package/dist/storage/storage-index.js.map +0 -1
- package/src/db/db-index.ts +0 -1
- package/src/db/mongo.ts +0 -81
- package/src/locks/MongoLockManager.ts +0 -123
- package/src/migrations/MongoMigrationAgent.ts +0 -39
- package/src/migrations/db/migrations/1684951997326-init.ts +0 -39
- package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +0 -5
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +0 -105
- package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +0 -38
- package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +0 -40
- package/src/migrations/mongo-migration-store.ts +0 -62
- package/src/storage/MongoBucketStorage.ts +0 -531
- package/src/storage/implementation/MongoBucketBatch.ts +0 -896
- package/src/storage/implementation/MongoCompactor.ts +0 -381
- package/src/storage/implementation/MongoIdSequence.ts +0 -24
- package/src/storage/implementation/MongoPersistedSyncRules.ts +0 -16
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +0 -49
- package/src/storage/implementation/MongoStorageProvider.ts +0 -42
- package/src/storage/implementation/MongoSyncBucketStorage.ts +0 -612
- package/src/storage/implementation/MongoSyncRulesLock.ts +0 -88
- package/src/storage/implementation/MongoWriteCheckpointAPI.ts +0 -146
- package/src/storage/implementation/OperationBatch.ts +0 -129
- package/src/storage/implementation/PersistedBatch.ts +0 -283
- package/src/storage/implementation/config.ts +0 -40
- package/src/storage/implementation/db.ts +0 -87
- package/src/storage/implementation/models.ts +0 -161
- package/src/storage/implementation/util.ts +0 -154
- package/src/storage/storage-index.ts +0 -14
- package/test/src/__snapshots__/storage_sync.test.ts.snap +0 -332
- package/test/src/storage.test.ts +0 -7
- package/test/src/storage_compacting.test.ts +0 -6
- package/test/src/storage_sync.test.ts +0 -113
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { storage } from '@powersync/service-core';
|
|
2
|
-
import * as db from '../../db/db-index.js';
|
|
3
|
-
export function createPowerSyncMongo(config) {
|
|
4
|
-
return new PowerSyncMongo(db.mongo.createMongoClient(config), { database: config.database });
|
|
5
|
-
}
|
|
6
|
-
export class PowerSyncMongo {
|
|
7
|
-
constructor(client, options) {
|
|
8
|
-
this.client = client;
|
|
9
|
-
const db = client.db(options?.database, {
|
|
10
|
-
...storage.BSON_DESERIALIZE_OPTIONS
|
|
11
|
-
});
|
|
12
|
-
this.db = db;
|
|
13
|
-
this.current_data = db.collection('current_data');
|
|
14
|
-
this.bucket_data = db.collection('bucket_data');
|
|
15
|
-
this.bucket_parameters = db.collection('bucket_parameters');
|
|
16
|
-
this.op_id_sequence = db.collection('op_id_sequence');
|
|
17
|
-
this.sync_rules = db.collection('sync_rules');
|
|
18
|
-
this.source_tables = db.collection('source_tables');
|
|
19
|
-
this.custom_write_checkpoints = db.collection('custom_write_checkpoints');
|
|
20
|
-
this.write_checkpoints = db.collection('write_checkpoints');
|
|
21
|
-
this.instance = db.collection('instance');
|
|
22
|
-
this.locks = this.db.collection('locks');
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Clear all collections.
|
|
26
|
-
*/
|
|
27
|
-
async clear() {
|
|
28
|
-
await this.current_data.deleteMany({});
|
|
29
|
-
await this.bucket_data.deleteMany({});
|
|
30
|
-
await this.bucket_parameters.deleteMany({});
|
|
31
|
-
await this.op_id_sequence.deleteMany({});
|
|
32
|
-
await this.sync_rules.deleteMany({});
|
|
33
|
-
await this.source_tables.deleteMany({});
|
|
34
|
-
await this.write_checkpoints.deleteMany({});
|
|
35
|
-
await this.instance.deleteOne({});
|
|
36
|
-
await this.locks.deleteMany({});
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Drop the entire database.
|
|
40
|
-
*
|
|
41
|
-
* Primarily for tests.
|
|
42
|
-
*/
|
|
43
|
-
async drop() {
|
|
44
|
-
await this.db.dropDatabase();
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
//# sourceMappingURL=db.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"db.js","sourceRoot":"","sources":["../../../src/storage/implementation/db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAGlD,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAqB3C,MAAM,UAAU,oBAAoB,CAAC,MAAqC;IACxE,OAAO,IAAI,cAAc,CAAC,EAAE,CAAC,KAAK,CAAC,iBAAiB,CAAC,MAAM,CAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;AAC/F,CAAC;AAED,MAAM,OAAO,cAAc;IAezB,YAAY,MAAyB,EAAE,OAA+B;QACpE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QAErB,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,QAAQ,EAAE;YACtC,GAAG,OAAO,CAAC,wBAAwB;SACpC,CAAC,CAAC;QACH,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QAEb,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC,UAAU,CAAsB,cAAc,CAAC,CAAC;QACvE,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;QAChD,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC,UAAU,CAAC,mBAAmB,CAAC,CAAC;QAC5D,IAAI,CAAC,cAAc,GAAG,EAAE,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;QACtD,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;QAC9C,IAAI,CAAC,aAAa,GAAG,EAAE,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC;QACpD,IAAI,CAAC,wBAAwB,GAAG,EAAE,CAAC,UAAU,CAAC,0BAA0B,CAAC,CAAC;QAC1E,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC,UAAU,CAAC,mBAAmB,CAAC,CAAC;QAC5D,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;QAC1C,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC3C,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAK;QACT,MAAM,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QACvC,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QACtC,MAAM,IAAI,CAAC,iBAAiB,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QAC5C,MAAM,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QACzC,MAAM,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QACrC,MAAM,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QACxC,MAAM,IAAI,CAAC,iBAAiB,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QAC5C,MAAM,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;QAClC,MAAM,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,IAAI;QACR,MAAM,IAAI,CAAC,EAAE,CAAC,YAAY,EAAE,CAAC;IAC/B,CAAC;CACF"}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { storage } from '@powersync/service-core';
|
|
2
|
-
import { SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
3
|
-
import * as bson from 'bson';
|
|
4
|
-
/**
|
|
5
|
-
* Replica id uniquely identifying a row on the source database.
|
|
6
|
-
*
|
|
7
|
-
* Can be any value serializable to BSON.
|
|
8
|
-
*
|
|
9
|
-
* If the value is an entire document, the data serialized to a v5 UUID may be a good choice here.
|
|
10
|
-
*/
|
|
11
|
-
export type ReplicaId = bson.UUID | bson.Document | any;
|
|
12
|
-
export interface SourceKey {
|
|
13
|
-
/** group_id */
|
|
14
|
-
g: number;
|
|
15
|
-
/** source table id */
|
|
16
|
-
t: bson.ObjectId;
|
|
17
|
-
/** source key */
|
|
18
|
-
k: ReplicaId;
|
|
19
|
-
}
|
|
20
|
-
export interface BucketDataKey {
|
|
21
|
-
/** group_id */
|
|
22
|
-
g: number;
|
|
23
|
-
/** bucket name */
|
|
24
|
-
b: string;
|
|
25
|
-
/** op_id */
|
|
26
|
-
o: bigint;
|
|
27
|
-
}
|
|
28
|
-
export interface CurrentDataDocument {
|
|
29
|
-
_id: SourceKey;
|
|
30
|
-
data: bson.Binary;
|
|
31
|
-
buckets: CurrentBucket[];
|
|
32
|
-
lookups: bson.Binary[];
|
|
33
|
-
}
|
|
34
|
-
export interface CurrentBucket {
|
|
35
|
-
bucket: string;
|
|
36
|
-
table: string;
|
|
37
|
-
id: string;
|
|
38
|
-
}
|
|
39
|
-
export interface BucketParameterDocument {
|
|
40
|
-
_id: bigint;
|
|
41
|
-
key: SourceKey;
|
|
42
|
-
lookup: bson.Binary;
|
|
43
|
-
bucket_parameters: Record<string, SqliteJsonValue>[];
|
|
44
|
-
}
|
|
45
|
-
export interface BucketDataDocument {
|
|
46
|
-
_id: BucketDataKey;
|
|
47
|
-
op: OpType;
|
|
48
|
-
source_table?: bson.ObjectId;
|
|
49
|
-
source_key?: ReplicaId;
|
|
50
|
-
table?: string;
|
|
51
|
-
row_id?: string;
|
|
52
|
-
checksum: number;
|
|
53
|
-
data: string | null;
|
|
54
|
-
target_op?: bigint | null;
|
|
55
|
-
}
|
|
56
|
-
export type OpType = 'PUT' | 'REMOVE' | 'MOVE' | 'CLEAR';
|
|
57
|
-
export interface SourceTableDocument {
|
|
58
|
-
_id: bson.ObjectId;
|
|
59
|
-
group_id: number;
|
|
60
|
-
connection_id: number;
|
|
61
|
-
relation_id: number | string | undefined;
|
|
62
|
-
schema_name: string;
|
|
63
|
-
table_name: string;
|
|
64
|
-
replica_id_columns: string[] | null;
|
|
65
|
-
replica_id_columns2: {
|
|
66
|
-
name: string;
|
|
67
|
-
type_oid?: number;
|
|
68
|
-
type?: string;
|
|
69
|
-
}[] | undefined;
|
|
70
|
-
snapshot_done: boolean | undefined;
|
|
71
|
-
}
|
|
72
|
-
export interface IdSequenceDocument {
|
|
73
|
-
_id: string;
|
|
74
|
-
op_id: bigint;
|
|
75
|
-
}
|
|
76
|
-
export interface SyncRuleDocument {
|
|
77
|
-
_id: number;
|
|
78
|
-
state: storage.SyncRuleState;
|
|
79
|
-
/**
|
|
80
|
-
* True if initial snapshot has been replicated.
|
|
81
|
-
*
|
|
82
|
-
* Can only be false if state == PROCESSING.
|
|
83
|
-
*/
|
|
84
|
-
snapshot_done: boolean;
|
|
85
|
-
/**
|
|
86
|
-
* The last consistent checkpoint.
|
|
87
|
-
*
|
|
88
|
-
* There may be higher OpIds used in the database if we're in the middle of replicating a large transaction.
|
|
89
|
-
*/
|
|
90
|
-
last_checkpoint: bigint | null;
|
|
91
|
-
/**
|
|
92
|
-
* The LSN associated with the last consistent checkpoint.
|
|
93
|
-
*/
|
|
94
|
-
last_checkpoint_lsn: string | null;
|
|
95
|
-
/**
|
|
96
|
-
* If set, no new checkpoints may be created < this value.
|
|
97
|
-
*/
|
|
98
|
-
no_checkpoint_before: string | null;
|
|
99
|
-
/**
|
|
100
|
-
* Goes together with no_checkpoint_before.
|
|
101
|
-
*
|
|
102
|
-
* If a keepalive is triggered that creates the checkpoint > no_checkpoint_before,
|
|
103
|
-
* then the checkpoint must be equal to this keepalive_op.
|
|
104
|
-
*/
|
|
105
|
-
keepalive_op: string | null;
|
|
106
|
-
slot_name: string | null;
|
|
107
|
-
/**
|
|
108
|
-
* Last time we persisted a checkpoint.
|
|
109
|
-
*
|
|
110
|
-
* This may be old if no data is incoming.
|
|
111
|
-
*/
|
|
112
|
-
last_checkpoint_ts: Date | null;
|
|
113
|
-
/**
|
|
114
|
-
* Last time we persisted a checkpoint or keepalive.
|
|
115
|
-
*
|
|
116
|
-
* This should stay fairly current while replicating.
|
|
117
|
-
*/
|
|
118
|
-
last_keepalive_ts: Date | null;
|
|
119
|
-
/**
|
|
120
|
-
* If an error is stopping replication, it will be stored here.
|
|
121
|
-
*/
|
|
122
|
-
last_fatal_error: string | null;
|
|
123
|
-
content: string;
|
|
124
|
-
}
|
|
125
|
-
export interface CustomWriteCheckpointDocument {
|
|
126
|
-
_id: bson.ObjectId;
|
|
127
|
-
user_id: string;
|
|
128
|
-
checkpoint: bigint;
|
|
129
|
-
sync_rules_id: number;
|
|
130
|
-
}
|
|
131
|
-
export interface WriteCheckpointDocument {
|
|
132
|
-
_id: bson.ObjectId;
|
|
133
|
-
user_id: string;
|
|
134
|
-
lsns: Record<string, string>;
|
|
135
|
-
client_id: bigint;
|
|
136
|
-
}
|
|
137
|
-
export interface InstanceDocument {
|
|
138
|
-
_id: string;
|
|
139
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"models.js","sourceRoot":"","sources":["../../../src/storage/implementation/models.ts"],"names":[],"mappings":""}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { storage, utils } from '@powersync/service-core';
|
|
2
|
-
import * as bson from 'bson';
|
|
3
|
-
import * as mongo from 'mongodb';
|
|
4
|
-
import { BucketDataDocument } from './models.js';
|
|
5
|
-
export declare function idPrefixFilter<T>(prefix: Partial<T>, rest: (keyof T)[]): mongo.Condition<T>;
|
|
6
|
-
export declare function generateSlotName(prefix: string, sync_rules_id: number): string;
|
|
7
|
-
/**
|
|
8
|
-
* Read a single batch of data from a cursor, then close it.
|
|
9
|
-
*
|
|
10
|
-
* We do our best to avoid MongoDB fetching any more data than this single batch.
|
|
11
|
-
*
|
|
12
|
-
* This is similar to using `singleBatch: true` in find options.
|
|
13
|
-
* However, that makes `has_more` detection very difficult, since the cursor is always closed
|
|
14
|
-
* after the first batch. Instead, we do a workaround to only fetch a single batch below.
|
|
15
|
-
*
|
|
16
|
-
* For this to be effective, set batchSize = limit in the find command.
|
|
17
|
-
*/
|
|
18
|
-
export declare function readSingleBatch<T>(cursor: mongo.FindCursor<T>): Promise<{
|
|
19
|
-
data: T[];
|
|
20
|
-
hasMore: boolean;
|
|
21
|
-
}>;
|
|
22
|
-
export declare function mapOpEntry(row: BucketDataDocument): utils.OplogEntry;
|
|
23
|
-
export declare function replicaIdToSubkey(table: bson.ObjectId, id: storage.ReplicaId): string;
|
|
24
|
-
/**
|
|
25
|
-
* Helper function for creating a MongoDB client from consumers of this package
|
|
26
|
-
*/
|
|
27
|
-
export declare const createMongoClient: (url: string, options?: mongo.MongoClientOptions) => mongo.MongoClient;
|
|
28
|
-
/**
|
|
29
|
-
* MongoDB bulkWrite internally splits the operations into batches
|
|
30
|
-
* so that no batch exceeds 16MB. However, there are cases where
|
|
31
|
-
* the batch size is very close to 16MB, where additional metadata
|
|
32
|
-
* on the server pushes it over the limit, resulting in this error
|
|
33
|
-
* from the server:
|
|
34
|
-
*
|
|
35
|
-
* > MongoBulkWriteError: BSONObj size: 16814023 (0x1008FC7) is invalid. Size must be between 0 and 16793600(16MB) First element: insert: "bucket_data"
|
|
36
|
-
*
|
|
37
|
-
* We work around the issue by doing our own batching, limiting the
|
|
38
|
-
* batch size to 15MB. This does add additional overhead with
|
|
39
|
-
* BSON.calculateObjectSize.
|
|
40
|
-
*/
|
|
41
|
-
export declare function safeBulkWrite<T extends mongo.Document>(collection: mongo.Collection<T>, operations: mongo.AnyBulkWriteOperation<T>[], options: mongo.BulkWriteOptions): Promise<void>;
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { storage, utils } from '@powersync/service-core';
|
|
2
|
-
import * as bson from 'bson';
|
|
3
|
-
import * as crypto from 'crypto';
|
|
4
|
-
import * as mongo from 'mongodb';
|
|
5
|
-
import * as uuid from 'uuid';
|
|
6
|
-
export function idPrefixFilter(prefix, rest) {
|
|
7
|
-
let filter = {
|
|
8
|
-
$gte: {
|
|
9
|
-
...prefix
|
|
10
|
-
},
|
|
11
|
-
$lt: {
|
|
12
|
-
...prefix
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
for (let key of rest) {
|
|
16
|
-
filter.$gte[key] = new bson.MinKey();
|
|
17
|
-
filter.$lt[key] = new bson.MaxKey();
|
|
18
|
-
}
|
|
19
|
-
return filter;
|
|
20
|
-
}
|
|
21
|
-
export function generateSlotName(prefix, sync_rules_id) {
|
|
22
|
-
const slot_suffix = crypto.randomBytes(2).toString('hex');
|
|
23
|
-
return `${prefix}${sync_rules_id}_${slot_suffix}`;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Read a single batch of data from a cursor, then close it.
|
|
27
|
-
*
|
|
28
|
-
* We do our best to avoid MongoDB fetching any more data than this single batch.
|
|
29
|
-
*
|
|
30
|
-
* This is similar to using `singleBatch: true` in find options.
|
|
31
|
-
* However, that makes `has_more` detection very difficult, since the cursor is always closed
|
|
32
|
-
* after the first batch. Instead, we do a workaround to only fetch a single batch below.
|
|
33
|
-
*
|
|
34
|
-
* For this to be effective, set batchSize = limit in the find command.
|
|
35
|
-
*/
|
|
36
|
-
export async function readSingleBatch(cursor) {
|
|
37
|
-
try {
|
|
38
|
-
let data;
|
|
39
|
-
let hasMore = true;
|
|
40
|
-
// Let MongoDB load the first batch of data
|
|
41
|
-
const hasAny = await cursor.hasNext();
|
|
42
|
-
// Now it's in memory, and we can read it
|
|
43
|
-
data = cursor.readBufferedDocuments();
|
|
44
|
-
if (!hasAny || cursor.id?.isZero()) {
|
|
45
|
-
// A zero id means the cursor is exhaused.
|
|
46
|
-
// No results (hasAny == false) means even this batch doesn't have data.
|
|
47
|
-
// This should similar results as `await cursor.hasNext()`, but without
|
|
48
|
-
// actually fetching the next batch.
|
|
49
|
-
//
|
|
50
|
-
// Note that it is safe (but slightly inefficient) to return `hasMore: true`
|
|
51
|
-
// without there being more data, as long as the next batch
|
|
52
|
-
// will return `hasMore: false`.
|
|
53
|
-
hasMore = false;
|
|
54
|
-
}
|
|
55
|
-
return { data, hasMore };
|
|
56
|
-
}
|
|
57
|
-
finally {
|
|
58
|
-
// Match the from the cursor iterator logic here:
|
|
59
|
-
// https://github.com/mongodb/node-mongodb-native/blob/e02534e7d1c627bf50b85ca39f5995dbf165ad44/src/cursor/abstract_cursor.ts#L327-L331
|
|
60
|
-
if (!cursor.closed) {
|
|
61
|
-
await cursor.close();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
export function mapOpEntry(row) {
|
|
66
|
-
if (row.op == 'PUT' || row.op == 'REMOVE') {
|
|
67
|
-
return {
|
|
68
|
-
op_id: utils.timestampToOpId(row._id.o),
|
|
69
|
-
op: row.op,
|
|
70
|
-
object_type: row.table,
|
|
71
|
-
object_id: row.row_id,
|
|
72
|
-
checksum: Number(row.checksum),
|
|
73
|
-
subkey: replicaIdToSubkey(row.source_table, row.source_key),
|
|
74
|
-
data: row.data
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
// MOVE, CLEAR
|
|
79
|
-
return {
|
|
80
|
-
op_id: utils.timestampToOpId(row._id.o),
|
|
81
|
-
op: row.op,
|
|
82
|
-
checksum: Number(row.checksum)
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
export function replicaIdToSubkey(table, id) {
|
|
87
|
-
if (storage.isUUID(id)) {
|
|
88
|
-
// Special case for UUID for backwards-compatiblity
|
|
89
|
-
return `${table.toHexString()}/${id.toHexString()}`;
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
// Hashed UUID from the table and id
|
|
93
|
-
const repr = bson.serialize({ table, id });
|
|
94
|
-
return uuid.v5(repr, utils.ID_NAMESPACE);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Helper function for creating a MongoDB client from consumers of this package
|
|
99
|
-
*/
|
|
100
|
-
export const createMongoClient = (url, options) => {
|
|
101
|
-
return new mongo.MongoClient(url, options);
|
|
102
|
-
};
|
|
103
|
-
/**
|
|
104
|
-
* MongoDB bulkWrite internally splits the operations into batches
|
|
105
|
-
* so that no batch exceeds 16MB. However, there are cases where
|
|
106
|
-
* the batch size is very close to 16MB, where additional metadata
|
|
107
|
-
* on the server pushes it over the limit, resulting in this error
|
|
108
|
-
* from the server:
|
|
109
|
-
*
|
|
110
|
-
* > MongoBulkWriteError: BSONObj size: 16814023 (0x1008FC7) is invalid. Size must be between 0 and 16793600(16MB) First element: insert: "bucket_data"
|
|
111
|
-
*
|
|
112
|
-
* We work around the issue by doing our own batching, limiting the
|
|
113
|
-
* batch size to 15MB. This does add additional overhead with
|
|
114
|
-
* BSON.calculateObjectSize.
|
|
115
|
-
*/
|
|
116
|
-
export async function safeBulkWrite(collection, operations, options) {
|
|
117
|
-
// Must be below 16MB.
|
|
118
|
-
// We could probably go a little closer, but 15MB is a safe threshold.
|
|
119
|
-
const BULK_WRITE_LIMIT = 15 * 1024 * 1024;
|
|
120
|
-
let batch = [];
|
|
121
|
-
let currentSize = 0;
|
|
122
|
-
// Estimated overhead per operation, should be smaller in reality.
|
|
123
|
-
const keySize = 8;
|
|
124
|
-
for (let op of operations) {
|
|
125
|
-
const bsonSize = mongo.BSON.calculateObjectSize(op, {
|
|
126
|
-
checkKeys: false,
|
|
127
|
-
ignoreUndefined: true
|
|
128
|
-
}) + keySize;
|
|
129
|
-
if (batch.length > 0 && currentSize + bsonSize > BULK_WRITE_LIMIT) {
|
|
130
|
-
await collection.bulkWrite(batch, options);
|
|
131
|
-
currentSize = 0;
|
|
132
|
-
batch = [];
|
|
133
|
-
}
|
|
134
|
-
batch.push(op);
|
|
135
|
-
currentSize += bsonSize;
|
|
136
|
-
}
|
|
137
|
-
if (batch.length > 0) {
|
|
138
|
-
await collection.bulkWrite(batch, options);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
//# sourceMappingURL=util.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/storage/implementation/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAG7B,MAAM,UAAU,cAAc,CAAI,MAAkB,EAAE,IAAiB;IACrE,IAAI,MAAM,GAAG;QACX,IAAI,EAAE;YACJ,GAAG,MAAM;SACH;QACR,GAAG,EAAE;YACH,GAAG,MAAM;SACH;KACT,CAAC;IAEF,KAAK,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;QACrB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;QACrC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;IACtC,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,MAAc,EAAE,aAAqB;IACpE,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC1D,OAAO,GAAG,MAAM,GAAG,aAAa,IAAI,WAAW,EAAE,CAAC;AACpD,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAI,MAA2B;IAClE,IAAI,CAAC;QACH,IAAI,IAAS,CAAC;QACd,IAAI,OAAO,GAAG,IAAI,CAAC;QACnB,2CAA2C;QAC3C,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC;QACtC,yCAAyC;QACzC,IAAI,GAAG,MAAM,CAAC,qBAAqB,EAAE,CAAC;QACtC,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC;YACnC,0CAA0C;YAC1C,wEAAwE;YACxE,uEAAuE;YACvE,oCAAoC;YACpC,EAAE;YACF,4EAA4E;YAC5E,2DAA2D;YAC3D,gCAAgC;YAChC,OAAO,GAAG,KAAK,CAAC;QAClB,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC;YAAS,CAAC;QACT,iDAAiD;QACjD,uIAAuI;QACvI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;YACnB,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;AACH,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,GAAuB;IAChD,IAAI,GAAG,CAAC,EAAE,IAAI,KAAK,IAAI,GAAG,CAAC,EAAE,IAAI,QAAQ,EAAE,CAAC;QAC1C,OAAO;YACL,KAAK,EAAE,KAAK,CAAC,eAAe,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;YACvC,EAAE,EAAE,GAAG,CAAC,EAAE;YACV,WAAW,EAAE,GAAG,CAAC,KAAK;YACtB,SAAS,EAAE,GAAG,CAAC,MAAM;YACrB,QAAQ,EAAE,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;YAC9B,MAAM,EAAE,iBAAiB,CAAC,GAAG,CAAC,YAAa,EAAE,GAAG,CAAC,UAAW,CAAC;YAC7D,IAAI,EAAE,GAAG,CAAC,IAAI;SACf,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,cAAc;QAEd,OAAO;YACL,KAAK,EAAE,KAAK,CAAC,eAAe,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;YACvC,EAAE,EAAE,GAAG,CAAC,EAAE;YACV,QAAQ,EAAE,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;SAC/B,CAAC;IACJ,CAAC;AACH,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,KAAoB,EAAE,EAAqB;IAC3E,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;QACvB,mDAAmD;QACnD,OAAO,GAAG,KAAK,CAAC,WAAW,EAAE,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC;IACtD,CAAC;SAAM,CAAC;QACN,oCAAoC;QACpC,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,CAAC;QAC3C,OAAO,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC;IAC3C,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,GAAW,EAAE,OAAkC,EAAE,EAAE;IACnF,OAAO,IAAI,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AAC7C,CAAC,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,UAA+B,EAC/B,UAA4C,EAC5C,OAA+B;IAE/B,sBAAsB;IACtB,sEAAsE;IACtE,MAAM,gBAAgB,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;IAE1C,IAAI,KAAK,GAAqC,EAAE,CAAC;IACjD,IAAI,WAAW,GAAG,CAAC,CAAC;IACpB,kEAAkE;IAClE,MAAM,OAAO,GAAG,CAAC,CAAC;IAClB,KAAK,IAAI,EAAE,IAAI,UAAU,EAAE,CAAC;QAC1B,MAAM,QAAQ,GACZ,KAAK,CAAC,IAAI,CAAC,mBAAmB,CAAC,EAAE,EAAE;YACjC,SAAS,EAAE,KAAK;YAChB,eAAe,EAAE,IAAI;SACf,CAAC,GAAG,OAAO,CAAC;QACtB,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,WAAW,GAAG,QAAQ,GAAG,gBAAgB,EAAE,CAAC;YAClE,MAAM,UAAU,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YAC3C,WAAW,GAAG,CAAC,CAAC;YAChB,KAAK,GAAG,EAAE,CAAC;QACb,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACf,WAAW,IAAI,QAAQ,CAAC;IAC1B,CAAC;IACD,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACrB,MAAM,UAAU,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC7C,CAAC;AACH,CAAC"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export * from './implementation/config.js';
|
|
2
|
-
export * from './implementation/db.js';
|
|
3
|
-
export * from './implementation/models.js';
|
|
4
|
-
export * from './implementation/MongoBucketBatch.js';
|
|
5
|
-
export * from './implementation/MongoIdSequence.js';
|
|
6
|
-
export * from './implementation/MongoPersistedSyncRules.js';
|
|
7
|
-
export * from './implementation/MongoPersistedSyncRulesContent.js';
|
|
8
|
-
export * from './implementation/MongoStorageProvider.js';
|
|
9
|
-
export * from './implementation/MongoSyncBucketStorage.js';
|
|
10
|
-
export * from './implementation/MongoSyncRulesLock.js';
|
|
11
|
-
export * from './implementation/OperationBatch.js';
|
|
12
|
-
export * from './implementation/PersistedBatch.js';
|
|
13
|
-
export * from './implementation/util.js';
|
|
14
|
-
export * from './MongoBucketStorage.js';
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export * from './implementation/config.js';
|
|
2
|
-
export * from './implementation/db.js';
|
|
3
|
-
export * from './implementation/models.js';
|
|
4
|
-
export * from './implementation/MongoBucketBatch.js';
|
|
5
|
-
export * from './implementation/MongoIdSequence.js';
|
|
6
|
-
export * from './implementation/MongoPersistedSyncRules.js';
|
|
7
|
-
export * from './implementation/MongoPersistedSyncRulesContent.js';
|
|
8
|
-
export * from './implementation/MongoStorageProvider.js';
|
|
9
|
-
export * from './implementation/MongoSyncBucketStorage.js';
|
|
10
|
-
export * from './implementation/MongoSyncRulesLock.js';
|
|
11
|
-
export * from './implementation/OperationBatch.js';
|
|
12
|
-
export * from './implementation/PersistedBatch.js';
|
|
13
|
-
export * from './implementation/util.js';
|
|
14
|
-
export * from './MongoBucketStorage.js';
|
|
15
|
-
//# sourceMappingURL=storage-index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"storage-index.js","sourceRoot":"","sources":["../../src/storage/storage-index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,sCAAsC,CAAC;AACrD,cAAc,qCAAqC,CAAC;AACpD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,oDAAoD,CAAC;AACnE,cAAc,0CAA0C,CAAC;AACzD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,wCAAwC,CAAC;AACvD,cAAc,oCAAoC,CAAC;AACnD,cAAc,oCAAoC,CAAC;AACnD,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC"}
|
package/src/db/db-index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * as mongo from './mongo.js';
|
package/src/db/mongo.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import * as mongo from 'mongodb';
|
|
2
|
-
import * as timers from 'timers/promises';
|
|
3
|
-
|
|
4
|
-
import { configFile } from '@powersync/service-types';
|
|
5
|
-
import { normalizeMongoConfig } from '../storage/storage-index.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Time for new connection to timeout.
|
|
9
|
-
*/
|
|
10
|
-
export const MONGO_CONNECT_TIMEOUT_MS = 10_000;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Time for individual requests to timeout the socket.
|
|
14
|
-
*/
|
|
15
|
-
export const MONGO_SOCKET_TIMEOUT_MS = 60_000;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Time for individual requests to timeout the operation.
|
|
19
|
-
*
|
|
20
|
-
* This is time spent on the cursor, not total time.
|
|
21
|
-
*
|
|
22
|
-
* Must be less than MONGO_SOCKET_TIMEOUT_MS to ensure proper error handling.
|
|
23
|
-
*/
|
|
24
|
-
export const MONGO_OPERATION_TIMEOUT_MS = 30_000;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Same as above, but specifically for clear operations.
|
|
28
|
-
*
|
|
29
|
-
* These are retried when reaching the timeout.
|
|
30
|
-
*/
|
|
31
|
-
export const MONGO_CLEAR_OPERATION_TIMEOUT_MS = 5_000;
|
|
32
|
-
|
|
33
|
-
export function createMongoClient(config: configFile.MongoStorageConfig) {
|
|
34
|
-
const normalized = normalizeMongoConfig(config);
|
|
35
|
-
return new mongo.MongoClient(normalized.uri, {
|
|
36
|
-
auth: {
|
|
37
|
-
username: normalized.username,
|
|
38
|
-
password: normalized.password
|
|
39
|
-
},
|
|
40
|
-
// Time for connection to timeout
|
|
41
|
-
connectTimeoutMS: MONGO_CONNECT_TIMEOUT_MS,
|
|
42
|
-
// Time for individual requests to timeout
|
|
43
|
-
socketTimeoutMS: MONGO_SOCKET_TIMEOUT_MS,
|
|
44
|
-
// How long to wait for new primary selection
|
|
45
|
-
serverSelectionTimeoutMS: 30_000,
|
|
46
|
-
|
|
47
|
-
// Avoid too many connections:
|
|
48
|
-
// 1. It can overwhelm the source database.
|
|
49
|
-
// 2. Processing too many queries in parallel can cause the process to run out of memory.
|
|
50
|
-
maxPoolSize: 8,
|
|
51
|
-
|
|
52
|
-
maxConnecting: 3,
|
|
53
|
-
maxIdleTimeMS: 60_000
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Wait up to a minute for authentication errors to resolve.
|
|
59
|
-
*
|
|
60
|
-
* There can be a delay between an Atlas user being created, and that user being
|
|
61
|
-
* available on the database cluster. This works around it.
|
|
62
|
-
*
|
|
63
|
-
* This is specifically relevant for migrations and teardown - other parts of the stack
|
|
64
|
-
* can generate handle these failures and just retry or restart.
|
|
65
|
-
*/
|
|
66
|
-
export async function waitForAuth(db: mongo.Db) {
|
|
67
|
-
const start = Date.now();
|
|
68
|
-
while (Date.now() - start < 60_000) {
|
|
69
|
-
try {
|
|
70
|
-
await db.command({ ping: 1 });
|
|
71
|
-
// Success
|
|
72
|
-
break;
|
|
73
|
-
} catch (e) {
|
|
74
|
-
if (e.codeName == 'AuthenticationFailed') {
|
|
75
|
-
await timers.setTimeout(1_000);
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
throw e;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import * as framework from '@powersync/lib-services-framework';
|
|
2
|
-
import * as bson from 'bson';
|
|
3
|
-
import * as mongo from 'mongodb';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Lock Document Schema
|
|
7
|
-
*/
|
|
8
|
-
export type Lock = {
|
|
9
|
-
name: string;
|
|
10
|
-
active_lock?: {
|
|
11
|
-
lock_id: bson.ObjectId;
|
|
12
|
-
ts: Date;
|
|
13
|
-
};
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export type Collection = mongo.Collection<Lock>;
|
|
17
|
-
|
|
18
|
-
export type MongoLockManagerParams = framework.locks.LockManagerParams & {
|
|
19
|
-
collection: Collection;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const DEFAULT_LOCK_TIMEOUT = 60 * 1000; // 1 minute
|
|
23
|
-
|
|
24
|
-
export class MongoLockManager extends framework.locks.AbstractLockManager {
|
|
25
|
-
collection: Collection;
|
|
26
|
-
constructor(params: MongoLockManagerParams) {
|
|
27
|
-
super(params);
|
|
28
|
-
this.collection = params.collection;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
protected async acquireHandle(options?: framework.LockAcquireOptions): Promise<framework.LockHandle | null> {
|
|
32
|
-
const lock_id = await this.getHandle();
|
|
33
|
-
if (!lock_id) {
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
refresh: () => this.refreshHandle(lock_id),
|
|
38
|
-
release: () => this.releaseHandle(lock_id)
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
protected async refreshHandle(lock_id: bson.ObjectId) {
|
|
43
|
-
const res = await this.collection.updateOne(
|
|
44
|
-
{
|
|
45
|
-
'active_lock.lock_id': lock_id
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
$set: {
|
|
49
|
-
'active_lock.ts': new Date()
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
if (res.modifiedCount === 0) {
|
|
55
|
-
throw new Error('Lock not found, could not refresh');
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
protected async getHandle() {
|
|
60
|
-
const now = new Date();
|
|
61
|
-
const lock_timeout = this.params.timeout ?? DEFAULT_LOCK_TIMEOUT;
|
|
62
|
-
const lock_id = new bson.ObjectId();
|
|
63
|
-
|
|
64
|
-
const { name } = this.params;
|
|
65
|
-
await this.collection.updateOne(
|
|
66
|
-
{
|
|
67
|
-
name
|
|
68
|
-
},
|
|
69
|
-
{
|
|
70
|
-
$setOnInsert: {
|
|
71
|
-
name
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
upsert: true
|
|
76
|
-
}
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
const expired_ts = now.getTime() - lock_timeout;
|
|
80
|
-
|
|
81
|
-
const res = await this.collection.updateOne(
|
|
82
|
-
{
|
|
83
|
-
$and: [
|
|
84
|
-
{ name: name },
|
|
85
|
-
{
|
|
86
|
-
$or: [{ active_lock: { $exists: false } }, { 'active_lock.ts': { $lte: new Date(expired_ts) } }]
|
|
87
|
-
}
|
|
88
|
-
]
|
|
89
|
-
},
|
|
90
|
-
{
|
|
91
|
-
$set: {
|
|
92
|
-
active_lock: {
|
|
93
|
-
lock_id: lock_id,
|
|
94
|
-
ts: now
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
if (res.modifiedCount === 0) {
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return lock_id;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
protected async releaseHandle(lock_id: bson.ObjectId) {
|
|
108
|
-
const res = await this.collection.updateOne(
|
|
109
|
-
{
|
|
110
|
-
'active_lock.lock_id': lock_id
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
$unset: {
|
|
114
|
-
active_lock: true
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
if (res.modifiedCount === 0) {
|
|
120
|
-
throw new Error('Lock not found, could not release');
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import * as framework from '@powersync/lib-services-framework';
|
|
2
|
-
|
|
3
|
-
import { migrations } from '@powersync/service-core';
|
|
4
|
-
import { configFile } from '@powersync/service-types';
|
|
5
|
-
import * as path from 'path';
|
|
6
|
-
import { fileURLToPath } from 'url';
|
|
7
|
-
import { MongoLockManager } from '../locks/MongoLockManager.js';
|
|
8
|
-
import { createPowerSyncMongo, PowerSyncMongo } from '../storage/storage-index.js';
|
|
9
|
-
import { createMongoMigrationStore } from './mongo-migration-store.js';
|
|
10
|
-
|
|
11
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
-
const __dirname = path.dirname(__filename);
|
|
13
|
-
|
|
14
|
-
const MONGO_LOCK_PROCESS = 'migrations';
|
|
15
|
-
const MIGRATIONS_DIR = path.join(__dirname, '/db/migrations');
|
|
16
|
-
|
|
17
|
-
export class MongoMigrationAgent extends migrations.AbstractPowerSyncMigrationAgent {
|
|
18
|
-
store: framework.MigrationStore;
|
|
19
|
-
locks: framework.LockManager;
|
|
20
|
-
|
|
21
|
-
protected client: PowerSyncMongo;
|
|
22
|
-
|
|
23
|
-
constructor(mongoConfig: configFile.MongoStorageConfig) {
|
|
24
|
-
super();
|
|
25
|
-
|
|
26
|
-
this.client = createPowerSyncMongo(mongoConfig);
|
|
27
|
-
|
|
28
|
-
this.store = createMongoMigrationStore(this.client.db);
|
|
29
|
-
this.locks = new MongoLockManager({ collection: this.client.locks, name: MONGO_LOCK_PROCESS });
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
getInternalScriptsDir(): string {
|
|
33
|
-
return MIGRATIONS_DIR;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async [Symbol.asyncDispose](): Promise<void> {
|
|
37
|
-
await this.client.client.close();
|
|
38
|
-
}
|
|
39
|
-
}
|