diff options
Diffstat (limited to 'server/helpers/database-utils.ts')
-rw-r--r-- | server/helpers/database-utils.ts | 31 |
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 @@ | |||
1 | import * as retry from 'async/retry' | 1 | import * as retry from 'async/retry' |
2 | import * as Bluebird from 'bluebird' | 2 | import * as Bluebird from 'bluebird' |
3 | import { QueryTypes, Transaction } from 'sequelize' | 3 | import { BindOrReplacements, QueryTypes, Transaction } from 'sequelize' |
4 | import { Model } from 'sequelize-typescript' | 4 | import { Model } from 'sequelize-typescript' |
5 | import { sequelizeTypescript } from '@server/initializers/database' | 5 | import { sequelizeTypescript } from '@server/initializers/database' |
6 | import { logger } from './logger' | 6 | import { logger } from './logger' |
@@ -84,13 +84,15 @@ function resetSequelizeInstance (instance: Model<any>, savedFields: object) { | |||
84 | }) | 84 | }) |
85 | } | 85 | } |
86 | 86 | ||
87 | function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Pick<Model, 'destroy'>> ( | 87 | function 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 | |||
94 | function 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 | ||
126 | function 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 | |||
124 | export { | 139 | export { |
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 | } |