@powersync/service-module-mongodb 0.0.0-dev-20250102111825 → 0.0.0-dev-20250108084515
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,161 +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
|
-
/**
|
|
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 BucketDataKey {
|
|
24
|
-
/** group_id */
|
|
25
|
-
g: number;
|
|
26
|
-
/** bucket name */
|
|
27
|
-
b: string;
|
|
28
|
-
/** op_id */
|
|
29
|
-
o: bigint;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface CurrentDataDocument {
|
|
33
|
-
_id: SourceKey;
|
|
34
|
-
data: bson.Binary;
|
|
35
|
-
buckets: CurrentBucket[];
|
|
36
|
-
lookups: bson.Binary[];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface CurrentBucket {
|
|
40
|
-
bucket: string;
|
|
41
|
-
table: string;
|
|
42
|
-
id: string;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface BucketParameterDocument {
|
|
46
|
-
_id: bigint;
|
|
47
|
-
key: SourceKey;
|
|
48
|
-
lookup: bson.Binary;
|
|
49
|
-
bucket_parameters: Record<string, SqliteJsonValue>[];
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface BucketDataDocument {
|
|
53
|
-
_id: BucketDataKey;
|
|
54
|
-
op: OpType;
|
|
55
|
-
source_table?: bson.ObjectId;
|
|
56
|
-
source_key?: ReplicaId;
|
|
57
|
-
table?: string;
|
|
58
|
-
row_id?: string;
|
|
59
|
-
checksum: number;
|
|
60
|
-
data: string | null;
|
|
61
|
-
target_op?: bigint | null;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export type OpType = 'PUT' | 'REMOVE' | 'MOVE' | 'CLEAR';
|
|
65
|
-
|
|
66
|
-
export interface SourceTableDocument {
|
|
67
|
-
_id: bson.ObjectId;
|
|
68
|
-
group_id: number;
|
|
69
|
-
connection_id: number;
|
|
70
|
-
relation_id: number | string | undefined;
|
|
71
|
-
schema_name: string;
|
|
72
|
-
table_name: string;
|
|
73
|
-
replica_id_columns: string[] | null;
|
|
74
|
-
replica_id_columns2: { name: string; type_oid?: number; type?: string }[] | undefined;
|
|
75
|
-
snapshot_done: boolean | undefined;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export interface IdSequenceDocument {
|
|
79
|
-
_id: string;
|
|
80
|
-
op_id: bigint;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export interface SyncRuleDocument {
|
|
84
|
-
_id: number;
|
|
85
|
-
|
|
86
|
-
state: storage.SyncRuleState;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* True if initial snapshot has been replicated.
|
|
90
|
-
*
|
|
91
|
-
* Can only be false if state == PROCESSING.
|
|
92
|
-
*/
|
|
93
|
-
snapshot_done: boolean;
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* The last consistent checkpoint.
|
|
97
|
-
*
|
|
98
|
-
* There may be higher OpIds used in the database if we're in the middle of replicating a large transaction.
|
|
99
|
-
*/
|
|
100
|
-
last_checkpoint: bigint | null;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* The LSN associated with the last consistent checkpoint.
|
|
104
|
-
*/
|
|
105
|
-
last_checkpoint_lsn: string | null;
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* If set, no new checkpoints may be created < this value.
|
|
109
|
-
*/
|
|
110
|
-
no_checkpoint_before: string | null;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Goes together with no_checkpoint_before.
|
|
114
|
-
*
|
|
115
|
-
* If a keepalive is triggered that creates the checkpoint > no_checkpoint_before,
|
|
116
|
-
* then the checkpoint must be equal to this keepalive_op.
|
|
117
|
-
*/
|
|
118
|
-
keepalive_op: string | null;
|
|
119
|
-
|
|
120
|
-
slot_name: string | null;
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Last time we persisted a checkpoint.
|
|
124
|
-
*
|
|
125
|
-
* This may be old if no data is incoming.
|
|
126
|
-
*/
|
|
127
|
-
last_checkpoint_ts: Date | null;
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Last time we persisted a checkpoint or keepalive.
|
|
131
|
-
*
|
|
132
|
-
* This should stay fairly current while replicating.
|
|
133
|
-
*/
|
|
134
|
-
last_keepalive_ts: Date | null;
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* If an error is stopping replication, it will be stored here.
|
|
138
|
-
*/
|
|
139
|
-
last_fatal_error: string | null;
|
|
140
|
-
|
|
141
|
-
content: string;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
export interface CustomWriteCheckpointDocument {
|
|
145
|
-
_id: bson.ObjectId;
|
|
146
|
-
user_id: string;
|
|
147
|
-
checkpoint: bigint;
|
|
148
|
-
sync_rules_id: number;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export interface WriteCheckpointDocument {
|
|
152
|
-
_id: bson.ObjectId;
|
|
153
|
-
user_id: string;
|
|
154
|
-
lsns: Record<string, string>;
|
|
155
|
-
client_id: bigint;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export interface InstanceDocument {
|
|
159
|
-
// The instance UUID
|
|
160
|
-
_id: string;
|
|
161
|
-
}
|
|
@@ -1,154 +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
|
-
import { BucketDataDocument } from './models.js';
|
|
7
|
-
|
|
8
|
-
export function idPrefixFilter<T>(prefix: Partial<T>, rest: (keyof T)[]): mongo.Condition<T> {
|
|
9
|
-
let filter = {
|
|
10
|
-
$gte: {
|
|
11
|
-
...prefix
|
|
12
|
-
} as any,
|
|
13
|
-
$lt: {
|
|
14
|
-
...prefix
|
|
15
|
-
} as any
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
for (let key of rest) {
|
|
19
|
-
filter.$gte[key] = new bson.MinKey();
|
|
20
|
-
filter.$lt[key] = new bson.MaxKey();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return filter;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function generateSlotName(prefix: string, sync_rules_id: number) {
|
|
27
|
-
const slot_suffix = crypto.randomBytes(2).toString('hex');
|
|
28
|
-
return `${prefix}${sync_rules_id}_${slot_suffix}`;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Read a single batch of data from a cursor, then close it.
|
|
33
|
-
*
|
|
34
|
-
* We do our best to avoid MongoDB fetching any more data than this single batch.
|
|
35
|
-
*
|
|
36
|
-
* This is similar to using `singleBatch: true` in find options.
|
|
37
|
-
* However, that makes `has_more` detection very difficult, since the cursor is always closed
|
|
38
|
-
* after the first batch. Instead, we do a workaround to only fetch a single batch below.
|
|
39
|
-
*
|
|
40
|
-
* For this to be effective, set batchSize = limit in the find command.
|
|
41
|
-
*/
|
|
42
|
-
export async function readSingleBatch<T>(cursor: mongo.FindCursor<T>): Promise<{ data: T[]; hasMore: boolean }> {
|
|
43
|
-
try {
|
|
44
|
-
let data: T[];
|
|
45
|
-
let hasMore = true;
|
|
46
|
-
// Let MongoDB load the first batch of data
|
|
47
|
-
const hasAny = await cursor.hasNext();
|
|
48
|
-
// Now it's in memory, and we can read it
|
|
49
|
-
data = cursor.readBufferedDocuments();
|
|
50
|
-
if (!hasAny || cursor.id?.isZero()) {
|
|
51
|
-
// A zero id means the cursor is exhaused.
|
|
52
|
-
// No results (hasAny == false) means even this batch doesn't have data.
|
|
53
|
-
// This should similar results as `await cursor.hasNext()`, but without
|
|
54
|
-
// actually fetching the next batch.
|
|
55
|
-
//
|
|
56
|
-
// Note that it is safe (but slightly inefficient) to return `hasMore: true`
|
|
57
|
-
// without there being more data, as long as the next batch
|
|
58
|
-
// will return `hasMore: false`.
|
|
59
|
-
hasMore = false;
|
|
60
|
-
}
|
|
61
|
-
return { data, hasMore };
|
|
62
|
-
} finally {
|
|
63
|
-
// Match the from the cursor iterator logic here:
|
|
64
|
-
// https://github.com/mongodb/node-mongodb-native/blob/e02534e7d1c627bf50b85ca39f5995dbf165ad44/src/cursor/abstract_cursor.ts#L327-L331
|
|
65
|
-
if (!cursor.closed) {
|
|
66
|
-
await cursor.close();
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function mapOpEntry(row: BucketDataDocument): utils.OplogEntry {
|
|
72
|
-
if (row.op == 'PUT' || row.op == 'REMOVE') {
|
|
73
|
-
return {
|
|
74
|
-
op_id: utils.timestampToOpId(row._id.o),
|
|
75
|
-
op: row.op,
|
|
76
|
-
object_type: row.table,
|
|
77
|
-
object_id: row.row_id,
|
|
78
|
-
checksum: Number(row.checksum),
|
|
79
|
-
subkey: replicaIdToSubkey(row.source_table!, row.source_key!),
|
|
80
|
-
data: row.data
|
|
81
|
-
};
|
|
82
|
-
} else {
|
|
83
|
-
// MOVE, CLEAR
|
|
84
|
-
|
|
85
|
-
return {
|
|
86
|
-
op_id: utils.timestampToOpId(row._id.o),
|
|
87
|
-
op: row.op,
|
|
88
|
-
checksum: Number(row.checksum)
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function replicaIdToSubkey(table: bson.ObjectId, id: storage.ReplicaId): string {
|
|
94
|
-
if (storage.isUUID(id)) {
|
|
95
|
-
// Special case for UUID for backwards-compatiblity
|
|
96
|
-
return `${table.toHexString()}/${id.toHexString()}`;
|
|
97
|
-
} else {
|
|
98
|
-
// Hashed UUID from the table and id
|
|
99
|
-
const repr = bson.serialize({ table, id });
|
|
100
|
-
return uuid.v5(repr, utils.ID_NAMESPACE);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Helper function for creating a MongoDB client from consumers of this package
|
|
106
|
-
*/
|
|
107
|
-
export const createMongoClient = (url: string, options?: mongo.MongoClientOptions) => {
|
|
108
|
-
return new mongo.MongoClient(url, options);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* MongoDB bulkWrite internally splits the operations into batches
|
|
113
|
-
* so that no batch exceeds 16MB. However, there are cases where
|
|
114
|
-
* the batch size is very close to 16MB, where additional metadata
|
|
115
|
-
* on the server pushes it over the limit, resulting in this error
|
|
116
|
-
* from the server:
|
|
117
|
-
*
|
|
118
|
-
* > MongoBulkWriteError: BSONObj size: 16814023 (0x1008FC7) is invalid. Size must be between 0 and 16793600(16MB) First element: insert: "bucket_data"
|
|
119
|
-
*
|
|
120
|
-
* We work around the issue by doing our own batching, limiting the
|
|
121
|
-
* batch size to 15MB. This does add additional overhead with
|
|
122
|
-
* BSON.calculateObjectSize.
|
|
123
|
-
*/
|
|
124
|
-
export async function safeBulkWrite<T extends mongo.Document>(
|
|
125
|
-
collection: mongo.Collection<T>,
|
|
126
|
-
operations: mongo.AnyBulkWriteOperation<T>[],
|
|
127
|
-
options: mongo.BulkWriteOptions
|
|
128
|
-
) {
|
|
129
|
-
// Must be below 16MB.
|
|
130
|
-
// We could probably go a little closer, but 15MB is a safe threshold.
|
|
131
|
-
const BULK_WRITE_LIMIT = 15 * 1024 * 1024;
|
|
132
|
-
|
|
133
|
-
let batch: mongo.AnyBulkWriteOperation<T>[] = [];
|
|
134
|
-
let currentSize = 0;
|
|
135
|
-
// Estimated overhead per operation, should be smaller in reality.
|
|
136
|
-
const keySize = 8;
|
|
137
|
-
for (let op of operations) {
|
|
138
|
-
const bsonSize =
|
|
139
|
-
mongo.BSON.calculateObjectSize(op, {
|
|
140
|
-
checkKeys: false,
|
|
141
|
-
ignoreUndefined: true
|
|
142
|
-
} as any) + keySize;
|
|
143
|
-
if (batch.length > 0 && currentSize + bsonSize > BULK_WRITE_LIMIT) {
|
|
144
|
-
await collection.bulkWrite(batch, options);
|
|
145
|
-
currentSize = 0;
|
|
146
|
-
batch = [];
|
|
147
|
-
}
|
|
148
|
-
batch.push(op);
|
|
149
|
-
currentSize += bsonSize;
|
|
150
|
-
}
|
|
151
|
-
if (batch.length > 0) {
|
|
152
|
-
await collection.bulkWrite(batch, options);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
@@ -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,332 +0,0 @@
|
|
|
1
|
-
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
-
|
|
3
|
-
exports[`sync - mongodb > compacting data - invalidate checkpoint 1`] = `
|
|
4
|
-
[
|
|
5
|
-
{
|
|
6
|
-
"checkpoint": {
|
|
7
|
-
"buckets": [
|
|
8
|
-
{
|
|
9
|
-
"bucket": "mybucket[]",
|
|
10
|
-
"checksum": -93886621,
|
|
11
|
-
"count": 2,
|
|
12
|
-
},
|
|
13
|
-
],
|
|
14
|
-
"last_op_id": "2",
|
|
15
|
-
"write_checkpoint": undefined,
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
]
|
|
19
|
-
`;
|
|
20
|
-
|
|
21
|
-
exports[`sync - mongodb > compacting data - invalidate checkpoint 2`] = `
|
|
22
|
-
[
|
|
23
|
-
{
|
|
24
|
-
"data": {
|
|
25
|
-
"after": "0",
|
|
26
|
-
"bucket": "mybucket[]",
|
|
27
|
-
"data": [
|
|
28
|
-
{
|
|
29
|
-
"checksum": -93886621n,
|
|
30
|
-
"op": "CLEAR",
|
|
31
|
-
"op_id": "2",
|
|
32
|
-
},
|
|
33
|
-
],
|
|
34
|
-
"has_more": false,
|
|
35
|
-
"next_after": "2",
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
"checkpoint_diff": {
|
|
40
|
-
"last_op_id": "4",
|
|
41
|
-
"removed_buckets": [],
|
|
42
|
-
"updated_buckets": [
|
|
43
|
-
{
|
|
44
|
-
"bucket": "mybucket[]",
|
|
45
|
-
"checksum": 499012468,
|
|
46
|
-
"count": 4,
|
|
47
|
-
},
|
|
48
|
-
],
|
|
49
|
-
"write_checkpoint": undefined,
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
"data": {
|
|
54
|
-
"after": "2",
|
|
55
|
-
"bucket": "mybucket[]",
|
|
56
|
-
"data": [
|
|
57
|
-
{
|
|
58
|
-
"checksum": 1859363232n,
|
|
59
|
-
"data": "{"id":"t1","description":"Test 1b"}",
|
|
60
|
-
"object_id": "t1",
|
|
61
|
-
"object_type": "test",
|
|
62
|
-
"op": "PUT",
|
|
63
|
-
"op_id": "3",
|
|
64
|
-
"subkey": "e5aa2ddc-1328-58fa-a000-0b5ed31eaf1a",
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
"checksum": 3028503153n,
|
|
68
|
-
"data": "{"id":"t2","description":"Test 2b"}",
|
|
69
|
-
"object_id": "t2",
|
|
70
|
-
"object_type": "test",
|
|
71
|
-
"op": "PUT",
|
|
72
|
-
"op_id": "4",
|
|
73
|
-
"subkey": "13423353-9f27-59b4-baf0-64a5e09f1769",
|
|
74
|
-
},
|
|
75
|
-
],
|
|
76
|
-
"has_more": false,
|
|
77
|
-
"next_after": "4",
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
"checkpoint_complete": {
|
|
82
|
-
"last_op_id": "4",
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
]
|
|
86
|
-
`;
|
|
87
|
-
|
|
88
|
-
exports[`sync - mongodb > expired token 1`] = `
|
|
89
|
-
[
|
|
90
|
-
{
|
|
91
|
-
"token_expires_in": 0,
|
|
92
|
-
},
|
|
93
|
-
]
|
|
94
|
-
`;
|
|
95
|
-
|
|
96
|
-
exports[`sync - mongodb > expiring token 1`] = `
|
|
97
|
-
[
|
|
98
|
-
{
|
|
99
|
-
"checkpoint": {
|
|
100
|
-
"buckets": [
|
|
101
|
-
{
|
|
102
|
-
"bucket": "mybucket[]",
|
|
103
|
-
"checksum": 0,
|
|
104
|
-
"count": 0,
|
|
105
|
-
},
|
|
106
|
-
],
|
|
107
|
-
"last_op_id": "0",
|
|
108
|
-
"write_checkpoint": undefined,
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
"checkpoint_complete": {
|
|
113
|
-
"last_op_id": "0",
|
|
114
|
-
},
|
|
115
|
-
},
|
|
116
|
-
]
|
|
117
|
-
`;
|
|
118
|
-
|
|
119
|
-
exports[`sync - mongodb > expiring token 2`] = `
|
|
120
|
-
[
|
|
121
|
-
{
|
|
122
|
-
"token_expires_in": 0,
|
|
123
|
-
},
|
|
124
|
-
]
|
|
125
|
-
`;
|
|
126
|
-
|
|
127
|
-
exports[`sync - mongodb > sync global data 1`] = `
|
|
128
|
-
[
|
|
129
|
-
{
|
|
130
|
-
"checkpoint": {
|
|
131
|
-
"buckets": [
|
|
132
|
-
{
|
|
133
|
-
"bucket": "mybucket[]",
|
|
134
|
-
"checksum": -93886621,
|
|
135
|
-
"count": 2,
|
|
136
|
-
},
|
|
137
|
-
],
|
|
138
|
-
"last_op_id": "2",
|
|
139
|
-
"write_checkpoint": undefined,
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
{
|
|
143
|
-
"data": {
|
|
144
|
-
"after": "0",
|
|
145
|
-
"bucket": "mybucket[]",
|
|
146
|
-
"data": [
|
|
147
|
-
{
|
|
148
|
-
"checksum": 920318466n,
|
|
149
|
-
"data": "{"id":"t1","description":"Test 1"}",
|
|
150
|
-
"object_id": "t1",
|
|
151
|
-
"object_type": "test",
|
|
152
|
-
"op": "PUT",
|
|
153
|
-
"op_id": "1",
|
|
154
|
-
"subkey": "e5aa2ddc-1328-58fa-a000-0b5ed31eaf1a",
|
|
155
|
-
},
|
|
156
|
-
{
|
|
157
|
-
"checksum": 3280762209n,
|
|
158
|
-
"data": "{"id":"t2","description":"Test 2"}",
|
|
159
|
-
"object_id": "t2",
|
|
160
|
-
"object_type": "test",
|
|
161
|
-
"op": "PUT",
|
|
162
|
-
"op_id": "2",
|
|
163
|
-
"subkey": "13423353-9f27-59b4-baf0-64a5e09f1769",
|
|
164
|
-
},
|
|
165
|
-
],
|
|
166
|
-
"has_more": false,
|
|
167
|
-
"next_after": "2",
|
|
168
|
-
},
|
|
169
|
-
},
|
|
170
|
-
{
|
|
171
|
-
"checkpoint_complete": {
|
|
172
|
-
"last_op_id": "2",
|
|
173
|
-
},
|
|
174
|
-
},
|
|
175
|
-
]
|
|
176
|
-
`;
|
|
177
|
-
|
|
178
|
-
exports[`sync - mongodb > sync legacy non-raw data 1`] = `
|
|
179
|
-
[
|
|
180
|
-
{
|
|
181
|
-
"checkpoint": {
|
|
182
|
-
"buckets": [
|
|
183
|
-
{
|
|
184
|
-
"bucket": "mybucket[]",
|
|
185
|
-
"checksum": -852817836,
|
|
186
|
-
"count": 1,
|
|
187
|
-
},
|
|
188
|
-
],
|
|
189
|
-
"last_op_id": "1",
|
|
190
|
-
"write_checkpoint": undefined,
|
|
191
|
-
},
|
|
192
|
-
},
|
|
193
|
-
{
|
|
194
|
-
"data": {
|
|
195
|
-
"after": "0",
|
|
196
|
-
"bucket": "mybucket[]",
|
|
197
|
-
"data": [
|
|
198
|
-
{
|
|
199
|
-
"checksum": 3442149460n,
|
|
200
|
-
"data": {
|
|
201
|
-
"description": "Test
|
|
202
|
-
"string"",
|
|
203
|
-
"id": "t1",
|
|
204
|
-
"large_num": 12345678901234567890n,
|
|
205
|
-
},
|
|
206
|
-
"object_id": "t1",
|
|
207
|
-
"object_type": "test",
|
|
208
|
-
"op": "PUT",
|
|
209
|
-
"op_id": "1",
|
|
210
|
-
"subkey": "e5aa2ddc-1328-58fa-a000-0b5ed31eaf1a",
|
|
211
|
-
},
|
|
212
|
-
],
|
|
213
|
-
"has_more": false,
|
|
214
|
-
"next_after": "1",
|
|
215
|
-
},
|
|
216
|
-
},
|
|
217
|
-
{
|
|
218
|
-
"checkpoint_complete": {
|
|
219
|
-
"last_op_id": "1",
|
|
220
|
-
},
|
|
221
|
-
},
|
|
222
|
-
]
|
|
223
|
-
`;
|
|
224
|
-
|
|
225
|
-
exports[`sync - mongodb > sync updates to global data 1`] = `
|
|
226
|
-
[
|
|
227
|
-
{
|
|
228
|
-
"checkpoint": {
|
|
229
|
-
"buckets": [
|
|
230
|
-
{
|
|
231
|
-
"bucket": "mybucket[]",
|
|
232
|
-
"checksum": 0,
|
|
233
|
-
"count": 0,
|
|
234
|
-
},
|
|
235
|
-
],
|
|
236
|
-
"last_op_id": "0",
|
|
237
|
-
"write_checkpoint": undefined,
|
|
238
|
-
},
|
|
239
|
-
},
|
|
240
|
-
{
|
|
241
|
-
"checkpoint_complete": {
|
|
242
|
-
"last_op_id": "0",
|
|
243
|
-
},
|
|
244
|
-
},
|
|
245
|
-
]
|
|
246
|
-
`;
|
|
247
|
-
|
|
248
|
-
exports[`sync - mongodb > sync updates to global data 2`] = `
|
|
249
|
-
[
|
|
250
|
-
{
|
|
251
|
-
"checkpoint_diff": {
|
|
252
|
-
"last_op_id": "1",
|
|
253
|
-
"removed_buckets": [],
|
|
254
|
-
"updated_buckets": [
|
|
255
|
-
{
|
|
256
|
-
"bucket": "mybucket[]",
|
|
257
|
-
"checksum": 920318466,
|
|
258
|
-
"count": 1,
|
|
259
|
-
},
|
|
260
|
-
],
|
|
261
|
-
"write_checkpoint": undefined,
|
|
262
|
-
},
|
|
263
|
-
},
|
|
264
|
-
{
|
|
265
|
-
"data": {
|
|
266
|
-
"after": "0",
|
|
267
|
-
"bucket": "mybucket[]",
|
|
268
|
-
"data": [
|
|
269
|
-
{
|
|
270
|
-
"checksum": 920318466n,
|
|
271
|
-
"data": "{"id":"t1","description":"Test 1"}",
|
|
272
|
-
"object_id": "t1",
|
|
273
|
-
"object_type": "test",
|
|
274
|
-
"op": "PUT",
|
|
275
|
-
"op_id": "1",
|
|
276
|
-
"subkey": "e5aa2ddc-1328-58fa-a000-0b5ed31eaf1a",
|
|
277
|
-
},
|
|
278
|
-
],
|
|
279
|
-
"has_more": false,
|
|
280
|
-
"next_after": "1",
|
|
281
|
-
},
|
|
282
|
-
},
|
|
283
|
-
{
|
|
284
|
-
"checkpoint_complete": {
|
|
285
|
-
"last_op_id": "1",
|
|
286
|
-
},
|
|
287
|
-
},
|
|
288
|
-
]
|
|
289
|
-
`;
|
|
290
|
-
|
|
291
|
-
exports[`sync - mongodb > sync updates to global data 3`] = `
|
|
292
|
-
[
|
|
293
|
-
{
|
|
294
|
-
"checkpoint_diff": {
|
|
295
|
-
"last_op_id": "2",
|
|
296
|
-
"removed_buckets": [],
|
|
297
|
-
"updated_buckets": [
|
|
298
|
-
{
|
|
299
|
-
"bucket": "mybucket[]",
|
|
300
|
-
"checksum": -93886621,
|
|
301
|
-
"count": 2,
|
|
302
|
-
},
|
|
303
|
-
],
|
|
304
|
-
"write_checkpoint": undefined,
|
|
305
|
-
},
|
|
306
|
-
},
|
|
307
|
-
{
|
|
308
|
-
"data": {
|
|
309
|
-
"after": "1",
|
|
310
|
-
"bucket": "mybucket[]",
|
|
311
|
-
"data": [
|
|
312
|
-
{
|
|
313
|
-
"checksum": 3280762209n,
|
|
314
|
-
"data": "{"id":"t2","description":"Test 2"}",
|
|
315
|
-
"object_id": "t2",
|
|
316
|
-
"object_type": "test",
|
|
317
|
-
"op": "PUT",
|
|
318
|
-
"op_id": "2",
|
|
319
|
-
"subkey": "13423353-9f27-59b4-baf0-64a5e09f1769",
|
|
320
|
-
},
|
|
321
|
-
],
|
|
322
|
-
"has_more": false,
|
|
323
|
-
"next_after": "2",
|
|
324
|
-
},
|
|
325
|
-
},
|
|
326
|
-
{
|
|
327
|
-
"checkpoint_complete": {
|
|
328
|
-
"last_op_id": "2",
|
|
329
|
-
},
|
|
330
|
-
},
|
|
331
|
-
]
|
|
332
|
-
`;
|
package/test/src/storage.test.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { register } from '@powersync/service-core-tests';
|
|
2
|
-
import { describe } from 'vitest';
|
|
3
|
-
import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
|
|
4
|
-
|
|
5
|
-
describe('Mongo Sync Bucket Storage', () => register.registerDataStorageTests(INITIALIZED_MONGO_STORAGE_FACTORY));
|
|
6
|
-
|
|
7
|
-
describe('Sync Bucket Validation', register.registerBucketValidationTests);
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { MongoCompactOptions } from '@module/storage/implementation/MongoCompactor.js';
|
|
2
|
-
import { register } from '@powersync/service-core-tests';
|
|
3
|
-
import { describe } from 'vitest';
|
|
4
|
-
import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
|
|
5
|
-
|
|
6
|
-
describe('Mongo Sync Bucket Storage Compact', () => register.registerCompactTests<MongoCompactOptions>(INITIALIZED_MONGO_STORAGE_FACTORY, { clearBatchLimit: 2, moveBatchLimit: 1, moveBatchQueryLimit: 1 }));
|