aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/shared/update.ts
blob: 96db437309baf4db289305430c8b4a661b4482ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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: {
  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)

  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
}