【发布时间】: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