import * as Bluebird from 'bluebird'
import { Model } from 'sequelize-typescript'
import { logger } from './logger'
+import { Transaction } from 'sequelize'
function retryTransactionWrapper <T, A, B, C> (
functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T> | Bluebird<T>,
arg1: A
): Promise<T>
+function retryTransactionWrapper <T> (
+ functionToRetry: () => Promise<T> | Bluebird<T>
+): Promise<T>
+
function retryTransactionWrapper <T> (
functionToRetry: (...args: any[]) => Promise<T> | Bluebird<T>,
...args: any[]
): Promise<T> {
return transactionRetryer<T>(callback => {
- functionToRetry.apply(this, args)
+ functionToRetry.apply(null, args)
.then((result: T) => callback(null, result))
.catch(err => callback(err))
})
.catch(err => {
- logger.error('Cannot execute %s with many retries.', functionToRetry.toString(), { err })
+ logger.error(`Cannot execute ${functionToRetry.name} with many retries.`, { err })
throw err
})
}
const obj = baseInstance.toJSON()
for (const key of Object.keys(obj)) {
- instanceToUpdate.set(key, obj[key])
+ instanceToUpdate[key] = obj[key]
}
}
+function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
+ Object.keys(savedFields).forEach(key => {
+ instance[key] = savedFields[key]
+ })
+}
+
+function afterCommitIfTransaction (t: Transaction, fn: Function) {
+ if (t) return t.afterCommit(() => fn())
+
+ return fn()
+}
+
// ---------------------------------------------------------------------------
export {
+ resetSequelizeInstance,
retryTransactionWrapper,
transactionRetryer,
- updateInstanceWithAnother
+ updateInstanceWithAnother,
+ afterCommitIfTransaction
}