@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
package/dist/query/dbQuery.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyObjectWithId, ObjectWithId, AsyncMapper, Saved
|
|
1
|
+
import { AnyObjectWithId, ObjectWithId, AsyncMapper, Saved } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDaoOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, DBPatch } from '..';
|
|
4
4
|
import { CommonDao } from '../commondao/common.dao';
|
|
@@ -24,12 +24,12 @@ import { RunQueryResult } from '../db.model';
|
|
|
24
24
|
*/
|
|
25
25
|
export type DBQueryFilterOperator = '<' | '<=' | '==' | '!=' | '>=' | '>' | 'in' | 'not-in' | 'array-contains' | 'array-contains-any';
|
|
26
26
|
export declare const dbQueryFilterOperatorValues: DBQueryFilterOperator[];
|
|
27
|
-
export interface DBQueryFilter<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
27
|
+
export interface DBQueryFilter<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
28
28
|
name: keyof ROW;
|
|
29
29
|
op: DBQueryFilterOperator;
|
|
30
30
|
val: any;
|
|
31
31
|
}
|
|
32
|
-
export interface DBQueryOrder<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
32
|
+
export interface DBQueryOrder<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
33
33
|
name: keyof ROW;
|
|
34
34
|
descending?: boolean;
|
|
35
35
|
}
|
|
@@ -43,14 +43,14 @@ export interface DBQueryOrder<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
|
43
43
|
*
|
|
44
44
|
* <DBM> is the type of **queried** object (so e.g `key of DBM` can be used), not **returned** object.
|
|
45
45
|
*/
|
|
46
|
-
export declare class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
46
|
+
export declare class DBQuery<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
47
47
|
table: string;
|
|
48
48
|
constructor(table: string);
|
|
49
49
|
/**
|
|
50
50
|
* Convenience method.
|
|
51
51
|
*/
|
|
52
52
|
static create<ROW extends ObjectWithId = AnyObjectWithId>(table: string): DBQuery<ROW>;
|
|
53
|
-
static fromPlainObject<ROW extends ObjectWithId = AnyObjectWithId>(obj: Partial<DBQuery<ROW>> & {
|
|
53
|
+
static fromPlainObject<ROW extends Partial<ObjectWithId> = AnyObjectWithId>(obj: Partial<DBQuery<ROW>> & {
|
|
54
54
|
table: string;
|
|
55
55
|
}): DBQuery<ROW>;
|
|
56
56
|
_filters: DBQueryFilter<ROW>[];
|
|
@@ -84,19 +84,17 @@ export declare class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
|
84
84
|
/**
|
|
85
85
|
* DBQuery that has additional method to support Fluent API style.
|
|
86
86
|
*/
|
|
87
|
-
export declare class RunnableDBQuery<BM extends Partial<ObjectWithId<ID>>, DBM extends ObjectWithId<ID
|
|
88
|
-
dao: CommonDao<BM, DBM,
|
|
87
|
+
export declare class RunnableDBQuery<BM extends Partial<ObjectWithId<ID>>, DBM extends Partial<ObjectWithId<ID>> = BM, ID extends string | number = NonNullable<BM['id']>> extends DBQuery<DBM> {
|
|
88
|
+
dao: CommonDao<BM, DBM, ID>;
|
|
89
89
|
/**
|
|
90
90
|
* Pass `table` to override table.
|
|
91
91
|
*/
|
|
92
|
-
constructor(dao: CommonDao<BM, DBM,
|
|
92
|
+
constructor(dao: CommonDao<BM, DBM, ID>, table?: string);
|
|
93
93
|
runQuery(opt?: CommonDaoOptions): Promise<Saved<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<Saved<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<Saved<BM>, void>, opt?: CommonDaoStreamForEachOptions<Saved<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
|
@@ -13,7 +13,6 @@ function runCommonDaoTest(db, features = {}, quirks = {}) {
|
|
|
13
13
|
db,
|
|
14
14
|
dbmSchema: test_model_1.testItemDBMSchema,
|
|
15
15
|
bmSchema: test_model_1.testItemBMSchema,
|
|
16
|
-
tmSchema: test_model_1.testItemTMSchema,
|
|
17
16
|
logStarted: true,
|
|
18
17
|
logLevel: __1.CommonDaoLogLevel.DATA_FULL,
|
|
19
18
|
});
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Readable } from 'node:stream'
|
|
2
2
|
import {
|
|
3
|
+
_isTruthy,
|
|
3
4
|
JsonSchemaObject,
|
|
4
5
|
JsonSchemaRootObject,
|
|
5
6
|
ObjectWithId,
|
|
@@ -52,13 +53,13 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
52
53
|
return await this.cfg.downstreamDB.getTables()
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
override async getTableSchema<ROW extends ObjectWithId
|
|
56
|
+
override async getTableSchema<ROW extends Partial<ObjectWithId>>(
|
|
56
57
|
table: string,
|
|
57
58
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
58
59
|
return await this.cfg.downstreamDB.getTableSchema<ROW>(table)
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
override async createTable<ROW extends ObjectWithId
|
|
62
|
+
override async createTable<ROW extends Partial<ObjectWithId>>(
|
|
62
63
|
table: string,
|
|
63
64
|
schema: JsonSchemaObject<ROW>,
|
|
64
65
|
opt: CacheDBCreateOptions = {},
|
|
@@ -72,7 +73,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
override async getByIds<ROW extends ObjectWithId
|
|
76
|
+
override async getByIds<ROW extends Partial<ObjectWithId>>(
|
|
76
77
|
table: string,
|
|
77
78
|
ids: ROW['id'][],
|
|
78
79
|
opt: CacheDBSaveOptions<ROW> = {},
|
|
@@ -83,9 +84,9 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
83
84
|
if (!opt.skipCache && !this.cfg.skipCache) {
|
|
84
85
|
const results = await this.cfg.cacheDB.getByIds<ROW>(table, ids, opt)
|
|
85
86
|
|
|
86
|
-
results.forEach(r => (resultMap[r.id] = r))
|
|
87
|
+
results.forEach(r => (resultMap[r.id!] = r))
|
|
87
88
|
|
|
88
|
-
missingIds.push(...ids.filter(id => !resultMap[id]))
|
|
89
|
+
missingIds.push(...ids.filter(id => !resultMap[id!]))
|
|
89
90
|
|
|
90
91
|
if (this.cfg.logCached) {
|
|
91
92
|
this.cfg.logger?.log(
|
|
@@ -98,7 +99,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
98
99
|
|
|
99
100
|
if (missingIds.length && !opt.onlyCache && !this.cfg.onlyCache) {
|
|
100
101
|
const results = await this.cfg.downstreamDB.getByIds<ROW>(table, missingIds, opt)
|
|
101
|
-
results.forEach(r => (resultMap[r.id] = r))
|
|
102
|
+
results.forEach(r => (resultMap[r.id!] = r))
|
|
102
103
|
|
|
103
104
|
if (this.cfg.logDownstream) {
|
|
104
105
|
this.cfg.logger?.log(
|
|
@@ -115,7 +116,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
115
116
|
}
|
|
116
117
|
|
|
117
118
|
// return in right order
|
|
118
|
-
return ids.map(id => resultMap[id]
|
|
119
|
+
return ids.map(id => resultMap[id!]).filter(_isTruthy)
|
|
119
120
|
}
|
|
120
121
|
|
|
121
122
|
override async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
@@ -147,7 +148,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
147
148
|
}
|
|
148
149
|
}
|
|
149
150
|
|
|
150
|
-
override async runQuery<ROW extends ObjectWithId
|
|
151
|
+
override async runQuery<ROW extends Partial<ObjectWithId>>(
|
|
151
152
|
q: DBQuery<ROW>,
|
|
152
153
|
opt: CacheDBSaveOptions<ROW> = {},
|
|
153
154
|
): Promise<RunQueryResult<ROW>> {
|
|
@@ -177,7 +178,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
177
178
|
return { rows, ...queryResult }
|
|
178
179
|
}
|
|
179
180
|
|
|
180
|
-
override async runQueryCount<ROW extends ObjectWithId
|
|
181
|
+
override async runQueryCount<ROW extends Partial<ObjectWithId>>(
|
|
181
182
|
q: DBQuery<ROW>,
|
|
182
183
|
opt: CacheDBOptions = {},
|
|
183
184
|
): Promise<number> {
|
|
@@ -194,7 +195,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
194
195
|
return count
|
|
195
196
|
}
|
|
196
197
|
|
|
197
|
-
override streamQuery<ROW extends ObjectWithId
|
|
198
|
+
override streamQuery<ROW extends Partial<ObjectWithId>>(
|
|
198
199
|
q: DBQuery<ROW>,
|
|
199
200
|
opt: CacheDBStreamOptions = {},
|
|
200
201
|
): Readable {
|
|
@@ -233,7 +234,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
233
234
|
return stream
|
|
234
235
|
}
|
|
235
236
|
|
|
236
|
-
override async deleteByQuery<ROW extends ObjectWithId
|
|
237
|
+
override async deleteByQuery<ROW extends Partial<ObjectWithId>>(
|
|
237
238
|
q: DBQuery<ROW>,
|
|
238
239
|
opt: CacheDBOptions = {},
|
|
239
240
|
): Promise<number> {
|
|
@@ -265,7 +266,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
265
266
|
return deletedIds
|
|
266
267
|
}
|
|
267
268
|
|
|
268
|
-
override async updateByQuery<ROW extends ObjectWithId
|
|
269
|
+
override async updateByQuery<ROW extends Partial<ObjectWithId>>(
|
|
269
270
|
q: DBQuery<ROW>,
|
|
270
271
|
patch: DBPatch<ROW>,
|
|
271
272
|
opt: CacheDBOptions = {},
|
|
@@ -5,7 +5,7 @@ import type { DBQueryOrder } from '../../query/dbQuery'
|
|
|
5
5
|
export interface FileDBPersistencePlugin {
|
|
6
6
|
ping(): Promise<void>
|
|
7
7
|
getTables(): Promise<string[]>
|
|
8
|
-
loadFile<ROW extends ObjectWithId
|
|
8
|
+
loadFile<ROW extends Partial<ObjectWithId>>(table: string): Promise<ROW[]>
|
|
9
9
|
saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void>
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -65,13 +65,13 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
65
65
|
return tables
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
override async getByIds<ROW extends ObjectWithId
|
|
68
|
+
override async getByIds<ROW extends Partial<ObjectWithId>>(
|
|
69
69
|
table: string,
|
|
70
70
|
ids: ROW['id'][],
|
|
71
71
|
_opt?: CommonDBOptions,
|
|
72
72
|
): Promise<ROW[]> {
|
|
73
73
|
const byId = _by(await this.loadFile<ROW>(table), r => r.id)
|
|
74
|
-
return ids.map(id => byId[id]!).filter(Boolean)
|
|
74
|
+
return ids.map(id => byId[id!]!).filter(Boolean)
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
override async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
@@ -115,7 +115,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
115
115
|
await pMap(
|
|
116
116
|
tables,
|
|
117
117
|
async table => {
|
|
118
|
-
const rows = await this.loadFile(table)
|
|
118
|
+
const rows: ObjectWithId[] = await this.loadFile(table)
|
|
119
119
|
data[table] = _by(rows, r => r.id)
|
|
120
120
|
},
|
|
121
121
|
{ concurrency: 16 },
|
|
@@ -168,7 +168,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
override async runQuery<ROW extends ObjectWithId
|
|
171
|
+
override async runQuery<ROW extends Partial<ObjectWithId>>(
|
|
172
172
|
q: DBQuery<ROW>,
|
|
173
173
|
_opt?: CommonDBOptions,
|
|
174
174
|
): Promise<RunQueryResult<ROW>> {
|
|
@@ -177,14 +177,14 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
177
177
|
}
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
override async runQueryCount<ROW extends ObjectWithId
|
|
180
|
+
override async runQueryCount<ROW extends Partial<ObjectWithId>>(
|
|
181
181
|
q: DBQuery<ROW>,
|
|
182
182
|
_opt?: CommonDBOptions,
|
|
183
183
|
): Promise<number> {
|
|
184
184
|
return (await this.loadFile(q.table)).length
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
override streamQuery<ROW extends ObjectWithId
|
|
187
|
+
override streamQuery<ROW extends Partial<ObjectWithId>>(
|
|
188
188
|
q: DBQuery<ROW>,
|
|
189
189
|
opt?: CommonDBStreamOptions,
|
|
190
190
|
): ReadableTyped<ROW> {
|
|
@@ -198,7 +198,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
198
198
|
return readable
|
|
199
199
|
}
|
|
200
200
|
|
|
201
|
-
override async deleteByQuery<ROW extends ObjectWithId
|
|
201
|
+
override async deleteByQuery<ROW extends Partial<ObjectWithId>>(
|
|
202
202
|
q: DBQuery<ROW>,
|
|
203
203
|
_opt?: CommonDBOptions,
|
|
204
204
|
): Promise<number> {
|
|
@@ -206,7 +206,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
206
206
|
|
|
207
207
|
let deleted = 0
|
|
208
208
|
queryInMemory(q, _stringMapValues(byId)).forEach(r => {
|
|
209
|
-
delete byId[r.id]
|
|
209
|
+
delete byId[r.id!]
|
|
210
210
|
deleted++
|
|
211
211
|
})
|
|
212
212
|
|
|
@@ -217,7 +217,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
217
217
|
return deleted
|
|
218
218
|
}
|
|
219
219
|
|
|
220
|
-
override async getTableSchema<ROW extends ObjectWithId
|
|
220
|
+
override async getTableSchema<ROW extends Partial<ObjectWithId>>(
|
|
221
221
|
table: string,
|
|
222
222
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
223
223
|
const rows = await this.loadFile(table)
|
|
@@ -228,7 +228,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
228
228
|
}
|
|
229
229
|
|
|
230
230
|
// wrapper, to handle logging
|
|
231
|
-
async loadFile<ROW extends ObjectWithId
|
|
231
|
+
async loadFile<ROW extends Partial<ObjectWithId>>(table: string): Promise<ROW[]> {
|
|
232
232
|
const started = this.logStarted(`loadFile(${table})`)
|
|
233
233
|
const rows = await this.cfg.plugin.loadFile<ROW>(table)
|
|
234
234
|
this.logFinished(started, `loadFile(${table}) ${rows.length} row(s)`)
|
|
@@ -236,7 +236,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
236
236
|
}
|
|
237
237
|
|
|
238
238
|
// wrapper, to handle logging, sorting rows before saving
|
|
239
|
-
async saveFile<ROW extends ObjectWithId
|
|
239
|
+
async saveFile<ROW extends Partial<ObjectWithId>>(table: string, _rows: ROW[]): Promise<void> {
|
|
240
240
|
// if (!_rows.length) return // NO, it should be able to save file with 0 rows!
|
|
241
241
|
|
|
242
242
|
// Sort the rows, if needed
|
|
@@ -257,7 +257,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
257
257
|
this.logFinished(started, op)
|
|
258
258
|
}
|
|
259
259
|
|
|
260
|
-
private sortRows<ROW extends ObjectWithId
|
|
260
|
+
private sortRows<ROW extends Partial<ObjectWithId>>(rows: ROW[]): ROW[] {
|
|
261
261
|
rows = rows.map(r => _filterUndefinedValues(r))
|
|
262
262
|
|
|
263
263
|
if (this.cfg.sortOnSave) {
|
|
@@ -14,7 +14,7 @@ export class InMemoryPersistencePlugin implements FileDBPersistencePlugin {
|
|
|
14
14
|
return Object.keys(this.data)
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
async loadFile<ROW extends ObjectWithId
|
|
17
|
+
async loadFile<ROW extends Partial<ObjectWithId>>(table: string): Promise<ROW[]> {
|
|
18
18
|
return Object.values(this.data[table] || ({} as any))
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -46,7 +46,7 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
|
|
|
46
46
|
.map(f => f.split('.ndjson')[0]!)
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
async loadFile<ROW extends ObjectWithId
|
|
49
|
+
async loadFile<ROW extends Partial<ObjectWithId>>(table: string): Promise<ROW[]> {
|
|
50
50
|
await fs.ensureDir(this.cfg.storagePath)
|
|
51
51
|
const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
|
|
52
52
|
const filePath = `${this.cfg.storagePath}/${table}.${ext}`
|
|
@@ -9,7 +9,7 @@ export class NoopPersistencePlugin implements FileDBPersistencePlugin {
|
|
|
9
9
|
return []
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
async loadFile<ROW extends ObjectWithId
|
|
12
|
+
async loadFile<ROW extends Partial<ObjectWithId>>(_table: string): Promise<ROW[]> {
|
|
13
13
|
return []
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -120,7 +120,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
120
120
|
return Object.keys(this.data).filter(t => t.startsWith(this.cfg.tablesPrefix))
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
async getTableSchema<ROW extends ObjectWithId
|
|
123
|
+
async getTableSchema<ROW extends Partial<ObjectWithId>>(
|
|
124
124
|
_table: string,
|
|
125
125
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
126
126
|
const table = this.cfg.tablesPrefix + _table
|
|
@@ -130,7 +130,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
async createTable<ROW extends ObjectWithId
|
|
133
|
+
async createTable<ROW extends Partial<ObjectWithId>>(
|
|
134
134
|
_table: string,
|
|
135
135
|
_schema: JsonSchemaObject<ROW>,
|
|
136
136
|
opt: CommonDBCreateOptions = {},
|
|
@@ -143,14 +143,14 @@ export class InMemoryDB implements CommonDB {
|
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
async getByIds<ROW extends ObjectWithId
|
|
146
|
+
async getByIds<ROW extends Partial<ObjectWithId>>(
|
|
147
147
|
_table: string,
|
|
148
148
|
ids: ROW['id'][],
|
|
149
149
|
_opt?: CommonDBOptions,
|
|
150
150
|
): Promise<ROW[]> {
|
|
151
151
|
const table = this.cfg.tablesPrefix + _table
|
|
152
152
|
this.data[table] ||= {}
|
|
153
|
-
return ids.map(id => this.data[table]![id] as ROW).filter(Boolean)
|
|
153
|
+
return ids.map(id => this.data[table]![id!] as ROW).filter(Boolean)
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
@@ -184,7 +184,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
184
184
|
})
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
async deleteByQuery<ROW extends ObjectWithId
|
|
187
|
+
async deleteByQuery<ROW extends Partial<ObjectWithId>>(
|
|
188
188
|
q: DBQuery<ROW>,
|
|
189
189
|
_opt?: CommonDBOptions,
|
|
190
190
|
): Promise<number> {
|
|
@@ -192,14 +192,14 @@ export class InMemoryDB implements CommonDB {
|
|
|
192
192
|
this.data[table] ||= {}
|
|
193
193
|
let count = 0
|
|
194
194
|
queryInMemory(q, Object.values(this.data[table] || {}) as ROW[]).forEach(r => {
|
|
195
|
-
if (!this.data[table]![r.id]) return
|
|
196
|
-
delete this.data[table]![r.id]
|
|
195
|
+
if (!this.data[table]![r.id!]) return
|
|
196
|
+
delete this.data[table]![r.id!]
|
|
197
197
|
count++
|
|
198
198
|
})
|
|
199
199
|
return count
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
async updateByQuery<ROW extends ObjectWithId
|
|
202
|
+
async updateByQuery<ROW extends Partial<ObjectWithId>>(
|
|
203
203
|
q: DBQuery<ROW>,
|
|
204
204
|
patch: DBPatch<ROW>,
|
|
205
205
|
): Promise<number> {
|
|
@@ -221,7 +221,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
221
221
|
return rows.length
|
|
222
222
|
}
|
|
223
223
|
|
|
224
|
-
async runQuery<ROW extends ObjectWithId
|
|
224
|
+
async runQuery<ROW extends Partial<ObjectWithId>>(
|
|
225
225
|
q: DBQuery<ROW>,
|
|
226
226
|
_opt?: CommonDBOptions,
|
|
227
227
|
): Promise<RunQueryResult<ROW>> {
|
|
@@ -229,7 +229,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
229
229
|
return { rows: queryInMemory(q, Object.values(this.data[table] || {}) as ROW[]) }
|
|
230
230
|
}
|
|
231
231
|
|
|
232
|
-
async runQueryCount<ROW extends ObjectWithId
|
|
232
|
+
async runQueryCount<ROW extends Partial<ObjectWithId>>(
|
|
233
233
|
q: DBQuery<ROW>,
|
|
234
234
|
_opt?: CommonDBOptions,
|
|
235
235
|
): Promise<number> {
|
|
@@ -237,7 +237,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
237
237
|
return queryInMemory<any>(q, Object.values(this.data[table] || {})).length
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
-
streamQuery<ROW extends ObjectWithId
|
|
240
|
+
streamQuery<ROW extends Partial<ObjectWithId>>(
|
|
241
241
|
q: DBQuery<ROW>,
|
|
242
242
|
_opt?: CommonDBOptions,
|
|
243
243
|
): ReadableTyped<ROW> {
|
|
@@ -18,7 +18,10 @@ const FILTER_FNS: Record<DBQueryFilterOperator, FilterFn> = {
|
|
|
18
18
|
|
|
19
19
|
// Important: q.table is not used in this function, so tablesPrefix is not needed.
|
|
20
20
|
// But should be careful here..
|
|
21
|
-
export function queryInMemory<ROW extends ObjectWithId
|
|
21
|
+
export function queryInMemory<ROW extends Partial<ObjectWithId>>(
|
|
22
|
+
q: DBQuery<ROW>,
|
|
23
|
+
rows: ROW[] = [],
|
|
24
|
+
): ROW[] {
|
|
22
25
|
// .filter
|
|
23
26
|
// eslint-disable-next-line unicorn/no-array-reduce
|
|
24
27
|
rows = q._filters.reduce((rows, filter) => {
|
|
@@ -39,9 +42,8 @@ export function queryInMemory<ROW extends ObjectWithId>(q: DBQuery<ROW>, rows: R
|
|
|
39
42
|
|
|
40
43
|
if (descending) {
|
|
41
44
|
return a[name] < b[name] ? 1 : -1
|
|
42
|
-
} else {
|
|
43
|
-
return a[name] > b[name] ? 1 : -1
|
|
44
45
|
}
|
|
46
|
+
return a[name] > b[name] ? 1 : -1
|
|
45
47
|
})
|
|
46
48
|
}
|
|
47
49
|
|
package/src/base.common.db.ts
CHANGED
|
@@ -20,28 +20,31 @@ export class BaseCommonDB implements CommonDB {
|
|
|
20
20
|
throw new Error('getTables is not implemented')
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
async getTableSchema<ROW extends ObjectWithId
|
|
23
|
+
async getTableSchema<ROW extends Partial<ObjectWithId>>(
|
|
24
24
|
table: string,
|
|
25
25
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
26
26
|
throw new Error('getTableSchema is not implemented')
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
async createTable<ROW extends ObjectWithId
|
|
29
|
+
async createTable<ROW extends Partial<ObjectWithId>>(
|
|
30
30
|
table: string,
|
|
31
31
|
schema: JsonSchemaObject<ROW>,
|
|
32
32
|
): Promise<void> {
|
|
33
33
|
// no-op
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
async getByIds<ROW extends ObjectWithId
|
|
36
|
+
async getByIds<ROW extends Partial<ObjectWithId>>(
|
|
37
|
+
table: string,
|
|
38
|
+
ids: ROW['id'][],
|
|
39
|
+
): Promise<ROW[]> {
|
|
37
40
|
throw new Error('getByIds is not implemented')
|
|
38
41
|
}
|
|
39
42
|
|
|
40
|
-
async deleteByQuery<ROW extends ObjectWithId
|
|
43
|
+
async deleteByQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>): Promise<number> {
|
|
41
44
|
throw new Error('deleteByQuery is not implemented')
|
|
42
45
|
}
|
|
43
46
|
|
|
44
|
-
async updateByQuery<ROW extends ObjectWithId
|
|
47
|
+
async updateByQuery<ROW extends Partial<ObjectWithId>>(
|
|
45
48
|
q: DBQuery<ROW>,
|
|
46
49
|
patch: DBPatch<ROW>,
|
|
47
50
|
opt?: CommonDBOptions,
|
|
@@ -49,11 +52,11 @@ export class BaseCommonDB implements CommonDB {
|
|
|
49
52
|
throw new Error('updateByQuery is not implemented')
|
|
50
53
|
}
|
|
51
54
|
|
|
52
|
-
async runQuery<ROW extends ObjectWithId
|
|
55
|
+
async runQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>): Promise<RunQueryResult<ROW>> {
|
|
53
56
|
throw new Error('runQuery is not implemented')
|
|
54
57
|
}
|
|
55
58
|
|
|
56
|
-
async runQueryCount<ROW extends ObjectWithId
|
|
59
|
+
async runQueryCount<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>): Promise<number> {
|
|
57
60
|
throw new Error('runQueryCount is not implemented')
|
|
58
61
|
}
|
|
59
62
|
|
|
@@ -65,7 +68,7 @@ export class BaseCommonDB implements CommonDB {
|
|
|
65
68
|
throw new Error('saveBatch is not implemented')
|
|
66
69
|
}
|
|
67
70
|
|
|
68
|
-
streamQuery<ROW extends ObjectWithId
|
|
71
|
+
streamQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>): ReadableTyped<ROW> {
|
|
69
72
|
throw new Error('streamQuery is not implemented')
|
|
70
73
|
}
|
|
71
74
|
|
package/src/common.db.ts
CHANGED
|
@@ -31,13 +31,15 @@ export interface CommonDB {
|
|
|
31
31
|
*
|
|
32
32
|
* This is important for the code to rely on it, and it's verified by dbTest
|
|
33
33
|
*/
|
|
34
|
-
getTableSchema<ROW extends ObjectWithId
|
|
34
|
+
getTableSchema<ROW extends Partial<ObjectWithId>>(
|
|
35
|
+
table: string,
|
|
36
|
+
): Promise<JsonSchemaRootObject<ROW>>
|
|
35
37
|
|
|
36
38
|
/**
|
|
37
39
|
* Will do like `create table ...` for mysql.
|
|
38
40
|
* Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
|
|
39
41
|
*/
|
|
40
|
-
createTable<ROW extends ObjectWithId
|
|
42
|
+
createTable<ROW extends Partial<ObjectWithId>>(
|
|
41
43
|
table: string,
|
|
42
44
|
schema: JsonSchemaObject<ROW>,
|
|
43
45
|
opt?: CommonDBCreateOptions,
|
|
@@ -48,7 +50,7 @@ export interface CommonDB {
|
|
|
48
50
|
* Order of items returned is not guaranteed to match order of ids.
|
|
49
51
|
* (Such limitation exists because Datastore doesn't support it).
|
|
50
52
|
*/
|
|
51
|
-
getByIds<ROW extends ObjectWithId
|
|
53
|
+
getByIds<ROW extends Partial<ObjectWithId>>(
|
|
52
54
|
table: string,
|
|
53
55
|
ids: ROW['id'][],
|
|
54
56
|
opt?: CommonDBOptions,
|
|
@@ -58,14 +60,17 @@ export interface CommonDB {
|
|
|
58
60
|
/**
|
|
59
61
|
* Order by 'id' is not supported by all implementations (for example, Datastore doesn't support it).
|
|
60
62
|
*/
|
|
61
|
-
runQuery<ROW extends ObjectWithId
|
|
63
|
+
runQuery<ROW extends Partial<ObjectWithId>>(
|
|
62
64
|
q: DBQuery<ROW>,
|
|
63
65
|
opt?: CommonDBOptions,
|
|
64
66
|
): Promise<RunQueryResult<ROW>>
|
|
65
67
|
|
|
66
|
-
runQueryCount<ROW extends ObjectWithId
|
|
68
|
+
runQueryCount<ROW extends Partial<ObjectWithId>>(
|
|
69
|
+
q: DBQuery<ROW>,
|
|
70
|
+
opt?: CommonDBOptions,
|
|
71
|
+
): Promise<number>
|
|
67
72
|
|
|
68
|
-
streamQuery<ROW extends ObjectWithId
|
|
73
|
+
streamQuery<ROW extends Partial<ObjectWithId>>(
|
|
69
74
|
q: DBQuery<ROW>,
|
|
70
75
|
opt?: CommonDBStreamOptions,
|
|
71
76
|
): ReadableTyped<ROW>
|
|
@@ -85,7 +90,10 @@ export interface CommonDB {
|
|
|
85
90
|
* Returns number of deleted items.
|
|
86
91
|
* Not supported by all implementations (e.g Datastore will always return same number as number of ids).
|
|
87
92
|
*/
|
|
88
|
-
deleteByQuery<ROW extends ObjectWithId
|
|
93
|
+
deleteByQuery<ROW extends Partial<ObjectWithId>>(
|
|
94
|
+
q: DBQuery<ROW>,
|
|
95
|
+
opt?: CommonDBOptions,
|
|
96
|
+
): Promise<number>
|
|
89
97
|
|
|
90
98
|
/**
|
|
91
99
|
* Applies patch to the rows returned by the query.
|
|
@@ -105,7 +113,7 @@ export interface CommonDB {
|
|
|
105
113
|
*
|
|
106
114
|
* Returns number of rows affected.
|
|
107
115
|
*/
|
|
108
|
-
updateByQuery<ROW extends ObjectWithId
|
|
116
|
+
updateByQuery<ROW extends Partial<ObjectWithId>>(
|
|
109
117
|
q: DBQuery<ROW>,
|
|
110
118
|
patch: DBPatch<ROW>,
|
|
111
119
|
opt?: CommonDBOptions,
|