mongoose-acid
😎 mongoose transaction helper
prompt
if you are using mongodb's multi-document transaction capabilities, your version of mongodb needs to be larger than 4.0, and you need a replication set for transactions to work, in mongodb4.2, multi-document transactions will be supported on sharding
install
npm i mongoose-acid mongoose -S
required
- nodejs >= 7.6
- mongoose >= 5.2
- mongodb >= 4.0
usage
normal
const Acid = await
in koa
const Acid = const app = appapp
session option position
Model.create([], { session })
Model.deleteOne(condition, { session })
Model.deleteMany(condition, { session })
Model.updateOne(condition, update, { session })
Model.updateMany(condition, update, { session })
Model.update(condition, update, { session })
Model.insertMany(docs, { session })
Model.findById(id, selects, { session })
Model.findByIdAndUpdate(id, update, { session })
Model.findByIdAndRemove(id, { session })
Model.findByIdAndDelete(id, { session })
Model.findOne(condition, selects, { session })
Model.findOneAndUpdate(condition, update, { session })
Model.findOneAndRemove(condition, { session })
Model.findOneAndDelete(condition, { session })
model.save({ session })
model.remove({ session })
debug
Acid
notice
- The collection cannot be created in a transaction and will report an error, so you need to create the collection before using the
mongoose
andmongodb
transaction functions - Note that if the model is returned by a query with the session option set, you do not need to set it again
- Note that the first argument to the
Model.create
method must be an array, and the second argument can set the session option
test
npm test