【问题标题】:Node.js - How to use Sequelize transactionNode.js - 如何使用 Sequelize 交易
【发布时间】:2020-03-05 19:31:26
【问题描述】:

我是sequelize 的初学者,无法进行交易。文档不清楚,使以下示例无法适应我的要求。

  return sequelize.transaction(t => {
  // chain all your queries here. make sure you return them.
  return User.create({
    firstName: 'Abraham',
    lastName: 'Lincoln'
  }, {transaction: t}).then(user => {
    return user.setShooter({
      firstName: 'John',
      lastName: 'Boothe'
    }, {transaction: t});
  });

}).then(result => {
  // Transaction has been committed
  // result is whatever the result of the promise chain returned to the transaction callback
}).catch(err => {
  // Transaction has been rolled back
  // err is whatever rejected the promise chain returned to the transaction callback
});

首先我必须在“Conto”中插入一个元组,然后在“Preferenze”中插入另一个元组,最后根据“tipo”属性在“ContoPersonale”或“ContoAziendale”中插入一个元组。

如果这些查询中只有一个失败,事务必须进行完全回滚,提交。

查询是:

Conto.create({
        id: nextId(),
        mail: reg.email,
        password: reg.password,
        tipo: reg.tipo,
        telefono: reg.telefono,
        idTelegram: reg.telegram,
        saldo: saldoIniziale,
        iban: generaIBAN()
    })

Preferenze.create({
        refConto: 68541
    })

if (tipo == 0) {
        ContoPersonale.create({
        nomeint: reg.nome,
        cognomeint: reg.cognome,
        dataN: reg.datan,
        cf: reg.cf,
        refConto: nextId()
        }) 
        }
else if (tipo == 1) { 
        ContoAziendale.create({
        pIva: reg.piva,
        ragioneSociale: reg.ragsoc,
        refConto: nextId()
        })
        }

【问题讨论】:

    标签: javascript mysql node.js transactions sequelize.js


    【解决方案1】:

    使用事务将其传递给您希望成为事务一部分的每个查询,然后在完成时调用transaction.commit(),或调用transaction.rollback() 以回滚所有更改。这可以使用 thenables 来完成,但使用 async/await 时会更清晰。

    由于您的所有查询都不相互依赖,您也可以使用Promise.all() 同时进行查询。

    thenables(自动提交)

    sequelize.transaction((transaction) => {
      // execute all queries, pass in transaction
      return Promise.all([
        Conto.create({
          id: nextId(),
          mail: reg.email,
          password: reg.password,
          tipo: reg.tipo,
          telefono: reg.telefono,
          idTelegram: reg.telegram,
          saldo: saldoIniziale,
          iban: generaIBAN()
        }, { transaction }),
    
        Preferenze.create({
          refConto: 68541
        }, { transaction }),
    
        // this query is determined by "tipo"
        tipo === 0
          ? ContoPersonale.create({
              nomeint: reg.nome,
              cognomeint: reg.cognome,
              dataN: reg.datan,
              cf: reg.cf,
              refConto: nextId()
            }, { transaction })
          : ContoAziendale.create({
              pIva: reg.piva,
              ragioneSociale: reg.ragsoc,
              refConto: nextId()
            }, { transaction })
      ]);
    
      // if we get here it will auto commit
      // if there is an error it with automatically roll back.
    
    })
    .then(() => {
      console.log('queries ran successfully');
    })
    .catch((err) => {
      console.log('queries failed', err);
    });
    

    异步/等待

    let transaction;
    try {
      // start a new transaction
      transaction = await sequelize.transaction();
    
      // run queries, pass in transaction
      await Promise.all([
        Conto.create({
          id: nextId(),
          mail: reg.email,
          password: reg.password,
          tipo: reg.tipo,
          telefono: reg.telefono,
          idTelegram: reg.telegram,
          saldo: saldoIniziale,
          iban: generaIBAN()
        }, { transaction }),
    
        Preferenze.create({
          refConto: 68541
        }, { transaction }),
    
        // this query is determined by "tipo"
        tipo === 0
          ? ContoPersonale.create({
              nomeint: reg.nome,
              cognomeint: reg.cognome,
              dataN: reg.datan,
              cf: reg.cf,
              refConto: nextId()
            }, { transaction })
          : ContoAziendale.create({
              pIva: reg.piva,
              ragioneSociale: reg.ragsoc,
              refConto: nextId()
            }, { transaction })
      ]);
    
      // if we get here they ran successfully, so...
      await transaction.commit();
    } catch (err) {
      // if we got an error and we created the transaction, roll it back
      if (transaction) {
        await transaction.rollback();
      }
      console.log('Err', err);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-02
      • 1970-01-01
      • 2015-02-27
      • 2017-12-03
      • 2019-02-14
      • 2019-05-23
      • 2022-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多