@naturalcycles/db-lib 8.16.1 → 8.19.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/CHANGELOG.md +28 -0
- package/dist/adapter/cachedb/cache.db.d.ts +8 -8
- package/dist/adapter/cachedb/cache.db.model.d.ts +4 -4
- package/dist/adapter/file/file.db.d.ts +3 -3
- package/dist/adapter/inmemory/inMemory.db.d.ts +3 -3
- package/dist/base.common.db.d.ts +3 -3
- package/dist/common.db.d.ts +3 -3
- package/dist/commondao/common.dao.d.ts +10 -10
- package/dist/commondao/common.dao.js +6 -4
- package/dist/commondao/common.dao.model.d.ts +2 -2
- package/dist/db.model.d.ts +5 -22
- package/dist/db.model.js +1 -14
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -3
- package/dist/query/dbQuery.d.ts +16 -16
- package/dist/query/dbQuery.js +0 -11
- package/dist/testing/test.model.d.ts +1 -2
- package/dist/testing/test.model.js +2 -3
- package/dist/timeseries/commonTimeSeriesDao.js +1 -1
- package/dist/transaction/dbTransaction.d.ts +3 -3
- package/dist/validation/index.d.ts +3 -3
- package/package.json +1 -1
- package/src/adapter/cachedb/cache.db.model.ts +13 -4
- package/src/adapter/cachedb/cache.db.ts +11 -11
- package/src/adapter/file/file.db.ts +11 -5
- package/src/adapter/inmemory/inMemory.db.ts +4 -4
- package/src/base.common.db.ts +6 -3
- package/src/common.db.ts +7 -3
- package/src/commondao/common.dao.model.ts +4 -2
- package/src/commondao/common.dao.ts +24 -13
- package/src/db.model.ts +6 -41
- package/src/index.ts +2 -14
- package/src/pipeline/dbPipelineRestore.ts +2 -1
- package/src/query/dbQuery.ts +16 -28
- package/src/testing/test.model.ts +3 -2
- package/src/timeseries/commonTimeSeriesDao.ts +2 -1
- package/src/transaction/dbTransaction.ts +3 -3
|
@@ -49,10 +49,10 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
49
49
|
return await this.cfg.downstreamDB.getTableSchema<ROW>(table)
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
override async createTable(
|
|
52
|
+
override async createTable<ROW extends ObjectWithId>(
|
|
53
53
|
table: string,
|
|
54
|
-
schema: JsonSchemaObject
|
|
55
|
-
opt: CacheDBCreateOptions = {},
|
|
54
|
+
schema: JsonSchemaObject<ROW>,
|
|
55
|
+
opt: CacheDBCreateOptions<ROW> = {},
|
|
56
56
|
): Promise<void> {
|
|
57
57
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
58
58
|
await this.cfg.downstreamDB.createTable(table, schema, opt)
|
|
@@ -66,7 +66,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
66
66
|
override async getByIds<ROW extends ObjectWithId>(
|
|
67
67
|
table: string,
|
|
68
68
|
ids: string[],
|
|
69
|
-
opt: CacheDBOptions = {},
|
|
69
|
+
opt: CacheDBOptions<ROW> = {},
|
|
70
70
|
): Promise<ROW[]> {
|
|
71
71
|
const resultMap: Record<string, ROW> = {}
|
|
72
72
|
const missingIds: string[] = []
|
|
@@ -109,10 +109,10 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
109
109
|
return ids.map(id => resultMap[id]!).filter(Boolean)
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
override async deleteByIds(
|
|
112
|
+
override async deleteByIds<ROW extends ObjectWithId>(
|
|
113
113
|
table: string,
|
|
114
114
|
ids: string[],
|
|
115
|
-
opt: CacheDBOptions = {},
|
|
115
|
+
opt: CacheDBOptions<ROW> = {},
|
|
116
116
|
): Promise<number> {
|
|
117
117
|
let deletedIds = 0
|
|
118
118
|
|
|
@@ -139,7 +139,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
139
139
|
override async saveBatch<ROW extends ObjectWithId>(
|
|
140
140
|
table: string,
|
|
141
141
|
rows: ROW[],
|
|
142
|
-
opt: CacheDBOptions = {},
|
|
142
|
+
opt: CacheDBOptions<ROW> = {},
|
|
143
143
|
): Promise<void> {
|
|
144
144
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
145
145
|
await this.cfg.downstreamDB.saveBatch(table, rows, opt)
|
|
@@ -167,7 +167,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
167
167
|
|
|
168
168
|
override async runQuery<ROW extends ObjectWithId>(
|
|
169
169
|
q: DBQuery<ROW>,
|
|
170
|
-
opt: CacheDBOptions = {},
|
|
170
|
+
opt: CacheDBOptions<ROW> = {},
|
|
171
171
|
): Promise<RunQueryResult<ROW>> {
|
|
172
172
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
173
173
|
const { rows, ...queryResult } = await this.cfg.downstreamDB.runQuery(q, opt)
|
|
@@ -197,7 +197,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
197
197
|
|
|
198
198
|
override async runQueryCount<ROW extends ObjectWithId>(
|
|
199
199
|
q: DBQuery<ROW>,
|
|
200
|
-
opt: CacheDBOptions = {},
|
|
200
|
+
opt: CacheDBOptions<ROW> = {},
|
|
201
201
|
): Promise<number> {
|
|
202
202
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
203
203
|
return await this.cfg.downstreamDB.runQueryCount(q, opt)
|
|
@@ -214,7 +214,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
214
214
|
|
|
215
215
|
override streamQuery<ROW extends ObjectWithId>(
|
|
216
216
|
q: DBQuery<ROW>,
|
|
217
|
-
opt: CacheDBStreamOptions = {},
|
|
217
|
+
opt: CacheDBStreamOptions<ROW> = {},
|
|
218
218
|
): Readable {
|
|
219
219
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
220
220
|
const stream = this.cfg.downstreamDB.streamQuery<ROW>(q, opt)
|
|
@@ -253,7 +253,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
253
253
|
|
|
254
254
|
override async deleteByQuery<ROW extends ObjectWithId>(
|
|
255
255
|
q: DBQuery<ROW>,
|
|
256
|
-
opt: CacheDBOptions = {},
|
|
256
|
+
opt: CacheDBOptions<ROW> = {},
|
|
257
257
|
): Promise<number> {
|
|
258
258
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
259
259
|
const deletedIds = await this.cfg.downstreamDB.deleteByQuery(q, opt)
|
|
@@ -13,7 +13,13 @@ import {
|
|
|
13
13
|
} from '@naturalcycles/js-lib'
|
|
14
14
|
import { Debug, readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
15
15
|
import { dimGrey } from '@naturalcycles/nodejs-lib/dist/colors'
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
AnyObjectWithId,
|
|
18
|
+
BaseCommonDB,
|
|
19
|
+
DBSaveBatchOperation,
|
|
20
|
+
ObjectWithId,
|
|
21
|
+
queryInMemory,
|
|
22
|
+
} from '../..'
|
|
17
23
|
import { CommonDB } from '../../common.db'
|
|
18
24
|
import {
|
|
19
25
|
CommonDBOptions,
|
|
@@ -72,7 +78,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
72
78
|
override async saveBatch<ROW extends ObjectWithId>(
|
|
73
79
|
table: string,
|
|
74
80
|
rows: ROW[],
|
|
75
|
-
_opt?: CommonDBSaveOptions
|
|
81
|
+
_opt?: CommonDBSaveOptions<ROW>,
|
|
76
82
|
): Promise<void> {
|
|
77
83
|
if (!rows.length) return // save some api calls
|
|
78
84
|
|
|
@@ -98,7 +104,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
98
104
|
/**
|
|
99
105
|
* Implementation is optimized for loading/saving _whole files_.
|
|
100
106
|
*/
|
|
101
|
-
override async commitTransaction(tx: DBTransaction, _opt?:
|
|
107
|
+
override async commitTransaction(tx: DBTransaction, _opt?: CommonDBOptions): Promise<void> {
|
|
102
108
|
// data[table][id] => row
|
|
103
109
|
const data: StringMap<StringMap<ObjectWithId>> = {}
|
|
104
110
|
|
|
@@ -131,7 +137,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
131
137
|
return {
|
|
132
138
|
type: 'saveBatch',
|
|
133
139
|
table,
|
|
134
|
-
rows: this.sortRows(Object.values(data[table]!)),
|
|
140
|
+
rows: this.sortRows(Object.values(data[table]!) as AnyObjectWithId[]),
|
|
135
141
|
}
|
|
136
142
|
})
|
|
137
143
|
|
|
@@ -242,7 +248,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
242
248
|
this.logFinished(started, op)
|
|
243
249
|
}
|
|
244
250
|
|
|
245
|
-
async saveFiles(ops: DBSaveBatchOperation[]): Promise<void> {
|
|
251
|
+
async saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void> {
|
|
246
252
|
if (!ops.length) return
|
|
247
253
|
const op =
|
|
248
254
|
`saveFiles ${ops.length} op(s):\n` + ops.map(o => `${o.table} (${o.rows.length})`).join('\n')
|
|
@@ -122,9 +122,9 @@ export class InMemoryDB implements CommonDB {
|
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
async createTable(
|
|
125
|
+
async createTable<ROW extends ObjectWithId>(
|
|
126
126
|
_table: string,
|
|
127
|
-
_schema: JsonSchemaObject
|
|
127
|
+
_schema: JsonSchemaObject<ROW>,
|
|
128
128
|
opt: CommonDBCreateOptions = {},
|
|
129
129
|
): Promise<void> {
|
|
130
130
|
const table = this.cfg.tablesPrefix + _table
|
|
@@ -148,7 +148,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
148
148
|
async saveBatch<ROW extends ObjectWithId>(
|
|
149
149
|
_table: string,
|
|
150
150
|
rows: ROW[],
|
|
151
|
-
_opt?: CommonDBSaveOptions
|
|
151
|
+
_opt?: CommonDBSaveOptions<ROW>,
|
|
152
152
|
): Promise<void> {
|
|
153
153
|
const table = this.cfg.tablesPrefix + _table
|
|
154
154
|
this.data[table] = this.data[table] || {}
|
|
@@ -220,7 +220,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
220
220
|
return Readable.from(queryInMemory(q, Object.values(this.data[table] || {}) as ROW[]))
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
async commitTransaction(tx: DBTransaction, opt?:
|
|
223
|
+
async commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void> {
|
|
224
224
|
for await (const op of tx.ops) {
|
|
225
225
|
if (op.type === 'saveBatch') {
|
|
226
226
|
await this.saveBatch(op.table, op.rows, opt)
|
package/src/base.common.db.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Readable } from 'stream'
|
|
|
2
2
|
import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib'
|
|
3
3
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
4
4
|
import { CommonDB } from './common.db'
|
|
5
|
-
import {
|
|
5
|
+
import { CommonDBOptions, ObjectWithId, RunQueryResult } from './db.model'
|
|
6
6
|
import { DBQuery } from './query/dbQuery'
|
|
7
7
|
import { DBTransaction } from './transaction/dbTransaction'
|
|
8
8
|
import { commitDBTransactionSimple } from './transaction/dbTransaction.util'
|
|
@@ -30,7 +30,10 @@ export class BaseCommonDB implements CommonDB {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
async createTable
|
|
33
|
+
async createTable<ROW extends ObjectWithId>(
|
|
34
|
+
_table: string,
|
|
35
|
+
_schema: JsonSchemaObject<ROW>,
|
|
36
|
+
): Promise<void> {}
|
|
34
37
|
|
|
35
38
|
async deleteByIds(_table: string, _ids: string[]): Promise<number> {
|
|
36
39
|
return 0
|
|
@@ -62,7 +65,7 @@ export class BaseCommonDB implements CommonDB {
|
|
|
62
65
|
* Naive implementation.
|
|
63
66
|
* To be extended.
|
|
64
67
|
*/
|
|
65
|
-
async commitTransaction(tx: DBTransaction, opt?:
|
|
68
|
+
async commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void> {
|
|
66
69
|
await commitDBTransactionSimple(this, tx, opt)
|
|
67
70
|
}
|
|
68
71
|
}
|
package/src/common.db.ts
CHANGED
|
@@ -37,7 +37,11 @@ export interface CommonDB {
|
|
|
37
37
|
* Will do like `create table ...` for mysql.
|
|
38
38
|
* Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
|
|
39
39
|
*/
|
|
40
|
-
createTable
|
|
40
|
+
createTable<ROW extends ObjectWithId>(
|
|
41
|
+
table: string,
|
|
42
|
+
schema: JsonSchemaObject<ROW>,
|
|
43
|
+
opt?: CommonDBCreateOptions,
|
|
44
|
+
): Promise<void>
|
|
41
45
|
|
|
42
46
|
// GET
|
|
43
47
|
/**
|
|
@@ -70,7 +74,7 @@ export interface CommonDB {
|
|
|
70
74
|
saveBatch<ROW extends ObjectWithId>(
|
|
71
75
|
table: string,
|
|
72
76
|
rows: ROW[],
|
|
73
|
-
opt?: CommonDBSaveOptions
|
|
77
|
+
opt?: CommonDBSaveOptions<ROW>,
|
|
74
78
|
): Promise<void>
|
|
75
79
|
|
|
76
80
|
// DELETE
|
|
@@ -87,5 +91,5 @@ export interface CommonDB {
|
|
|
87
91
|
* Should be implemented as a Transaction (best effort), which means that
|
|
88
92
|
* either ALL or NONE of the operations should be applied.
|
|
89
93
|
*/
|
|
90
|
-
commitTransaction(tx: DBTransaction, opt?:
|
|
94
|
+
commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void>
|
|
91
95
|
}
|
|
@@ -67,7 +67,7 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends Obje
|
|
|
67
67
|
bmSchema?: ObjectSchemaTyped<BM> | AjvSchema<BM>
|
|
68
68
|
tmSchema?: ObjectSchemaTyped<TM> | AjvSchema<TM>
|
|
69
69
|
|
|
70
|
-
excludeFromIndexes?:
|
|
70
|
+
excludeFromIndexes?: (keyof DBM)[]
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
73
|
* @default to false
|
|
@@ -149,7 +149,9 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
149
149
|
/**
|
|
150
150
|
* All properties default to undefined.
|
|
151
151
|
*/
|
|
152
|
-
export interface CommonDaoSaveOptions extends
|
|
152
|
+
export interface CommonDaoSaveOptions<DBM extends ObjectWithId>
|
|
153
|
+
extends CommonDaoOptions,
|
|
154
|
+
CommonDBSaveOptions<DBM> {
|
|
153
155
|
/**
|
|
154
156
|
* @default false
|
|
155
157
|
*
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
_truncate,
|
|
10
10
|
pMap,
|
|
11
11
|
JsonSchemaRootObject,
|
|
12
|
+
Saved,
|
|
12
13
|
} from '@naturalcycles/js-lib'
|
|
13
14
|
import {
|
|
14
15
|
AjvSchema,
|
|
@@ -28,7 +29,7 @@ import {
|
|
|
28
29
|
_pipeline,
|
|
29
30
|
} from '@naturalcycles/nodejs-lib'
|
|
30
31
|
import { DBLibError } from '../cnst'
|
|
31
|
-
import { DBModelType, ObjectWithId, RunQueryResult
|
|
32
|
+
import { DBModelType, ObjectWithId, RunQueryResult } from '../db.model'
|
|
32
33
|
import { DBQuery, RunnableDBQuery } from '../query/dbQuery'
|
|
33
34
|
import {
|
|
34
35
|
CommonDaoCfg,
|
|
@@ -215,12 +216,12 @@ export class CommonDao<
|
|
|
215
216
|
}
|
|
216
217
|
}
|
|
217
218
|
|
|
218
|
-
async getBy(by:
|
|
219
|
-
return await this.query().
|
|
219
|
+
async getBy(by: keyof DBM, value: any, limit = 0, opt?: CommonDaoOptions): Promise<Saved<BM>[]> {
|
|
220
|
+
return await this.query().filterEq(by, value).limit(limit).runQuery(opt)
|
|
220
221
|
}
|
|
221
222
|
|
|
222
|
-
async getOneBy(by:
|
|
223
|
-
const [bm] = await this.query().
|
|
223
|
+
async getOneBy(by: keyof DBM, value: any, opt?: CommonDaoOptions): Promise<Saved<BM> | null> {
|
|
224
|
+
const [bm] = await this.query().filterEq(by, value).limit(1).runQuery(opt)
|
|
224
225
|
return bm || null
|
|
225
226
|
}
|
|
226
227
|
|
|
@@ -530,7 +531,7 @@ export class CommonDao<
|
|
|
530
531
|
/**
|
|
531
532
|
* Mutates with id, created, updated
|
|
532
533
|
*/
|
|
533
|
-
async save(bm: BM, opt: CommonDaoSaveOptions = {}): Promise<Saved<BM>> {
|
|
534
|
+
async save(bm: BM, opt: CommonDaoSaveOptions<DBM> = {}): Promise<Saved<BM>> {
|
|
534
535
|
this.requireWriteAccess()
|
|
535
536
|
const idWasGenerated = !bm.id
|
|
536
537
|
this.assignIdCreatedUpdated(bm, opt) // mutates
|
|
@@ -571,7 +572,11 @@ export class CommonDao<
|
|
|
571
572
|
*
|
|
572
573
|
* Convenience method to replace 3 operations (loading+patching+saving) with one.
|
|
573
574
|
*/
|
|
574
|
-
async patch(
|
|
575
|
+
async patch(
|
|
576
|
+
id: string,
|
|
577
|
+
patch: Partial<BM>,
|
|
578
|
+
opt: CommonDaoSaveOptions<DBM> = {},
|
|
579
|
+
): Promise<Saved<BM>> {
|
|
575
580
|
return await this.save(
|
|
576
581
|
{
|
|
577
582
|
...(await this.getByIdOrCreate(id, patch, opt)),
|
|
@@ -581,7 +586,11 @@ export class CommonDao<
|
|
|
581
586
|
)
|
|
582
587
|
}
|
|
583
588
|
|
|
584
|
-
async patchAsDBM(
|
|
589
|
+
async patchAsDBM(
|
|
590
|
+
id: string,
|
|
591
|
+
patch: Partial<DBM>,
|
|
592
|
+
opt: CommonDaoSaveOptions<DBM> = {},
|
|
593
|
+
): Promise<DBM> {
|
|
585
594
|
const dbm =
|
|
586
595
|
(await this.getByIdAsDBM(id, opt)) ||
|
|
587
596
|
(this.create({ ...patch, id } as Partial<BM>, opt) as any as DBM)
|
|
@@ -595,7 +604,7 @@ export class CommonDao<
|
|
|
595
604
|
)
|
|
596
605
|
}
|
|
597
606
|
|
|
598
|
-
async saveAsDBM(dbm: DBM, opt: CommonDaoSaveOptions = {}): Promise<DBM> {
|
|
607
|
+
async saveAsDBM(dbm: DBM, opt: CommonDaoSaveOptions<DBM> = {}): Promise<DBM> {
|
|
599
608
|
this.requireWriteAccess()
|
|
600
609
|
const table = opt.table || this.cfg.table
|
|
601
610
|
|
|
@@ -617,7 +626,7 @@ export class CommonDao<
|
|
|
617
626
|
return dbm
|
|
618
627
|
}
|
|
619
628
|
|
|
620
|
-
async saveBatch(bms: BM[], opt: CommonDaoSaveOptions = {}): Promise<Saved<BM>[]> {
|
|
629
|
+
async saveBatch(bms: BM[], opt: CommonDaoSaveOptions<DBM> = {}): Promise<Saved<BM>[]> {
|
|
621
630
|
this.requireWriteAccess()
|
|
622
631
|
const table = opt.table || this.cfg.table
|
|
623
632
|
bms.forEach(bm => this.assignIdCreatedUpdated(bm, opt))
|
|
@@ -642,7 +651,7 @@ export class CommonDao<
|
|
|
642
651
|
return bms as any[]
|
|
643
652
|
}
|
|
644
653
|
|
|
645
|
-
async saveBatchAsDBM(dbms: DBM[], opt: CommonDaoSaveOptions = {}): Promise<DBM[]> {
|
|
654
|
+
async saveBatchAsDBM(dbms: DBM[], opt: CommonDaoSaveOptions<DBM> = {}): Promise<DBM[]> {
|
|
646
655
|
this.requireWriteAccess()
|
|
647
656
|
const table = opt.table || this.cfg.table
|
|
648
657
|
if (!opt.raw) {
|
|
@@ -724,7 +733,8 @@ export class CommonDao<
|
|
|
724
733
|
const bm = await this.cfg.hooks!.beforeDBMToBM!(dbm)
|
|
725
734
|
|
|
726
735
|
// Validate/convert BM
|
|
727
|
-
return
|
|
736
|
+
// eslint-disable-next-line @typescript-eslint/return-await
|
|
737
|
+
return this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
728
738
|
}
|
|
729
739
|
|
|
730
740
|
async dbmsToBM(dbms: DBM[], opt: CommonDaoOptions = {}): Promise<Saved<BM>[]> {
|
|
@@ -752,7 +762,8 @@ export class CommonDao<
|
|
|
752
762
|
const dbm = { ...(await this.cfg.hooks!.beforeBMToDBM!(bm)) }
|
|
753
763
|
|
|
754
764
|
// Validate/convert DBM
|
|
755
|
-
return
|
|
765
|
+
// eslint-disable-next-line @typescript-eslint/return-await
|
|
766
|
+
return this.validateAndConvert(dbm, this.cfg.dbmSchema, DBModelType.DBM, opt)
|
|
756
767
|
}
|
|
757
768
|
|
|
758
769
|
async bmsToDBM(bms: BM[], opt: CommonDaoOptions = {}): Promise<DBM[]> {
|
package/src/db.model.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { objectSchema, stringSchema, unixTimestampSchema } from '@naturalcycles/nodejs-lib'
|
|
1
|
+
import { AnyObject } from '@naturalcycles/js-lib'
|
|
3
2
|
import { CommonDB } from './common.db'
|
|
4
3
|
|
|
5
4
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
@@ -8,8 +7,9 @@ export interface CommonDBOptions {}
|
|
|
8
7
|
/**
|
|
9
8
|
* All properties default to undefined.
|
|
10
9
|
*/
|
|
11
|
-
export interface CommonDBSaveOptions extends
|
|
12
|
-
|
|
10
|
+
export interface CommonDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId>
|
|
11
|
+
extends CommonDBOptions {
|
|
12
|
+
excludeFromIndexes?: (keyof ROW)[]
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export type CommonDBStreamOptions = CommonDBOptions
|
|
@@ -29,7 +29,7 @@ export interface RunQueryResult<T> {
|
|
|
29
29
|
|
|
30
30
|
export type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation
|
|
31
31
|
|
|
32
|
-
export interface DBSaveBatchOperation<ROW extends ObjectWithId =
|
|
32
|
+
export interface DBSaveBatchOperation<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
33
33
|
type: 'saveBatch'
|
|
34
34
|
table: string
|
|
35
35
|
rows: ROW[]
|
|
@@ -61,46 +61,11 @@ export interface CreatedUpdatedId extends CreatedUpdated {
|
|
|
61
61
|
id: string
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
export interface CreatedUpdatedVer {
|
|
65
|
-
created: number
|
|
66
|
-
updated: number
|
|
67
|
-
_ver?: number
|
|
68
|
-
}
|
|
69
|
-
|
|
70
64
|
export interface ObjectWithId {
|
|
71
65
|
id: string
|
|
72
66
|
}
|
|
73
67
|
|
|
74
|
-
export interface
|
|
75
|
-
id?: string
|
|
76
|
-
created?: number
|
|
77
|
-
updated?: number
|
|
78
|
-
// _ver?: number
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export interface SavedDBEntity {
|
|
82
|
-
id: string
|
|
83
|
-
created: number
|
|
84
|
-
updated: number
|
|
85
|
-
// _ver?: number
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export type Saved<E> = Merge<E, SavedDBEntity>
|
|
89
|
-
export type Unsaved<E> = Merge<E, BaseDBEntity>
|
|
90
|
-
|
|
91
|
-
export const baseDBEntitySchema = objectSchema<BaseDBEntity>({
|
|
92
|
-
id: stringSchema.optional(),
|
|
93
|
-
created: unixTimestampSchema.optional(),
|
|
94
|
-
updated: unixTimestampSchema.optional(),
|
|
95
|
-
// _ver: verSchema.optional(),
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
export const savedDBEntitySchema = objectSchema<SavedDBEntity>({
|
|
99
|
-
id: stringSchema,
|
|
100
|
-
created: unixTimestampSchema,
|
|
101
|
-
updated: unixTimestampSchema,
|
|
102
|
-
// _ver: verSchema.optional(),
|
|
103
|
-
})
|
|
68
|
+
export interface AnyObjectWithId extends AnyObject, ObjectWithId {}
|
|
104
69
|
|
|
105
70
|
/**
|
|
106
71
|
* Interface for a module (lib) that implements CommonDB.
|
package/src/index.ts
CHANGED
|
@@ -24,8 +24,7 @@ import {
|
|
|
24
24
|
CommonDaoStreamOptions,
|
|
25
25
|
} from './commondao/common.dao.model'
|
|
26
26
|
import {
|
|
27
|
-
|
|
28
|
-
baseDBEntitySchema,
|
|
27
|
+
AnyObjectWithId,
|
|
29
28
|
CommonDBAdapter,
|
|
30
29
|
CommonDBCreateOptions,
|
|
31
30
|
CommonDBOptions,
|
|
@@ -33,7 +32,6 @@ import {
|
|
|
33
32
|
CommonDBStreamOptions,
|
|
34
33
|
CreatedUpdated,
|
|
35
34
|
CreatedUpdatedId,
|
|
36
|
-
CreatedUpdatedVer,
|
|
37
35
|
DBDeleteByIdsOperation,
|
|
38
36
|
DBModelType,
|
|
39
37
|
DBOperation,
|
|
@@ -41,10 +39,6 @@ import {
|
|
|
41
39
|
DBSaveBatchOperation,
|
|
42
40
|
ObjectWithId,
|
|
43
41
|
RunQueryResult,
|
|
44
|
-
Saved,
|
|
45
|
-
SavedDBEntity,
|
|
46
|
-
savedDBEntitySchema,
|
|
47
|
-
Unsaved,
|
|
48
42
|
} from './db.model'
|
|
49
43
|
import { getDB } from './getDB'
|
|
50
44
|
import { CommonKeyValueDao, CommonKeyValueDaoCfg } from './kv/commonKeyValueDao'
|
|
@@ -87,12 +81,8 @@ export type {
|
|
|
87
81
|
RunQueryResult,
|
|
88
82
|
CreatedUpdated,
|
|
89
83
|
CreatedUpdatedId,
|
|
90
|
-
|
|
84
|
+
AnyObjectWithId,
|
|
91
85
|
ObjectWithId,
|
|
92
|
-
BaseDBEntity,
|
|
93
|
-
SavedDBEntity,
|
|
94
|
-
Saved,
|
|
95
|
-
Unsaved,
|
|
96
86
|
CommonDaoCfg,
|
|
97
87
|
CommonDaoCreateIdHook,
|
|
98
88
|
CommonDaoParseNaturalIdHook,
|
|
@@ -124,8 +114,6 @@ export {
|
|
|
124
114
|
CommonDaoLogLevel,
|
|
125
115
|
DBRelation,
|
|
126
116
|
DBModelType,
|
|
127
|
-
baseDBEntitySchema,
|
|
128
|
-
savedDBEntitySchema,
|
|
129
117
|
CommonDao,
|
|
130
118
|
createdUpdatedFields,
|
|
131
119
|
createdUpdatedIdFields,
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
_hb,
|
|
7
7
|
_mapValues,
|
|
8
8
|
_passthroughMapper,
|
|
9
|
+
SavedDBEntity,
|
|
9
10
|
} from '@naturalcycles/js-lib'
|
|
10
11
|
import {
|
|
11
12
|
NDJsonStats,
|
|
@@ -26,7 +27,7 @@ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dis
|
|
|
26
27
|
import { dayjs } from '@naturalcycles/time-lib'
|
|
27
28
|
import * as fs from 'fs-extra'
|
|
28
29
|
import { CommonDB } from '../common.db'
|
|
29
|
-
import { CommonDBSaveOptions
|
|
30
|
+
import { CommonDBSaveOptions } from '../index'
|
|
30
31
|
|
|
31
32
|
export interface DBPipelineRestoreOptions extends TransformLogProgressOptions {
|
|
32
33
|
/**
|
package/src/query/dbQuery.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { AsyncMapper, _truncate } from '@naturalcycles/js-lib'
|
|
1
|
+
import { AsyncMapper, _truncate, Saved } from '@naturalcycles/js-lib'
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
3
3
|
import { CommonDaoOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions } from '..'
|
|
4
4
|
import { CommonDao } from '../commondao/common.dao'
|
|
5
|
-
import { ObjectWithId, RunQueryResult
|
|
5
|
+
import { AnyObjectWithId, ObjectWithId, RunQueryResult } from '../db.model'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Modeled after Firestore operators (WhereFilterOp type)
|
|
@@ -46,29 +46,17 @@ export const dbQueryFilterOperatorValues = [
|
|
|
46
46
|
'array-contains-any',
|
|
47
47
|
]
|
|
48
48
|
|
|
49
|
-
export interface DBQueryFilter {
|
|
50
|
-
name:
|
|
49
|
+
export interface DBQueryFilter<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
50
|
+
name: keyof ROW
|
|
51
51
|
op: DBQueryFilterOperator
|
|
52
52
|
val: any
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
export interface DBQueryOrder {
|
|
56
|
-
name:
|
|
55
|
+
export interface DBQueryOrder<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
56
|
+
name: keyof ROW
|
|
57
57
|
descending?: boolean
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
// export interface DBQueryData {
|
|
61
|
-
// _filters: DBQueryFilter[]
|
|
62
|
-
// _limitValue: number
|
|
63
|
-
// _orders: DBQueryOrder[]
|
|
64
|
-
//
|
|
65
|
-
// /**
|
|
66
|
-
// * If defined - only those fields will be selected.
|
|
67
|
-
// * In undefined - all fields (*) will be returned.
|
|
68
|
-
// */
|
|
69
|
-
// _selectedFieldNames?: string[]
|
|
70
|
-
// }
|
|
71
|
-
|
|
72
60
|
/**
|
|
73
61
|
* Lowest Common Denominator Query object.
|
|
74
62
|
* To be executed by CommonDao / CommonDB.
|
|
@@ -79,26 +67,26 @@ export interface DBQueryOrder {
|
|
|
79
67
|
*
|
|
80
68
|
* <DBM> is the type of **queried** object (so e.g `key of DBM` can be used), not **returned** object.
|
|
81
69
|
*/
|
|
82
|
-
export class DBQuery<ROW extends ObjectWithId> {
|
|
70
|
+
export class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
83
71
|
constructor(public table: string) {}
|
|
84
72
|
|
|
85
73
|
/**
|
|
86
74
|
* Convenience method.
|
|
87
75
|
*/
|
|
88
|
-
static create<ROW extends ObjectWithId =
|
|
76
|
+
static create<ROW extends ObjectWithId = AnyObjectWithId>(table: string): DBQuery<ROW> {
|
|
89
77
|
return new DBQuery(table)
|
|
90
78
|
}
|
|
91
79
|
|
|
92
|
-
static fromPlainObject<ROW extends ObjectWithId =
|
|
80
|
+
static fromPlainObject<ROW extends ObjectWithId = AnyObjectWithId>(
|
|
93
81
|
obj: Partial<DBQuery<ROW>> & { table: string },
|
|
94
82
|
): DBQuery<ROW> {
|
|
95
83
|
return Object.assign(new DBQuery<ROW>(obj.table), obj)
|
|
96
84
|
}
|
|
97
85
|
|
|
98
|
-
_filters: DBQueryFilter[] = []
|
|
86
|
+
_filters: DBQueryFilter<ROW>[] = []
|
|
99
87
|
_limitValue = 0 // 0 means "no limit"
|
|
100
88
|
_offsetValue = 0 // 0 means "no offset"
|
|
101
|
-
_orders: DBQueryOrder[] = []
|
|
89
|
+
_orders: DBQueryOrder<ROW>[] = []
|
|
102
90
|
|
|
103
91
|
_startCursor?: string
|
|
104
92
|
_endCursor?: string
|
|
@@ -107,14 +95,14 @@ export class DBQuery<ROW extends ObjectWithId> {
|
|
|
107
95
|
* If defined - only those fields will be selected.
|
|
108
96
|
* In undefined - all fields (*) will be returned.
|
|
109
97
|
*/
|
|
110
|
-
_selectedFieldNames?:
|
|
98
|
+
_selectedFieldNames?: (keyof ROW)[]
|
|
111
99
|
|
|
112
|
-
filter(name:
|
|
100
|
+
filter(name: keyof ROW, op: DBQueryFilterOperator, val: any): this {
|
|
113
101
|
this._filters.push({ name, op, val })
|
|
114
102
|
return this
|
|
115
103
|
}
|
|
116
104
|
|
|
117
|
-
filterEq(name:
|
|
105
|
+
filterEq(name: keyof ROW, val: any): this {
|
|
118
106
|
this._filters.push({ name, op: '==', val })
|
|
119
107
|
return this
|
|
120
108
|
}
|
|
@@ -129,7 +117,7 @@ export class DBQuery<ROW extends ObjectWithId> {
|
|
|
129
117
|
return this
|
|
130
118
|
}
|
|
131
119
|
|
|
132
|
-
order(name:
|
|
120
|
+
order(name: keyof ROW, descending?: boolean): this {
|
|
133
121
|
this._orders.push({
|
|
134
122
|
name,
|
|
135
123
|
descending,
|
|
@@ -137,7 +125,7 @@ export class DBQuery<ROW extends ObjectWithId> {
|
|
|
137
125
|
return this
|
|
138
126
|
}
|
|
139
127
|
|
|
140
|
-
select(fieldNames:
|
|
128
|
+
select(fieldNames: (keyof ROW)[]): this {
|
|
141
129
|
this._selectedFieldNames = fieldNames
|
|
142
130
|
return this
|
|
143
131
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { jsonSchema, JsonSchemaObject, _range } from '@naturalcycles/js-lib'
|
|
1
|
+
import { jsonSchema, JsonSchemaObject, _range, BaseDBEntity, Saved } from '@naturalcycles/js-lib'
|
|
2
2
|
import {
|
|
3
|
+
baseDBEntitySchema,
|
|
3
4
|
binarySchema,
|
|
4
5
|
booleanSchema,
|
|
5
6
|
numberSchema,
|
|
6
7
|
objectSchema,
|
|
8
|
+
savedDBEntitySchema,
|
|
7
9
|
stringSchema,
|
|
8
10
|
} from '@naturalcycles/nodejs-lib'
|
|
9
|
-
import { BaseDBEntity, baseDBEntitySchema, Saved, savedDBEntitySchema } from '../db.model'
|
|
10
11
|
|
|
11
12
|
const MOCK_TS_2018_06_21 = 1529539200
|
|
12
13
|
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
CommonTimeSeriesDaoCfg,
|
|
6
6
|
TimeSeriesDataPoint,
|
|
7
7
|
TimeSeriesQuery,
|
|
8
|
+
TimeSeriesRow,
|
|
8
9
|
TimeSeriesSaveBatchOp,
|
|
9
10
|
} from './timeSeries.model'
|
|
10
11
|
|
|
@@ -84,7 +85,7 @@ export class CommonTimeSeriesDao {
|
|
|
84
85
|
if (q.fromIncl) dbq.filter('ts', '>=', q.fromIncl)
|
|
85
86
|
if (q.toExcl) dbq.filter('ts', '<', q.toExcl)
|
|
86
87
|
|
|
87
|
-
const
|
|
88
|
+
const rows = (await this.cfg.db.runQuery(dbq)).rows as any as TimeSeriesRow[]
|
|
88
89
|
|
|
89
90
|
// todo: query from aggregated tables when step is above 'hour'
|
|
90
91
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CommonDB } from '../common.db'
|
|
2
|
-
import type { CommonDBSaveOptions, DBOperation, ObjectWithId } from '../db.model'
|
|
2
|
+
import type { AnyObjectWithId, CommonDBSaveOptions, DBOperation, ObjectWithId } from '../db.model'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Convenience class that stores the list of DBOperations and provides a fluent API to add them.
|
|
@@ -7,7 +7,7 @@ import type { CommonDBSaveOptions, DBOperation, ObjectWithId } from '../db.model
|
|
|
7
7
|
export class DBTransaction {
|
|
8
8
|
public ops: DBOperation[] = []
|
|
9
9
|
|
|
10
|
-
saveBatch<ROW extends ObjectWithId =
|
|
10
|
+
saveBatch<ROW extends ObjectWithId = AnyObjectWithId>(table: string, rows: ROW[]): this {
|
|
11
11
|
this.ops.push({
|
|
12
12
|
type: 'saveBatch',
|
|
13
13
|
table,
|
|
@@ -35,7 +35,7 @@ export class RunnableDBTransaction extends DBTransaction {
|
|
|
35
35
|
super()
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
async commit(opt?: CommonDBSaveOptions): Promise<void> {
|
|
38
|
+
async commit<ROW extends ObjectWithId>(opt?: CommonDBSaveOptions<ROW>): Promise<void> {
|
|
39
39
|
await this.db.commitTransaction(this, opt)
|
|
40
40
|
}
|
|
41
41
|
}
|