事务

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

  1. //实例化模型
  2. let model = new user(config);
  3. //开始事务
  4. return model.transaction(async function (t) {
  5. //同步模式
  6. // for (var i = 1; i < 5; i++) {
  7. // await model.add({name: 'rrrrrrrrrrrrr'});
  8. // await model.add({name: 'rrrrrrr'});
  9. // await model.add({name: 'rrrrrrrrrrrrr'});
  10. // }
  11. //Promise.all并行模式
  12. //var ps = [];
  13. //for (var i = 1; i < 5; i++) {
  14. // ps.push(model.add({name: 'rrrrrrrrrrrrr'}));
  15. // ps.push(model.add({name: 'rrrr'}));
  16. // ps.push(model.add({name: 'rrrrrrrrrrrrr'}));
  17. //}
  18. //return Promise.all(ps);
  19. //跨模型执行
  20. await model.add({name: 'rrrrrrrrrrrrr'}); //主模型写入数据
  21. let profileModel = await THINK.model('Common/Profile', {}).initDB(t);//实例化profile模型
  22. return profileModel.add({test: ['rrrtest']});//profile模型写入数据
  23. }).then(data => {
  24. console.log(data);//事务commit后打印
  25. });