aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/database-utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/database-utils.ts')
-rw-r--r--server/helpers/database-utils.ts22
1 files changed, 18 insertions, 4 deletions
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts
index 2b916efc2..7befa2c49 100644
--- a/server/helpers/database-utils.ts
+++ b/server/helpers/database-utils.ts
@@ -1,8 +1,9 @@
1import * as retry from 'async/retry' 1import * as retry from 'async/retry'
2import * as Bluebird from 'bluebird' 2import * as Bluebird from 'bluebird'
3import { QueryTypes, Transaction } from 'sequelize'
3import { Model } from 'sequelize-typescript' 4import { Model } from 'sequelize-typescript'
5import { sequelizeTypescript } from '@server/initializers/database'
4import { logger } from './logger' 6import { logger } from './logger'
5import { Transaction } from 'sequelize'
6 7
7function retryTransactionWrapper <T, A, B, C, D> ( 8function retryTransactionWrapper <T, A, B, C, D> (
8 functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise<T> | Bluebird<T>, 9 functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise<T> | Bluebird<T>,
@@ -67,7 +68,7 @@ function transactionRetryer <T> (func: (err: any, data: T) => any) {
67 }) 68 })
68} 69}
69 70
70function updateInstanceWithAnother <T extends Model<T>> (instanceToUpdate: Model<T>, baseInstance: Model<T>) { 71function updateInstanceWithAnother <M, T extends U, U extends Model<M>> (instanceToUpdate: T, baseInstance: U) {
71 const obj = baseInstance.toJSON() 72 const obj = baseInstance.toJSON()
72 73
73 for (const key of Object.keys(obj)) { 74 for (const key of Object.keys(obj)) {
@@ -87,7 +88,7 @@ function afterCommitIfTransaction (t: Transaction, fn: Function) {
87 return fn() 88 return fn()
88} 89}
89 90
90function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Model<T>> ( 91function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Pick<Model, 'destroy'>> (
91 fromDatabase: T[], 92 fromDatabase: T[],
92 newModels: T[], 93 newModels: T[],
93 t: Transaction 94 t: Transaction
@@ -96,6 +97,18 @@ function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T):
96 .map(f => f.destroy({ transaction: t })) 97 .map(f => f.destroy({ transaction: t }))
97} 98}
98 99
100// Sequelize always skip the update if we only update updatedAt field
101function setAsUpdated (table: string, id: number, transaction?: Transaction) {
102 return sequelizeTypescript.query(
103 `UPDATE "${table}" SET "updatedAt" = :updatedAt WHERE id = :id`,
104 {
105 replacements: { table, id, updatedAt: new Date() },
106 type: QueryTypes.UPDATE,
107 transaction
108 }
109 )
110}
111
99// --------------------------------------------------------------------------- 112// ---------------------------------------------------------------------------
100 113
101export { 114export {
@@ -104,5 +117,6 @@ export {
104 transactionRetryer, 117 transactionRetryer,
105 updateInstanceWithAnother, 118 updateInstanceWithAnother,
106 afterCommitIfTransaction, 119 afterCommitIfTransaction,
107 deleteNonExistingModels 120 deleteNonExistingModels,
121 setAsUpdated
108} 122}