From 1bed0c701c86ab3b38c9df3fe06fb7493ded02e1 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 20 Sep 2023 16:40:45 -0400 Subject: [PATCH 1/2] fix(model): make `bulkSave()` persist changes that happen in pre('save') middleware Fix #13799 --- lib/model.js | 6 +++--- test/document.test.js | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/model.js b/lib/model.js index a6743f87d3d..436f9710831 100644 --- a/lib/model.js +++ b/lib/model.js @@ -3565,11 +3565,9 @@ Model.bulkWrite = async function bulkWrite(ops, options) { * @param {Boolean} [options.j=true] If false, disable [journal acknowledgement](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option) * */ -Model.bulkSave = async function(documents, options) { +Model.bulkSave = async function bulkSave(documents, options) { options = options || {}; - const writeOperations = this.buildBulkWriteOperations(documents, { skipValidation: true, timestamps: options.timestamps }); - if (options.timestamps != null) { for (const document of documents) { document.$__.saveOptions = document.$__.saveOptions || {}; @@ -3586,6 +3584,8 @@ Model.bulkSave = async function(documents, options) { await Promise.all(documents.map(buildPreSavePromise)); + const writeOperations = this.buildBulkWriteOperations(documents, { skipValidation: true, timestamps: options.timestamps }); + const { bulkWriteResult, bulkWriteError } = await this.bulkWrite(writeOperations, options).then( (res) => ({ bulkWriteResult: res, bulkWriteError: null }), (err) => ({ bulkWriteResult: null, bulkWriteError: err }) diff --git a/test/document.test.js b/test/document.test.js index 456677f32ef..7e714e4134b 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -12478,6 +12478,28 @@ describe('document', function() { await doc.save(); assert.ok(doc); }); + + it('bulkSave() picks up changes in pre("save") middleware (gh-13799)', async() => { + const schema = new Schema({ name: String, _age: { type: Number, min: 0, default: 0 } }); + schema.pre('save', function() { + this._age = this._age + 1; + }); + + const Person = db.model('Person', schema, 'Persons'); + const person = new Person({ name: 'Jean-Luc Picard', _age: 59 }); + + await Person.bulkSave([person]); + + let updatedPerson = await Person.findById(person._id); + + assert.equal(updatedPerson?._age, 60); + + await Person.bulkSave([updatedPerson]); + + updatedPerson = await Person.findById(person._id); + + assert.equal(updatedPerson?._age, 61); + }); }); describe('Check if instance function that is supplied in schema option is availabe', function() { From 947786079c09d9051915c5696d3ed4810b587d57 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 25 Sep 2023 11:08:22 -0400 Subject: [PATCH 2/2] style: fix lint --- test/document.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/document.test.js b/test/document.test.js index bb0dda2ad8a..1a0ba31721f 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -12500,7 +12500,7 @@ describe('document', function() { assert.equal(updatedPerson?._age, 61); }); - + it('handles default embedded discriminator values (gh-13835)', async function() { const childAbstractSchema = new Schema( { kind: { type: Schema.Types.String, enum: ['concreteKind'], required: true, default: 'concreteKind' } },