@naturalcycles/db-lib 9.4.2 → 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 +10 -17
- package/dist/commondao/common.dao.js +0 -56
- package/dist/commondao/common.dao.model.d.ts +4 -6
- package/dist/db.model.d.ts +1 -2
- package/dist/db.model.js +0 -1
- package/dist/query/dbQuery.d.ts +4 -6
- package/dist/query/dbQuery.js +0 -6
- package/dist/testing/daoTest.js +0 -1
- package/package.json +1 -1
- package/src/commondao/common.dao.model.ts +3 -10
- package/src/commondao/common.dao.ts +10 -89
- package/src/db.model.ts +0 -1
- package/src/query/dbQuery.ts +1 -11
- package/src/testing/daoTest.ts +0 -2
|
@@ -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,9 +12,9 @@ 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
|
|
15
|
+
export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
|
|
16
|
+
cfg: CommonDaoCfg<BM, DBM>;
|
|
17
|
+
constructor(cfg: CommonDaoCfg<BM, DBM>);
|
|
18
18
|
create(part?: Partial<BM>, opt?: CommonDaoOptions): BM;
|
|
19
19
|
getById(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
|
|
20
20
|
getById(id?: string | null, opt?: CommonDaoOptions): Promise<BM | null>;
|
|
@@ -22,8 +22,6 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
22
22
|
getByIdAsDBMOrEmpty(id: string, part?: Partial<BM>, opt?: CommonDaoOptions): Promise<DBM>;
|
|
23
23
|
getByIdAsDBM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
|
|
24
24
|
getByIdAsDBM(id?: string | null, opt?: CommonDaoOptions): Promise<DBM | null>;
|
|
25
|
-
getByIdAsTM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
|
|
26
|
-
getByIdAsTM(id?: string | null, opt?: CommonDaoOptions): Promise<TM | null>;
|
|
27
25
|
getByIds(ids: string[], opt?: CommonDaoOptions): Promise<BM[]>;
|
|
28
26
|
getByIdsAsDBM(ids: string[], opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
29
27
|
requireById(id: string, opt?: CommonDaoOptions): Promise<BM>;
|
|
@@ -44,7 +42,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
44
42
|
/**
|
|
45
43
|
* Pass `table` to override table
|
|
46
44
|
*/
|
|
47
|
-
query(table?: string): RunnableDBQuery<BM, DBM
|
|
45
|
+
query(table?: string): RunnableDBQuery<BM, DBM>;
|
|
48
46
|
runQuery(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<BM[]>;
|
|
49
47
|
runQuerySingleColumn<T = any>(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<T[]>;
|
|
50
48
|
/**
|
|
@@ -56,8 +54,6 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
56
54
|
runQueryExtended(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<BM>>;
|
|
57
55
|
runQueryAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
58
56
|
runQueryExtendedAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<DBM>>;
|
|
59
|
-
runQueryAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<TM[]>;
|
|
60
|
-
runQueryExtendedAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
|
|
61
57
|
runQueryCount(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
62
58
|
streamQueryForEach(q: DBQuery<DBM>, mapper: AsyncMapper<BM, void>, opt?: CommonDaoStreamForEachOptions<BM>): Promise<void>;
|
|
63
59
|
streamQueryAsDBMForEach(q: DBQuery<DBM>, mapper: AsyncMapper<DBM, void>, opt?: CommonDaoStreamForEachOptions<DBM>): Promise<void>;
|
|
@@ -159,16 +155,13 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
159
155
|
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): undefined;
|
|
160
156
|
anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM;
|
|
161
157
|
anyToDBMs(entities: DBM[], opt?: CommonDaoOptions): DBM[];
|
|
162
|
-
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined;
|
|
163
|
-
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM;
|
|
164
|
-
bmsToTM(bms: BM[], opt?: CommonDaoOptions): TM[];
|
|
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()})`;
|
|
@@ -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).
|
|
@@ -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
|
*
|
|
@@ -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,19 +84,17 @@ 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
|
|
92
|
+
constructor(dao: CommonDao<BM, DBM>, table?: string);
|
|
93
93
|
runQuery(opt?: CommonDaoOptions): Promise<BM[]>;
|
|
94
94
|
runQuerySingleColumn<T = any>(opt?: CommonDaoOptions): Promise<T[]>;
|
|
95
95
|
runQueryAsDBM(opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
96
|
-
runQueryAsTM(opt?: CommonDaoOptions): Promise<TM[]>;
|
|
97
96
|
runQueryExtended(opt?: CommonDaoOptions): Promise<RunQueryResult<BM>>;
|
|
98
97
|
runQueryExtendedAsDBM(opt?: CommonDaoOptions): Promise<RunQueryResult<DBM>>;
|
|
99
|
-
runQueryExtendedAsTM(opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
|
|
100
98
|
runQueryCount(opt?: CommonDaoOptions): Promise<number>;
|
|
101
99
|
updateByQuery(patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
102
100
|
streamQueryForEach(mapper: AsyncMapper<BM, void>, opt?: CommonDaoStreamForEachOptions<BM>): 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
package/package.json
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {
|
|
2
|
-
AnyObject,
|
|
3
2
|
BaseDBEntity,
|
|
4
3
|
CommonLogger,
|
|
5
4
|
ErrorMode,
|
|
@@ -18,7 +17,7 @@ import {
|
|
|
18
17
|
import { CommonDB } from '../common.db'
|
|
19
18
|
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model'
|
|
20
19
|
|
|
21
|
-
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
20
|
+
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity> {
|
|
22
21
|
/**
|
|
23
22
|
* Allows to override the id generation function.
|
|
24
23
|
* By default it uses `stringId` from nodejs-lib
|
|
@@ -61,7 +60,6 @@ export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntit
|
|
|
61
60
|
|
|
62
61
|
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>
|
|
63
62
|
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>
|
|
64
|
-
beforeBMToTM: (bm: BM) => Partial<TM>
|
|
65
63
|
|
|
66
64
|
/**
|
|
67
65
|
* Allows to access the DBM just after it has been loaded from the DB.
|
|
@@ -131,11 +129,7 @@ export enum CommonDaoLogLevel {
|
|
|
131
129
|
DATA_FULL = 30,
|
|
132
130
|
}
|
|
133
131
|
|
|
134
|
-
export interface CommonDaoCfg<
|
|
135
|
-
BM extends BaseDBEntity,
|
|
136
|
-
DBM extends BaseDBEntity = BM,
|
|
137
|
-
TM extends AnyObject = BM,
|
|
138
|
-
> {
|
|
132
|
+
export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
|
|
139
133
|
db: CommonDB
|
|
140
134
|
table: string
|
|
141
135
|
|
|
@@ -144,7 +138,6 @@ export interface CommonDaoCfg<
|
|
|
144
138
|
*/
|
|
145
139
|
dbmSchema?: ObjectSchema<DBM> | AjvSchema<DBM> | ZodSchema<DBM>
|
|
146
140
|
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodSchema<BM>
|
|
147
|
-
tmSchema?: ObjectSchema<TM> | AjvSchema<TM> | ZodSchema<TM>
|
|
148
141
|
|
|
149
142
|
excludeFromIndexes?: (keyof DBM)[]
|
|
150
143
|
|
|
@@ -181,7 +174,7 @@ export interface CommonDaoCfg<
|
|
|
181
174
|
logStarted?: boolean
|
|
182
175
|
|
|
183
176
|
// Hooks are designed with inspiration from got/ky interface
|
|
184
|
-
hooks?: Partial<CommonDaoHooks<BM, DBM
|
|
177
|
+
hooks?: Partial<CommonDaoHooks<BM, DBM>>
|
|
185
178
|
|
|
186
179
|
/**
|
|
187
180
|
* Defaults to true.
|
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
_truncate,
|
|
12
12
|
_typeCast,
|
|
13
13
|
_uniqBy,
|
|
14
|
-
AnyObject,
|
|
15
14
|
AppError,
|
|
16
15
|
AsyncMapper,
|
|
17
16
|
BaseDBEntity,
|
|
@@ -76,12 +75,8 @@ const isCI = !!process.env['CI']
|
|
|
76
75
|
* BM = Backend model (optimized for API access)
|
|
77
76
|
* TM = Transport model (optimized to be sent over the wire)
|
|
78
77
|
*/
|
|
79
|
-
export class CommonDao<
|
|
80
|
-
BM
|
|
81
|
-
DBM extends BaseDBEntity = BM,
|
|
82
|
-
TM extends AnyObject = BM,
|
|
83
|
-
> {
|
|
84
|
-
constructor(public cfg: CommonDaoCfg<BM, DBM, TM>) {
|
|
78
|
+
export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
|
|
79
|
+
constructor(public cfg: CommonDaoCfg<BM, DBM>) {
|
|
85
80
|
this.cfg = {
|
|
86
81
|
// Default is to NOT log in AppEngine and in CI,
|
|
87
82
|
// otherwise to log Operations
|
|
@@ -99,11 +94,10 @@ export class CommonDao<
|
|
|
99
94
|
beforeDBMValidate: dbm => dbm,
|
|
100
95
|
beforeDBMToBM: dbm => dbm as any,
|
|
101
96
|
beforeBMToDBM: bm => bm as any,
|
|
102
|
-
beforeBMToTM: bm => bm as any,
|
|
103
97
|
anonymize: dbm => dbm,
|
|
104
98
|
onValidationError: err => err,
|
|
105
99
|
...cfg.hooks,
|
|
106
|
-
} satisfies Partial<CommonDaoHooks<BM, DBM
|
|
100
|
+
} satisfies Partial<CommonDaoHooks<BM, DBM>>,
|
|
107
101
|
}
|
|
108
102
|
|
|
109
103
|
if (this.cfg.generateId) {
|
|
@@ -178,28 +172,6 @@ export class CommonDao<
|
|
|
178
172
|
return dbm || null
|
|
179
173
|
}
|
|
180
174
|
|
|
181
|
-
async getByIdAsTM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>
|
|
182
|
-
async getByIdAsTM(id?: string | null, opt?: CommonDaoOptions): Promise<TM | null>
|
|
183
|
-
async getByIdAsTM(id?: string | null, opt: CommonDaoOptions = {}): Promise<TM | null> {
|
|
184
|
-
if (!id) return null
|
|
185
|
-
const op = `getByIdAsTM(${id})`
|
|
186
|
-
const table = opt.table || this.cfg.table
|
|
187
|
-
const started = this.logStarted(op, table)
|
|
188
|
-
let [dbm] = await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id])
|
|
189
|
-
if (dbm && !opt.raw && this.cfg.hooks!.afterLoad) {
|
|
190
|
-
dbm = (await this.cfg.hooks!.afterLoad(dbm)) || undefined
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (opt.raw) {
|
|
194
|
-
this.logResult(started, op, dbm, table)
|
|
195
|
-
return (dbm as any) || null
|
|
196
|
-
}
|
|
197
|
-
const bm = await this.dbmToBM(dbm, opt)
|
|
198
|
-
const tm = this.bmToTM(bm, opt)
|
|
199
|
-
this.logResult(started, op, tm, table)
|
|
200
|
-
return tm || null
|
|
201
|
-
}
|
|
202
|
-
|
|
203
175
|
async getByIds(ids: string[], opt: CommonDaoOptions = {}): Promise<BM[]> {
|
|
204
176
|
if (!ids.length) return []
|
|
205
177
|
const op = `getByIds ${ids.length} id(s) (${_truncate(ids.slice(0, 10).join(', '), 50)})`
|
|
@@ -307,8 +279,8 @@ export class CommonDao<
|
|
|
307
279
|
/**
|
|
308
280
|
* Pass `table` to override table
|
|
309
281
|
*/
|
|
310
|
-
query(table?: string): RunnableDBQuery<BM, DBM
|
|
311
|
-
return new RunnableDBQuery<BM, DBM
|
|
282
|
+
query(table?: string): RunnableDBQuery<BM, DBM> {
|
|
283
|
+
return new RunnableDBQuery<BM, DBM>(this, table)
|
|
312
284
|
}
|
|
313
285
|
|
|
314
286
|
async runQuery(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<BM[]> {
|
|
@@ -385,35 +357,6 @@ export class CommonDao<
|
|
|
385
357
|
return { rows: dbms, ...queryResult }
|
|
386
358
|
}
|
|
387
359
|
|
|
388
|
-
async runQueryAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<TM[]> {
|
|
389
|
-
const { rows } = await this.runQueryExtendedAsTM(q, opt)
|
|
390
|
-
return rows
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
async runQueryExtendedAsTM(
|
|
394
|
-
q: DBQuery<DBM>,
|
|
395
|
-
opt: CommonDaoOptions = {},
|
|
396
|
-
): Promise<RunQueryResult<TM>> {
|
|
397
|
-
q.table = opt.table || q.table
|
|
398
|
-
const op = `runQueryAsTM(${q.pretty()})`
|
|
399
|
-
const started = this.logStarted(op, q.table)
|
|
400
|
-
let { rows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
|
|
401
|
-
if (!opt.raw && this.cfg.hooks!.afterLoad && rows.length) {
|
|
402
|
-
rows = (await pMap(rows, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter(
|
|
403
|
-
_isTruthy,
|
|
404
|
-
)
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
const partialQuery = !!q._selectedFieldNames
|
|
408
|
-
const tms =
|
|
409
|
-
partialQuery || opt.raw ? (rows as any[]) : this.bmsToTM(await this.dbmsToBM(rows, opt), opt)
|
|
410
|
-
this.logResult(started, op, tms, q.table)
|
|
411
|
-
return {
|
|
412
|
-
rows: tms,
|
|
413
|
-
...queryResult,
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
360
|
async runQueryCount(q: DBQuery<DBM>, opt: CommonDaoOptions = {}): Promise<number> {
|
|
418
361
|
q.table = opt.table || q.table
|
|
419
362
|
const op = `runQueryCount(${q.pretty()})`
|
|
@@ -1239,28 +1182,6 @@ export class CommonDao<
|
|
|
1239
1182
|
return entities.map(entity => this.anyToDBM(entity, opt))
|
|
1240
1183
|
}
|
|
1241
1184
|
|
|
1242
|
-
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined
|
|
1243
|
-
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM
|
|
1244
|
-
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM | undefined {
|
|
1245
|
-
if (bm === undefined) return
|
|
1246
|
-
|
|
1247
|
-
// optimization: 1 validation is enough
|
|
1248
|
-
// Validate/convert BM
|
|
1249
|
-
// bm gets assigned to the new reference
|
|
1250
|
-
// bm = this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
1251
|
-
|
|
1252
|
-
// BM > TM
|
|
1253
|
-
const tm = this.cfg.hooks!.beforeBMToTM!(bm)
|
|
1254
|
-
|
|
1255
|
-
// Validate/convert DBM
|
|
1256
|
-
return this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt)
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
bmsToTM(bms: BM[], opt: CommonDaoOptions = {}): TM[] {
|
|
1260
|
-
// try/catch?
|
|
1261
|
-
return bms.map(bm => this.bmToTM(bm, opt))
|
|
1262
|
-
}
|
|
1263
|
-
|
|
1264
1185
|
/**
|
|
1265
1186
|
* Returns *converted value*.
|
|
1266
1187
|
* Validates (unless `skipValidation=true` passed).
|
|
@@ -1270,7 +1191,7 @@ export class CommonDao<
|
|
|
1270
1191
|
validateAndConvert<T>(
|
|
1271
1192
|
obj: Partial<T>,
|
|
1272
1193
|
schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined,
|
|
1273
|
-
modelType
|
|
1194
|
+
modelType?: DBModelType,
|
|
1274
1195
|
opt: CommonDaoOptions = {},
|
|
1275
1196
|
): any {
|
|
1276
1197
|
// `raw` option completely bypasses any processing
|
|
@@ -1460,7 +1381,7 @@ export class CommonDaoTransaction {
|
|
|
1460
1381
|
}
|
|
1461
1382
|
|
|
1462
1383
|
async getById<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
|
|
1463
|
-
dao: CommonDao<BM, DBM
|
|
1384
|
+
dao: CommonDao<BM, DBM>,
|
|
1464
1385
|
id?: string | null,
|
|
1465
1386
|
opt?: CommonDaoOptions,
|
|
1466
1387
|
): Promise<BM | null> {
|
|
@@ -1468,7 +1389,7 @@ export class CommonDaoTransaction {
|
|
|
1468
1389
|
}
|
|
1469
1390
|
|
|
1470
1391
|
async getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
|
|
1471
|
-
dao: CommonDao<BM, DBM
|
|
1392
|
+
dao: CommonDao<BM, DBM>,
|
|
1472
1393
|
ids: string[],
|
|
1473
1394
|
opt?: CommonDaoOptions,
|
|
1474
1395
|
): Promise<BM[]> {
|
|
@@ -1490,7 +1411,7 @@ export class CommonDaoTransaction {
|
|
|
1490
1411
|
// }
|
|
1491
1412
|
|
|
1492
1413
|
async save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
|
|
1493
|
-
dao: CommonDao<BM, DBM
|
|
1414
|
+
dao: CommonDao<BM, DBM>,
|
|
1494
1415
|
bm: Unsaved<BM>,
|
|
1495
1416
|
opt?: CommonDaoSaveBatchOptions<DBM>,
|
|
1496
1417
|
): Promise<BM> {
|
|
@@ -1498,7 +1419,7 @@ export class CommonDaoTransaction {
|
|
|
1498
1419
|
}
|
|
1499
1420
|
|
|
1500
1421
|
async saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
|
|
1501
|
-
dao: CommonDao<BM, DBM
|
|
1422
|
+
dao: CommonDao<BM, DBM>,
|
|
1502
1423
|
bms: Unsaved<BM>[],
|
|
1503
1424
|
opt?: CommonDaoSaveBatchOptions<DBM>,
|
|
1504
1425
|
): Promise<BM[]> {
|
package/src/db.model.ts
CHANGED
package/src/query/dbQuery.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AsyncMapper,
|
|
3
3
|
_truncate,
|
|
4
|
-
AnyObject,
|
|
5
4
|
_objectAssign,
|
|
6
5
|
BaseDBEntity,
|
|
7
6
|
ObjectWithId,
|
|
@@ -238,13 +237,12 @@ export class DBQuery<ROW extends ObjectWithId> {
|
|
|
238
237
|
export class RunnableDBQuery<
|
|
239
238
|
BM extends BaseDBEntity,
|
|
240
239
|
DBM extends BaseDBEntity = BM,
|
|
241
|
-
TM extends AnyObject = BM,
|
|
242
240
|
> extends DBQuery<DBM> {
|
|
243
241
|
/**
|
|
244
242
|
* Pass `table` to override table.
|
|
245
243
|
*/
|
|
246
244
|
constructor(
|
|
247
|
-
public dao: CommonDao<BM, DBM
|
|
245
|
+
public dao: CommonDao<BM, DBM>,
|
|
248
246
|
table?: string,
|
|
249
247
|
) {
|
|
250
248
|
super(table || dao.cfg.table)
|
|
@@ -262,10 +260,6 @@ export class RunnableDBQuery<
|
|
|
262
260
|
return await this.dao.runQueryAsDBM(this, opt)
|
|
263
261
|
}
|
|
264
262
|
|
|
265
|
-
async runQueryAsTM(opt?: CommonDaoOptions): Promise<TM[]> {
|
|
266
|
-
return await this.dao.runQueryAsTM(this, opt)
|
|
267
|
-
}
|
|
268
|
-
|
|
269
263
|
async runQueryExtended(opt?: CommonDaoOptions): Promise<RunQueryResult<BM>> {
|
|
270
264
|
return await this.dao.runQueryExtended(this, opt)
|
|
271
265
|
}
|
|
@@ -274,10 +268,6 @@ export class RunnableDBQuery<
|
|
|
274
268
|
return await this.dao.runQueryExtendedAsDBM(this, opt)
|
|
275
269
|
}
|
|
276
270
|
|
|
277
|
-
async runQueryExtendedAsTM(opt?: CommonDaoOptions): Promise<RunQueryResult<TM>> {
|
|
278
|
-
return await this.dao.runQueryExtendedAsTM(this, opt)
|
|
279
|
-
}
|
|
280
|
-
|
|
281
271
|
async runQueryCount(opt?: CommonDaoOptions): Promise<number> {
|
|
282
272
|
return await this.dao.runQueryCount(this, opt)
|
|
283
273
|
}
|
package/src/testing/daoTest.ts
CHANGED
|
@@ -9,7 +9,6 @@ import {
|
|
|
9
9
|
createTestItemsBM,
|
|
10
10
|
testItemBMSchema,
|
|
11
11
|
testItemDBMSchema,
|
|
12
|
-
testItemTMSchema,
|
|
13
12
|
TEST_TABLE,
|
|
14
13
|
createTestItemBM,
|
|
15
14
|
testItemDBMJsonSchema,
|
|
@@ -23,7 +22,6 @@ export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQui
|
|
|
23
22
|
db,
|
|
24
23
|
dbmSchema: testItemDBMSchema,
|
|
25
24
|
bmSchema: testItemBMSchema,
|
|
26
|
-
tmSchema: testItemTMSchema,
|
|
27
25
|
logStarted: true,
|
|
28
26
|
logLevel: CommonDaoLogLevel.DATA_FULL,
|
|
29
27
|
})
|