Skip to content

Commit

Permalink
refactor(NODE-2978)!: remove deprecated bulk ops (#2794)
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum authored May 5, 2021
1 parent b45e3b3 commit c3a1839
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 48 deletions.
33 changes: 1 addition & 32 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,9 @@ export interface UpdateManyModel {
/** @public */
export type AnyBulkWriteOperation =
| { insertOne: InsertOneModel }
| { insertMany: Document[] }
| { replaceOne: ReplaceOneModel }
| { updateOne: UpdateOneModel }
| { updateMany: UpdateManyModel }
| { removeOne: DeleteOneModel }
| { removeMany: DeleteManyModel }
| { deleteOne: DeleteOneModel }
| { deleteMany: DeleteManyModel };

Expand Down Expand Up @@ -786,14 +783,6 @@ export class FindOperators {
);
}

removeOne(): BulkOperationBase {
return this.deleteOne();
}

remove(): BulkOperationBase {
return this.delete();
}

/** Upsert modifier for update bulk operation, noting that this operation is an upsert. */
upsert(): this {
if (!this.bulkOperation.s.currentOp) {
Expand Down Expand Up @@ -1070,12 +1059,6 @@ export abstract class BulkOperationBase {
return this.addToOperationsList(BatchType.INSERT, op.insertOne.document);
}

// NOTE: incompatible with CRUD specification, consider removing
if ('insertMany' in op) {
op.insertMany.forEach(insertOp => this.raw({ insertOne: { document: insertOp } }));
return this;
}

if ('replaceOne' in op || 'updateOne' in op || 'updateMany' in op) {
if ('replaceOne' in op) {
if ('q' in op.replaceOne) {
Expand Down Expand Up @@ -1121,20 +1104,6 @@ export abstract class BulkOperationBase {
}
}

if ('removeOne' in op) {
return this.addToOperationsList(
BatchType.DELETE,
makeDeleteStatement(op.removeOne.filter, { ...op.removeOne, limit: 1 })
);
}

if ('removeMany' in op) {
return this.addToOperationsList(
BatchType.DELETE,
makeDeleteStatement(op.removeMany.filter, { ...op.removeMany, limit: 0 })
);
}

if ('deleteOne' in op) {
if ('q' in op.deleteOne) {
throw new TypeError('Raw operations are not allowed');
Expand All @@ -1157,7 +1126,7 @@ export abstract class BulkOperationBase {

// otherwise an unknown operation was provided
throw TypeError(
'bulkWrite only supports insertOne, insertMany, updateOne, updateMany, removeOne, removeMany, deleteOne, deleteMany'
'bulkWrite only supports insertOne, updateOne, updateMany, deleteOne, deleteMany'
);
}

Expand Down
4 changes: 1 addition & 3 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,6 @@ export class Collection {
* ```js
* { insertOne: { document: { a: 1 } } }
*
* { insertMany: [{ g: 1 }, { g: 2 }]}
*
* { updateOne: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } }
*
* { updateMany: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } }
Expand All @@ -326,7 +324,7 @@ export class Collection {
*
* { deleteMany: { filter: {c:1} } }
*
* { replaceOne: { filter: {c:3}, replacement: {c:4}, upsert:true}}
* { replaceOne: { filter: {c:3}, replacement: {c:4}, upsert:true} }
*```
* Please note that raw operations are no longer accepted as of driver version 4.0.
*
Expand Down
2 changes: 1 addition & 1 deletion src/operations/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class InsertManyOperation extends AbstractOperation<InsertManyResult> {
const writeConcern = WriteConcern.fromOptions(options);
const bulkWriteOperation = new BulkWriteOperation(
coll,
[{ insertMany: prepareDocs(coll, this.docs, options) }],
prepareDocs(coll, this.docs, options).map(document => ({ insertOne: { document } })),
options
);

Expand Down
8 changes: 4 additions & 4 deletions test/functional/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ describe('Bulk', function () {
}

bulk.find({ b: 1 }).upsert().update({ b: 1 });
bulk.find({ c: 1 }).remove();
bulk.find({ c: 1 }).delete();

bulk.execute({ writeConcern: { w: 0 } }, function (err, result) {
expect(err).to.not.exist;
Expand Down Expand Up @@ -1135,7 +1135,7 @@ describe('Bulk', function () {
}

bulk.find({ b: 1 }).upsert().update({ b: 1 });
bulk.find({ c: 1 }).remove();
bulk.find({ c: 1 }).delete();

bulk.execute({ writeConcern: { w: 0 } }, function (err, result) {
expect(err).to.not.exist;
Expand Down Expand Up @@ -1917,8 +1917,8 @@ describe('Bulk', function () {
bulk.find({ b: 3 }).collation({ locale: locales[2] }).replaceOne({ b: 2 });

// deletes
bulk.find({ b: 2 }).collation({ locale: locales[0] }).removeOne();
bulk.find({ b: 1 }).collation({ locale: locales[1] }).remove();
bulk.find({ b: 2 }).collation({ locale: locales[0] }).deleteOne();
bulk.find({ b: 1 }).collation({ locale: locales[1] }).delete();

bulk.execute(err => {
expect(err).to.not.exist;
Expand Down
6 changes: 4 additions & 2 deletions test/functional/crud_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ describe('CRUD API', function () {
db.collection('t2_5').bulkWrite(
[
{ insertOne: { document: { a: 1 } } },
{ insertMany: [{ g: 1 }, { g: 2 }] },
{ insertOne: { document: { g: 1 } } },
{ insertOne: { document: { g: 2 } } },
{ updateOne: { filter: { a: 2 }, update: { $set: { a: 2 } }, upsert: true } },
{ updateMany: { filter: { a: 2 }, update: { $set: { a: 2 } }, upsert: true } },
{ deleteOne: { filter: { c: 1 } } },
Expand Down Expand Up @@ -443,7 +444,8 @@ describe('CRUD API', function () {
db.collection('t2_7').bulkWrite(
[
{ insertOne: { document: { a: 1 } } },
{ insertMany: [{ g: 1 }, { g: 2 }] },
{ insertOne: { document: { g: 1 } } },
{ insertOne: { document: { g: 2 } } },
{ updateOne: { filter: { a: 2 }, update: { $set: { a: 2 } }, upsert: true } },
{ updateMany: { filter: { a: 2 }, update: { $set: { a: 2 } }, upsert: true } },
{ deleteOne: { filter: { c: 1 } } },
Expand Down
4 changes: 2 additions & 2 deletions test/functional/operation_example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5903,7 +5903,7 @@ describe('Operation Examples', function () {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

// Execute the operations
batch.execute(function (err, result) {
Expand Down Expand Up @@ -5969,7 +5969,7 @@ describe('Operation Examples', function () {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

// Execute the operations
batch.execute(function (err, result) {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/operation_generators_example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3861,7 +3861,7 @@ describe('Operation (Generators)', function () {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

// Execute the operations
var result = yield batch.execute();
Expand Down Expand Up @@ -3932,7 +3932,7 @@ describe('Operation (Generators)', function () {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

// Execute the operations
var result = yield batch.execute();
Expand Down
4 changes: 2 additions & 2 deletions test/functional/operation_promises_example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4241,7 +4241,7 @@ describe('Operation (Promises)', function () {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

// Execute the operations
return batch.execute().then(function (result) {
Expand Down Expand Up @@ -4308,7 +4308,7 @@ describe('Operation (Promises)', function () {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

// Execute the operations
return batch.execute().then(function (result) {
Expand Down

0 comments on commit c3a1839

Please sign in to comment.