-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
52 lines (39 loc) · 1.37 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Sequelize from 'sequelize'
import { Uow, UowObject } from 'uow-template'
type SequelizeModel<T> =
Sequelize.Model<Sequelize.Instance<T>, T>
export type SequelizeUowInstance<T> =
Sequelize.Instance<T> & UowObject<Sequelize.Transaction>
export type SequelizeUowModel<T> =
Sequelize.Model<SequelizeUowInstance<T>, T>
export function toUowModel<T> (model: SequelizeModel<T>): SequelizeUowModel<T> {
Object.assign((model as any).prototype, UowModel.prototype)
return model as SequelizeUowModel<T>
}
export class UowModel implements UowObject<Sequelize.Transaction> {
public async createByTx (tx: Sequelize.Transaction) {
await (this as any as Sequelize.Instance<this>).save({ transaction: tx })
}
public async updateByTx (tx: Sequelize.Transaction) {
await (this as any as Sequelize.Instance<this>).save({ transaction: tx })
}
public async deleteByTx (tx: Sequelize.Transaction) {
await (this as any as Sequelize.Instance<this>).destroy({ transaction: tx })
}
}
export class UowRepository extends Uow<Sequelize.Transaction> {
public constructor (
private sequelize: Sequelize.Sequelize
) {
super()
}
protected async begin () {
return this.sequelize.transaction()
}
protected async commit (tx: Sequelize.Transaction) {
return tx.commit()
}
protected async rollback (tx: Sequelize.Transaction) {
return tx.rollback()
}
}