在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:mongo-tx开源软件地址:https://gitee.com/zane_young/mongo-tx开源软件介绍:mongo-txA flexible & extensible mongodb transaction library. Install
or
How toIntroWhen you create a transaction and run it, You need to use model wrappers to modify data during transactions, each model wrapper would create lock and snapshot before find/findOne/create/modifie/remove documents, after all operations of these documents in this transaction have succeeded, transaction manager will remove all snapshots and release all locks (committing). If error happened in this transaction, all changed documents will be replaced by snapshots and locks will also be released (rollingback). LockBuilt-in lock is implemented by mongo's unique key, and using jdarling/MongoMQ to create a waiting lock. You can create your own lock by redis, ssdb or other library. NOTICE:
const lock = runTx.createDocLock(colName: string, docId: ObjectId|string)await lock.lock()// do somethingawait lock.release() Fix process crashIf your whole process is crashed during the transaction, call WriteConcernAll collections in this library is using Usageimport mongoTx from 'mongo-tx'import createMongoModel from 'mongo-tx/lib/implements/create-mongo-model'import createMongoLock from 'mongo-tx/lib/implements/create-mongo-lock'const runTx = mongoTx({ // mongoTx options createModel: createMongoModel({ db: nativeDb }), createLock: createMongoLock({ db: nativeDb, wait: true }), // wait is true: wait until current release is release instead of throw an error txColName: 'tx_manager', // collection name of transactions, default `tx_manager` commitRetry: 3, // commit retry times, default is 3 commitInterval: 300, // commit retry interval, default is 300ms rollbackRetry: 3, // rollback retry times, default is 3 rollbackInterval: 300, // rollback retry interval, default is 300ms lockTxName: false, // whether create a lock for the transaction name, this would cause transactions with the same name runs serially, default is false})/** * @param {string|array} txName can be an array to create multi locks for txName * @param {object} options optional, would override mongoTx options * @param {function} fn async function to run your transtaion * @type {[type]} */await runTx('some_transfer', async tx => { const TxAccounts = tx.wrap('accounts') let acc1 = await TxAccounts.findOne({name: 'u1'}, {_id: 1}) let acc2 = await TxAccounts.findOne({name: 'u2'}, {_id: 1}) await TxAccounts.findOneAndUpdate({ name: 'u1', }, { $set: { money: acc1.money - 100, }, }) throw new Error('Some error cause auto rollback!') await TxAccounts.findOneAndUpdate({ name: 'u2', }, { $set: { money: acc2.money + 100, }, })})// other code TxModelclass TxModel { /** * insert document * @param {object} doc * @return {} document with _id */ create(doc: object) { // ... }, /** * find documents * @param {object} match query expression * @return {Array<object>} documents */ find(match) { // ... }, /** * find one document * @param {object} match query expression * @return {object} document */ findOne(match) { // ... }, /** * findOneAndUpdate * @param {object} match query expression * @param {object} update update expression * @param {object} options see native-mongo-driver * @return {object} original doc or new doc */ findOneAndUpdate(match, update, options) { // ... }, /** * findOneAndRemove * @param {object} match query expression * @param {object} options see native-mongo-driver * @return {object} original doc */ findOneAndRemove(match, options) { // ... },} TipsFor more use case please see test folder. For more options please see each implements. |
请发表评论