let transaction;
    try {
      // 建立事务对象
      transaction = await this.ctx.model.transaction();
      const house = await ctx.model.House.create({
        address,
        area: data.area,
        floor: data.floor,
        attribute: data.attributeID,
        sign: data.signID,
        remark: data.remark,
      }, {
        transaction,
      });

      await ctx.model.CommunityHouse.create({
        community_id: data.communityID,
        house_id: house.id,
      }, {
        transaction,
      });
      // 提交事务
      await transaction.commit();
    } catch (e) {
      // 事务回滚
      await transaction.rollback();
    }

其他增删改查

let transaction;
try {
    // 建立事务对象
    transaction = await this.ctx.model.transaction();
    
    // 事务增操作
    await this.ctx.model.VirtualDeptMember.create({
        JSON格式数据
    }, {
        transaction,
    });
    
    // 事务删操作
    await this.ctx.model.VirtualDeptMember.destroy({
        where: {
            JSON格式数据
        },
        transaction,
    });
    
    // 事务改操作
    await this.ctx.model.Device.update({
        修改的JSON数据
    }, {
        where: {
            查询的JSON数据
        },
        transaction,
    });
    
    // 提交事务
    await transaction.commit();
} catch (err) {
    // 事务回滚
    await transaction.rollback();
}
posted on 2021-11-27 22:15  无聊猿  阅读(168)  评论(0编辑  收藏  举报