@naturalcycles/db-lib 8.49.0 → 8.50.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/adapter/cachedb/cache.db.d.ts +8 -8
- package/dist/adapter/cachedb/cache.db.js +2 -1
- package/dist/adapter/file/file.db.d.ts +8 -8
- package/dist/adapter/file/file.db.model.d.ts +1 -1
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +1 -1
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +1 -1
- package/dist/adapter/file/noop.persistence.plugin.d.ts +1 -1
- package/dist/adapter/inmemory/inMemory.db.d.ts +8 -8
- package/dist/adapter/inmemory/queryInMemory.d.ts +1 -1
- package/dist/adapter/inmemory/queryInMemory.js +1 -3
- package/dist/base.common.db.d.ts +8 -8
- package/dist/common.db.d.ts +8 -8
- package/dist/commondao/common.dao.d.ts +5 -16
- package/dist/commondao/common.dao.js +1 -81
- package/dist/commondao/common.dao.model.d.ts +20 -24
- package/dist/db.model.d.ts +1 -2
- package/dist/db.model.js +0 -1
- package/dist/kv/commonKeyValueDao.d.ts +3 -3
- package/dist/pipeline/dbPipelineBackup.js +1 -3
- package/dist/pipeline/dbPipelineCopy.js +1 -3
- package/dist/query/dbQuery.d.ts +8 -10
- package/dist/query/dbQuery.js +0 -6
- package/dist/testing/daoTest.js +0 -1
- package/package.json +1 -1
- package/src/adapter/cachedb/cache.db.ts +13 -12
- package/src/adapter/file/file.db.model.ts +1 -1
- package/src/adapter/file/file.db.ts +12 -12
- package/src/adapter/file/inMemory.persistence.plugin.ts +1 -1
- package/src/adapter/file/localFile.persistence.plugin.ts +1 -1
- package/src/adapter/file/noop.persistence.plugin.ts +1 -1
- package/src/adapter/inmemory/inMemory.db.ts +11 -11
- package/src/adapter/inmemory/queryInMemory.ts +5 -3
- package/src/base.common.db.ts +11 -8
- package/src/common.db.ts +16 -8
- package/src/commondao/common.dao.model.ts +23 -29
- package/src/commondao/common.dao.ts +11 -117
- package/src/db.model.ts +0 -1
- package/src/kv/commonKeyValueDao.ts +3 -3
- package/src/pipeline/dbPipelineBackup.ts +1 -3
- package/src/pipeline/dbPipelineCopy.ts +1 -3
- package/src/query/dbQuery.ts +7 -17
- package/src/testing/daoTest.ts +0 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CommonLogger, ErrorMode, ObjectWithId
|
|
1
|
+
import { CommonLogger, ErrorMode, ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
2
|
import {
|
|
3
3
|
AjvSchema,
|
|
4
4
|
AjvValidationError,
|
|
@@ -12,24 +12,25 @@ import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../
|
|
|
12
12
|
|
|
13
13
|
export interface CommonDaoHooks<
|
|
14
14
|
BM extends Partial<ObjectWithId<ID>>,
|
|
15
|
-
DBM extends ObjectWithId<ID
|
|
16
|
-
|
|
17
|
-
ID extends string | number,
|
|
15
|
+
DBM extends Partial<ObjectWithId<ID>> = BM,
|
|
16
|
+
ID extends string | number = NonNullable<BM['id']>,
|
|
18
17
|
> {
|
|
19
|
-
createRandomId
|
|
18
|
+
createRandomId(): ID
|
|
20
19
|
/**
|
|
21
20
|
* createNaturalId hook is called (tried) first.
|
|
22
21
|
* If it doesn't exist - createRandomId is called.
|
|
23
22
|
*/
|
|
24
|
-
createNaturalId
|
|
25
|
-
parseNaturalId
|
|
26
|
-
beforeCreate
|
|
27
|
-
beforeDBMValidate
|
|
28
|
-
beforeDBMToBM
|
|
29
|
-
beforeBMToDBM
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
createNaturalId(obj: DBM | BM): ID
|
|
24
|
+
parseNaturalId(id: ID): Partial<DBM>
|
|
25
|
+
beforeCreate(bm: Partial<BM>): Partial<BM>
|
|
26
|
+
beforeDBMValidate(dbm: Partial<DBM>): Partial<DBM>
|
|
27
|
+
beforeDBMToBM(dbm: DBM): Partial<BM> | Promise<Partial<BM>>
|
|
28
|
+
beforeBMToDBM(bm: BM): Partial<DBM> | Promise<Partial<DBM>>
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Applicable to **read** operations, never to **save** operations.
|
|
32
|
+
*/
|
|
33
|
+
anonymize(dbm: DBM): DBM
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* If hook is defined - allows to prevent or modify the error thrown.
|
|
@@ -37,7 +38,7 @@ export interface CommonDaoHooks<
|
|
|
37
38
|
* Return original `err` to pass the error through (will be thrown in CommonDao).
|
|
38
39
|
* Return modified/new `Error` if needed.
|
|
39
40
|
*/
|
|
40
|
-
onValidationError
|
|
41
|
+
onValidationError(err: JoiValidationError | AjvValidationError): Error | false
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
export enum CommonDaoLogLevel {
|
|
@@ -61,9 +62,8 @@ export enum CommonDaoLogLevel {
|
|
|
61
62
|
|
|
62
63
|
export interface CommonDaoCfg<
|
|
63
64
|
BM extends Partial<ObjectWithId<ID>>,
|
|
64
|
-
DBM extends ObjectWithId<ID
|
|
65
|
-
|
|
66
|
-
ID extends string | number = string,
|
|
65
|
+
DBM extends Partial<ObjectWithId<ID>> = BM,
|
|
66
|
+
ID extends string | number = NonNullable<BM['id']>,
|
|
67
67
|
> {
|
|
68
68
|
db: CommonDB
|
|
69
69
|
table: string
|
|
@@ -73,7 +73,6 @@ export interface CommonDaoCfg<
|
|
|
73
73
|
*/
|
|
74
74
|
dbmSchema?: ObjectSchemaTyped<DBM> | AjvSchema<DBM>
|
|
75
75
|
bmSchema?: ObjectSchemaTyped<BM> | AjvSchema<BM>
|
|
76
|
-
tmSchema?: ObjectSchemaTyped<TM> | AjvSchema<TM>
|
|
77
76
|
|
|
78
77
|
excludeFromIndexes?: (keyof DBM)[]
|
|
79
78
|
|
|
@@ -85,6 +84,8 @@ export interface CommonDaoCfg<
|
|
|
85
84
|
* `delete*` and `patch` will throw.
|
|
86
85
|
*
|
|
87
86
|
* You can still override saveMethod, or set opt.allowMutability to allow deletion.
|
|
87
|
+
*
|
|
88
|
+
* todo: consider merging it with readOnly, as it's almost the same
|
|
88
89
|
*/
|
|
89
90
|
immutable?: boolean
|
|
90
91
|
|
|
@@ -110,7 +111,7 @@ export interface CommonDaoCfg<
|
|
|
110
111
|
logStarted?: boolean
|
|
111
112
|
|
|
112
113
|
// Hooks are designed with inspiration from got/ky interface
|
|
113
|
-
hooks?: Partial<CommonDaoHooks<BM, DBM,
|
|
114
|
+
hooks?: Partial<CommonDaoHooks<BM, DBM, ID>>
|
|
114
115
|
|
|
115
116
|
/**
|
|
116
117
|
* Defaults to 'string'
|
|
@@ -127,6 +128,7 @@ export interface CommonDaoCfg<
|
|
|
127
128
|
/**
|
|
128
129
|
* See the same option in CommonDB.
|
|
129
130
|
* Defaults to false normally.
|
|
131
|
+
* "Generated" means "generated by the underlying DB" (e.g MySQL autoincrement).
|
|
130
132
|
*/
|
|
131
133
|
assignGeneratedIds?: boolean
|
|
132
134
|
|
|
@@ -209,14 +211,6 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
209
211
|
*/
|
|
210
212
|
table?: string
|
|
211
213
|
|
|
212
|
-
/**
|
|
213
|
-
* If set - wraps the method in `pTimeout` with a timeout of given number of milliseconds.
|
|
214
|
-
* Currently, it is only used to debug an ongoing GCP infra issue.
|
|
215
|
-
*
|
|
216
|
-
* @experimental
|
|
217
|
-
*/
|
|
218
|
-
timeout?: number
|
|
219
|
-
|
|
220
214
|
/**
|
|
221
215
|
* If passed - operation will not be performed immediately, but instead "added" to the transaction.
|
|
222
216
|
* In the end - transaction needs to be committed (by calling `commit`).
|
|
@@ -232,7 +226,7 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
232
226
|
/**
|
|
233
227
|
* All properties default to undefined.
|
|
234
228
|
*/
|
|
235
|
-
export interface CommonDaoSaveOptions<DBM extends ObjectWithId
|
|
229
|
+
export interface CommonDaoSaveOptions<DBM extends Partial<ObjectWithId>>
|
|
236
230
|
extends CommonDaoOptions,
|
|
237
231
|
CommonDBSaveOptions<DBM> {
|
|
238
232
|
/**
|
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
_since,
|
|
7
7
|
_truncate,
|
|
8
8
|
_uniqBy,
|
|
9
|
-
AnyObject,
|
|
10
9
|
AppError,
|
|
11
10
|
AsyncMapper,
|
|
12
11
|
ErrorMode,
|
|
@@ -14,7 +13,6 @@ import {
|
|
|
14
13
|
JsonSchemaRootObject,
|
|
15
14
|
ObjectWithId,
|
|
16
15
|
pMap,
|
|
17
|
-
pTimeout,
|
|
18
16
|
Saved,
|
|
19
17
|
Unsaved,
|
|
20
18
|
} from '@naturalcycles/js-lib'
|
|
@@ -64,15 +62,13 @@ const isCI = !!process.env['CI']
|
|
|
64
62
|
*
|
|
65
63
|
* DBM = Database model (how it's stored in DB)
|
|
66
64
|
* BM = Backend model (optimized for API access)
|
|
67
|
-
* TM = Transport model (optimized to be sent over the wire)
|
|
68
65
|
*/
|
|
69
66
|
export class CommonDao<
|
|
70
67
|
BM extends Partial<ObjectWithId<ID>>,
|
|
71
|
-
DBM extends ObjectWithId<ID
|
|
72
|
-
TM extends AnyObject = BM,
|
|
68
|
+
DBM extends Partial<ObjectWithId<ID>> = BM,
|
|
73
69
|
ID extends string | number = NonNullable<BM['id']>,
|
|
74
70
|
> {
|
|
75
|
-
constructor(public cfg: CommonDaoCfg<BM, DBM,
|
|
71
|
+
constructor(public cfg: CommonDaoCfg<BM, DBM, ID>) {
|
|
76
72
|
this.cfg = {
|
|
77
73
|
// Default is to NOT log in AppEngine and in CI,
|
|
78
74
|
// otherwise to log Operations
|
|
@@ -91,8 +87,6 @@ export class CommonDao<
|
|
|
91
87
|
beforeDBMValidate: dbm => dbm,
|
|
92
88
|
beforeDBMToBM: dbm => dbm as any,
|
|
93
89
|
beforeBMToDBM: bm => bm as any,
|
|
94
|
-
beforeTMToBM: tm => tm as any,
|
|
95
|
-
beforeBMToTM: bm => bm as any,
|
|
96
90
|
anonymize: dbm => dbm,
|
|
97
91
|
onValidationError: err => err,
|
|
98
92
|
...cfg.hooks,
|
|
@@ -128,22 +122,7 @@ export class CommonDao<
|
|
|
128
122
|
const table = opt.table || this.cfg.table
|
|
129
123
|
const started = this.logStarted(op, table)
|
|
130
124
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (opt.timeout) {
|
|
134
|
-
// todo: possibly remove it after debugging is done
|
|
135
|
-
dbm = await pTimeout(
|
|
136
|
-
async () => {
|
|
137
|
-
return (await this.cfg.db.getByIds<DBM>(table, [id]))[0]
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
timeout: opt.timeout,
|
|
141
|
-
name: `getById(${table})`,
|
|
142
|
-
},
|
|
143
|
-
)
|
|
144
|
-
} else {
|
|
145
|
-
dbm = (await this.cfg.db.getByIds<DBM>(table, [id]))[0]
|
|
146
|
-
}
|
|
125
|
+
const dbm = (await this.cfg.db.getByIds<DBM>(table, [id]))[0]
|
|
147
126
|
|
|
148
127
|
const bm = opt.raw ? (dbm as any) : await this.dbmToBM(dbm, opt)
|
|
149
128
|
this.logResult(started, op, bm, table)
|
|
@@ -180,24 +159,6 @@ export class CommonDao<
|
|
|
180
159
|
return dbm || null
|
|
181
160
|
}
|
|
182
161
|
|
|
183
|
-
async getByIdAsTM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>
|
|
184
|
-
async getByIdAsTM(id?: ID | null, opt?: CommonDaoOptions): Promise<TM | null>
|
|
185
|
-
async getByIdAsTM(id?: ID | null, opt: CommonDaoOptions = {}): Promise<TM | null> {
|
|
186
|
-
if (!id) return null
|
|
187
|
-
const op = `getByIdAsTM(${id})`
|
|
188
|
-
const table = opt.table || this.cfg.table
|
|
189
|
-
const started = this.logStarted(op, table)
|
|
190
|
-
const [dbm] = await this.cfg.db.getByIds<DBM>(table, [id])
|
|
191
|
-
if (opt.raw) {
|
|
192
|
-
this.logResult(started, op, dbm, table)
|
|
193
|
-
return (dbm as any) || null
|
|
194
|
-
}
|
|
195
|
-
const bm = await this.dbmToBM(dbm, opt)
|
|
196
|
-
const tm = this.bmToTM(bm, opt)
|
|
197
|
-
this.logResult(started, op, tm, table)
|
|
198
|
-
return tm || null
|
|
199
|
-
}
|
|
200
|
-
|
|
201
162
|
async getByIds(ids: ID[], opt: CommonDaoOptions = {}): Promise<Saved<BM>[]> {
|
|
202
163
|
const op = `getByIds ${ids.length} id(s) (${_truncate(ids.slice(0, 10).join(', '), 50)})`
|
|
203
164
|
const table = opt.table || this.cfg.table
|
|
@@ -295,8 +256,8 @@ export class CommonDao<
|
|
|
295
256
|
/**
|
|
296
257
|
* Pass `table` to override table
|
|
297
258
|
*/
|
|
298
|
-
query(table?: string): RunnableDBQuery<BM, DBM,
|
|
299
|
-
return new RunnableDBQuery<BM, DBM,
|
|
259
|
+
query(table?: string): RunnableDBQuery<BM, DBM, ID> {
|
|
260
|
+
return new RunnableDBQuery<BM, DBM, ID>(this, table)
|
|
300
261
|
}
|
|
301
262
|
|
|
302
263
|
async runQuery(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<Saved<BM>[]> {
|
|
@@ -364,29 +325,6 @@ export class CommonDao<
|
|
|
364
325
|
return { rows: dbms, ...queryResult }
|
|
365
326
|
}
|
|
366
327
|
|
|
367
|
-
async runQueryAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<TM[]> {
|
|
368
|
-
const { rows } = await this.runQueryExtendedAsTM(q, opt)
|
|
369
|
-
return rows
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
async runQueryExtendedAsTM(
|
|
373
|
-
q: DBQuery<DBM>,
|
|
374
|
-
opt: CommonDaoOptions = {},
|
|
375
|
-
): Promise<RunQueryResult<TM>> {
|
|
376
|
-
q.table = opt.table || q.table
|
|
377
|
-
const op = `runQueryAsTM(${q.pretty()})`
|
|
378
|
-
const started = this.logStarted(op, q.table)
|
|
379
|
-
const { rows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
|
|
380
|
-
const partialQuery = !!q._selectedFieldNames
|
|
381
|
-
const tms =
|
|
382
|
-
partialQuery || opt.raw ? (rows as any[]) : this.bmsToTM(await this.dbmsToBM(rows, opt), opt)
|
|
383
|
-
this.logResult(started, op, tms, q.table)
|
|
384
|
-
return {
|
|
385
|
-
rows: tms,
|
|
386
|
-
...queryResult,
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
328
|
async runQueryCount(q: DBQuery<DBM>, opt: CommonDaoOptions = {}): Promise<number> {
|
|
391
329
|
q.table = opt.table || q.table
|
|
392
330
|
const op = `runQueryCount(${q.pretty()})`
|
|
@@ -548,7 +486,7 @@ export class CommonDao<
|
|
|
548
486
|
async queryIds(q: DBQuery<DBM>, opt: CommonDaoOptions = {}): Promise<ID[]> {
|
|
549
487
|
q.table = opt.table || q.table
|
|
550
488
|
const { rows } = await this.cfg.db.runQuery(q.select(['id']), opt)
|
|
551
|
-
return rows.map(r => r.id)
|
|
489
|
+
return rows.map(r => r.id!)
|
|
552
490
|
}
|
|
553
491
|
|
|
554
492
|
streamQueryIds(q: DBQuery<DBM>, opt: CommonDaoStreamOptions = {}): ReadableTyped<ID> {
|
|
@@ -559,7 +497,7 @@ export class CommonDao<
|
|
|
559
497
|
.streamQuery<DBM>(q.select(['id']), opt)
|
|
560
498
|
.on('error', err => stream.emit('error', err))
|
|
561
499
|
.pipe(
|
|
562
|
-
transformMapSimple<DBM, ID>(objectWithId => objectWithId.id
|
|
500
|
+
transformMapSimple<DBM, ID>(objectWithId => objectWithId.id!, {
|
|
563
501
|
errorMode: ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
|
|
564
502
|
}),
|
|
565
503
|
)
|
|
@@ -581,7 +519,7 @@ export class CommonDao<
|
|
|
581
519
|
|
|
582
520
|
await _pipeline([
|
|
583
521
|
this.cfg.db.streamQuery<DBM>(q.select(['id']), opt),
|
|
584
|
-
transformMapSimple<DBM, ID>(objectWithId => objectWithId.id),
|
|
522
|
+
transformMapSimple<DBM, ID>(objectWithId => objectWithId.id!),
|
|
585
523
|
transformTap(() => count++),
|
|
586
524
|
transformMap<ID, void>(mapper, {
|
|
587
525
|
...opt,
|
|
@@ -907,7 +845,7 @@ export class CommonDao<
|
|
|
907
845
|
|
|
908
846
|
await _pipeline([
|
|
909
847
|
this.cfg.db.streamQuery<DBM>(q.select(['id']), opt),
|
|
910
|
-
transformMapSimple<DBM, ID>(objectWithId => objectWithId.id
|
|
848
|
+
transformMapSimple<DBM, ID>(objectWithId => objectWithId.id!, {
|
|
911
849
|
errorMode: ErrorMode.SUPPRESS,
|
|
912
850
|
}),
|
|
913
851
|
transformBuffer<string>({ batchSize }),
|
|
@@ -971,7 +909,7 @@ export class CommonDao<
|
|
|
971
909
|
|
|
972
910
|
// optimization: no need to run full joi DBM validation, cause BM validation will be run
|
|
973
911
|
// const dbm = this.anyToDBM(_dbm, opt)
|
|
974
|
-
let dbm: DBM = { ..._dbm, ...this.cfg.hooks!.parseNaturalId!(_dbm.id) }
|
|
912
|
+
let dbm: DBM = { ..._dbm, ...this.cfg.hooks!.parseNaturalId!(_dbm.id!) }
|
|
975
913
|
|
|
976
914
|
if (opt.anonymize) {
|
|
977
915
|
dbm = this.cfg.hooks!.anonymize!(dbm)
|
|
@@ -1027,7 +965,7 @@ export class CommonDao<
|
|
|
1027
965
|
// this shouldn't be happening on load! but should on save!
|
|
1028
966
|
// this.assignIdCreatedUpdated(dbm, opt)
|
|
1029
967
|
|
|
1030
|
-
dbm = { ...dbm, ...this.cfg.hooks!.parseNaturalId!(dbm.id) }
|
|
968
|
+
dbm = { ...dbm, ...this.cfg.hooks!.parseNaturalId!(dbm.id!) }
|
|
1031
969
|
|
|
1032
970
|
if (opt.anonymize) {
|
|
1033
971
|
dbm = this.cfg.hooks!.anonymize!(dbm)
|
|
@@ -1041,50 +979,6 @@ export class CommonDao<
|
|
|
1041
979
|
return entities.map(entity => this.anyToDBM(entity, opt))
|
|
1042
980
|
}
|
|
1043
981
|
|
|
1044
|
-
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined
|
|
1045
|
-
bmToTM(bm?: Saved<BM>, opt?: CommonDaoOptions): TM
|
|
1046
|
-
bmToTM(bm?: Saved<BM>, opt?: CommonDaoOptions): TM | undefined {
|
|
1047
|
-
if (bm === undefined) return
|
|
1048
|
-
|
|
1049
|
-
// optimization: 1 validation is enough
|
|
1050
|
-
// Validate/convert BM
|
|
1051
|
-
// bm gets assigned to the new reference
|
|
1052
|
-
// bm = this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
1053
|
-
|
|
1054
|
-
// BM > TM
|
|
1055
|
-
const tm = this.cfg.hooks!.beforeBMToTM!(bm as any)
|
|
1056
|
-
|
|
1057
|
-
// Validate/convert DBM
|
|
1058
|
-
return this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt)
|
|
1059
|
-
}
|
|
1060
|
-
|
|
1061
|
-
bmsToTM(bms: Saved<BM>[], opt: CommonDaoOptions = {}): TM[] {
|
|
1062
|
-
// try/catch?
|
|
1063
|
-
return bms.map(bm => this.bmToTM(bm, opt))
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
|
-
tmToBM(tm: undefined, opt?: CommonDaoOptions): undefined
|
|
1067
|
-
tmToBM(tm?: TM, opt?: CommonDaoOptions): BM
|
|
1068
|
-
tmToBM(tm?: TM, opt: CommonDaoOptions = {}): BM | undefined {
|
|
1069
|
-
if (!tm) return
|
|
1070
|
-
|
|
1071
|
-
// optimization: 1 validation is enough
|
|
1072
|
-
// Validate/convert TM
|
|
1073
|
-
// bm gets assigned to the new reference
|
|
1074
|
-
// tm = this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt)
|
|
1075
|
-
|
|
1076
|
-
// TM > BM
|
|
1077
|
-
const bm = this.cfg.hooks!.beforeTMToBM!(tm) as BM
|
|
1078
|
-
|
|
1079
|
-
// Validate/convert BM
|
|
1080
|
-
return this.validateAndConvert<BM>(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
1081
|
-
}
|
|
1082
|
-
|
|
1083
|
-
tmsToBM(tms: TM[], opt: CommonDaoOptions = {}): BM[] {
|
|
1084
|
-
// try/catch?
|
|
1085
|
-
return tms.map(tm => this.tmToBM(tm, opt))
|
|
1086
|
-
}
|
|
1087
|
-
|
|
1088
982
|
/**
|
|
1089
983
|
* Returns *converted value*.
|
|
1090
984
|
* Validates (unless `skipValidation=true` passed).
|
package/src/db.model.ts
CHANGED
|
@@ -32,9 +32,9 @@ export interface CommonKeyValueDaoCfg<T> {
|
|
|
32
32
|
logStarted?: boolean
|
|
33
33
|
|
|
34
34
|
hooks?: {
|
|
35
|
-
mapValueToBuffer
|
|
36
|
-
mapBufferToValue
|
|
37
|
-
beforeCreate
|
|
35
|
+
mapValueToBuffer?(v: T): Promise<Buffer>
|
|
36
|
+
mapBufferToValue?(b: Buffer): Promise<T>
|
|
37
|
+
beforeCreate?(v: Partial<T>): Partial<T>
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
/**
|
|
@@ -158,9 +158,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
|
|
|
158
158
|
|
|
159
159
|
fs.ensureDirSync(outputDirPath)
|
|
160
160
|
|
|
161
|
-
|
|
162
|
-
tables = await db.getTables()
|
|
163
|
-
}
|
|
161
|
+
tables ||= await db.getTables()
|
|
164
162
|
|
|
165
163
|
console.log(`${yellow(tables.length)} ${boldWhite('table(s)')}:\n` + tables.join('\n'))
|
|
166
164
|
|
|
@@ -106,9 +106,7 @@ export async function dbPipelineCopy(opt: DBPipelineCopyOptions): Promise<NDJson
|
|
|
106
106
|
|
|
107
107
|
console.log(`>> ${dimWhite('dbPipelineCopy')} started...${sinceUpdatedStr}`)
|
|
108
108
|
|
|
109
|
-
|
|
110
|
-
tables = await dbInput.getTables()
|
|
111
|
-
}
|
|
109
|
+
tables ||= await dbInput.getTables()
|
|
112
110
|
|
|
113
111
|
console.log(`${yellow(tables.length)} ${boldWhite('table(s)')}:\n` + tables.join('\n'))
|
|
114
112
|
|
package/src/query/dbQuery.ts
CHANGED
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
AsyncMapper,
|
|
5
5
|
_truncate,
|
|
6
6
|
Saved,
|
|
7
|
-
AnyObject,
|
|
8
7
|
_objectAssign,
|
|
9
8
|
} from '@naturalcycles/js-lib'
|
|
10
9
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
@@ -61,13 +60,13 @@ export const dbQueryFilterOperatorValues: DBQueryFilterOperator[] = [
|
|
|
61
60
|
'array-contains-any',
|
|
62
61
|
]
|
|
63
62
|
|
|
64
|
-
export interface DBQueryFilter<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
63
|
+
export interface DBQueryFilter<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
65
64
|
name: keyof ROW
|
|
66
65
|
op: DBQueryFilterOperator
|
|
67
66
|
val: any
|
|
68
67
|
}
|
|
69
68
|
|
|
70
|
-
export interface DBQueryOrder<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
69
|
+
export interface DBQueryOrder<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
71
70
|
name: keyof ROW
|
|
72
71
|
descending?: boolean
|
|
73
72
|
}
|
|
@@ -82,7 +81,7 @@ export interface DBQueryOrder<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
|
82
81
|
*
|
|
83
82
|
* <DBM> is the type of **queried** object (so e.g `key of DBM` can be used), not **returned** object.
|
|
84
83
|
*/
|
|
85
|
-
export class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
84
|
+
export class DBQuery<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
86
85
|
constructor(public table: string) {}
|
|
87
86
|
|
|
88
87
|
/**
|
|
@@ -92,7 +91,7 @@ export class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
|
92
91
|
return new DBQuery(table)
|
|
93
92
|
}
|
|
94
93
|
|
|
95
|
-
static fromPlainObject<ROW extends ObjectWithId = AnyObjectWithId>(
|
|
94
|
+
static fromPlainObject<ROW extends Partial<ObjectWithId> = AnyObjectWithId>(
|
|
96
95
|
obj: Partial<DBQuery<ROW>> & { table: string },
|
|
97
96
|
): DBQuery<ROW> {
|
|
98
97
|
return Object.assign(new DBQuery<ROW>(obj.table), obj)
|
|
@@ -237,14 +236,13 @@ export class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
|
237
236
|
*/
|
|
238
237
|
export class RunnableDBQuery<
|
|
239
238
|
BM extends Partial<ObjectWithId<ID>>,
|
|
240
|
-
DBM extends ObjectWithId<ID
|
|
241
|
-
|
|
242
|
-
ID extends string | number = string,
|
|
239
|
+
DBM extends Partial<ObjectWithId<ID>> = BM,
|
|
240
|
+
ID extends string | number = NonNullable<BM['id']>,
|
|
243
241
|
> extends DBQuery<DBM> {
|
|
244
242
|
/**
|
|
245
243
|
* Pass `table` to override table.
|
|
246
244
|
*/
|
|
247
|
-
constructor(public dao: CommonDao<BM, DBM,
|
|
245
|
+
constructor(public dao: CommonDao<BM, DBM, ID>, table?: string) {
|
|
248
246
|
super(table || dao.cfg.table)
|
|
249
247
|
}
|
|
250
248
|
|
|
@@ -260,10 +258,6 @@ export class RunnableDBQuery<
|
|
|
260
258
|
return await this.dao.runQueryAsDBM(this, opt)
|
|
261
259
|
}
|
|
262
260
|
|
|
263
|
-
async runQueryAsTM(opt?: CommonDaoOptions): Promise<TM[]> {
|
|
264
|
-
return await this.dao.runQueryAsTM(this, opt)
|
|
265
|
-
}
|
|
266
|
-
|
|
267
261
|
async runQueryExtended(opt?: CommonDaoOptions): Promise<RunQueryResult<Saved<BM>>> {
|
|
268
262
|
return await this.dao.runQueryExtended(this, opt)
|
|
269
263
|
}
|
|
@@ -272,10 +266,6 @@ export class RunnableDBQuery<
|
|
|
272
266
|
return await this.dao.runQueryExtendedAsDBM(this, opt)
|
|
273
267
|
}
|
|
274
268
|
|
|
275
|
-
async runQueryExtendedAsTM(opt?: CommonDaoOptions): Promise<RunQueryResult<TM>> {
|
|
276
|
-
return await this.dao.runQueryExtendedAsTM(this, opt)
|
|
277
|
-
}
|
|
278
|
-
|
|
279
269
|
async runQueryCount(opt?: CommonDaoOptions): Promise<number> {
|
|
280
270
|
return await this.dao.runQueryCount(this, opt)
|
|
281
271
|
}
|
package/src/testing/daoTest.ts
CHANGED
|
@@ -8,7 +8,6 @@ import {
|
|
|
8
8
|
createTestItemsBM,
|
|
9
9
|
testItemBMSchema,
|
|
10
10
|
testItemDBMSchema,
|
|
11
|
-
testItemTMSchema,
|
|
12
11
|
TEST_TABLE,
|
|
13
12
|
createTestItemBM,
|
|
14
13
|
testItemDBMJsonSchema,
|
|
@@ -25,7 +24,6 @@ export function runCommonDaoTest(
|
|
|
25
24
|
db,
|
|
26
25
|
dbmSchema: testItemDBMSchema,
|
|
27
26
|
bmSchema: testItemBMSchema,
|
|
28
|
-
tmSchema: testItemTMSchema,
|
|
29
27
|
logStarted: true,
|
|
30
28
|
logLevel: CommonDaoLogLevel.DATA_FULL,
|
|
31
29
|
})
|