Created
May 2, 2018 17:29
-
-
Save theotherdon/83e0faf979d603e67e8fefed28aaf5ac to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const mongoose = require('mongoose') | |
const Promise = require('bluebird') | |
mongoose.Promise = Promise | |
const mixinBulkWriteValidation = require('./mixinBulkWriteValidation') | |
mongoose.connect('mongodb://localhost/bulk_update_validation_test', { useMongoClient: true }) | |
const connection = mongoose.connection | |
const Schema = mongoose.Schema | |
const schema = new Schema({ | |
name: String | |
}) | |
const WithoutValidation = mongoose.model('WithoutValidation', schema) | |
const WithValidation = mixinBulkWriteValidation( | |
mongoose.model('WithValidation', schema) | |
) | |
const updateOne = (document, update) => { | |
return { | |
updateOne: { | |
filter: { | |
_id: document._id | |
}, | |
update: { | |
$set: update | |
} | |
} | |
} | |
} | |
async function run () { | |
await connection.dropDatabase() | |
const withoutValidationBefore = await WithoutValidation.create({ | |
name: 'Some name' | |
}) | |
const withValidationBefore = await WithValidation.create({ | |
name: 'Some name' | |
}) | |
await WithoutValidation.bulkWrite([ | |
updateOne(withoutValidationBefore, { name: 'Some other name', foo: 'bar' }) | |
]) | |
await WithValidation.bulkWrite([ | |
updateOne(withValidationBefore, { name: 'Some other name', foo: 'bar' }) | |
]) | |
const withoutValidationAfter = await WithoutValidation.findOne().lean() | |
const withValidationAfter = await WithValidation.findOne().lean() | |
console.log( | |
'withoutValidationBefore', | |
JSON.stringify(withoutValidationBefore, null, 2) | |
) | |
console.log( | |
'withoutValidationAfter', | |
JSON.stringify(withoutValidationAfter, null, 2) | |
) | |
console.log( | |
'withValidationBefore', | |
JSON.stringify(withValidationBefore, null, 2) | |
) | |
console.log( | |
'withValidationAfter', | |
JSON.stringify(withValidationAfter, null, 2) | |
) | |
return connection.close() | |
} | |
run().catch(console.error) |
This file contains hidden or 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
const _ = require('lodash') | |
module.exports = model => { | |
const originalBulkWrite = model.bulkWrite | |
model.bulkWrite = function () { | |
arguments[0] = removeKeysNotInSchema(arguments[0], model) | |
return originalBulkWrite.apply(model, arguments) | |
} | |
return model | |
} | |
const removeKeysNotInSchema = (ops, model) => | |
_.map(ops, op => { | |
const $set = _.get(op, 'updateOne.update.$set') | |
if (!$set) return op | |
const updateKeys = _.keys($set) | |
const schemaKeys = _.keys(model.schema.paths) | |
const keysNotInSchema = _.difference(updateKeys, schemaKeys) | |
_.each(keysNotInSchema, keyNotInSchema => { | |
delete op.updateOne.update.$set[keyNotInSchema] | |
}) | |
return op | |
}) |
This file contains hidden or 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
node bulkUpdateValidation.js | |
withoutValidationBefore { | |
"__v": 0, | |
"name": "Some name", | |
"_id": "5ae9f4e8219d9b1e34ab9258" | |
} | |
withoutValidationAfter { | |
"_id": "5ae9f4e8219d9b1e34ab9258", | |
"name": "Some other name", | |
"__v": 0, | |
"foo": "bar" | |
} | |
withValidationBefore { | |
"__v": 0, | |
"name": "Some name", | |
"_id": "5ae9f4e8219d9b1e34ab9259" | |
} | |
withValidationAfter { | |
"_id": "5ae9f4e8219d9b1e34ab9259", | |
"name": "Some other name", | |
"__v": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Removes keys / values that aren't part of a Mongoose model's schema during a
Model.bulkWrite
.