Protect your documents in MongoDB (Mongoose) from concurrency error while saving.
This plugin adds a version check when saving the document, or subdocuments (array of objects within a schema). Allows concurrency when saving subdocuments, and throws error when there is a change in the same subdocument.
npm i viniciusps2/mongoose-version-save
const VersionSavePlugin = require('mongoose-version-save')
const ChildSchema = Schema({
name: String,
age: Number
})
ChildSchema.plugin(VersionSavePlugin)
const ParentSchema = Schema({
name: String,
age: Number,
children: [Child],
})
ParentSchema.plugin(VersionSavePlugin)
const Parent = mongoose.model('Parent', ParentSchema)
And then create, delete, update, when someone else edit the document, it throws an error!