事务

ThinkORM目前支持Mysql、postgreSQL的事务操作,代码示例:


//实例化模型
let userModel = new user(config);

//开始事务
return userModel.transaction(async function (t) {
    //同步模式
    for (var i = 1; i < 5; i++) {
       await userModel.add({name: 'rrrrrrrrrrrrr'});
       await userModel.add({name: 'rrrrrrr'});
       await userModel.add({name: 'rrrrrrrrrrrrr'});
    }

    //Promise.all并行模式
    let ps = [];
    for (var i = 1; i < 5; i++) {
       ps.push(userModel.add({name: 'rrrrrrrrrrrrr'}));
       ps.push(userModel.add({name: 'rrrr'}));
       ps.push(userModel.add({name: 'rrrrrrrrrrrrr'}));
    }
    return Promise.all(ps);

    //跨模型执行

    //主模型写入数据
    await userModel.add({name: 'rrrrrrrrrrrrr'}); 
    //profile模型写入数据
    await profileModel.setInstance(t).add({test: ['rrrtest']});
    // 或者
    await userModel.model(profileModel).add({test: ['rrrtest']});

}).then(data => {
  console.log(data);//事务commit后打印
});