2023-01-10 05:09:30 -05:00
|
|
|
import { QueryTypes, Sequelize, Transaction } from 'sequelize'
|
2021-07-28 04:32:40 -04:00
|
|
|
|
2023-04-21 08:55:10 -04:00
|
|
|
const updating = new Set<string>()
|
|
|
|
|
2021-07-28 04:32:40 -04:00
|
|
|
// Sequelize always skip the update if we only update updatedAt field
|
2023-04-21 08:55:10 -04:00
|
|
|
async function setAsUpdated (options: {
|
2023-01-10 05:09:30 -05:00
|
|
|
sequelize: Sequelize
|
|
|
|
table: string
|
|
|
|
id: number
|
|
|
|
transaction?: Transaction
|
|
|
|
}) {
|
|
|
|
const { sequelize, table, id, transaction } = options
|
2023-04-21 08:55:10 -04:00
|
|
|
const key = table + '-' + id
|
|
|
|
|
|
|
|
if (updating.has(key)) return
|
|
|
|
updating.add(key)
|
2023-01-10 05:09:30 -05:00
|
|
|
|
2023-04-21 08:55:10 -04:00
|
|
|
try {
|
|
|
|
await sequelize.query(
|
|
|
|
`UPDATE "${table}" SET "updatedAt" = :updatedAt WHERE id = :id`,
|
|
|
|
{
|
|
|
|
replacements: { table, id, updatedAt: new Date() },
|
|
|
|
type: QueryTypes.UPDATE,
|
|
|
|
transaction
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} finally {
|
|
|
|
updating.delete(key)
|
|
|
|
}
|
2021-07-28 04:32:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
setAsUpdated
|
|
|
|
}
|