@powersync/service-module-mongodb 0.0.0-dev-20241128134723 → 0.0.0-dev-20241219110735
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 +69 -4
- package/dist/db/db-index.d.ts +1 -0
- package/dist/db/db-index.js +2 -0
- package/dist/db/db-index.js.map +1 -0
- package/dist/db/mongo.d.ts +35 -0
- package/dist/db/mongo.js +73 -0
- package/dist/db/mongo.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/locks/MonogLocks.d.ts +36 -0
- package/dist/locks/MonogLocks.js +83 -0
- package/dist/locks/MonogLocks.js.map +1 -0
- package/dist/migrations/MonogMigrationAgent.d.ts +12 -0
- package/dist/migrations/MonogMigrationAgent.js +25 -0
- package/dist/migrations/MonogMigrationAgent.js.map +1 -0
- package/dist/migrations/db/migrations/1684951997326-init.d.ts +3 -0
- package/dist/migrations/db/migrations/1684951997326-init.js +30 -0
- package/dist/migrations/db/migrations/1684951997326-init.js.map +1 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +2 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +5 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +3 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +54 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +3 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +26 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +1 -0
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +3 -0
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +28 -0
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +1 -0
- package/dist/migrations/mongo-migration-store.d.ts +7 -0
- package/dist/migrations/mongo-migration-store.js +49 -0
- package/dist/migrations/mongo-migration-store.js.map +1 -0
- package/dist/module/MongoModule.js +15 -4
- package/dist/module/MongoModule.js.map +1 -1
- package/dist/replication/MongoManager.d.ts +1 -1
- package/dist/replication/MongoManager.js +3 -2
- package/dist/replication/MongoManager.js.map +1 -1
- package/dist/storage/MongoBucketStorage.d.ts +48 -0
- package/dist/storage/MongoBucketStorage.js +425 -0
- package/dist/storage/MongoBucketStorage.js.map +1 -0
- package/dist/storage/implementation/MongoBucketBatch.d.ts +72 -0
- package/dist/storage/implementation/MongoBucketBatch.js +681 -0
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -0
- package/dist/storage/implementation/MongoCompactor.d.ts +40 -0
- package/dist/storage/implementation/MongoCompactor.js +310 -0
- package/dist/storage/implementation/MongoCompactor.js.map +1 -0
- package/dist/storage/implementation/MongoIdSequence.d.ts +12 -0
- package/dist/storage/implementation/MongoIdSequence.js +21 -0
- package/dist/storage/implementation/MongoIdSequence.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +9 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.js +9 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +20 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +26 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +1 -0
- package/dist/storage/implementation/MongoStorageProvider.d.ts +6 -0
- package/dist/storage/implementation/MongoStorageProvider.js +34 -0
- package/dist/storage/implementation/MongoStorageProvider.js.map +1 -0
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +36 -0
- package/dist/storage/implementation/MongoSyncBucketStorage.js +529 -0
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -0
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +16 -0
- package/dist/storage/implementation/MongoSyncRulesLock.js +65 -0
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -0
- package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +20 -0
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js +104 -0
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -0
- package/dist/storage/implementation/OperationBatch.d.ts +34 -0
- package/dist/storage/implementation/OperationBatch.js +119 -0
- package/dist/storage/implementation/OperationBatch.js.map +1 -0
- package/dist/storage/implementation/PersistedBatch.d.ts +46 -0
- package/dist/storage/implementation/PersistedBatch.js +223 -0
- package/dist/storage/implementation/PersistedBatch.js.map +1 -0
- package/dist/storage/implementation/config.d.ts +19 -0
- package/dist/storage/implementation/config.js +26 -0
- package/dist/storage/implementation/config.js.map +1 -0
- package/dist/storage/implementation/db.d.ts +36 -0
- package/dist/storage/implementation/db.js +47 -0
- package/dist/storage/implementation/db.js.map +1 -0
- package/dist/storage/implementation/models.d.ts +139 -0
- package/dist/storage/implementation/models.js +2 -0
- package/dist/storage/implementation/models.js.map +1 -0
- package/dist/storage/implementation/util.d.ts +58 -0
- package/dist/storage/implementation/util.js +196 -0
- package/dist/storage/implementation/util.js.map +1 -0
- package/dist/storage/storage-index.d.ts +14 -0
- package/dist/storage/storage-index.js +15 -0
- package/dist/storage/storage-index.js.map +1 -0
- package/dist/types/types.d.ts +3 -0
- package/dist/types/types.js +4 -1
- package/dist/types/types.js.map +1 -1
- package/package.json +11 -8
- package/src/db/db-index.ts +1 -0
- package/src/db/mongo.ts +81 -0
- package/src/index.ts +4 -0
- package/src/locks/MonogLocks.ts +147 -0
- package/src/migrations/MonogMigrationAgent.ts +39 -0
- package/src/migrations/db/migrations/1684951997326-init.ts +39 -0
- package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +5 -0
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +105 -0
- package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +38 -0
- package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +40 -0
- package/src/migrations/mongo-migration-store.ts +62 -0
- package/src/module/MongoModule.ts +18 -4
- package/src/replication/MongoManager.ts +6 -2
- package/src/storage/MongoBucketStorage.ts +530 -0
- package/src/storage/implementation/MongoBucketBatch.ts +893 -0
- package/src/storage/implementation/MongoCompactor.ts +392 -0
- package/src/storage/implementation/MongoIdSequence.ts +24 -0
- package/src/storage/implementation/MongoPersistedSyncRules.ts +16 -0
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +49 -0
- package/src/storage/implementation/MongoStorageProvider.ts +42 -0
- package/src/storage/implementation/MongoSyncBucketStorage.ts +612 -0
- package/src/storage/implementation/MongoSyncRulesLock.ts +88 -0
- package/src/storage/implementation/MongoWriteCheckpointAPI.ts +146 -0
- package/src/storage/implementation/OperationBatch.ts +130 -0
- package/src/storage/implementation/PersistedBatch.ts +283 -0
- package/src/storage/implementation/config.ts +40 -0
- package/src/storage/implementation/db.ts +88 -0
- package/src/storage/implementation/models.ts +160 -0
- package/src/storage/implementation/util.ts +209 -0
- package/src/storage/storage-index.ts +14 -0
- package/src/types/types.ts +8 -1
- package/test/src/__snapshots__/storage_sync.test.ts.snap +332 -0
- package/test/src/change_stream.test.ts +34 -33
- package/test/src/change_stream_utils.ts +6 -6
- package/test/src/env.ts +1 -0
- package/test/src/slow_tests.test.ts +4 -4
- package/test/src/storage.test.ts +7 -0
- package/test/src/storage_compacting.test.ts +6 -0
- package/test/src/storage_sync.test.ts +113 -0
- package/test/src/util.ts +20 -7
- package/test/tsconfig.json +4 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +1 -1
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { storage } from '@powersync/service-core';
|
|
2
|
+
import { SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
3
|
+
import * as bson from 'bson';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Replica id uniquely identifying a row on the source database.
|
|
7
|
+
*
|
|
8
|
+
* Can be any value serializable to BSON.
|
|
9
|
+
*
|
|
10
|
+
* If the value is an entire document, the data serialized to a v5 UUID may be a good choice here.
|
|
11
|
+
*/
|
|
12
|
+
export type ReplicaId = bson.UUID | bson.Document | any;
|
|
13
|
+
|
|
14
|
+
export interface SourceKey {
|
|
15
|
+
/** group_id */
|
|
16
|
+
g: number;
|
|
17
|
+
/** source table id */
|
|
18
|
+
t: bson.ObjectId;
|
|
19
|
+
/** source key */
|
|
20
|
+
k: ReplicaId;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CurrentDataDocument {
|
|
24
|
+
_id: SourceKey;
|
|
25
|
+
data: bson.Binary;
|
|
26
|
+
buckets: CurrentBucket[];
|
|
27
|
+
lookups: bson.Binary[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface CurrentBucket {
|
|
31
|
+
bucket: string;
|
|
32
|
+
table: string;
|
|
33
|
+
id: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface BucketParameterDocument {
|
|
37
|
+
_id: bigint;
|
|
38
|
+
key: SourceKey;
|
|
39
|
+
lookup: bson.Binary;
|
|
40
|
+
bucket_parameters: Record<string, SqliteJsonValue>[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface BucketDataKey {
|
|
44
|
+
/** group_id */
|
|
45
|
+
g: number;
|
|
46
|
+
/** bucket name */
|
|
47
|
+
b: string;
|
|
48
|
+
/** op_id */
|
|
49
|
+
o: bigint;
|
|
50
|
+
}
|
|
51
|
+
export interface BucketDataDocument {
|
|
52
|
+
_id: BucketDataKey;
|
|
53
|
+
op: OpType;
|
|
54
|
+
source_table?: bson.ObjectId;
|
|
55
|
+
source_key?: storage.ReplicaId;
|
|
56
|
+
table?: string;
|
|
57
|
+
row_id?: string;
|
|
58
|
+
checksum: number;
|
|
59
|
+
data: string | null;
|
|
60
|
+
target_op?: bigint | null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type OpType = 'PUT' | 'REMOVE' | 'MOVE' | 'CLEAR';
|
|
64
|
+
|
|
65
|
+
export interface SourceTableDocument {
|
|
66
|
+
_id: bson.ObjectId;
|
|
67
|
+
group_id: number;
|
|
68
|
+
connection_id: number;
|
|
69
|
+
relation_id: number | string | undefined;
|
|
70
|
+
schema_name: string;
|
|
71
|
+
table_name: string;
|
|
72
|
+
replica_id_columns: string[] | null;
|
|
73
|
+
replica_id_columns2: { name: string; type_oid?: number; type?: string }[] | undefined;
|
|
74
|
+
snapshot_done: boolean | undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface IdSequenceDocument {
|
|
78
|
+
_id: string;
|
|
79
|
+
op_id: bigint;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface SyncRuleDocument {
|
|
83
|
+
_id: number;
|
|
84
|
+
|
|
85
|
+
state: storage.SyncRuleState;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* True if initial snapshot has been replicated.
|
|
89
|
+
*
|
|
90
|
+
* Can only be false if state == PROCESSING.
|
|
91
|
+
*/
|
|
92
|
+
snapshot_done: boolean;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The last consistent checkpoint.
|
|
96
|
+
*
|
|
97
|
+
* There may be higher OpIds used in the database if we're in the middle of replicating a large transaction.
|
|
98
|
+
*/
|
|
99
|
+
last_checkpoint: bigint | null;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The LSN associated with the last consistent checkpoint.
|
|
103
|
+
*/
|
|
104
|
+
last_checkpoint_lsn: string | null;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* If set, no new checkpoints may be created < this value.
|
|
108
|
+
*/
|
|
109
|
+
no_checkpoint_before: string | null;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Goes together with no_checkpoint_before.
|
|
113
|
+
*
|
|
114
|
+
* If a keepalive is triggered that creates the checkpoint > no_checkpoint_before,
|
|
115
|
+
* then the checkpoint must be equal to this keepalive_op.
|
|
116
|
+
*/
|
|
117
|
+
keepalive_op: string | null;
|
|
118
|
+
|
|
119
|
+
slot_name: string | null;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Last time we persisted a checkpoint.
|
|
123
|
+
*
|
|
124
|
+
* This may be old if no data is incoming.
|
|
125
|
+
*/
|
|
126
|
+
last_checkpoint_ts: Date | null;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Last time we persisted a checkpoint or keepalive.
|
|
130
|
+
*
|
|
131
|
+
* This should stay fairly current while replicating.
|
|
132
|
+
*/
|
|
133
|
+
last_keepalive_ts: Date | null;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* If an error is stopping replication, it will be stored here.
|
|
137
|
+
*/
|
|
138
|
+
last_fatal_error: string | null;
|
|
139
|
+
|
|
140
|
+
content: string;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface CustomWriteCheckpointDocument {
|
|
144
|
+
_id: bson.ObjectId;
|
|
145
|
+
user_id: string;
|
|
146
|
+
checkpoint: bigint;
|
|
147
|
+
sync_rules_id: number;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface WriteCheckpointDocument {
|
|
151
|
+
_id: bson.ObjectId;
|
|
152
|
+
user_id: string;
|
|
153
|
+
lsns: Record<string, string>;
|
|
154
|
+
client_id: bigint;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface InstanceDocument {
|
|
158
|
+
// The instance UUID
|
|
159
|
+
_id: string;
|
|
160
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { storage, utils } from '@powersync/service-core';
|
|
2
|
+
import { SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
3
|
+
import * as bson from 'bson';
|
|
4
|
+
import * as crypto from 'crypto';
|
|
5
|
+
import * as mongo from 'mongodb';
|
|
6
|
+
import * as uuid from 'uuid';
|
|
7
|
+
import { BucketDataDocument } from './models.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Lookup serialization must be number-agnostic. I.e. normalize numbers, instead of preserving numbers.
|
|
11
|
+
* @param lookup
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export function serializeLookup(lookup: SqliteJsonValue[]) {
|
|
15
|
+
const normalized = lookup.map((value) => {
|
|
16
|
+
if (typeof value == 'number' && Number.isInteger(value)) {
|
|
17
|
+
return BigInt(value);
|
|
18
|
+
} else {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
return new bson.Binary(bson.serialize({ l: normalized }));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function idPrefixFilter<T>(prefix: Partial<T>, rest: (keyof T)[]): mongo.Condition<T> {
|
|
26
|
+
let filter = {
|
|
27
|
+
$gte: {
|
|
28
|
+
...prefix
|
|
29
|
+
} as any,
|
|
30
|
+
$lt: {
|
|
31
|
+
...prefix
|
|
32
|
+
} as any
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
for (let key of rest) {
|
|
36
|
+
filter.$gte[key] = new bson.MinKey();
|
|
37
|
+
filter.$lt[key] = new bson.MaxKey();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return filter;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function generateSlotName(prefix: string, sync_rules_id: number) {
|
|
44
|
+
const slot_suffix = crypto.randomBytes(2).toString('hex');
|
|
45
|
+
return `${prefix}${sync_rules_id}_${slot_suffix}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Read a single batch of data from a cursor, then close it.
|
|
50
|
+
*
|
|
51
|
+
* We do our best to avoid MongoDB fetching any more data than this single batch.
|
|
52
|
+
*
|
|
53
|
+
* This is similar to using `singleBatch: true` in find options.
|
|
54
|
+
* However, that makes `has_more` detection very difficult, since the cursor is always closed
|
|
55
|
+
* after the first batch. Instead, we do a workaround to only fetch a single batch below.
|
|
56
|
+
*
|
|
57
|
+
* For this to be effective, set batchSize = limit in the find command.
|
|
58
|
+
*/
|
|
59
|
+
export async function readSingleBatch<T>(cursor: mongo.FindCursor<T>): Promise<{ data: T[]; hasMore: boolean }> {
|
|
60
|
+
try {
|
|
61
|
+
let data: T[];
|
|
62
|
+
let hasMore = true;
|
|
63
|
+
// Let MongoDB load the first batch of data
|
|
64
|
+
const hasAny = await cursor.hasNext();
|
|
65
|
+
// Now it's in memory, and we can read it
|
|
66
|
+
data = cursor.readBufferedDocuments();
|
|
67
|
+
if (!hasAny || cursor.id?.isZero()) {
|
|
68
|
+
// A zero id means the cursor is exhaused.
|
|
69
|
+
// No results (hasAny == false) means even this batch doesn't have data.
|
|
70
|
+
// This should similar results as `await cursor.hasNext()`, but without
|
|
71
|
+
// actually fetching the next batch.
|
|
72
|
+
//
|
|
73
|
+
// Note that it is safe (but slightly inefficient) to return `hasMore: true`
|
|
74
|
+
// without there being more data, as long as the next batch
|
|
75
|
+
// will return `hasMore: false`.
|
|
76
|
+
hasMore = false;
|
|
77
|
+
}
|
|
78
|
+
return { data, hasMore };
|
|
79
|
+
} finally {
|
|
80
|
+
// Match the from the cursor iterator logic here:
|
|
81
|
+
// https://github.com/mongodb/node-mongodb-native/blob/e02534e7d1c627bf50b85ca39f5995dbf165ad44/src/cursor/abstract_cursor.ts#L327-L331
|
|
82
|
+
if (!cursor.closed) {
|
|
83
|
+
await cursor.close();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const BSON_DESERIALIZE_OPTIONS: bson.DeserializeOptions = {
|
|
89
|
+
// use bigint instead of Long
|
|
90
|
+
useBigInt64: true
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export function mapOpEntry(row: BucketDataDocument): utils.OplogEntry {
|
|
94
|
+
if (row.op == 'PUT' || row.op == 'REMOVE') {
|
|
95
|
+
return {
|
|
96
|
+
op_id: utils.timestampToOpId(row._id.o),
|
|
97
|
+
op: row.op,
|
|
98
|
+
object_type: row.table,
|
|
99
|
+
object_id: row.row_id,
|
|
100
|
+
checksum: Number(row.checksum),
|
|
101
|
+
subkey: replicaIdToSubkey(row.source_table!, row.source_key!),
|
|
102
|
+
data: row.data
|
|
103
|
+
};
|
|
104
|
+
} else {
|
|
105
|
+
// MOVE, CLEAR
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
op_id: utils.timestampToOpId(row._id.o),
|
|
109
|
+
op: row.op,
|
|
110
|
+
checksum: Number(row.checksum)
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Returns true if two ReplicaId values are the same (serializes to the same BSON value).
|
|
117
|
+
*/
|
|
118
|
+
export function replicaIdEquals(a: storage.ReplicaId, b: storage.ReplicaId) {
|
|
119
|
+
if (a === b) {
|
|
120
|
+
return true;
|
|
121
|
+
} else if (typeof a == 'string' && typeof b == 'string') {
|
|
122
|
+
return a == b;
|
|
123
|
+
} else if (isUUID(a) && isUUID(b)) {
|
|
124
|
+
return a.equals(b);
|
|
125
|
+
} else if (a == null && b == null) {
|
|
126
|
+
return true;
|
|
127
|
+
} else if ((b == null && a != null) || (a == null && b != null)) {
|
|
128
|
+
return false;
|
|
129
|
+
} else {
|
|
130
|
+
// There are many possible primitive values, this covers them all
|
|
131
|
+
return (bson.serialize({ id: a }) as Buffer).equals(bson.serialize({ id: b }));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function replicaIdToSubkey(table: bson.ObjectId, id: storage.ReplicaId): string {
|
|
136
|
+
if (isUUID(id)) {
|
|
137
|
+
// Special case for UUID for backwards-compatiblity
|
|
138
|
+
return `${table.toHexString()}/${id.toHexString()}`;
|
|
139
|
+
} else {
|
|
140
|
+
// Hashed UUID from the table and id
|
|
141
|
+
const repr = bson.serialize({ table, id });
|
|
142
|
+
return uuid.v5(repr, utils.ID_NAMESPACE);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* True if this is a bson.UUID.
|
|
148
|
+
*
|
|
149
|
+
* Works even with multiple copies of the bson package.
|
|
150
|
+
*/
|
|
151
|
+
export function isUUID(value: any): value is bson.UUID {
|
|
152
|
+
if (value == null || typeof value != 'object') {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
const uuid = value as bson.UUID;
|
|
156
|
+
return uuid._bsontype == 'Binary' && uuid.sub_type == bson.Binary.SUBTYPE_UUID;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Helper function for creating a MongoDB client from consumers of this package
|
|
161
|
+
*/
|
|
162
|
+
export const createMongoClient = (url: string, options?: mongo.MongoClientOptions) => {
|
|
163
|
+
return new mongo.MongoClient(url, options);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* MongoDB bulkWrite internally splits the operations into batches
|
|
168
|
+
* so that no batch exceeds 16MB. However, there are cases where
|
|
169
|
+
* the batch size is very close to 16MB, where additional metadata
|
|
170
|
+
* on the server pushes it over the limit, resulting in this error
|
|
171
|
+
* from the server:
|
|
172
|
+
*
|
|
173
|
+
* > MongoBulkWriteError: BSONObj size: 16814023 (0x1008FC7) is invalid. Size must be between 0 and 16793600(16MB) First element: insert: "bucket_data"
|
|
174
|
+
*
|
|
175
|
+
* We work around the issue by doing our own batching, limiting the
|
|
176
|
+
* batch size to 15MB. This does add additional overhead with
|
|
177
|
+
* BSON.calculateObjectSize.
|
|
178
|
+
*/
|
|
179
|
+
export async function safeBulkWrite<T extends mongo.Document>(
|
|
180
|
+
collection: mongo.Collection<T>,
|
|
181
|
+
operations: mongo.AnyBulkWriteOperation<T>[],
|
|
182
|
+
options: mongo.BulkWriteOptions
|
|
183
|
+
) {
|
|
184
|
+
// Must be below 16MB.
|
|
185
|
+
// We could probably go a little closer, but 15MB is a safe threshold.
|
|
186
|
+
const BULK_WRITE_LIMIT = 15 * 1024 * 1024;
|
|
187
|
+
|
|
188
|
+
let batch: mongo.AnyBulkWriteOperation<T>[] = [];
|
|
189
|
+
let currentSize = 0;
|
|
190
|
+
// Estimated overhead per operation, should be smaller in reality.
|
|
191
|
+
const keySize = 8;
|
|
192
|
+
for (let op of operations) {
|
|
193
|
+
const bsonSize =
|
|
194
|
+
mongo.BSON.calculateObjectSize(op, {
|
|
195
|
+
checkKeys: false,
|
|
196
|
+
ignoreUndefined: true
|
|
197
|
+
} as any) + keySize;
|
|
198
|
+
if (batch.length > 0 && currentSize + bsonSize > BULK_WRITE_LIMIT) {
|
|
199
|
+
await collection.bulkWrite(batch, options);
|
|
200
|
+
currentSize = 0;
|
|
201
|
+
batch = [];
|
|
202
|
+
}
|
|
203
|
+
batch.push(op);
|
|
204
|
+
currentSize += bsonSize;
|
|
205
|
+
}
|
|
206
|
+
if (batch.length > 0) {
|
|
207
|
+
await collection.bulkWrite(batch, options);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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';
|
package/src/types/types.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { normalizeMongoConfig } from '@powersync/service-core';
|
|
2
1
|
import * as service_types from '@powersync/service-types';
|
|
2
|
+
import { MongoStorageConfig } from '@powersync/service-types/src/config/PowerSyncConfig.js';
|
|
3
3
|
import * as t from 'ts-codec';
|
|
4
|
+
import { MONGO_STORAGE_TYPE, normalizeMongoConfig } from '../storage/storage-index.js';
|
|
4
5
|
|
|
5
6
|
export const MONGO_CONNECTION_TYPE = 'mongodb' as const;
|
|
6
7
|
|
|
@@ -103,3 +104,9 @@ export function normalizeConnectionConfig(options: MongoConnectionConfig): Norma
|
|
|
103
104
|
export function baseUri(options: NormalizedMongoConnectionConfig) {
|
|
104
105
|
return options.uri;
|
|
105
106
|
}
|
|
107
|
+
|
|
108
|
+
export function isMongoStorageConfig(
|
|
109
|
+
config: service_types.configFile.GenericStorageConfig
|
|
110
|
+
): config is MongoStorageConfig {
|
|
111
|
+
return config.type == MONGO_STORAGE_TYPE;
|
|
112
|
+
}
|