+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()
+}
+
+function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Model<T>> (
+ fromDatabase: T[],
+ newModels: T[],
+ t: Transaction
+) {
+ return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f)))
+ .map(f => f.destroy({ transaction: t }))
+}
+
+// Sequelize always skip the update if we only update updatedAt field
+function setAsUpdated (table: string, id: number, transaction?: Transaction) {
+ return sequelizeTypescript.query(
+ `UPDATE "${table}" SET "updatedAt" = :updatedAt WHERE id = :id`,
+ {
+ replacements: { table, id, updatedAt: new Date() },
+ type: QueryTypes.UPDATE,
+ transaction
+ }
+ )
+}
+