We need to implement two types of Sequelize transactions:
# Terminal commands npm install npx sequelize-cli db:migrate npx sequelize-cli db:seed:all
File Location: app.js
app.get('/unmanaged', async (req, res, next) => {
// Create the transaction
const t = await sequelize.transaction();
try {
// Find Rose's account and update
let rose = await Account.findOne({
where: {
firstName: 'Rose',
lastName: 'Tyler'
},
transaction: t
});
await rose.update({
balance: rose.balance + 200
}, {
transaction: t
});
// Find Martha's account and update
let martha = await Account.findOne({
where: {
firstName: 'Martha',
lastName: 'Jones'
},
transaction: t
});
await martha.update({
balance: martha.balance - 200
}, {
transaction: t
});
// Commit the transaction
await t.commit();
// Return all accounts
let allAccounts = await Account.findAll({
order: [['firstName', 'ASC']]
});
res.json(allAccounts);
} catch (error) {
// Rollback on error
await t.rollback();
next(error);
}
});
app.get('/managed', async (req, res, next) => {
try {
await sequelize.transaction(async (t) => {
// Find Rose's account and update
let rose = await Account.findOne({
where: {
firstName: 'Rose',
lastName: 'Tyler'
},
transaction: t
});
await rose.update({
balance: rose.balance + 200
}, {
transaction: t
});
// Find Amy's account and update
let amy = await Account.findOne({
where: {
firstName: 'Amy',
lastName: 'Pond'
},
transaction: t
});
await amy.update({
balance: amy.balance - 200
}, {
transaction: t
});
});
// Return all accounts
let allAccounts = await Account.findAll({
order: [['firstName', 'ASC']]
});
res.json(allAccounts);
} catch (error) {
next(error);
}
});
[
{
"firstName": "Amy",
"lastName": "Pond",
"balance": 900
},
{
"firstName": "Donna",
"lastName": "Noble",
"balance": 200
},
{
"firstName": "Martha",
"lastName": "Jones",
"balance": 0
},
{
"firstName": "River",
"lastName": "Song",
"balance": 1200
},
{
"firstName": "Rose",
"lastName": "Tyler",
"balance": 1400
}
]
[
{
"firstName": "Amy",
"lastName": "Pond",
"balance": 100
},
{
"firstName": "Donna",
"lastName": "Noble",
"balance": 200
},
{
"firstName": "Martha",
"lastName": "Jones",
"balance": 800
},
{
"firstName": "River",
"lastName": "Song",
"balance": 1200
},
{
"firstName": "Rose",
"lastName": "Tyler",
"balance": 1400
}
]