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.ts31
1 files changed, 24 insertions, 7 deletions
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts
index cbd7aa401..422774022 100644
--- a/server/helpers/database-utils.ts
+++ b/server/helpers/database-utils.ts
@@ -1,6 +1,6 @@
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 { BindOrReplacements, QueryTypes, Transaction } from 'sequelize'
4import { Model } from 'sequelize-typescript' 4import { Model } from 'sequelize-typescript'
5import { sequelizeTypescript } from '@server/initializers/database' 5import { sequelizeTypescript } from '@server/initializers/database'
6import { logger } from './logger' 6import { logger } from './logger'
@@ -84,13 +84,15 @@ function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
84 }) 84 })
85} 85}
86 86
87function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Pick<Model, 'destroy'>> ( 87function filterNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean }> (
88 fromDatabase: T[], 88 fromDatabase: T[],
89 newModels: T[], 89 newModels: T[]
90 t: Transaction
91) { 90) {
92 return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f))) 91 return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f)))
93 .map(f => f.destroy({ transaction: t })) 92}
93
94function deleteAllModels <T extends Pick<Model, 'destroy'>> (models: T[], transaction: Transaction) {
95 return Promise.all(models.map(f => f.destroy({ transaction })))
94} 96}
95 97
96// Sequelize always skip the update if we only update updatedAt field 98// Sequelize always skip the update if we only update updatedAt field
@@ -121,13 +123,28 @@ function afterCommitIfTransaction (t: Transaction, fn: Function) {
121 123
122// --------------------------------------------------------------------------- 124// ---------------------------------------------------------------------------
123 125
126function doesExist (query: string, bind?: BindOrReplacements) {
127 const options = {
128 type: QueryTypes.SELECT as QueryTypes.SELECT,
129 bind,
130 raw: true
131 }
132
133 return sequelizeTypescript.query(query, options)
134 .then(results => results.length === 1)
135}
136
137// ---------------------------------------------------------------------------
138
124export { 139export {
125 resetSequelizeInstance, 140 resetSequelizeInstance,
126 retryTransactionWrapper, 141 retryTransactionWrapper,
127 transactionRetryer, 142 transactionRetryer,
128 updateInstanceWithAnother, 143 updateInstanceWithAnother,
129 afterCommitIfTransaction, 144 afterCommitIfTransaction,
130 deleteNonExistingModels, 145 filterNonExistingModels,
146 deleteAllModels,
131 setAsUpdated, 147 setAsUpdated,
132 runInReadCommittedTransaction 148 runInReadCommittedTransaction,
149 doesExist
133} 150}