-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(model): throw MongooseBulkSaveIncompleteError if bulkSave() didn'…
…t completely succeed Fix #14763
- Loading branch information
Showing
3 changed files
with
77 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const MongooseError = require('./mongooseError'); | ||
|
||
|
||
/** | ||
* If the underwriting `bulkWrite()` for `bulkSave()` succeeded, but wasn't able to update or | ||
* insert all documents, we throw this error. | ||
* | ||
* @api private | ||
*/ | ||
|
||
class MongooseBulkSaveIncompleteError extends MongooseError { | ||
constructor(modelName, documents, bulkWriteResult) { | ||
const matchedCount = bulkWriteResult?.matchedCount ?? 0; | ||
const insertedCount = bulkWriteResult?.insertedCount ?? 0; | ||
let preview = documents.map(doc => doc._id).join(', '); | ||
if (preview.length > 100) { | ||
preview = preview.slice(0, 100) + '...'; | ||
} | ||
|
||
const numDocumentsNotUpdated = documents.length - matchedCount - insertedCount; | ||
super(`${modelName}.bulkSave() was not able to update ${numDocumentsNotUpdated} of the given documents due to incorrect version or optimistic concurrency, document ids: ${preview}`); | ||
|
||
this.modelName = modelName; | ||
this.documents = documents; | ||
this.bulkWriteResult = bulkWriteResult; | ||
this.numDocumentsNotUpdated = numDocumentsNotUpdated; | ||
} | ||
} | ||
|
||
Object.defineProperty(MongooseBulkSaveIncompleteError.prototype, 'name', { | ||
value: 'MongooseBulkSaveIncompleteError' | ||
}); | ||
|
||
/*! | ||
* exports | ||
*/ | ||
|
||
module.exports = MongooseBulkSaveIncompleteError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters