Skip to content

Commit b4fe4ae

Browse files
authored
Remove mongodb/bson workarounds (#211)
* bson 6.10.3 * Remove bson-related workarounds. * Add changeset. * Fix missing await.
1 parent a6b1c1c commit b4fe4ae

File tree

16 files changed

+62
-112
lines changed

16 files changed

+62
-112
lines changed

.changeset/clever-kids-pretend.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@powersync/service-module-mongodb-storage': patch
3+
'@powersync/service-rsocket-router': patch
4+
'@powersync/service-module-mongodb': patch
5+
'@powersync/service-core': patch
6+
'@powersync/lib-services-framework': patch
7+
'@powersync/lib-service-mongodb': patch
8+
'@powersync/service-image': patch
9+
---
10+
11+
Upgrade mongodb and bson packages, removing the need for some workarounds.

libs/lib-mongodb/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
},
3030
"dependencies": {
3131
"@powersync/lib-services-framework": "workspace:*",
32-
"bson": "^6.8.0",
33-
"mongodb": "^6.11.0",
32+
"bson": "^6.10.3",
33+
"mongodb": "^6.13.0",
3434
"ts-codec": "^1.3.0",
3535
"uri-js": "^4.4.1"
3636
},

libs/lib-services/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@powersync/service-errors": "workspace:*",
2424
"ajv": "^8.12.0",
2525
"better-ajv-errors": "^1.2.0",
26-
"bson": "^6.8.0",
26+
"bson": "^6.10.3",
2727
"dotenv": "^16.4.5",
2828
"ipaddr.js": "^2.1.0",
2929
"lodash": "^4.17.21",

modules/module-mongodb-storage/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@powersync/service-sync-rules": "workspace:*",
3535
"@powersync/service-types": "workspace:*",
3636
"@powersync/lib-service-mongodb": "workspace:*",
37-
"bson": "^6.8.0",
37+
"bson": "^6.10.3",
3838
"ts-codec": "^1.3.0",
3939
"ix": "^5.0.0",
4040
"lru-cache": "^10.2.2",

modules/module-mongodb-storage/src/storage/implementation/MongoCompactor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { storage, utils } from '@powersync/service-core';
55
import { PowerSyncMongo } from './db.js';
66
import { BucketDataDocument, BucketDataKey } from './models.js';
77
import { cacheKey } from './OperationBatch.js';
8-
import { safeBulkWrite } from './util.js';
98

