-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
43 lines (33 loc) · 1.02 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
import { Connection, QueryRunner } from 'typeorm'
import { Uow, UowObject } from 'uow-template'
export class UowEntity implements UowObject<QueryRunner> {
public async createByTx (tx: QueryRunner) {
await tx.manager.insert(this.constructor, this)
}
public async updateByTx (tx: QueryRunner) {
const metadata = tx.connection.getMetadata(this.constructor)
await tx.manager.update(this.constructor, { ...metadata.getEntityIdMap(this) }, this)
}
public async deleteByTx (tx: QueryRunner) {
await tx.manager.remove(this)
}
}
export class UowRepository extends Uow<QueryRunner> {
public constructor (private connection: Connection) {
super()
}
protected async begin () {
const tx = this.connection.createQueryRunner()
await tx.startTransaction()
return tx
}
protected commit (tx: QueryRunner) {
return tx.commitTransaction()
}
protected rollback (tx: QueryRunner) {
return tx.rollbackTransaction()
}
protected release (tx: QueryRunner) {
return tx.release()
}
}