@naturalcycles/db-lib 9.4.0 → 9.4.1
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 +10 -10
- package/dist/adapter/cachedb/cache.db.model.d.ts +2 -2
- package/dist/adapter/file/file.db.d.ts +12 -12
- package/dist/adapter/file/file.db.model.d.ts +3 -3
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +1 -1
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +4 -4
- package/dist/adapter/file/noop.persistence.plugin.d.ts +3 -3
- package/dist/adapter/inmemory/inMemory.db.d.ts +12 -12
- package/dist/adapter/inmemory/queryInMemory.d.ts +2 -2
- package/dist/base.common.db.d.ts +10 -10
- package/dist/common.db.d.ts +10 -10
- package/dist/commondao/common.dao.d.ts +19 -19
- package/dist/commondao/common.dao.js +1 -0
- package/dist/commondao/common.dao.model.d.ts +9 -9
- package/dist/db.model.d.ts +5 -5
- package/dist/pipeline/dbPipelineBackup.d.ts +1 -1
- package/dist/pipeline/dbPipelineCopy.d.ts +1 -1
- package/dist/pipeline/dbPipelineRestore.d.ts +1 -1
- package/dist/query/dbQuery.d.ts +7 -7
- package/dist/transaction/dbTransaction.util.d.ts +3 -3
- package/dist/validation/index.d.ts +2 -2
- package/package.json +1 -1
- package/src/adapter/cachedb/cache.db.model.ts +2 -2
- package/src/adapter/cachedb/cache.db.ts +14 -15
- package/src/adapter/file/file.db.model.ts +3 -3
- package/src/adapter/file/file.db.ts +15 -18
- package/src/adapter/file/inMemory.persistence.plugin.ts +1 -1
- package/src/adapter/file/localFile.persistence.plugin.ts +5 -5
- package/src/adapter/file/noop.persistence.plugin.ts +3 -3
- package/src/adapter/inmemory/inMemory.db.ts +17 -19
- package/src/adapter/inmemory/queryInMemory.ts +2 -5
- package/src/base.common.db.ts +10 -20
- package/src/common.db.ts +13 -20
- package/src/commondao/common.dao.model.ts +11 -17
- package/src/commondao/common.dao.ts +50 -42
- package/src/db.model.ts +5 -6
- package/src/pipeline/dbPipelineBackup.ts +1 -1
- package/src/pipeline/dbPipelineCopy.ts +11 -4
- package/src/pipeline/dbPipelineRestore.ts +5 -4
- package/src/query/dbQuery.ts +9 -11
- package/src/timeseries/commonTimeSeriesDao.ts +1 -1
- package/src/transaction/dbTransaction.util.ts +3 -3
- package/src/validation/index.ts +3 -3
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CommonLogger,
|
|
1
|
+
import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
2
|
import { CommonDB } from '../../common.db'
|
|
3
3
|
import {
|
|
4
4
|
CommonDBCreateOptions,
|
|
@@ -62,7 +62,7 @@ export interface CacheDBOptions extends CommonDBOptions {
|
|
|
62
62
|
onlyCache?: boolean
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
export interface CacheDBSaveOptions<ROW extends
|
|
65
|
+
export interface CacheDBSaveOptions<ROW extends ObjectWithId>
|
|
66
66
|
extends CacheDBOptions,
|
|
67
67
|
CommonDBSaveOptions<ROW> {}
|
|
68
68
|
|
|
@@ -3,8 +3,7 @@ import {
|
|
|
3
3
|
_isTruthy,
|
|
4
4
|
JsonSchemaObject,
|
|
5
5
|
JsonSchemaRootObject,
|
|
6
|
-
|
|
7
|
-
Saved,
|
|
6
|
+
ObjectWithId,
|
|
8
7
|
StringMap,
|
|
9
8
|
} from '@naturalcycles/js-lib'
|
|
10
9
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
@@ -59,13 +58,13 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
59
58
|
return await this.cfg.downstreamDB.getTables()
|
|
60
59
|
}
|
|
61
60
|
|
|
62
|
-
override async getTableSchema<ROW extends
|
|
61
|
+
override async getTableSchema<ROW extends ObjectWithId>(
|
|
63
62
|
table: string,
|
|
64
63
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
65
64
|
return await this.cfg.downstreamDB.getTableSchema<ROW>(table)
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
override async createTable<ROW extends
|
|
67
|
+
override async createTable<ROW extends ObjectWithId>(
|
|
69
68
|
table: string,
|
|
70
69
|
schema: JsonSchemaObject<ROW>,
|
|
71
70
|
opt: CacheDBCreateOptions = {},
|
|
@@ -79,12 +78,12 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
79
78
|
}
|
|
80
79
|
}
|
|
81
80
|
|
|
82
|
-
override async getByIds<ROW extends
|
|
81
|
+
override async getByIds<ROW extends ObjectWithId>(
|
|
83
82
|
table: string,
|
|
84
83
|
ids: string[],
|
|
85
84
|
opt: CacheDBSaveOptions<ROW> = {},
|
|
86
|
-
): Promise<
|
|
87
|
-
const resultMap: StringMap<
|
|
85
|
+
): Promise<ROW[]> {
|
|
86
|
+
const resultMap: StringMap<ROW> = {}
|
|
88
87
|
const missingIds: string[] = []
|
|
89
88
|
|
|
90
89
|
if (!opt.skipCache && !this.cfg.skipCache) {
|
|
@@ -125,7 +124,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
125
124
|
return ids.map(id => resultMap[id]).filter(_isTruthy)
|
|
126
125
|
}
|
|
127
126
|
|
|
128
|
-
override async saveBatch<ROW extends
|
|
127
|
+
override async saveBatch<ROW extends ObjectWithId>(
|
|
129
128
|
table: string,
|
|
130
129
|
rows: ROW[],
|
|
131
130
|
opt: CacheDBSaveOptions<ROW> = {},
|
|
@@ -154,10 +153,10 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
154
153
|
}
|
|
155
154
|
}
|
|
156
155
|
|
|
157
|
-
override async runQuery<ROW extends
|
|
156
|
+
override async runQuery<ROW extends ObjectWithId>(
|
|
158
157
|
q: DBQuery<ROW>,
|
|
159
158
|
opt: CacheDBSaveOptions<ROW> = {},
|
|
160
|
-
): Promise<RunQueryResult<
|
|
159
|
+
): Promise<RunQueryResult<ROW>> {
|
|
161
160
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
162
161
|
const { rows, ...queryResult } = await this.cfg.downstreamDB.runQuery(q, opt)
|
|
163
162
|
|
|
@@ -184,7 +183,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
184
183
|
return { rows, ...queryResult }
|
|
185
184
|
}
|
|
186
185
|
|
|
187
|
-
override async runQueryCount<ROW extends
|
|
186
|
+
override async runQueryCount<ROW extends ObjectWithId>(
|
|
188
187
|
q: DBQuery<ROW>,
|
|
189
188
|
opt: CacheDBOptions = {},
|
|
190
189
|
): Promise<number> {
|
|
@@ -201,10 +200,10 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
201
200
|
return count
|
|
202
201
|
}
|
|
203
202
|
|
|
204
|
-
override streamQuery<ROW extends
|
|
203
|
+
override streamQuery<ROW extends ObjectWithId>(
|
|
205
204
|
q: DBQuery<ROW>,
|
|
206
205
|
opt: CacheDBStreamOptions = {},
|
|
207
|
-
): ReadableTyped<
|
|
206
|
+
): ReadableTyped<ROW> {
|
|
208
207
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
209
208
|
const stream = this.cfg.downstreamDB.streamQuery<ROW>(q, opt)
|
|
210
209
|
|
|
@@ -240,7 +239,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
240
239
|
return stream
|
|
241
240
|
}
|
|
242
241
|
|
|
243
|
-
override async deleteByQuery<ROW extends
|
|
242
|
+
override async deleteByQuery<ROW extends ObjectWithId>(
|
|
244
243
|
q: DBQuery<ROW>,
|
|
245
244
|
opt: CacheDBOptions = {},
|
|
246
245
|
): Promise<number> {
|
|
@@ -272,7 +271,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
272
271
|
return deletedIds
|
|
273
272
|
}
|
|
274
273
|
|
|
275
|
-
override async updateByQuery<ROW extends
|
|
274
|
+
override async updateByQuery<ROW extends ObjectWithId>(
|
|
276
275
|
q: DBQuery<ROW>,
|
|
277
276
|
patch: DBPatch<ROW>,
|
|
278
277
|
opt: CacheDBOptions = {},
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { CommonLogger,
|
|
1
|
+
import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
2
|
import { DBSaveBatchOperation } from '../../db.model'
|
|
3
3
|
import type { DBQueryOrder } from '../../query/dbQuery'
|
|
4
4
|
|
|
5
5
|
export interface FileDBPersistencePlugin {
|
|
6
6
|
ping: () => Promise<void>
|
|
7
7
|
getTables: () => Promise<string[]>
|
|
8
|
-
loadFile: <ROW extends
|
|
8
|
+
loadFile: <ROW extends ObjectWithId>(table: string) => Promise<ROW[]>
|
|
9
9
|
saveFiles: (ops: DBSaveBatchOperation<any>[]) => Promise<void>
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -15,7 +15,7 @@ export interface FileDBCfg {
|
|
|
15
15
|
/**
|
|
16
16
|
* @default undefined, which means "insertion order"
|
|
17
17
|
*/
|
|
18
|
-
sortOnSave?: DBQueryOrder
|
|
18
|
+
sortOnSave?: DBQueryOrder<any>
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* @default true
|
|
@@ -9,8 +9,7 @@ import {
|
|
|
9
9
|
JsonSchemaRootObject,
|
|
10
10
|
_filterUndefinedValues,
|
|
11
11
|
_assert,
|
|
12
|
-
|
|
13
|
-
PartialObjectWithId,
|
|
12
|
+
ObjectWithId,
|
|
14
13
|
} from '@naturalcycles/js-lib'
|
|
15
14
|
import { readableCreate, ReadableTyped, dimGrey } from '@naturalcycles/nodejs-lib'
|
|
16
15
|
import {
|
|
@@ -74,16 +73,16 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
74
73
|
return tables
|
|
75
74
|
}
|
|
76
75
|
|
|
77
|
-
override async getByIds<ROW extends
|
|
76
|
+
override async getByIds<ROW extends ObjectWithId>(
|
|
78
77
|
table: string,
|
|
79
78
|
ids: string[],
|
|
80
79
|
_opt?: CommonDBOptions,
|
|
81
|
-
): Promise<
|
|
80
|
+
): Promise<ROW[]> {
|
|
82
81
|
const byId = _by(await this.loadFile<ROW>(table), r => r.id)
|
|
83
82
|
return ids.map(id => byId[id]!).filter(Boolean)
|
|
84
83
|
}
|
|
85
84
|
|
|
86
|
-
override async saveBatch<ROW extends
|
|
85
|
+
override async saveBatch<ROW extends ObjectWithId>(
|
|
87
86
|
table: string,
|
|
88
87
|
rows: ROW[],
|
|
89
88
|
_opt?: CommonDBSaveOptions<ROW>,
|
|
@@ -91,7 +90,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
91
90
|
if (!rows.length) return // save some api calls
|
|
92
91
|
|
|
93
92
|
// 1. Load the whole file
|
|
94
|
-
const byId = _by(await this.loadFile<
|
|
93
|
+
const byId = _by(await this.loadFile<ROW>(table), r => r.id)
|
|
95
94
|
|
|
96
95
|
// 2. Merge with new data (using ids)
|
|
97
96
|
let saved = 0
|
|
@@ -111,23 +110,23 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
111
110
|
}
|
|
112
111
|
}
|
|
113
112
|
|
|
114
|
-
override async runQuery<ROW extends
|
|
113
|
+
override async runQuery<ROW extends ObjectWithId>(
|
|
115
114
|
q: DBQuery<ROW>,
|
|
116
115
|
_opt?: CommonDBOptions,
|
|
117
|
-
): Promise<RunQueryResult<
|
|
116
|
+
): Promise<RunQueryResult<ROW>> {
|
|
118
117
|
return {
|
|
119
118
|
rows: queryInMemory(q, await this.loadFile<ROW>(q.table)),
|
|
120
119
|
}
|
|
121
120
|
}
|
|
122
121
|
|
|
123
|
-
override async runQueryCount<ROW extends
|
|
122
|
+
override async runQueryCount<ROW extends ObjectWithId>(
|
|
124
123
|
q: DBQuery<ROW>,
|
|
125
124
|
_opt?: CommonDBOptions,
|
|
126
125
|
): Promise<number> {
|
|
127
126
|
return (await this.loadFile(q.table)).length
|
|
128
127
|
}
|
|
129
128
|
|
|
130
|
-
override streamQuery<ROW extends
|
|
129
|
+
override streamQuery<ROW extends ObjectWithId>(
|
|
131
130
|
q: DBQuery<ROW>,
|
|
132
131
|
opt?: CommonDBStreamOptions,
|
|
133
132
|
): ReadableTyped<ROW> {
|
|
@@ -141,7 +140,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
141
140
|
return readable
|
|
142
141
|
}
|
|
143
142
|
|
|
144
|
-
override async deleteByQuery<ROW extends
|
|
143
|
+
override async deleteByQuery<ROW extends ObjectWithId>(
|
|
145
144
|
q: DBQuery<ROW>,
|
|
146
145
|
_opt?: CommonDBOptions,
|
|
147
146
|
): Promise<number> {
|
|
@@ -181,7 +180,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
181
180
|
return deleted
|
|
182
181
|
}
|
|
183
182
|
|
|
184
|
-
override async getTableSchema<ROW extends
|
|
183
|
+
override async getTableSchema<ROW extends ObjectWithId>(
|
|
185
184
|
table: string,
|
|
186
185
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
187
186
|
const rows = await this.loadFile(table)
|
|
@@ -192,7 +191,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
192
191
|
}
|
|
193
192
|
|
|
194
193
|
// wrapper, to handle logging
|
|
195
|
-
async loadFile<ROW extends
|
|
194
|
+
async loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]> {
|
|
196
195
|
const started = this.logStarted(`loadFile(${table})`)
|
|
197
196
|
const rows = await this.cfg.plugin.loadFile<ROW>(table)
|
|
198
197
|
this.logFinished(started, `loadFile(${table}) ${rows.length} row(s)`)
|
|
@@ -200,7 +199,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
200
199
|
}
|
|
201
200
|
|
|
202
201
|
// wrapper, to handle logging, sorting rows before saving
|
|
203
|
-
async saveFile<ROW extends
|
|
202
|
+
async saveFile<ROW extends ObjectWithId>(table: string, _rows: ROW[]): Promise<void> {
|
|
204
203
|
// if (!_rows.length) return // NO, it should be able to save file with 0 rows!
|
|
205
204
|
|
|
206
205
|
// Sort the rows, if needed
|
|
@@ -212,9 +211,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
212
211
|
this.logFinished(started, op)
|
|
213
212
|
}
|
|
214
213
|
|
|
215
|
-
async saveFiles<ROW extends
|
|
216
|
-
ops: DBSaveBatchOperation<ROW>[],
|
|
217
|
-
): Promise<void> {
|
|
214
|
+
async saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void> {
|
|
218
215
|
if (!ops.length) return
|
|
219
216
|
const op =
|
|
220
217
|
`saveFiles ${ops.length} op(s):\n` + ops.map(o => `${o.table} (${o.rows.length})`).join('\n')
|
|
@@ -227,7 +224,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
227
224
|
// return new FileDBTransaction(this)
|
|
228
225
|
// }
|
|
229
226
|
|
|
230
|
-
sortRows<ROW extends
|
|
227
|
+
sortRows<ROW extends ObjectWithId>(rows: ROW[]): ROW[] {
|
|
231
228
|
rows = rows.map(r => _filterUndefinedValues(r))
|
|
232
229
|
|
|
233
230
|
if (this.cfg.sortOnSave) {
|
|
@@ -18,7 +18,7 @@ export class InMemoryPersistencePlugin implements FileDBPersistencePlugin {
|
|
|
18
18
|
return Object.values(this.data[table] || ({} as any))
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
async saveFiles(ops: DBSaveBatchOperation[]): Promise<void> {
|
|
21
|
+
async saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void> {
|
|
22
22
|
ops.forEach(op => {
|
|
23
23
|
this.data[op.table] = _by(op.rows, r => r.id)
|
|
24
24
|
})
|
|
@@ -2,7 +2,7 @@ import fs from 'node:fs'
|
|
|
2
2
|
import fsp from 'node:fs/promises'
|
|
3
3
|
import { Readable } from 'node:stream'
|
|
4
4
|
import { createGzip, createUnzip } from 'node:zlib'
|
|
5
|
-
import {
|
|
5
|
+
import { ObjectWithId, pMap } from '@naturalcycles/js-lib'
|
|
6
6
|
import {
|
|
7
7
|
transformJsonParse,
|
|
8
8
|
transformSplit,
|
|
@@ -48,7 +48,7 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
|
|
|
48
48
|
.map(f => f.split('.ndjson')[0]!)
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
async loadFile<ROW extends
|
|
51
|
+
async loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]> {
|
|
52
52
|
await fs2.ensureDirAsync(this.cfg.storagePath)
|
|
53
53
|
const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
|
|
54
54
|
const filePath = `${this.cfg.storagePath}/${table}.${ext}`
|
|
@@ -57,7 +57,7 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
|
|
|
57
57
|
|
|
58
58
|
const transformUnzip = this.cfg.gzip ? [createUnzip()] : []
|
|
59
59
|
|
|
60
|
-
const rows:
|
|
60
|
+
const rows: ROW[] = []
|
|
61
61
|
|
|
62
62
|
await _pipeline([
|
|
63
63
|
fs.createReadStream(filePath),
|
|
@@ -70,11 +70,11 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
|
|
|
70
70
|
return rows
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
async saveFiles(ops: DBSaveBatchOperation[]): Promise<void> {
|
|
73
|
+
async saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void> {
|
|
74
74
|
await pMap(ops, async op => await this.saveFile(op.table, op.rows), { concurrency: 16 })
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
async saveFile<ROW extends
|
|
77
|
+
async saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void> {
|
|
78
78
|
await fs2.ensureDirAsync(this.cfg.storagePath)
|
|
79
79
|
const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
|
|
80
80
|
const filePath = `${this.cfg.storagePath}/${table}.${ext}`
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
2
|
import { DBSaveBatchOperation } from '../../db.model'
|
|
3
3
|
import { FileDBPersistencePlugin } from './file.db.model'
|
|
4
4
|
|
|
@@ -9,9 +9,9 @@ export class NoopPersistencePlugin implements FileDBPersistencePlugin {
|
|
|
9
9
|
return []
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
async loadFile<ROW extends
|
|
12
|
+
async loadFile<ROW extends ObjectWithId>(_table: string): Promise<ROW[]> {
|
|
13
13
|
return []
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
async saveFiles(_ops: DBSaveBatchOperation[]): Promise<void> {}
|
|
16
|
+
async saveFiles(_ops: DBSaveBatchOperation<any>[]): Promise<void> {}
|
|
17
17
|
}
|
|
@@ -16,8 +16,6 @@ import {
|
|
|
16
16
|
CommonLogger,
|
|
17
17
|
_deepCopy,
|
|
18
18
|
_assert,
|
|
19
|
-
PartialObjectWithId,
|
|
20
|
-
Saved,
|
|
21
19
|
} from '@naturalcycles/js-lib'
|
|
22
20
|
import {
|
|
23
21
|
bufferReviver,
|
|
@@ -154,7 +152,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
154
152
|
return Object.keys(this.data).filter(t => t.startsWith(this.cfg.tablesPrefix))
|
|
155
153
|
}
|
|
156
154
|
|
|
157
|
-
async getTableSchema<ROW extends
|
|
155
|
+
async getTableSchema<ROW extends ObjectWithId>(
|
|
158
156
|
_table: string,
|
|
159
157
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
160
158
|
const table = this.cfg.tablesPrefix + _table
|
|
@@ -164,7 +162,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
164
162
|
}
|
|
165
163
|
}
|
|
166
164
|
|
|
167
|
-
async createTable<ROW extends
|
|
165
|
+
async createTable<ROW extends ObjectWithId>(
|
|
168
166
|
_table: string,
|
|
169
167
|
_schema: JsonSchemaObject<ROW>,
|
|
170
168
|
opt: CommonDBCreateOptions = {},
|
|
@@ -177,17 +175,17 @@ export class InMemoryDB implements CommonDB {
|
|
|
177
175
|
}
|
|
178
176
|
}
|
|
179
177
|
|
|
180
|
-
async getByIds<ROW extends
|
|
178
|
+
async getByIds<ROW extends ObjectWithId>(
|
|
181
179
|
_table: string,
|
|
182
180
|
ids: string[],
|
|
183
181
|
_opt?: CommonDBOptions,
|
|
184
|
-
): Promise<
|
|
182
|
+
): Promise<ROW[]> {
|
|
185
183
|
const table = this.cfg.tablesPrefix + _table
|
|
186
184
|
this.data[table] ||= {}
|
|
187
|
-
return ids.map(id => this.data[table]![id] as
|
|
185
|
+
return ids.map(id => this.data[table]![id] as ROW).filter(Boolean)
|
|
188
186
|
}
|
|
189
187
|
|
|
190
|
-
async saveBatch<ROW extends
|
|
188
|
+
async saveBatch<ROW extends ObjectWithId>(
|
|
191
189
|
_table: string,
|
|
192
190
|
rows: ROW[],
|
|
193
191
|
opt: CommonDBSaveOptions<ROW> = {},
|
|
@@ -218,13 +216,13 @@ export class InMemoryDB implements CommonDB {
|
|
|
218
216
|
})
|
|
219
217
|
}
|
|
220
218
|
|
|
221
|
-
async deleteByQuery<ROW extends
|
|
219
|
+
async deleteByQuery<ROW extends ObjectWithId>(
|
|
222
220
|
q: DBQuery<ROW>,
|
|
223
221
|
_opt?: CommonDBOptions,
|
|
224
222
|
): Promise<number> {
|
|
225
223
|
const table = this.cfg.tablesPrefix + q.table
|
|
226
224
|
if (!this.data[table]) return 0
|
|
227
|
-
const ids = queryInMemory(q, Object.values(this.data[table]!) as
|
|
225
|
+
const ids = queryInMemory(q, Object.values(this.data[table]!) as ROW[]).map(r => r.id)
|
|
228
226
|
return await this.deleteByIds(q.table, ids)
|
|
229
227
|
}
|
|
230
228
|
|
|
@@ -242,7 +240,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
242
240
|
return count
|
|
243
241
|
}
|
|
244
242
|
|
|
245
|
-
async updateByQuery<ROW extends
|
|
243
|
+
async updateByQuery<ROW extends ObjectWithId>(
|
|
246
244
|
q: DBQuery<ROW>,
|
|
247
245
|
patch: DBPatch<ROW>,
|
|
248
246
|
): Promise<number> {
|
|
@@ -264,15 +262,15 @@ export class InMemoryDB implements CommonDB {
|
|
|
264
262
|
return rows.length
|
|
265
263
|
}
|
|
266
264
|
|
|
267
|
-
async runQuery<ROW extends
|
|
265
|
+
async runQuery<ROW extends ObjectWithId>(
|
|
268
266
|
q: DBQuery<ROW>,
|
|
269
267
|
_opt?: CommonDBOptions,
|
|
270
|
-
): Promise<RunQueryResult<
|
|
268
|
+
): Promise<RunQueryResult<ROW>> {
|
|
271
269
|
const table = this.cfg.tablesPrefix + q.table
|
|
272
|
-
return { rows: queryInMemory(q, Object.values(this.data[table] || {}) as
|
|
270
|
+
return { rows: queryInMemory(q, Object.values(this.data[table] || {}) as ROW[]) }
|
|
273
271
|
}
|
|
274
272
|
|
|
275
|
-
async runQueryCount<ROW extends
|
|
273
|
+
async runQueryCount<ROW extends ObjectWithId>(
|
|
276
274
|
q: DBQuery<ROW>,
|
|
277
275
|
_opt?: CommonDBOptions,
|
|
278
276
|
): Promise<number> {
|
|
@@ -280,10 +278,10 @@ export class InMemoryDB implements CommonDB {
|
|
|
280
278
|
return queryInMemory<any>(q, Object.values(this.data[table] || {})).length
|
|
281
279
|
}
|
|
282
280
|
|
|
283
|
-
streamQuery<ROW extends
|
|
281
|
+
streamQuery<ROW extends ObjectWithId>(
|
|
284
282
|
q: DBQuery<ROW>,
|
|
285
283
|
_opt?: CommonDBOptions,
|
|
286
|
-
): ReadableTyped<
|
|
284
|
+
): ReadableTyped<ROW> {
|
|
287
285
|
const table = this.cfg.tablesPrefix + q.table
|
|
288
286
|
return Readable.from(queryInMemory(q, Object.values(this.data[table] || {}) as ROW[]))
|
|
289
287
|
}
|
|
@@ -390,7 +388,7 @@ export class InMemoryDBTransaction implements DBTransaction {
|
|
|
390
388
|
// used to enforce forbidReadAfterWrite setting
|
|
391
389
|
writeOperationHappened = false
|
|
392
390
|
|
|
393
|
-
async getByIds<ROW extends
|
|
391
|
+
async getByIds<ROW extends ObjectWithId>(
|
|
394
392
|
table: string,
|
|
395
393
|
ids: string[],
|
|
396
394
|
opt?: CommonDBOptions,
|
|
@@ -405,7 +403,7 @@ export class InMemoryDBTransaction implements DBTransaction {
|
|
|
405
403
|
return await this.db.getByIds(table, ids, opt)
|
|
406
404
|
}
|
|
407
405
|
|
|
408
|
-
async saveBatch<ROW extends
|
|
406
|
+
async saveBatch<ROW extends ObjectWithId>(
|
|
409
407
|
table: string,
|
|
410
408
|
rows: ROW[],
|
|
411
409
|
opt?: CommonDBSaveOptions<ROW>,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _pick,
|
|
1
|
+
import { _pick, ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
2
|
import { DBQuery, DBQueryFilterOperator } from '../../query/dbQuery'
|
|
3
3
|
|
|
4
4
|
type FilterFn = (v: any, val: any) => boolean
|
|
@@ -18,10 +18,7 @@ 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
|
|
22
|
-
q: DBQuery<ROW>,
|
|
23
|
-
rows: ROW[] = [],
|
|
24
|
-
): ROW[] {
|
|
21
|
+
export function queryInMemory<ROW extends ObjectWithId>(q: DBQuery<ROW>, rows: ROW[] = []): ROW[] {
|
|
25
22
|
// .filter
|
|
26
23
|
// eslint-disable-next-line unicorn/no-array-reduce
|
|
27
24
|
rows = q._filters.reduce((rows, filter) => {
|
package/src/base.common.db.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
JsonSchemaObject,
|
|
3
|
-
JsonSchemaRootObject,
|
|
4
|
-
PartialObjectWithId,
|
|
5
|
-
Saved,
|
|
6
|
-
} from '@naturalcycles/js-lib'
|
|
1
|
+
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'
|
|
7
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
8
3
|
import { CommonDB, CommonDBSupport, CommonDBType } from './common.db'
|
|
9
4
|
import {
|
|
@@ -36,31 +31,28 @@ export class BaseCommonDB implements CommonDB {
|
|
|
36
31
|
throw new Error('getTables is not implemented')
|
|
37
32
|
}
|
|
38
33
|
|
|
39
|
-
async getTableSchema<ROW extends
|
|
34
|
+
async getTableSchema<ROW extends ObjectWithId>(
|
|
40
35
|
table: string,
|
|
41
36
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
42
37
|
throw new Error('getTableSchema is not implemented')
|
|
43
38
|
}
|
|
44
39
|
|
|
45
|
-
async createTable<ROW extends
|
|
40
|
+
async createTable<ROW extends ObjectWithId>(
|
|
46
41
|
table: string,
|
|
47
42
|
schema: JsonSchemaObject<ROW>,
|
|
48
43
|
): Promise<void> {
|
|
49
44
|
// no-op
|
|
50
45
|
}
|
|
51
46
|
|
|
52
|
-
async getByIds<ROW extends
|
|
53
|
-
table: string,
|
|
54
|
-
ids: string[],
|
|
55
|
-
): Promise<Saved<ROW>[]> {
|
|
47
|
+
async getByIds<ROW extends ObjectWithId>(table: string, ids: string[]): Promise<ROW[]> {
|
|
56
48
|
throw new Error('getByIds is not implemented')
|
|
57
49
|
}
|
|
58
50
|
|
|
59
|
-
async deleteByQuery<ROW extends
|
|
51
|
+
async deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<number> {
|
|
60
52
|
throw new Error('deleteByQuery is not implemented')
|
|
61
53
|
}
|
|
62
54
|
|
|
63
|
-
async updateByQuery<ROW extends
|
|
55
|
+
async updateByQuery<ROW extends ObjectWithId>(
|
|
64
56
|
q: DBQuery<ROW>,
|
|
65
57
|
patch: DBPatch<ROW>,
|
|
66
58
|
opt?: CommonDBOptions,
|
|
@@ -68,17 +60,15 @@ export class BaseCommonDB implements CommonDB {
|
|
|
68
60
|
throw new Error('updateByQuery is not implemented')
|
|
69
61
|
}
|
|
70
62
|
|
|
71
|
-
async runQuery<ROW extends
|
|
72
|
-
q: DBQuery<ROW>,
|
|
73
|
-
): Promise<RunQueryResult<Saved<ROW>>> {
|
|
63
|
+
async runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<RunQueryResult<ROW>> {
|
|
74
64
|
throw new Error('runQuery is not implemented')
|
|
75
65
|
}
|
|
76
66
|
|
|
77
|
-
async runQueryCount<ROW extends
|
|
67
|
+
async runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<number> {
|
|
78
68
|
throw new Error('runQueryCount is not implemented')
|
|
79
69
|
}
|
|
80
70
|
|
|
81
|
-
async saveBatch<ROW extends
|
|
71
|
+
async saveBatch<ROW extends ObjectWithId>(
|
|
82
72
|
table: string,
|
|
83
73
|
rows: ROW[],
|
|
84
74
|
opt?: CommonDBSaveOptions<ROW>,
|
|
@@ -86,7 +76,7 @@ export class BaseCommonDB implements CommonDB {
|
|
|
86
76
|
throw new Error('saveBatch is not implemented')
|
|
87
77
|
}
|
|
88
78
|
|
|
89
|
-
streamQuery<ROW extends
|
|
79
|
+
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): ReadableTyped<ROW> {
|
|
90
80
|
throw new Error('streamQuery is not implemented')
|
|
91
81
|
}
|
|
92
82
|
|
package/src/common.db.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
JsonSchemaObject,
|
|
3
|
-
JsonSchemaRootObject,
|
|
4
|
-
PartialObjectWithId,
|
|
5
|
-
Saved,
|
|
6
|
-
} from '@naturalcycles/js-lib'
|
|
1
|
+
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'
|
|
7
2
|
import type { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
8
3
|
import {
|
|
9
4
|
CommonDBCreateOptions,
|
|
@@ -69,15 +64,13 @@ export interface CommonDB {
|
|
|
69
64
|
*
|
|
70
65
|
* This is important for the code to rely on it, and it's verified by dbTest
|
|
71
66
|
*/
|
|
72
|
-
getTableSchema: <ROW extends
|
|
73
|
-
table: string,
|
|
74
|
-
) => Promise<JsonSchemaRootObject<ROW>>
|
|
67
|
+
getTableSchema: <ROW extends ObjectWithId>(table: string) => Promise<JsonSchemaRootObject<ROW>>
|
|
75
68
|
|
|
76
69
|
/**
|
|
77
70
|
* Will do like `create table ...` for mysql.
|
|
78
71
|
* Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
|
|
79
72
|
*/
|
|
80
|
-
createTable: <ROW extends
|
|
73
|
+
createTable: <ROW extends ObjectWithId>(
|
|
81
74
|
table: string,
|
|
82
75
|
schema: JsonSchemaObject<ROW>,
|
|
83
76
|
opt?: CommonDBCreateOptions,
|
|
@@ -88,36 +81,36 @@ export interface CommonDB {
|
|
|
88
81
|
* Order of items returned is not guaranteed to match order of ids.
|
|
89
82
|
* (Such limitation exists because Datastore doesn't support it).
|
|
90
83
|
*/
|
|
91
|
-
getByIds: <ROW extends
|
|
84
|
+
getByIds: <ROW extends ObjectWithId>(
|
|
92
85
|
table: string,
|
|
93
86
|
ids: string[],
|
|
94
87
|
opt?: CommonDBOptions,
|
|
95
|
-
) => Promise<
|
|
88
|
+
) => Promise<ROW[]>
|
|
96
89
|
|
|
97
90
|
// QUERY
|
|
98
91
|
/**
|
|
99
92
|
* Order by 'id' is not supported by all implementations (for example, Datastore doesn't support it).
|
|
100
93
|
*/
|
|
101
|
-
runQuery: <ROW extends
|
|
94
|
+
runQuery: <ROW extends ObjectWithId>(
|
|
102
95
|
q: DBQuery<ROW>,
|
|
103
96
|
opt?: CommonDBOptions,
|
|
104
|
-
) => Promise<RunQueryResult<
|
|
97
|
+
) => Promise<RunQueryResult<ROW>>
|
|
105
98
|
|
|
106
|
-
runQueryCount: <ROW extends
|
|
99
|
+
runQueryCount: <ROW extends ObjectWithId>(
|
|
107
100
|
q: DBQuery<ROW>,
|
|
108
101
|
opt?: CommonDBOptions,
|
|
109
102
|
) => Promise<number>
|
|
110
103
|
|
|
111
|
-
streamQuery: <ROW extends
|
|
104
|
+
streamQuery: <ROW extends ObjectWithId>(
|
|
112
105
|
q: DBQuery<ROW>,
|
|
113
106
|
opt?: CommonDBStreamOptions,
|
|
114
|
-
) => ReadableTyped<
|
|
107
|
+
) => ReadableTyped<ROW>
|
|
115
108
|
|
|
116
109
|
// SAVE
|
|
117
110
|
/**
|
|
118
111
|
* rows can have missing ids only if DB supports auto-generating them (like mysql auto_increment).
|
|
119
112
|
*/
|
|
120
|
-
saveBatch: <ROW extends
|
|
113
|
+
saveBatch: <ROW extends ObjectWithId>(
|
|
121
114
|
table: string,
|
|
122
115
|
rows: ROW[],
|
|
123
116
|
opt?: CommonDBSaveOptions<ROW>,
|
|
@@ -134,7 +127,7 @@ export interface CommonDB {
|
|
|
134
127
|
* Returns number of deleted items.
|
|
135
128
|
* Not supported by all implementations (e.g Datastore will always return same number as number of ids).
|
|
136
129
|
*/
|
|
137
|
-
deleteByQuery: <ROW extends
|
|
130
|
+
deleteByQuery: <ROW extends ObjectWithId>(
|
|
138
131
|
q: DBQuery<ROW>,
|
|
139
132
|
opt?: CommonDBOptions,
|
|
140
133
|
) => Promise<number>
|
|
@@ -157,7 +150,7 @@ export interface CommonDB {
|
|
|
157
150
|
*
|
|
158
151
|
* Returns number of rows affected.
|
|
159
152
|
*/
|
|
160
|
-
updateByQuery: <ROW extends
|
|
153
|
+
updateByQuery: <ROW extends ObjectWithId>(
|
|
161
154
|
q: DBQuery<ROW>,
|
|
162
155
|
patch: DBPatch<ROW>,
|
|
163
156
|
opt?: CommonDBOptions,
|