109
interface CurrentBucketState {
1110
/** Bucket name */
@@ -265,7 +264,7 @@ export class MongoCompactor {
265264
private async flush() {
266265
if (this.updates.length > 0) {
267266
logger.info(`Compacting ${this.updates.length} ops`);
268-
await safeBulkWrite(this.db.bucket_data, this.updates, {
267+
await this.db.bucket_data.bulkWrite(this.updates, {
269268
// Order is not important.
270269
// Since checksums are not affected, these operations can happen in any order,
271270
// and it's fine if the operations are partially applied.

modules/module-mongodb-storage/src/storage/implementation/MongoSyncBucketStorage.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,7 @@ export class MongoSyncBucketStorage
313313
// 1. We can calculate the document size accurately without serializing again.
314314
// 2. We can delay parsing the results until it's needed.
315315
// We manually use bson.deserialize below
316-
raw: true,
317-
318-
// Since we're using raw: true and parsing ourselves later, we don't need bigint
319-
// support here.
320-
// Disabling due to https://jira.mongodb.org/browse/NODE-6165, and the fact that this
321-
// is one of our most common queries.
322-
useBigInt64: false
316+
raw: true
323317
}
324318
) as unknown as mongo.FindCursor<Buffer>;
325319

modules/module-mongodb-storage/src/storage/implementation/MongoWriteCheckpointAPI.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as framework from '@powersync/lib-services-framework';
22
import { storage } from '@powersync/service-core';
33
import { PowerSyncMongo } from './db.js';
4-
import { safeBulkWrite } from './util.js';
54

65
export type MongoCheckpointAPIOptions = {
76
db: PowerSyncMongo;
@@ -127,8 +126,7 @@ export async function batchCreateCustomWriteCheckpoints(
127126
return;
128127
}
129128

130-
await safeBulkWrite(
131-
db.custom_write_checkpoints,
129+
await db.custom_write_checkpoints.bulkWrite(
132130
checkpoints.map((checkpointOptions) => ({
133131
updateOne: {
134132
filter: { user_id: checkpointOptions.user_id, sync_rules_id: checkpointOptions.sync_rules_id },

modules/module-mongodb-storage/src/storage/implementation/PersistedBatch.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
CurrentDataDocument,
1616
SourceKey
1717
} from './models.js';
18-
import { replicaIdToSubkey, safeBulkWrite } from './util.js';
18+
import { replicaIdToSubkey } from './util.js';
1919

2020
/**
2121
* Maximum size of operations we write in a single transaction.
@@ -246,22 +246,21 @@ export class PersistedBatch {
246246

247247
async flush(db: PowerSyncMongo, session: mongo.ClientSession) {
248248
if (this.bucketData.length > 0) {
249-
// calculate total size
250-
await safeBulkWrite(db.bucket_data, this.bucketData, {
249+
await db.bucket_data.bulkWrite(this.bucketData, {
251250
session,
252251
// inserts only - order doesn't matter
253252
ordered: false
254253
});
255254
}
256255
if (this.bucketParameters.length > 0) {
257-
await safeBulkWrite(db.bucket_parameters, this.bucketParameters, {
256+
await db.bucket_parameters.bulkWrite(this.bucketParameters, {
258257
session,
259258
// inserts only - order doesn't matter
260259
ordered: false
261260
});
262261
}
263262
if (this.currentData.length > 0) {
264-
await safeBulkWrite(db.current_data, this.currentData, {
263+
await db.current_data.bulkWrite(this.currentData, {
265264
session,
266265
// may update and delete data within the same batch - order matters
267266
ordered: true

modules/module-mongodb-storage/src/storage/implementation/util.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -124,48 +124,3 @@ export const connectMongoForTests = (url: string, isCI: boolean) => {
124124
});
125125
return new PowerSyncMongo(client);
126126
};
127-
128-
/**
129-
* MongoDB bulkWrite internally splits the operations into batches
130-
* so that no batch exceeds 16MB. However, there are cases where
131-
* the batch size is very close to 16MB, where additional metadata
132-
* on the server pushes it over the limit, resulting in this error
133-
* from the server:
134-
*
135-
* > MongoBulkWriteError: BSONObj size: 16814023 (0x1008FC7) is invalid. Size must be between 0 and 16793600(16MB) First element: insert: "bucket_data"
136-
*
137-
* We work around the issue by doing our own batching, limiting the
138-
* batch size to 15MB. This does add additional overhead with
139-
* BSON.calculateObjectSize.
140-
*/
141-
export async function safeBulkWrite<T extends mongo.Document>(
142-
collection: mongo.Collection<T>,
143-
operations: mongo.AnyBulkWriteOperation<T>[],
144-
options: mongo.BulkWriteOptions
145-
) {
146-
// Must be below 16MB.
147-
// We could probably go a little closer, but 15MB is a safe threshold.
148-
const BULK_WRITE_LIMIT = 15 * 1024 * 1024;
149-
150-
let batch: mongo.AnyBulkWriteOperation<T>[] = [];
151-
let currentSize = 0;
152-
// Estimated overhead per operation, should be smaller in reality.
153-
const keySize = 8;
154-
for (let op of operations) {
155-
const bsonSize =
156-
mongo.BSON.calculateObjectSize(op, {
157-
checkKeys: false,
158-
ignoreUndefined: true
159-
} as any) + keySize;
160-
if (batch.length > 0 && currentSize + bsonSize > BULK_WRITE_LIMIT) {
161-
await collection.bulkWrite(batch, options);
162-
currentSize = 0;
163-
batch = [];
164-
}
165-
batch.push(op);
166-
currentSize += bsonSize;
167-
}
168-
if (batch.length > 0) {
169-
await collection.bulkWrite(batch, options);
170-
}
171-
}

modules/module-mongodb/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@powersync/service-sync-rules": "workspace:*",
3535
"@powersync/service-types": "workspace:*",
3636
"@powersync/lib-service-mongodb": "workspace:*",
37-
"bson": "^6.8.0",
37+
"bson": "^6.10.3",
3838
"ts-codec": "^1.3.0",
3939
"uuid": "^9.0.1"
4040
},

0 commit comments

Comments
 (0)