文件中说:
try {
const result = await sequelize.transaction(async (t) => {
const user = await User.create({
firstName: 'Abraham',
lastName: 'Lincoln'
}, { transaction: t });
await user.setShooter({
firstName: 'John',
lastName: 'Boothe'
}, { transaction: t });
return user;
});
// If the execution reaches this line, the transaction has been committed successfully
// `result` is whatever was returned from the transaction callback (the `user`, in this case)
} catch (error) {
// If the execution reaches this line, an error occurred.
// The transaction has already been rolled back automatically by Sequelize!
}
我想为此创建一个包装器,并想出如下所示的函数:
export const executor = async (query: Function, db: any) => {
try {
const result = await db.sequelize.transaction(async t => {
const resp = await query(t);
return resp;
});
// If the execution reaches this line, the transaction has been committed successfully
// `result` is whatever was returned from the transaction callback (the `user`, in this case)
return result;
} catch (error) {
// If the execution reaches this line, an error occurred.
// The transaction has already been rolled back automatically by Sequelize!
// log here
throw new Error('Internal server error');
}
};
像这样执行:
const r = executor(param => {
db.sequelize.query(
'CALL registerSportUser (:email, :password, :roleId, :firstName, :lastName, :age, :jobTitle, :prefLanguaged, :assigned, :sportId, :tableId, :position, :image, :imageName)',
{
replacements: {
email: args.input.Email,
password: PasswordHash,
roleId: args.input.RoleId,
firstName: args.input.FirstName,
lastName: args.input.LastName,
age: new Date(new Date(args.input.Age).toUTCString()),
jobTitle: args.input.JobTitle,
prefLanguaged: args.input.PrefLanguaged,
assigned: false,
sportId: '11ea859b-e3f3-6ba2-bf71-00ff98e8d5ab',
tableId: null,
position: null,
image: null,
imageName: null,
},
},
{ transaction: param }
);
}, db);
为了测试这一点,我发送了一个NULL值,该值被插入到不允许空的表colum中。然后,这将回滚我所有的db更改。
问题是它不会回滚任何东西..。
控制台显示:
Executing (6d1aaebb-5247-4b0d-a9c4-d5b81a6da8db): START TRANSACTION;
Executing (6d1aaebb-5247-4b0d-a9c4-d5b81a6da8db): COMMIT;
Executing (default): CALL registerSportUser('email', '$2b$12$i1mc.tLpG0HjhK0Y9C/gTO1ySwBguOa2Tlr1fFZHUf1IgIKESU3gC', 1, 'firstname', 'lastName', '1988-07-17 02:00:00', 'Jobtitle', 1, false, '11ea859b-e3f3-6ba2-bf71-00ff98e8d5ab', NULL, NULL, NULL, NULL)
错误:
Unhandled rejection SequelizeDatabaseError: Column 'TabelId' cannot be null
这个错误是有效的,因为im试图插入一个空值,其中不应该有空值,但是为什么不对回滚所有的更改进行后缀呢?
而不是我应该得到的错误:
Executing (e5fb0b6b-1340-4f9f-9bf0-f8913ba54751): ROLLBACK;
我认为我的包装是问题,但我不知道在哪里。
发布于 2020-05-12 15:09:44
你忘了回报db.sequelize.query的承诺
const r = executor(param => {
return db.sequelize.query(
'CALL registerSportUser (:email, :password, :roleId, :firstName, :lastName, :age, :jobTitle, :prefLanguaged, :assigned, :sportId, :tableId, :position, :image, :imageName)',
{
replacements: {
email: args.input.Email,
password: PasswordHash,
roleId: args.input.RoleId,
firstName: args.input.FirstName,
lastName: args.input.LastName,
age: new Date(new Date(args.input.Age).toUTCString()),
jobTitle: args.input.JobTitle,
prefLanguaged: args.input.PrefLanguaged,
assigned: false,
sportId: '11ea859b-e3f3-6ba2-bf71-00ff98e8d5ab',
tableId: null,
position: null,
image: null,
imageName: null,
},
},
{ transaction: param }
);
}, db);
https://stackoverflow.com/questions/61754750
复制相似问题