PeerTube/server/core/models/shared/update.ts

35 lines
749 B
TypeScript
Raw Normal View History

2023-01-10 11:09:30 +01:00
import { QueryTypes, Sequelize, Transaction } from 'sequelize'
const updating = new Set<string>()
// Sequelize always skip the update if we only update updatedAt field
async function setAsUpdated (options: {
2023-01-10 11:09:30 +01:00
sequelize: Sequelize
table: string
id: number
transaction?: Transaction
}) {
const { sequelize, table, id, transaction } = options
const key = table + '-' + id
if (updating.has(key)) return
updating.add(key)
2023-01-10 11:09:30 +01: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)
}
}
export {
setAsUpdated
}