Skip to content

Commit

Permalink
feat(model): allow passing `options to bulkSave() for sessions an…
Browse files Browse the repository at this point in the history
…d writeconcerns

Fix #11127
  • Loading branch information
vkarpov15 committed Jan 9, 2022
1 parent ff42635 commit 36c9e6a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3597,16 +3597,21 @@ Model.bulkWrite = function(ops, options, callback) {
* `bulkSave` uses `bulkWrite` under the hood, so it's mostly useful when dealing with many documents (10K+)
*
* @param {[Document]} documents
* @param {Object} [options] options passed to the underlying `bulkWrite()`
* @param {ClientSession} [options.session=null] The session associated with this bulk write. See [transactions docs](/docs/transactions.html).
* @param {String|number} [options.w=1] The [write concern](https://docs.mongodb.com/manual/reference/write-concern/). See [`Query#w()`](/docs/api.html#query_Query-w) for more information.
* @param {number} [options.wtimeout=null] The [write concern timeout](https://docs.mongodb.com/manual/reference/write-concern/#wtimeout).
* @param {Boolean} [options.j=true] If false, disable [journal acknowledgement](https://docs.mongodb.com/manual/reference/write-concern/#j-option)
*
*/
Model.bulkSave = function(documents) {
Model.bulkSave = function(documents, options) {
const preSavePromises = documents.map(buildPreSavePromise);

const writeOperations = this.buildBulkWriteOperations(documents, { skipValidation: true });

let bulkWriteResultPromise;
return Promise.all(preSavePromises)
.then(() => bulkWriteResultPromise = this.bulkWrite(writeOperations))
.then(() => bulkWriteResultPromise = this.bulkWrite(writeOperations, options))
.then(() => documents.map(buildSuccessfulWriteHandlerPromise))
.then(() => bulkWriteResultPromise)
.catch((err) => {
Expand Down

0 comments on commit 36c9e6a

Please sign in to comment.