sequelize#Transaction TypeScript Examples
The following examples show how to use
sequelize#Transaction.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: withState.ts From expresso with MIT License | 6 votes |
async getTransaction(id?: any): Promise<Transaction> {
id = id === undefined ? 1 : id
const txn = this.req._transaction[id]
if (txn) {
return txn
}
const newTxn = await db.sequelize.transaction()
this.req._transaction[id] = newTxn
return newTxn
}
Example #2
Source File: withState.ts From expresso with MIT License | 6 votes |
async rollbackTransactions(): Promise<void> {
const { _transaction } = this.req
const entryTransactions = Object.entries(_transaction)
for (let i = 0; i < entryTransactions.length; i += 1) {
const [id, value] = entryTransactions[i] as [any, Transaction]
await value.rollback()
delete _transaction[id]
}
}
Example #3
Source File: ExpressAutoHandleTransaction.ts From expresso with MIT License | 6 votes |
async function ExpressAutoHandleTransaction(
req: Request,
res: Response,
next: NextFunction
): Promise<void> {
res.once('finish', () => {
const { _transaction } = req
for (let i = 1; i <= Object.keys(_transaction).length; i += 1) {
const txn = _transaction[i] as Transaction & {
finished?: string
}
if (!txn?.finished) {
const endpoint = req.originalUrl
const errType = 'Sequelize Error:'
const message = `endpoint ${endpoint} potentianlly can lead to bug`
console.log(logErrServer(errType, message))
console.log(`${LOG_SERVER} AUTO COMMIT!!!`)
txn.commit()
}
}
})
next()
}
Example #4
Source File: name.service.ts From wise-old-man with MIT License | 6 votes |
async function transferSnapshots(filter: WhereOptions, targetId: number, transaction: Transaction) {
// Fetch all of new player's snapshots (post transition date)
const newSnapshots = await Snapshot.findAll({ where: filter });
// Transfer all snapshots to the old player id
const movedSnapshots = newSnapshots.map(s => {
return omit({ ...s.toJSON(), playerId: targetId }, 'id');
});
// Add all these snapshots, ignoring duplicates
await Snapshot.bulkCreate(movedSnapshots, { ignoreDuplicates: true, transaction });
}
Example #5
Source File: name.service.ts From wise-old-man with MIT License | 6 votes |
async function transferParticipations(filter: WhereOptions, targetId: number, transaction: Transaction) {
// Fetch all of new player's participations (post transition date)
const newParticipations = await Participation.findAll({ where: filter });
// Transfer all participations to the old player id
const movedParticipations = newParticipations.map(ns => ({
...ns.toJSON(),
playerId: targetId,
startSnapshotId: null,
endSnapshotId: null
}));
// Add all these participations, ignoring duplicates
await Participation.bulkCreate(movedParticipations, { ignoreDuplicates: true, transaction });
}
Example #6
Source File: name.service.ts From wise-old-man with MIT License | 6 votes |
async function transferMemberships(filter: WhereOptions, targetId: number, transaction: Transaction) {
// Fetch all of new player's memberships (post transition date)
const newMemberships = await Membership.findAll({ where: filter });
// Transfer all memberships to the old player id
const movedMemberships = newMemberships.map(ns => ({ ...ns.toJSON(), playerId: targetId }));
// Add all these memberships, ignoring duplicates
await Membership.bulkCreate(movedMemberships, { ignoreDuplicates: true, hooks: false, transaction });
}
Example #7
Source File: name.service.ts From wise-old-man with MIT License | 6 votes |
async function transferRecords(filter: WhereOptions, targetId: number, transaction: Transaction) {
// Fetch all of the old records, and the recent new records
const oldRecords = await Record.findAll({ where: { playerId: targetId } });
const newRecords = await Record.findAll({ where: filter });
const outdated: { record: Record; newValue: number }[] = [];
newRecords.forEach(n => {
const oldEquivalent = oldRecords.find(r => r.metric === n.metric && r.period === n.period);
// If the new player's record is higher than the old player's,
// add the old one to the outdated list
if (oldEquivalent && oldEquivalent.value < n.value) {
outdated.push({ record: oldEquivalent, newValue: n.value });
}
});
// Update all "outdated records"
await Promise.all(
outdated.map(async ({ record, newValue }) => {
await record.update({ value: newValue }, { transaction });
})
);
}