Skip to content

Commit fe69c85

Browse files
committed
fixes
1 parent 9883042 commit fe69c85

File tree

3 files changed

+50
-64
lines changed

3 files changed

+50
-64
lines changed

src/bulk/common.ts

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
Callback,
3232
getTopology,
3333
hasAtomicOperators,
34+
maybeCallback,
3435
MongoDBNamespace,
3536
resolveOptions
3637
} from '../utils';
@@ -1270,39 +1271,44 @@ export abstract class BulkOperationBase {
12701271
options?: BulkWriteOptions | Callback<BulkWriteResult>,
12711272
callback?: Callback<BulkWriteResult>
12721273
): Promise<BulkWriteResult> | void {
1273-
if (typeof options === 'function') (callback = options), (options = {});
1274-
options = options ?? {};
1275-
1276-
if (this.s.executed) {
1277-
return handleEarlyError(new MongoBatchReExecutionError(), callback);
1278-
}
1274+
callback =
1275+
typeof callback === 'function'
1276+
? callback
1277+
: typeof options === 'function'
1278+
? options
1279+
: undefined;
1280+
1281+
return maybeCallback(async () => {
1282+
options = options != null && typeof options !== 'function' ? options : {};
1283+
1284+
if (this.s.executed) {
1285+
throw new MongoBatchReExecutionError();
1286+
}
12791287

1280-
const writeConcern = WriteConcern.fromOptions(options);
1281-
if (writeConcern) {
1282-
this.s.writeConcern = writeConcern;
1283-
}
1288+
const writeConcern = WriteConcern.fromOptions(options);
1289+
if (writeConcern) {
1290+
this.s.writeConcern = writeConcern;
1291+
}
12841292

1285-
// If we have current batch
1286-
if (this.isOrdered) {
1287-
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
1288-
} else {
1289-
if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
1290-
if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
1291-
if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
1292-
}
1293-
// If we have no operations in the bulk raise an error
1294-
if (this.s.batches.length === 0) {
1295-
const emptyBatchError = new MongoInvalidArgumentError(
1296-
'Invalid BulkOperation, Batch cannot be empty'
1297-
);
1298-
return handleEarlyError(emptyBatchError, callback);
1299-
}
1293+
// If we have current batch
1294+
if (this.isOrdered) {
1295+
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
1296+
} else {
1297+
if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
1298+
if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
1299+
if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
1300+
}
1301+
// If we have no operations in the bulk raise an error
1302+
if (this.s.batches.length === 0) {
1303+
throw new MongoInvalidArgumentError('Invalid BulkOperation, Batch cannot be empty');
1304+
}
13001305

1301-
this.s.executed = true;
1302-
const finalOptions = { ...this.s.options, ...options };
1303-
const operation = new BulkWriteShimOperation(this, finalOptions);
1306+
this.s.executed = true;
1307+
const finalOptions = { ...this.s.options, ...options };
1308+
const operation = new BulkWriteShimOperation(this, finalOptions);
13041309

1305-
return executeOperation(this.s.collection.s.db.s.client, operation, callback);
1310+
return await executeOperation(this.s.collection.s.db.s.client, operation);
1311+
}, callback);
13061312
}
13071313

13081314
/**
@@ -1351,20 +1357,6 @@ Object.defineProperty(BulkOperationBase.prototype, 'length', {
13511357
}
13521358
});
13531359

1354-
/** helper function to assist with promiseOrCallback behavior */
1355-
function handleEarlyError(
1356-
err?: AnyError,
1357-
callback?: Callback<BulkWriteResult>
1358-
): Promise<BulkWriteResult> | void {
1359-
if (typeof callback === 'function') {
1360-
callback(err);
1361-
return;
1362-
}
1363-
1364-
const PromiseConstructor = PromiseProvider.get() ?? Promise;
1365-
return PromiseConstructor.reject(err);
1366-
}
1367-
13681360
function shouldForceServerObjectId(bulkOperation: BulkOperationBase): boolean {
13691361
if (typeof bulkOperation.s.options.forceServerObjectId === 'boolean') {
13701362
return bulkOperation.s.options.forceServerObjectId;

src/gridfs/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ export class GridFSBucket extends TypedEventEmitter<GridFSBucketEvents> {
146146
delete(id: ObjectId, callback?: Callback<void>): Promise<void> | void {
147147
return maybeCallback(async () => {
148148
const { deletedCount } = await this.s._filesCollection.deleteOne({ _id: id });
149-
await this.s._chunksCollection.deleteMany({ files_id: id });
150149

151150
// Delete orphaned chunks before returning FileNotFound
151+
await this.s._chunksCollection.deleteMany({ files_id: id });
152+
152153
if (deletedCount === 0) {
153154
// TODO(NODE-3483): Replace with more appropriate error
154155
// Consider creating new error MongoGridFSFileNotFoundError

src/mongo_client.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
Callback,
3030
ClientMetadata,
3131
HostAddress,
32+
maybeCallback,
3233
maybePromise,
3334
MongoDBNamespace,
3435
ns,
@@ -580,26 +581,18 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
580581
options?: MongoClientOptions | Callback<MongoClient>,
581582
callback?: Callback<MongoClient>
582583
): Promise<MongoClient> | void {
583-
if (typeof options === 'function') (callback = options), (options = {});
584-
options = options ?? {};
585-
586-
try {
587-
// Create client
588-
const mongoClient = new MongoClient(url, options);
589-
// Execute the connect method
590-
if (callback) {
591-
return mongoClient.connect(callback);
592-
} else {
593-
return mongoClient.connect();
594-
}
595-
} catch (error) {
596-
if (callback) {
597-
return callback(error);
598-
} else {
599-
const PromiseConstructor = PromiseProvider.get() ?? Promise;
600-
return PromiseConstructor.reject(error);
601-
}
602-
}
584+
callback =
585+
typeof callback === 'function'
586+
? callback
587+
: typeof options === 'function'
588+
? options
589+
: undefined;
590+
591+
return maybeCallback(async () => {
592+
options = typeof options !== 'function' ? options : undefined;
593+
const client = new this(url, options);
594+
return await client.connect();
595+
}, callback);
603596
}
604597

605598
/** Starts a new session on the server */

0 commit comments

Comments
 (0)