@multiple-transaction-manager/mongodb
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

@multiple-transaction-manager/mongodb

MongoDB context implementation for multiple-transaction-manager library.

API

Classes

MongoContext

constructor(txnMngr, mongoose, options)

  • txnMngr: {MultiTxnMngr} The multiple transaction manager to to bind with the context.
  • mongoose: {Mongoose} The mongoose instance to obtain the session from.
  • options: {ClientSessionOptions} The mongoose session options.
  • Returns: {MongoContext} The created MongoContext instance.

addFunctionTask(execFunc)

Adds a task to the transaction manager.

  • execFunc: {execFunc: (mongoose: Mongoose, txn: ClientSession, task: Task) => Promise<unknown>} The function to be executes in promise. Mongoose instance and current transaction handle are provided to the function.
  • Returns: {MongoTask} The created MongoTask instance.

MongoTask

constructor(context, execFunc)

  • context: {MongoContext} The MongoContext to to bind with the task.
  • execFunc: {execFunc: (mongoose: Mongoose, txn: ClientSession, task: Task) => Promise<unknown>} The function to be executes in promise. Mongoose instance and current transaction handle are provided to the function.
  • Returns: {MongoTask} The created MongoTask instance.

Example

https://github.com/kaplanke/mtxn-mongodb/blob/master/test/mtxn.mongo.test.ts

    // init manager & context
    const txnMngr: MultiTxnMngr = new MultiTxnMngr();
    const mongoContext = new MongoContext(txnMngr, theMongoose);

    // Add first step
    mongoContext.addFunctionTask((_mongoose, txn, _task) => studentModel.create([{ sid: 1, "name": "Kevin" }], { session: txn }));

    // Add second step
    mongoContext.addFunctionTask((_mongoose, txn, _task) => studentModel.create([{ sid: 2, "name": "Stuart" }], { session: txn }));

    // Uncomment next line if you want to test rollback scenario 
    // mongoContext.addFunctionTask(txnMngr, (mongoose, txn, task) => studentModel.create([{ sid: 2, "name": "Bob" }], { session: txn }));

    // Add control step
    const controlTask = mongoContext.addFunctionTask((_mongoose, txn, _task) => studentModel.findOne({ sid: 1 }).session(txn).exec());

    await txnMngr.exec();

    //jest
    expect(controlTask.getResult().name).toEqual("Kevin");

Package Sidebar

Install

npm i @multiple-transaction-manager/mongodb

Weekly Downloads

0

Version

1.0.5

License

MIT

Unpacked Size

15.4 kB

Total Files

9

Last publish

Collaborators

  • kaplanke