@naturalcycles/db-lib 9.4.1 → 9.5.0
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 +46 -53
- package/dist/commondao/common.dao.js +2 -58
- package/dist/commondao/common.dao.model.d.ts +6 -8
- package/dist/db.model.d.ts +1 -2
- package/dist/db.model.js +0 -1
- package/dist/query/dbQuery.d.ts +12 -14
- package/dist/query/dbQuery.js +0 -6
- package/dist/testing/daoTest.js +0 -1
- package/dist/testing/test.model.d.ts +5 -5
- package/dist/testing/test.model.js +3 -0
- package/package.json +1 -1
- package/src/commondao/common.dao.model.ts +5 -13
- package/src/commondao/common.dao.ts +86 -193
- package/src/db.model.ts +0 -1
- package/src/pipeline/dbPipelineRestore.ts +1 -2
- package/src/query/dbQuery.ts +10 -21
- package/src/testing/daoTest.ts +0 -2
- package/src/testing/test.model.ts +8 -5
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Transform } from 'node:stream';
|
|
3
|
-
import {
|
|
3
|
+
import { AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, UnixTimestampMillisNumber, Unsaved, ZodSchema } from '@naturalcycles/js-lib';
|
|
4
4
|
import { AjvSchema, ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
5
5
|
import { CommonDBTransactionOptions, DBModelType, DBPatch, DBTransaction, RunQueryResult } from '../db.model';
|
|
6
6
|
import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
|
|
@@ -12,22 +12,20 @@ import { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoSaveBa
|
|
|
12
12
|
* BM = Backend model (optimized for API access)
|
|
13
13
|
* TM = Transport model (optimized to be sent over the wire)
|
|
14
14
|
*/
|
|
15
|
-
export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM
|
|
16
|
-
cfg: CommonDaoCfg<BM, DBM
|
|
17
|
-
constructor(cfg: CommonDaoCfg<BM, DBM
|
|
18
|
-
create(part?: Partial<BM>, opt?: CommonDaoOptions):
|
|
15
|
+
export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
|
|
16
|
+
cfg: CommonDaoCfg<BM, DBM>;
|
|
17
|
+
constructor(cfg: CommonDaoCfg<BM, DBM>);
|
|
18
|
+
create(part?: Partial<BM>, opt?: CommonDaoOptions): BM;
|
|
19
19
|
getById(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
|
|
20
|
-
getById(id?: string | null, opt?: CommonDaoOptions): Promise<
|
|
21
|
-
getByIdOrEmpty(id: string, part?: Partial<BM>, opt?: CommonDaoOptions): Promise<
|
|
22
|
-
getByIdAsDBMOrEmpty(id: string, part?: Partial<BM>, opt?: CommonDaoOptions): Promise<
|
|
20
|
+
getById(id?: string | null, opt?: CommonDaoOptions): Promise<BM | null>;
|
|
21
|
+
getByIdOrEmpty(id: string, part?: Partial<BM>, opt?: CommonDaoOptions): Promise<BM>;
|
|
22
|
+
getByIdAsDBMOrEmpty(id: string, part?: Partial<BM>, opt?: CommonDaoOptions): Promise<DBM>;
|
|
23
23
|
getByIdAsDBM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
|
|
24
|
-
getByIdAsDBM(id?: string | null, opt?: CommonDaoOptions): Promise<
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
requireById(id: string, opt?: CommonDaoOptions): Promise<Saved<BM>>;
|
|
30
|
-
requireByIdAsDBM(id: string, opt?: CommonDaoOptions): Promise<Saved<DBM>>;
|
|
24
|
+
getByIdAsDBM(id?: string | null, opt?: CommonDaoOptions): Promise<DBM | null>;
|
|
25
|
+
getByIds(ids: string[], opt?: CommonDaoOptions): Promise<BM[]>;
|
|
26
|
+
getByIdsAsDBM(ids: string[], opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
27
|
+
requireById(id: string, opt?: CommonDaoOptions): Promise<BM>;
|
|
28
|
+
requireByIdAsDBM(id: string, opt?: CommonDaoOptions): Promise<DBM>;
|
|
31
29
|
private throwRequiredError;
|
|
32
30
|
/**
|
|
33
31
|
* Throws if readOnly is true
|
|
@@ -38,33 +36,31 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
38
36
|
*/
|
|
39
37
|
private requireObjectMutability;
|
|
40
38
|
private ensureUniqueId;
|
|
41
|
-
getBy(by: keyof DBM, value: any, limit?: number, opt?: CommonDaoOptions): Promise<
|
|
42
|
-
getOneBy(by: keyof DBM, value: any, opt?: CommonDaoOptions): Promise<
|
|
43
|
-
getAll(opt?: CommonDaoOptions): Promise<
|
|
39
|
+
getBy(by: keyof DBM, value: any, limit?: number, opt?: CommonDaoOptions): Promise<BM[]>;
|
|
40
|
+
getOneBy(by: keyof DBM, value: any, opt?: CommonDaoOptions): Promise<BM | null>;
|
|
41
|
+
getAll(opt?: CommonDaoOptions): Promise<BM[]>;
|
|
44
42
|
/**
|
|
45
43
|
* Pass `table` to override table
|
|
46
44
|
*/
|
|
47
|
-
query(table?: string): RunnableDBQuery<BM, DBM
|
|
48
|
-
runQuery(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<
|
|
45
|
+
query(table?: string): RunnableDBQuery<BM, DBM>;
|
|
46
|
+
runQuery(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<BM[]>;
|
|
49
47
|
runQuerySingleColumn<T = any>(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<T[]>;
|
|
50
48
|
/**
|
|
51
49
|
* Convenience method that runs multiple queries in parallel and then merges their results together.
|
|
52
50
|
* Does deduplication by id.
|
|
53
51
|
* Order is not guaranteed, as queries run in parallel.
|
|
54
52
|
*/
|
|
55
|
-
runUnionQueries(queries: DBQuery<DBM>[], opt?: CommonDaoOptions): Promise<
|
|
56
|
-
runQueryExtended(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<
|
|
57
|
-
runQueryAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<
|
|
58
|
-
runQueryExtendedAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<
|
|
59
|
-
runQueryAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<TM[]>;
|
|
60
|
-
runQueryExtendedAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
|
|
53
|
+
runUnionQueries(queries: DBQuery<DBM>[], opt?: CommonDaoOptions): Promise<BM[]>;
|
|
54
|
+
runQueryExtended(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<BM>>;
|
|
55
|
+
runQueryAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
56
|
+
runQueryExtendedAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<DBM>>;
|
|
61
57
|
runQueryCount(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
62
|
-
streamQueryForEach(q: DBQuery<DBM>, mapper: AsyncMapper<
|
|
63
|
-
streamQueryAsDBMForEach(q: DBQuery<DBM>, mapper: AsyncMapper<
|
|
58
|
+
streamQueryForEach(q: DBQuery<DBM>, mapper: AsyncMapper<BM, void>, opt?: CommonDaoStreamForEachOptions<BM>): Promise<void>;
|
|
59
|
+
streamQueryAsDBMForEach(q: DBQuery<DBM>, mapper: AsyncMapper<DBM, void>, opt?: CommonDaoStreamForEachOptions<DBM>): Promise<void>;
|
|
64
60
|
/**
|
|
65
61
|
* Stream as Readable, to be able to .pipe() it further with support of backpressure.
|
|
66
62
|
*/
|
|
67
|
-
streamQueryAsDBM(q: DBQuery<DBM>, opt?: CommonDaoStreamOptions<DBM>): ReadableTyped<
|
|
63
|
+
streamQueryAsDBM(q: DBQuery<DBM>, opt?: CommonDaoStreamOptions<DBM>): ReadableTyped<DBM>;
|
|
68
64
|
/**
|
|
69
65
|
* Stream as Readable, to be able to .pipe() it further with support of backpressure.
|
|
70
66
|
*
|
|
@@ -74,7 +70,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
74
70
|
*
|
|
75
71
|
* You can do `.pipe(transformNoOp)` to make it "valid again".
|
|
76
72
|
*/
|
|
77
|
-
streamQuery(q: DBQuery<DBM>, opt?: CommonDaoStreamOptions<
|
|
73
|
+
streamQuery(q: DBQuery<DBM>, opt?: CommonDaoStreamOptions<BM>): ReadableTyped<BM>;
|
|
78
74
|
queryIds(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<string[]>;
|
|
79
75
|
streamQueryIds(q: DBQuery<DBM>, opt?: CommonDaoStreamOptions<string>): ReadableTyped<string>;
|
|
80
76
|
streamQueryIdsForEach(q: DBQuery<DBM>, mapper: AsyncMapper<string, void>, opt?: CommonDaoStreamForEachOptions<string>): Promise<void>;
|
|
@@ -86,7 +82,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
86
82
|
/**
|
|
87
83
|
* Mutates with id, created, updated
|
|
88
84
|
*/
|
|
89
|
-
save(bm:
|
|
85
|
+
save(bm: Unsaved<BM>, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<BM>;
|
|
90
86
|
/**
|
|
91
87
|
* 1. Applies the patch
|
|
92
88
|
* 2. If object is the same after patching - skips saving it
|
|
@@ -95,7 +91,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
95
91
|
* Similar to `save` with skipIfEquals.
|
|
96
92
|
* Similar to `patch`, but doesn't load the object from the Database.
|
|
97
93
|
*/
|
|
98
|
-
savePatch(bm: BM, patch: Partial<BM>, opt: CommonDaoSaveBatchOptions<DBM>): Promise<
|
|
94
|
+
savePatch(bm: BM, patch: Partial<BM>, opt: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
|
|
99
95
|
/**
|
|
100
96
|
* Convenience method to replace 3 operations (loading+patching+saving) with one:
|
|
101
97
|
*
|
|
@@ -105,25 +101,25 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
105
101
|
* 2. Applies the patch on top of loaded data.
|
|
106
102
|
* 3. Saves (as fast as possible since the read) with the Patch applied, but only if the data has changed.
|
|
107
103
|
*/
|
|
108
|
-
patchById(id: string, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<
|
|
104
|
+
patchById(id: string, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
|
|
109
105
|
/**
|
|
110
106
|
* Like patchById, but runs all operations within a Transaction.
|
|
111
107
|
*/
|
|
112
|
-
patchByIdInTransaction(id: string, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<
|
|
108
|
+
patchByIdInTransaction(id: string, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
|
|
113
109
|
/**
|
|
114
110
|
* Same as patchById, but takes the whole object as input.
|
|
115
111
|
* This "whole object" is mutated with the patch and returned.
|
|
116
112
|
* Otherwise, similar behavior as patchById.
|
|
117
113
|
* It still loads the row from the DB.
|
|
118
114
|
*/
|
|
119
|
-
patch(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<
|
|
115
|
+
patch(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
|
|
120
116
|
/**
|
|
121
117
|
* Like patch, but runs all operations within a Transaction.
|
|
122
118
|
*/
|
|
123
|
-
patchInTransaction(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<
|
|
124
|
-
saveAsDBM(dbm:
|
|
125
|
-
saveBatch(bms:
|
|
126
|
-
saveBatchAsDBM(dbms:
|
|
119
|
+
patchInTransaction(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
|
|
120
|
+
saveAsDBM(dbm: Unsaved<DBM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<DBM>;
|
|
121
|
+
saveBatch(bms: Unsaved<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM[]>;
|
|
122
|
+
saveBatchAsDBM(dbms: Unsaved<DBM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<DBM[]>;
|
|
127
123
|
/**
|
|
128
124
|
* "Streaming" is implemented by buffering incoming rows into **batches**
|
|
129
125
|
* (of size opt.batchSize, which defaults to 500),
|
|
@@ -147,28 +143,25 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
147
143
|
updateByIds(ids: string[], patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
148
144
|
updateByQuery(q: DBQuery<DBM>, patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
149
145
|
dbmToBM(_dbm: undefined, opt?: CommonDaoOptions): Promise<undefined>;
|
|
150
|
-
dbmToBM(_dbm?: DBM, opt?: CommonDaoOptions): Promise<
|
|
151
|
-
dbmsToBM(dbms: DBM[], opt?: CommonDaoOptions): Promise<
|
|
146
|
+
dbmToBM(_dbm?: DBM, opt?: CommonDaoOptions): Promise<BM>;
|
|
147
|
+
dbmsToBM(dbms: DBM[], opt?: CommonDaoOptions): Promise<BM[]>;
|
|
152
148
|
/**
|
|
153
149
|
* Mutates object with properties: id, created, updated.
|
|
154
150
|
* Returns DBM (new reference).
|
|
155
151
|
*/
|
|
156
152
|
bmToDBM(bm: undefined, opt?: CommonDaoOptions): Promise<undefined>;
|
|
157
|
-
bmToDBM(bm?: BM, opt?: CommonDaoOptions): Promise<
|
|
158
|
-
bmsToDBM(bms: BM[], opt?: CommonDaoOptions): Promise<
|
|
153
|
+
bmToDBM(bm?: BM, opt?: CommonDaoOptions): Promise<DBM>;
|
|
154
|
+
bmsToDBM(bms: BM[], opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
159
155
|
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): undefined;
|
|
160
|
-
anyToDBM(dbm?: any, opt?: CommonDaoOptions):
|
|
161
|
-
anyToDBMs(entities: DBM[], opt?: CommonDaoOptions):
|
|
162
|
-
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined;
|
|
163
|
-
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM;
|
|
164
|
-
bmsToTM(bms: BM[], opt?: CommonDaoOptions): TM[];
|
|
156
|
+
anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM;
|
|
157
|
+
anyToDBMs(entities: DBM[], opt?: CommonDaoOptions): DBM[];
|
|
165
158
|
/**
|
|
166
159
|
* Returns *converted value*.
|
|
167
160
|
* Validates (unless `skipValidation=true` passed).
|
|
168
161
|
*
|
|
169
162
|
* Does NOT mutate the object.
|
|
170
163
|
*/
|
|
171
|
-
validateAndConvert<T>(obj: Partial<T>, schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined, modelType
|
|
164
|
+
validateAndConvert<T>(obj: Partial<T>, schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined, modelType?: DBModelType, opt?: CommonDaoOptions): any;
|
|
172
165
|
getTableSchema(): Promise<JsonSchemaRootObject<DBM>>;
|
|
173
166
|
createTable(schema: JsonSchemaObject<DBM>, opt?: CommonDaoCreateOptions): Promise<void>;
|
|
174
167
|
/**
|
|
@@ -199,10 +192,10 @@ export declare class CommonDaoTransaction {
|
|
|
199
192
|
* Perform a graceful rollback without throwing/re-throwing any error.
|
|
200
193
|
*/
|
|
201
194
|
rollback(): Promise<void>;
|
|
202
|
-
getById<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM
|
|
203
|
-
getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM
|
|
204
|
-
save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM
|
|
205
|
-
saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM
|
|
195
|
+
getById<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, id?: string | null, opt?: CommonDaoOptions): Promise<BM | null>;
|
|
196
|
+
getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, ids: string[], opt?: CommonDaoOptions): Promise<BM[]>;
|
|
197
|
+
save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, bm: Unsaved<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
|
|
198
|
+
saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, bms: Unsaved<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM[]>;
|
|
206
199
|
deleteById(dao: CommonDao<any>, id?: string | null, opt?: CommonDaoOptions): Promise<number>;
|
|
207
200
|
deleteByIds(dao: CommonDao<any>, ids: string[], opt?: CommonDaoOptions): Promise<number>;
|
|
208
201
|
}
|
|
@@ -36,7 +36,6 @@ class CommonDao {
|
|
|
36
36
|
beforeDBMValidate: dbm => dbm,
|
|
37
37
|
beforeDBMToBM: dbm => dbm,
|
|
38
38
|
beforeBMToDBM: bm => bm,
|
|
39
|
-
beforeBMToTM: bm => bm,
|
|
40
39
|
anonymize: dbm => dbm,
|
|
41
40
|
onValidationError: err => err,
|
|
42
41
|
...cfg.hooks,
|
|
@@ -99,25 +98,6 @@ class CommonDao {
|
|
|
99
98
|
this.logResult(started, op, dbm, table);
|
|
100
99
|
return dbm || null;
|
|
101
100
|
}
|
|
102
|
-
async getByIdAsTM(id, opt = {}) {
|
|
103
|
-
if (!id)
|
|
104
|
-
return null;
|
|
105
|
-
const op = `getByIdAsTM(${id})`;
|
|
106
|
-
const table = opt.table || this.cfg.table;
|
|
107
|
-
const started = this.logStarted(op, table);
|
|
108
|
-
let [dbm] = await (opt.tx || this.cfg.db).getByIds(table, [id]);
|
|
109
|
-
if (dbm && !opt.raw && this.cfg.hooks.afterLoad) {
|
|
110
|
-
dbm = (await this.cfg.hooks.afterLoad(dbm)) || undefined;
|
|
111
|
-
}
|
|
112
|
-
if (opt.raw) {
|
|
113
|
-
this.logResult(started, op, dbm, table);
|
|
114
|
-
return dbm || null;
|
|
115
|
-
}
|
|
116
|
-
const bm = await this.dbmToBM(dbm, opt);
|
|
117
|
-
const tm = this.bmToTM(bm, opt);
|
|
118
|
-
this.logResult(started, op, tm, table);
|
|
119
|
-
return tm || null;
|
|
120
|
-
}
|
|
121
101
|
async getByIds(ids, opt = {}) {
|
|
122
102
|
if (!ids.length)
|
|
123
103
|
return [];
|
|
@@ -265,26 +245,6 @@ class CommonDao {
|
|
|
265
245
|
this.logResult(started, op, dbms, q.table);
|
|
266
246
|
return { rows: dbms, ...queryResult };
|
|
267
247
|
}
|
|
268
|
-
async runQueryAsTM(q, opt) {
|
|
269
|
-
const { rows } = await this.runQueryExtendedAsTM(q, opt);
|
|
270
|
-
return rows;
|
|
271
|
-
}
|
|
272
|
-
async runQueryExtendedAsTM(q, opt = {}) {
|
|
273
|
-
q.table = opt.table || q.table;
|
|
274
|
-
const op = `runQueryAsTM(${q.pretty()})`;
|
|
275
|
-
const started = this.logStarted(op, q.table);
|
|
276
|
-
let { rows, ...queryResult } = await this.cfg.db.runQuery(q, opt);
|
|
277
|
-
if (!opt.raw && this.cfg.hooks.afterLoad && rows.length) {
|
|
278
|
-
rows = (await (0, js_lib_1.pMap)(rows, async (dbm) => await this.cfg.hooks.afterLoad(dbm))).filter(js_lib_1._isTruthy);
|
|
279
|
-
}
|
|
280
|
-
const partialQuery = !!q._selectedFieldNames;
|
|
281
|
-
const tms = partialQuery || opt.raw ? rows : this.bmsToTM(await this.dbmsToBM(rows, opt), opt);
|
|
282
|
-
this.logResult(started, op, tms, q.table);
|
|
283
|
-
return {
|
|
284
|
-
rows: tms,
|
|
285
|
-
...queryResult,
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
248
|
async runQueryCount(q, opt = {}) {
|
|
289
249
|
q.table = opt.table || q.table;
|
|
290
250
|
const op = `runQueryCount(${q.pretty()})`;
|
|
@@ -419,7 +379,7 @@ class CommonDao {
|
|
|
419
379
|
return (stream
|
|
420
380
|
// optimization: 1 validation is enough
|
|
421
381
|
// .pipe(transformMap<any, DBM>(dbm => this.anyToDBM(dbm, opt), safeOpt))
|
|
422
|
-
// .pipe(transformMap<DBM,
|
|
382
|
+
// .pipe(transformMap<DBM, BM>(dbm => this.dbmToBM(dbm, opt), safeOpt))
|
|
423
383
|
.on('error', err => stream.emit('error', err))
|
|
424
384
|
.pipe((0, nodejs_lib_1.transformMap)(async (dbm) => {
|
|
425
385
|
if (this.cfg.hooks.afterLoad) {
|
|
@@ -922,22 +882,6 @@ class CommonDao {
|
|
|
922
882
|
anyToDBMs(entities, opt = {}) {
|
|
923
883
|
return entities.map(entity => this.anyToDBM(entity, opt));
|
|
924
884
|
}
|
|
925
|
-
bmToTM(bm, opt) {
|
|
926
|
-
if (bm === undefined)
|
|
927
|
-
return;
|
|
928
|
-
// optimization: 1 validation is enough
|
|
929
|
-
// Validate/convert BM
|
|
930
|
-
// bm gets assigned to the new reference
|
|
931
|
-
// bm = this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
932
|
-
// BM > TM
|
|
933
|
-
const tm = this.cfg.hooks.beforeBMToTM(bm);
|
|
934
|
-
// Validate/convert DBM
|
|
935
|
-
return this.validateAndConvert(tm, this.cfg.tmSchema, db_model_1.DBModelType.TM, opt);
|
|
936
|
-
}
|
|
937
|
-
bmsToTM(bms, opt = {}) {
|
|
938
|
-
// try/catch?
|
|
939
|
-
return bms.map(bm => this.bmToTM(bm, opt));
|
|
940
|
-
}
|
|
941
885
|
/**
|
|
942
886
|
* Returns *converted value*.
|
|
943
887
|
* Validates (unless `skipValidation=true` passed).
|
|
@@ -1117,7 +1061,7 @@ class CommonDaoTransaction {
|
|
|
1117
1061
|
// dao: CommonDao<BM, DBM, any>,
|
|
1118
1062
|
// q: DBQuery<DBM>,
|
|
1119
1063
|
// opt?: CommonDaoOptions,
|
|
1120
|
-
// ): Promise<
|
|
1064
|
+
// ): Promise<BM[]> {
|
|
1121
1065
|
// try {
|
|
1122
1066
|
// return await dao.runQuery(q, { ...opt, tx: this.tx })
|
|
1123
1067
|
// } catch (err) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BaseDBEntity, CommonLogger, ErrorMode, Promisable, ZodError, ZodSchema } from '@naturalcycles/js-lib';
|
|
2
2
|
import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchema, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDB } from '../common.db';
|
|
4
4
|
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
|
|
5
|
-
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
5
|
+
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity> {
|
|
6
6
|
/**
|
|
7
7
|
* Allows to override the id generation function.
|
|
8
8
|
* By default it uses `stringId` from nodejs-lib
|
|
@@ -40,7 +40,6 @@ export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntit
|
|
|
40
40
|
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>;
|
|
41
41
|
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>;
|
|
42
42
|
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>;
|
|
43
|
-
beforeBMToTM: (bm: BM) => Partial<TM>;
|
|
44
43
|
/**
|
|
45
44
|
* Allows to access the DBM just after it has been loaded from the DB.
|
|
46
45
|
*
|
|
@@ -53,7 +52,7 @@ export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntit
|
|
|
53
52
|
*
|
|
54
53
|
* You can do validations as needed here and throw errors, they will be propagated.
|
|
55
54
|
*/
|
|
56
|
-
afterLoad?: (dbm:
|
|
55
|
+
afterLoad?: (dbm: DBM) => Promisable<DBM | null>;
|
|
57
56
|
/**
|
|
58
57
|
* Allows to access the DBM just before it's supposed to be saved to the DB.
|
|
59
58
|
*
|
|
@@ -67,7 +66,7 @@ export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntit
|
|
|
67
66
|
*
|
|
68
67
|
* You can do validations as needed here and throw errors, they will be propagated.
|
|
69
68
|
*/
|
|
70
|
-
beforeSave?: (dbm:
|
|
69
|
+
beforeSave?: (dbm: DBM) => Promisable<DBM | null>;
|
|
71
70
|
/**
|
|
72
71
|
* Called in:
|
|
73
72
|
* - dbmToBM (applied before DBM becomes BM)
|
|
@@ -104,7 +103,7 @@ export declare enum CommonDaoLogLevel {
|
|
|
104
103
|
*/
|
|
105
104
|
DATA_FULL = 30
|
|
106
105
|
}
|
|
107
|
-
export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM
|
|
106
|
+
export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
|
|
108
107
|
db: CommonDB;
|
|
109
108
|
table: string;
|
|
110
109
|
/**
|
|
@@ -112,7 +111,6 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
112
111
|
*/
|
|
113
112
|
dbmSchema?: ObjectSchema<DBM> | AjvSchema<DBM> | ZodSchema<DBM>;
|
|
114
113
|
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodSchema<BM>;
|
|
115
|
-
tmSchema?: ObjectSchema<TM> | AjvSchema<TM> | ZodSchema<TM>;
|
|
116
114
|
excludeFromIndexes?: (keyof DBM)[];
|
|
117
115
|
/**
|
|
118
116
|
* Defaults to false.
|
|
@@ -141,7 +139,7 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
141
139
|
* @default false
|
|
142
140
|
*/
|
|
143
141
|
logStarted?: boolean;
|
|
144
|
-
hooks?: Partial<CommonDaoHooks<BM, DBM
|
|
142
|
+
hooks?: Partial<CommonDaoHooks<BM, DBM>>;
|
|
145
143
|
/**
|
|
146
144
|
* Defaults to true.
|
|
147
145
|
* Set to false to disable auto-generation of `id`.
|
package/dist/db.model.d.ts
CHANGED
package/dist/db.model.js
CHANGED
package/dist/query/dbQuery.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AsyncMapper,
|
|
1
|
+
import { AsyncMapper, BaseDBEntity, ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDaoOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, DBPatch } from '..';
|
|
4
4
|
import { CommonDao } from '../commondao/common.dao';
|
|
@@ -84,25 +84,23 @@ export declare class DBQuery<ROW extends ObjectWithId> {
|
|
|
84
84
|
/**
|
|
85
85
|
* DBQuery that has additional method to support Fluent API style.
|
|
86
86
|
*/
|
|
87
|
-
export declare class RunnableDBQuery<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM
|
|
88
|
-
dao: CommonDao<BM, DBM
|
|
87
|
+
export declare class RunnableDBQuery<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> extends DBQuery<DBM> {
|
|
88
|
+
dao: CommonDao<BM, DBM>;
|
|
89
89
|
/**
|
|
90
90
|
* Pass `table` to override table.
|
|
91
91
|
*/
|
|
92
|
-
constructor(dao: CommonDao<BM, DBM
|
|
93
|
-
runQuery(opt?: CommonDaoOptions): Promise<
|
|
92
|
+
constructor(dao: CommonDao<BM, DBM>, table?: string);
|
|
93
|
+
runQuery(opt?: CommonDaoOptions): Promise<BM[]>;
|
|
94
94
|
runQuerySingleColumn<T = any>(opt?: CommonDaoOptions): Promise<T[]>;
|
|
95
|
-
runQueryAsDBM(opt?: CommonDaoOptions): Promise<
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
runQueryExtendedAsDBM(opt?: CommonDaoOptions): Promise<RunQueryResult<Saved<DBM>>>;
|
|
99
|
-
runQueryExtendedAsTM(opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
|
|
95
|
+
runQueryAsDBM(opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
96
|
+
runQueryExtended(opt?: CommonDaoOptions): Promise<RunQueryResult<BM>>;
|
|
97
|
+
runQueryExtendedAsDBM(opt?: CommonDaoOptions): Promise<RunQueryResult<DBM>>;
|
|
100
98
|
runQueryCount(opt?: CommonDaoOptions): Promise<number>;
|
|
101
99
|
updateByQuery(patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
102
|
-
streamQueryForEach(mapper: AsyncMapper<
|
|
103
|
-
streamQueryAsDBMForEach(mapper: AsyncMapper<
|
|
104
|
-
streamQuery(opt?: CommonDaoStreamOptions<
|
|
105
|
-
streamQueryAsDBM(opt?: CommonDaoStreamOptions<DBM>): ReadableTyped<
|
|
100
|
+
streamQueryForEach(mapper: AsyncMapper<BM, void>, opt?: CommonDaoStreamForEachOptions<BM>): Promise<void>;
|
|
101
|
+
streamQueryAsDBMForEach(mapper: AsyncMapper<DBM, void>, opt?: CommonDaoStreamForEachOptions<DBM>): Promise<void>;
|
|
102
|
+
streamQuery(opt?: CommonDaoStreamOptions<BM>): ReadableTyped<BM>;
|
|
103
|
+
streamQueryAsDBM(opt?: CommonDaoStreamOptions<DBM>): ReadableTyped<DBM>;
|
|
106
104
|
queryIds(opt?: CommonDaoOptions): Promise<string[]>;
|
|
107
105
|
streamQueryIds(opt?: CommonDaoStreamOptions<string>): ReadableTyped<string>;
|
|
108
106
|
streamQueryIdsForEach(mapper: AsyncMapper<string, void>, opt?: CommonDaoStreamForEachOptions<string>): Promise<void>;
|
package/dist/query/dbQuery.js
CHANGED
|
@@ -153,18 +153,12 @@ class RunnableDBQuery extends DBQuery {
|
|
|
153
153
|
async runQueryAsDBM(opt) {
|
|
154
154
|
return await this.dao.runQueryAsDBM(this, opt);
|
|
155
155
|
}
|
|
156
|
-
async runQueryAsTM(opt) {
|
|
157
|
-
return await this.dao.runQueryAsTM(this, opt);
|
|
158
|
-
}
|
|
159
156
|
async runQueryExtended(opt) {
|
|
160
157
|
return await this.dao.runQueryExtended(this, opt);
|
|
161
158
|
}
|
|
162
159
|
async runQueryExtendedAsDBM(opt) {
|
|
163
160
|
return await this.dao.runQueryExtendedAsDBM(this, opt);
|
|
164
161
|
}
|
|
165
|
-
async runQueryExtendedAsTM(opt) {
|
|
166
|
-
return await this.dao.runQueryExtendedAsTM(this, opt);
|
|
167
|
-
}
|
|
168
162
|
async runQueryCount(opt) {
|
|
169
163
|
return await this.dao.runQueryCount(this, opt);
|
|
170
164
|
}
|
package/dist/testing/daoTest.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { BaseDBEntity,
|
|
2
|
+
import { BaseDBEntity, JsonSchemaObject } from '@naturalcycles/js-lib';
|
|
3
3
|
export declare const TEST_TABLE = "TEST_TABLE";
|
|
4
4
|
export interface TestItemBM extends BaseDBEntity {
|
|
5
5
|
k1: string;
|
|
@@ -19,7 +19,7 @@ export declare const testItemDBMSchema: import("joi").ObjectSchema<TestItemDBM>;
|
|
|
19
19
|
export declare const testItemTMSchema: import("joi").ObjectSchema<TestItemTM>;
|
|
20
20
|
export declare const testItemBMJsonSchema: JsonSchemaObject<TestItemBM>;
|
|
21
21
|
export declare const testItemDBMJsonSchema: JsonSchemaObject<TestItemDBM>;
|
|
22
|
-
export declare function createTestItemDBM(num?: number):
|
|
23
|
-
export declare function createTestItemBM(num?: number):
|
|
24
|
-
export declare function createTestItemsDBM(count?: number):
|
|
25
|
-
export declare function createTestItemsBM(count?: number):
|
|
22
|
+
export declare function createTestItemDBM(num?: number): TestItemDBM;
|
|
23
|
+
export declare function createTestItemBM(num?: number): TestItemBM;
|
|
24
|
+
export declare function createTestItemsDBM(count?: number): TestItemDBM[];
|
|
25
|
+
export declare function createTestItemsBM(count?: number): TestItemBM[];
|
|
@@ -25,7 +25,10 @@ exports.testItemTMSchema = (0, nodejs_lib_1.objectSchema)({
|
|
|
25
25
|
});
|
|
26
26
|
exports.testItemBMJsonSchema = js_lib_1.jsonSchema
|
|
27
27
|
.rootObject({
|
|
28
|
+
// todo: figure out how to not copy-paste these 3 fields
|
|
28
29
|
id: js_lib_1.jsonSchema.string(), // todo: not strictly needed here
|
|
30
|
+
created: js_lib_1.jsonSchema.unixTimestamp(),
|
|
31
|
+
updated: js_lib_1.jsonSchema.unixTimestamp(),
|
|
29
32
|
k1: js_lib_1.jsonSchema.string(),
|
|
30
33
|
k2: js_lib_1.jsonSchema.oneOf([js_lib_1.jsonSchema.string(), js_lib_1.jsonSchema.null()]).optional(),
|
|
31
34
|
k3: js_lib_1.jsonSchema.number().optional(),
|
package/package.json
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
|
-
AnyObject,
|
|
3
2
|
BaseDBEntity,
|
|
4
3
|
CommonLogger,
|
|
5
4
|
ErrorMode,
|
|
6
5
|
Promisable,
|
|
7
|
-
Saved,
|
|
8
6
|
ZodError,
|
|
9
7
|
ZodSchema,
|
|
10
8
|
} from '@naturalcycles/js-lib'
|
|
@@ -19,7 +17,7 @@ import {
|
|
|
19
17
|
import { CommonDB } from '../common.db'
|
|
20
18
|
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model'
|
|
21
19
|
|
|
22
|
-
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
20
|
+
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity> {
|
|
23
21
|
/**
|
|
24
22
|
* Allows to override the id generation function.
|
|
25
23
|
* By default it uses `stringId` from nodejs-lib
|
|
@@ -62,7 +60,6 @@ export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntit
|
|
|
62
60
|
|
|
63
61
|
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>
|
|
64
62
|
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>
|
|
65
|
-
beforeBMToTM: (bm: BM) => Partial<TM>
|
|
66
63
|
|
|
67
64
|
/**
|
|
68
65
|
* Allows to access the DBM just after it has been loaded from the DB.
|
|
@@ -76,7 +73,7 @@ export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntit
|
|
|
76
73
|
*
|
|
77
74
|
* You can do validations as needed here and throw errors, they will be propagated.
|
|
78
75
|
*/
|
|
79
|
-
afterLoad?: (dbm:
|
|
76
|
+
afterLoad?: (dbm: DBM) => Promisable<DBM | null>
|
|
80
77
|
|
|
81
78
|
/**
|
|
82
79
|
* Allows to access the DBM just before it's supposed to be saved to the DB.
|
|
@@ -91,7 +88,7 @@ export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntit
|
|
|
91
88
|
*
|
|
92
89
|
* You can do validations as needed here and throw errors, they will be propagated.
|
|
93
90
|
*/
|
|
94
|
-
beforeSave?: (dbm:
|
|
91
|
+
beforeSave?: (dbm: DBM) => Promisable<DBM | null>
|
|
95
92
|
|
|
96
93
|
/**
|
|
97
94
|
* Called in:
|
|
@@ -132,11 +129,7 @@ export enum CommonDaoLogLevel {
|
|
|
132
129
|
DATA_FULL = 30,
|
|
133
130
|
}
|
|
134
131
|
|
|
135
|
-
export interface CommonDaoCfg<
|
|
136
|
-
BM extends BaseDBEntity,
|
|
137
|
-
DBM extends BaseDBEntity = BM,
|
|
138
|
-
TM extends AnyObject = BM,
|
|
139
|
-
> {
|
|
132
|
+
export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
|
|
140
133
|
db: CommonDB
|
|
141
134
|
table: string
|
|
142
135
|
|
|
@@ -145,7 +138,6 @@ export interface CommonDaoCfg<
|
|
|
145
138
|
*/
|
|
146
139
|
dbmSchema?: ObjectSchema<DBM> | AjvSchema<DBM> | ZodSchema<DBM>
|
|
147
140
|
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodSchema<BM>
|
|
148
|
-
tmSchema?: ObjectSchema<TM> | AjvSchema<TM> | ZodSchema<TM>
|
|
149
141
|
|
|
150
142
|
excludeFromIndexes?: (keyof DBM)[]
|
|
151
143
|
|
|
@@ -182,7 +174,7 @@ export interface CommonDaoCfg<
|
|
|
182
174
|
logStarted?: boolean
|
|
183
175
|
|
|
184
176
|
// Hooks are designed with inspiration from got/ky interface
|
|
185
|
-
hooks?: Partial<CommonDaoHooks<BM, DBM
|
|
177
|
+
hooks?: Partial<CommonDaoHooks<BM, DBM>>
|
|
186
178
|
|
|
187
179
|
/**
|
|
188
180
|
* Defaults to true.
|