javascript - Mongoose return value from withTransaction callback - Stack Overflow

I want to utilize mongoose's withTransaction helper particularly for its ability to automatically

I want to utilize mongoose's withTransaction helper particularly for its ability to automatically retry transient transaction errors. However, it seems that the withTransaction helper is incapable of returning data, which is a problem for me.

I have code that looks like:

import { startSession } from 'mongoose';

async addItem(itemData) {
    const session = await startSession();
 
    session.startTransaction();
    try {
        const item = await new Item({ itemData }).save({ session });
        
        // a bunch of other async operations...

       await sessionmitTransaction();
       session.endSession();

       return item;
    } catch (error) {
        await session.abortTransaction();
        session.endSession();
        throw error;
    }
}

How can I either (1) use the withTransaction helper but still have this function returning the item as it currently does, or (2) make this function automatically retry on transient transaction errors through some way other than using withTransaction.

I want to utilize mongoose's withTransaction helper particularly for its ability to automatically retry transient transaction errors. However, it seems that the withTransaction helper is incapable of returning data, which is a problem for me.

I have code that looks like:

import { startSession } from 'mongoose';

async addItem(itemData) {
    const session = await startSession();
 
    session.startTransaction();
    try {
        const item = await new Item({ itemData }).save({ session });
        
        // a bunch of other async operations...

       await session.mitTransaction();
       session.endSession();

       return item;
    } catch (error) {
        await session.abortTransaction();
        session.endSession();
        throw error;
    }
}

How can I either (1) use the withTransaction helper but still have this function returning the item as it currently does, or (2) make this function automatically retry on transient transaction errors through some way other than using withTransaction.

Share Improve this question edited Oct 26, 2020 at 0:29 D. SM 14.5k3 gold badges14 silver badges22 bronze badges asked Jul 12, 2020 at 0:03 Infamous911Infamous911 1,5314 gold badges26 silver badges46 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

This appears to be a known issue in the node driver. Some workarounds are provided in that ticket.

I wrote a simple helper that internally uses withTransaction to solve the problem and make transactions less verbose with mongoose.

After installing mongoose-trx you can simply do:

const transaction = require('mongoose-trx');

const [customer] = await transaction(session => Customer.create([{ name: 'Test' }], { session }));

// do whatever you need to do with the customer then return it

It supports transaction options as well, see the documentation on how to do it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742305005a4418761.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信