@naturalcycles/db-lib 8.49.1 → 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/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 +11 -15
- package/dist/db.model.d.ts +1 -2
- package/dist/db.model.js +0 -1
- 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 +4 -1
- package/src/base.common.db.ts +11 -8
- package/src/common.db.ts +16 -8
- package/src/commondao/common.dao.model.ts +14 -20
- package/src/commondao/common.dao.ts +11 -117
- package/src/db.model.ts +0 -1
- package/src/query/dbQuery.ts +7 -17
- package/src/testing/daoTest.ts +0 -2
|
@@ -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) => {
|
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,
|
|
@@ -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,9 +12,8 @@ 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
18
|
createRandomId(): ID
|
|
20
19
|
/**
|
|
@@ -27,8 +26,10 @@ export interface CommonDaoHooks<
|
|
|
27
26
|
beforeDBMValidate(dbm: Partial<DBM>): Partial<DBM>
|
|
28
27
|
beforeDBMToBM(dbm: DBM): Partial<BM> | Promise<Partial<BM>>
|
|
29
28
|
beforeBMToDBM(bm: BM): Partial<DBM> | Promise<Partial<DBM>>
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Applicable to **read** operations, never to **save** operations.
|
|
32
|
+
*/
|
|
32
33
|
anonymize(dbm: DBM): DBM
|
|
33
34
|
|
|
34
35
|
/**
|
|
@@ -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
|
/**
|