Skip to content

Commit d338909

Browse files
committed
fix: make bulkSave() continue to use validateSync() for backwards compat
1 parent c289cb4 commit d338909

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

lib/model.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3571,7 +3571,7 @@ Model.bulkSave = async function bulkSave(documents, options) {
35713571

35723572
await Promise.all(documents.map(doc => buildPreSavePromise(doc, options)));
35733573

3574-
const writeOperations = await this.buildBulkWriteOperations(documents, options);
3574+
const writeOperations = this.buildBulkWriteOperations(documents, options);
35753575
const opts = { skipValidation: true, _skipCastBulkWrite: true, ...options };
35763576
const { bulkWriteResult, bulkWriteError } = await this.bulkWrite(writeOperations, opts).then(
35773577
(res) => ({ bulkWriteResult: res, bulkWriteError: null }),
@@ -3866,20 +3866,23 @@ Model.castObject = function castObject(obj, options) {
38663866
* @api private
38673867
*/
38683868

3869-
Model.buildBulkWriteOperations = async function buildBulkWriteOperations(documents, options) {
3869+
Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, options) {
38703870
if (!Array.isArray(documents)) {
38713871
throw new Error(`bulkSave expects an array of documents to be passed, received \`${documents}\` instead`);
38723872
}
38733873

38743874
setDefaultOptions();
38753875

3876-
const writeOperations = await Promise.all(documents.map(async(document, i) => {
3876+
const writeOperations = documents.map((document, i) => {
38773877
if (!options.skipValidation) {
38783878
if (!(document instanceof Document)) {
38793879
throw new Error(`documents.${i} was not a mongoose document, documents must be an array of mongoose documents (instanceof mongoose.Document).`);
38803880
}
38813881
if (options.validateBeforeSave == null || options.validateBeforeSave) {
3882-
await document.validate();
3882+
const err = document.validateSync();
3883+
if (err != null) {
3884+
throw err;
3885+
}
38833886
}
38843887
}
38853888

@@ -3917,7 +3920,7 @@ Model.buildBulkWriteOperations = async function buildBulkWriteOperations(documen
39173920
}
39183921

39193922
return null;
3920-
})).then(results => results.filter(op => op !== null));
3923+
}).filter(op => op !== null);
39213924

39223925
return writeOperations;
39233926

test/model.test.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6780,7 +6780,7 @@ describe('Model', function() {
67806780
await users[2].save();
67816781
users[2].name = 'I am the updated third name';
67826782

6783-
const writeOperations = await User.buildBulkWriteOperations(users);
6783+
const writeOperations = User.buildBulkWriteOperations(users);
67846784

67856785
const desiredWriteOperations = [
67866786
{ insertOne: { document: users[0] } },
@@ -6795,7 +6795,7 @@ describe('Model', function() {
67956795

67966796
});
67976797

6798-
it('throws an error when one document is invalid', async() => {
6798+
it('throws an error when one document is invalid', () => {
67996799
const userSchema = new Schema({
68006800
name: { type: String, minLength: 5 }
68016801
});
@@ -6808,14 +6808,10 @@ describe('Model', function() {
68086808
new User({ name: 'b' })
68096809
];
68106810

6811-
let err;
6812-
try {
6813-
await User.buildBulkWriteOperations(users);
6814-
} catch (error) {
6815-
err = error;
6816-
}
6817-
6818-
assert.ok(err);
6811+
assert.throws(
6812+
() => User.buildBulkWriteOperations(users),
6813+
/name: Path `name` \(`a`\) is shorter than the minimum allowed length/
6814+
);
68196815
});
68206816

68216817
it('throws an error if documents is not an array', function() {
@@ -6826,8 +6822,8 @@ describe('Model', function() {
68266822
const User = db.model('User', userSchema);
68276823

68286824

6829-
assert.rejects(
6830-
User.buildBulkWriteOperations(null),
6825+
assert.throws(
6826+
() => User.buildBulkWriteOperations(null),
68316827
/bulkSave expects an array of documents to be passed/
68326828
);
68336829
});
@@ -6838,16 +6834,15 @@ describe('Model', function() {
68386834

68396835
const User = db.model('User', userSchema);
68406836

6841-
6842-
assert.rejects(
6843-
User.buildBulkWriteOperations([
6837+
assert.throws(
6838+
() => User.buildBulkWriteOperations([
68446839
new User({ name: 'Hafez' }),
68456840
{ name: 'I am not a document' }
68466841
]),
68476842
/documents\.1 was not a mongoose document/
68486843
);
68496844
});
6850-
it('skips validation when given `skipValidation` true', async() => {
6845+
it('skips validation when given `skipValidation` true', () => {
68516846
const userSchema = new Schema({
68526847
name: { type: String, minLength: 5 }
68536848
});
@@ -6860,7 +6855,7 @@ describe('Model', function() {
68606855
new User({ name: 'b' })
68616856
];
68626857

6863-
const writeOperations = await User.buildBulkWriteOperations(users, { skipValidation: true });
6858+
const writeOperations = User.buildBulkWriteOperations(users, { skipValidation: true });
68646859

68656860
assert.equal(writeOperations.length, 3);
68666861
});

0 commit comments

Comments
 (0)