@naturalcycles/db-lib 10.19.0 → 10.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commondao/common.dao.d.ts +38 -66
- package/dist/commondao/common.dao.js +138 -432
- package/dist/commondao/common.dao.model.d.ts +0 -13
- package/dist/commondao/commonDaoTransaction.d.ts +38 -0
- package/dist/commondao/commonDaoTransaction.js +78 -0
- package/dist/testing/commonDaoTest.js +0 -3
- package/package.json +1 -1
- package/src/commondao/common.dao.model.ts +4 -4
- package/src/commondao/common.dao.ts +190 -521
- package/src/commondao/commonDaoTransaction.ts +124 -0
- package/src/testing/commonDaoTest.ts +0 -3
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
2
|
+
import { _deepCopy } from '@naturalcycles/js-lib/object'
|
|
3
|
+
import type { BaseDBEntity, Unsaved } from '@naturalcycles/js-lib/types'
|
|
4
|
+
import type { DBTransaction } from '../db.model.js'
|
|
5
|
+
import type { AnyDao, CommonDao, InferID } from './common.dao.js'
|
|
6
|
+
import type {
|
|
7
|
+
CommonDaoOptions,
|
|
8
|
+
CommonDaoReadOptions,
|
|
9
|
+
CommonDaoSaveBatchOptions,
|
|
10
|
+
CommonDaoSaveOptions,
|
|
11
|
+
} from './common.dao.model.js'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Transaction context.
|
|
15
|
+
* Has similar API than CommonDao, but all operations are performed in the context of the transaction.
|
|
16
|
+
*/
|
|
17
|
+
export class CommonDaoTransaction {
|
|
18
|
+
constructor(
|
|
19
|
+
public tx: DBTransaction,
|
|
20
|
+
private logger: CommonLogger,
|
|
21
|
+
) {}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Commits the underlying DBTransaction.
|
|
25
|
+
* May throw.
|
|
26
|
+
*/
|
|
27
|
+
async commit(): Promise<void> {
|
|
28
|
+
await this.tx.commit()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Perform a graceful rollback without throwing/re-throwing any error.
|
|
33
|
+
* Never throws.
|
|
34
|
+
*/
|
|
35
|
+
async rollback(): Promise<void> {
|
|
36
|
+
try {
|
|
37
|
+
await this.tx.rollback()
|
|
38
|
+
} catch (err) {
|
|
39
|
+
// graceful rollback without re-throw
|
|
40
|
+
this.logger.error(err)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async getById<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID extends string = BM['id']>(
|
|
45
|
+
dao: CommonDao<BM, DBM, ID>,
|
|
46
|
+
id?: ID | null,
|
|
47
|
+
opt?: CommonDaoReadOptions,
|
|
48
|
+
): Promise<BM | null> {
|
|
49
|
+
return await dao.getById(id, { ...opt, tx: this.tx })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID extends string = BM['id']>(
|
|
53
|
+
dao: CommonDao<BM, DBM, ID>,
|
|
54
|
+
ids: ID[],
|
|
55
|
+
opt?: CommonDaoReadOptions,
|
|
56
|
+
): Promise<BM[]> {
|
|
57
|
+
return await dao.getByIds(ids, { ...opt, tx: this.tx })
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// todo: Queries inside Transaction are not supported yet
|
|
61
|
+
// async runQuery<BM extends PartialObjectWithId, DBM extends ObjectWithId>(
|
|
62
|
+
// dao: CommonDao<BM, DBM, any>,
|
|
63
|
+
// q: DBQuery<DBM>,
|
|
64
|
+
// opt?: CommonDaoOptions,
|
|
65
|
+
// ): Promise<BM[]> {
|
|
66
|
+
// try {
|
|
67
|
+
// return await dao.runQuery(q, { ...opt, tx: this.tx })
|
|
68
|
+
// } catch (err) {
|
|
69
|
+
// await this.rollback()
|
|
70
|
+
// throw err
|
|
71
|
+
// }
|
|
72
|
+
// }
|
|
73
|
+
|
|
74
|
+
async save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
|
|
75
|
+
dao: CommonDao<BM, DBM>,
|
|
76
|
+
bm: Unsaved<BM>,
|
|
77
|
+
opt?: CommonDaoSaveOptions<BM, DBM>,
|
|
78
|
+
): Promise<BM> {
|
|
79
|
+
return await dao.save(bm, { ...opt, tx: this.tx })
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
|
|
83
|
+
dao: CommonDao<BM, DBM>,
|
|
84
|
+
bms: Unsaved<BM>[],
|
|
85
|
+
opt?: CommonDaoSaveBatchOptions<DBM>,
|
|
86
|
+
): Promise<BM[]> {
|
|
87
|
+
return await dao.saveBatch(bms, { ...opt, tx: this.tx })
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* DaoTransaction.patch does not load from DB.
|
|
92
|
+
* It assumes the bm was previously loaded in the same Transaction, hence could not be
|
|
93
|
+
* concurrently modified. Hence it's safe to not sync with DB.
|
|
94
|
+
*
|
|
95
|
+
* So, this method is a rather simple convenience "Object.assign and then save".
|
|
96
|
+
*/
|
|
97
|
+
async patch<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID extends string = BM['id']>(
|
|
98
|
+
dao: CommonDao<BM, DBM, ID>,
|
|
99
|
+
bm: BM,
|
|
100
|
+
patch: Partial<BM>,
|
|
101
|
+
opt?: CommonDaoSaveOptions<BM, DBM>,
|
|
102
|
+
): Promise<BM> {
|
|
103
|
+
const skipIfEquals = _deepCopy(bm)
|
|
104
|
+
Object.assign(bm, patch)
|
|
105
|
+
return await dao.save(bm, { ...opt, skipIfEquals, tx: this.tx })
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async deleteById<DAO extends AnyDao>(
|
|
109
|
+
dao: DAO,
|
|
110
|
+
id?: InferID<DAO> | null,
|
|
111
|
+
opt?: CommonDaoOptions,
|
|
112
|
+
): Promise<number> {
|
|
113
|
+
if (!id) return 0
|
|
114
|
+
return await this.deleteByIds(dao, [id], opt)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async deleteByIds<DAO extends AnyDao>(
|
|
118
|
+
dao: DAO,
|
|
119
|
+
ids: InferID<DAO>[],
|
|
120
|
+
opt?: CommonDaoOptions,
|
|
121
|
+
): Promise<number> {
|
|
122
|
+
return await dao.deleteByIds(ids, { ...opt, tx: this.tx })
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -5,7 +5,6 @@ import { _deepCopy, _filterObject, _omit, _pick } from '@naturalcycles/js-lib/ob
|
|
|
5
5
|
import { getJoiValidationFunction } from '@naturalcycles/nodejs-lib/joi'
|
|
6
6
|
import { _pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
7
7
|
import { CommonDao } from '../commondao/common.dao.js'
|
|
8
|
-
import { CommonDaoLogLevel } from '../commondao/common.dao.model.js'
|
|
9
8
|
import type { CommonDB } from '../commondb/common.db.js'
|
|
10
9
|
import { DBQuery } from '../query/dbQuery.js'
|
|
11
10
|
import type { CommonDBImplementationQuirks } from './commonDBTest.js'
|
|
@@ -30,8 +29,6 @@ export async function runCommonDaoTest(
|
|
|
30
29
|
table: TEST_TABLE,
|
|
31
30
|
db,
|
|
32
31
|
validateBM: getJoiValidationFunction(testItemBMSchema),
|
|
33
|
-
logStarted: true,
|
|
34
|
-
logLevel: CommonDaoLogLevel.DATA_FULL,
|
|
35
32
|
})
|
|
36
33
|
|
|
37
34
|
const items = createTestItemsBM(3)
|