@naturalcycles/db-lib 8.50.1 → 8.50.3
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/file/file.db.d.ts +8 -8
- package/dist/adapter/file/file.db.model.d.ts +4 -4
- 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/common.db.d.ts +12 -12
- package/dist/commondao/common.dao.d.ts +16 -5
- package/dist/commondao/common.dao.js +68 -0
- package/dist/commondao/common.dao.model.d.ts +17 -20
- package/dist/db.model.d.ts +2 -1
- package/dist/db.model.js +1 -0
- package/dist/kv/commonKeyValueDB.d.ts +9 -9
- package/dist/kv/commonKeyValueDao.d.ts +3 -3
- package/dist/query/dbQuery.d.ts +10 -8
- package/dist/query/dbQuery.js +6 -0
- package/dist/testing/daoTest.js +1 -0
- package/package.json +1 -1
- package/src/adapter/file/file.db.model.ts +4 -4
- 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 +1 -4
- package/src/common.db.ts +24 -18
- package/src/commondao/common.dao.model.ts +22 -24
- package/src/commondao/common.dao.ts +96 -6
- package/src/db.model.ts +1 -0
- package/src/kv/commonKeyValueDB.ts +9 -9
- package/src/kv/commonKeyValueDao.ts +3 -3
- package/src/query/dbQuery.ts +18 -8
- package/src/testing/daoTest.ts +2 -0
|
@@ -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
|
|
68
|
+
override async getByIds<ROW extends 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
|
|
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
|
|
118
|
+
const rows = 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
|
|
171
|
+
override async runQuery<ROW extends 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
|
|
180
|
+
override async runQueryCount<ROW extends 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
|
|
187
|
+
override streamQuery<ROW extends 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
|
|
201
|
+
override async deleteByQuery<ROW extends 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
|
|
220
|
+
override async getTableSchema<ROW extends 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
|
|
231
|
+
async loadFile<ROW extends 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
|
|
239
|
+
async saveFile<ROW extends 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
|
|
260
|
+
private sortRows<ROW extends 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
|
|
17
|
+
async loadFile<ROW extends 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
|
|
49
|
+
async loadFile<ROW extends 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
|
|
12
|
+
async loadFile<ROW extends 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
|
|
123
|
+
async getTableSchema<ROW extends 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
|
|
133
|
+
async createTable<ROW extends 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
|
|
146
|
+
async getByIds<ROW extends 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
|
|
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
|
|
187
|
+
async deleteByQuery<ROW extends 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
|
|
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
|
|
202
|
+
async updateByQuery<ROW extends 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
|
|
224
|
+
async runQuery<ROW extends 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
|
|
232
|
+
async runQueryCount<ROW extends 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
|
|
240
|
+
streamQuery<ROW extends ObjectWithId>(
|
|
241
241
|
q: DBQuery<ROW>,
|
|
242
242
|
_opt?: CommonDBOptions,
|
|
243
243
|
): ReadableTyped<ROW> {
|
|
@@ -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/common.db.ts
CHANGED
|
@@ -18,12 +18,12 @@ export interface CommonDB {
|
|
|
18
18
|
* It SHOULD fail if DB setup is wrong (e.g on wrong credentials).
|
|
19
19
|
* It SHOULD succeed if e.g getByIds(['nonExistingKey']) doesn't throw.
|
|
20
20
|
*/
|
|
21
|
-
ping()
|
|
21
|
+
ping: () => Promise<void>
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* Return all tables (table names) available in this DB.
|
|
25
25
|
*/
|
|
26
|
-
getTables()
|
|
26
|
+
getTables: () => Promise<string[]>
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* $id of the schema SHOULD be like this:
|
|
@@ -31,61 +31,67 @@ 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>(table: string)
|
|
34
|
+
getTableSchema: <ROW extends ObjectWithId>(table: string) => Promise<JsonSchemaRootObject<ROW>>
|
|
35
35
|
|
|
36
36
|
/**
|
|
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<ROW extends ObjectWithId>(
|
|
40
|
+
createTable: <ROW extends ObjectWithId>(
|
|
41
41
|
table: string,
|
|
42
42
|
schema: JsonSchemaObject<ROW>,
|
|
43
43
|
opt?: CommonDBCreateOptions,
|
|
44
|
-
)
|
|
44
|
+
) => Promise<void>
|
|
45
45
|
|
|
46
46
|
// GET
|
|
47
47
|
/**
|
|
48
48
|
* Order of items returned is not guaranteed to match order of ids.
|
|
49
49
|
* (Such limitation exists because Datastore doesn't support it).
|
|
50
50
|
*/
|
|
51
|
-
getByIds<ROW extends ObjectWithId>(
|
|
51
|
+
getByIds: <ROW extends ObjectWithId>(
|
|
52
52
|
table: string,
|
|
53
53
|
ids: ROW['id'][],
|
|
54
54
|
opt?: CommonDBOptions,
|
|
55
|
-
)
|
|
55
|
+
) => Promise<ROW[]>
|
|
56
56
|
|
|
57
57
|
// QUERY
|
|
58
58
|
/**
|
|
59
59
|
* Order by 'id' is not supported by all implementations (for example, Datastore doesn't support it).
|
|
60
60
|
*/
|
|
61
|
-
runQuery<ROW extends ObjectWithId>(
|
|
61
|
+
runQuery: <ROW extends ObjectWithId>(
|
|
62
62
|
q: DBQuery<ROW>,
|
|
63
63
|
opt?: CommonDBOptions,
|
|
64
|
-
)
|
|
64
|
+
) => Promise<RunQueryResult<ROW>>
|
|
65
65
|
|
|
66
|
-
runQueryCount<ROW extends ObjectWithId>(
|
|
66
|
+
runQueryCount: <ROW extends ObjectWithId>(
|
|
67
|
+
q: DBQuery<ROW>,
|
|
68
|
+
opt?: CommonDBOptions,
|
|
69
|
+
) => Promise<number>
|
|
67
70
|
|
|
68
|
-
streamQuery<ROW extends ObjectWithId>(
|
|
71
|
+
streamQuery: <ROW extends ObjectWithId>(
|
|
69
72
|
q: DBQuery<ROW>,
|
|
70
73
|
opt?: CommonDBStreamOptions,
|
|
71
|
-
)
|
|
74
|
+
) => ReadableTyped<ROW>
|
|
72
75
|
|
|
73
76
|
// SAVE
|
|
74
77
|
/**
|
|
75
78
|
* rows can have missing ids only if DB supports auto-generating them (like mysql auto_increment).
|
|
76
79
|
*/
|
|
77
|
-
saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
80
|
+
saveBatch: <ROW extends Partial<ObjectWithId>>(
|
|
78
81
|
table: string,
|
|
79
82
|
rows: ROW[],
|
|
80
83
|
opt?: CommonDBSaveOptions<ROW>,
|
|
81
|
-
)
|
|
84
|
+
) => Promise<void>
|
|
82
85
|
|
|
83
86
|
// DELETE
|
|
84
87
|
/**
|
|
85
88
|
* Returns number of deleted items.
|
|
86
89
|
* Not supported by all implementations (e.g Datastore will always return same number as number of ids).
|
|
87
90
|
*/
|
|
88
|
-
deleteByQuery<ROW extends ObjectWithId>(
|
|
91
|
+
deleteByQuery: <ROW extends ObjectWithId>(
|
|
92
|
+
q: DBQuery<ROW>,
|
|
93
|
+
opt?: CommonDBOptions,
|
|
94
|
+
) => Promise<number>
|
|
89
95
|
|
|
90
96
|
/**
|
|
91
97
|
* Applies patch to the rows returned by the query.
|
|
@@ -105,16 +111,16 @@ export interface CommonDB {
|
|
|
105
111
|
*
|
|
106
112
|
* Returns number of rows affected.
|
|
107
113
|
*/
|
|
108
|
-
updateByQuery<ROW extends ObjectWithId>(
|
|
114
|
+
updateByQuery: <ROW extends ObjectWithId>(
|
|
109
115
|
q: DBQuery<ROW>,
|
|
110
116
|
patch: DBPatch<ROW>,
|
|
111
117
|
opt?: CommonDBOptions,
|
|
112
|
-
)
|
|
118
|
+
) => Promise<number>
|
|
113
119
|
|
|
114
120
|
// TRANSACTION
|
|
115
121
|
/**
|
|
116
122
|
* Should be implemented as a Transaction (best effort), which means that
|
|
117
123
|
* either ALL or NONE of the operations should be applied.
|
|
118
124
|
*/
|
|
119
|
-
commitTransaction(tx: DBTransaction, opt?: CommonDBOptions)
|
|
125
|
+
commitTransaction: (tx: DBTransaction, opt?: CommonDBOptions) => Promise<void>
|
|
120
126
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CommonLogger, ErrorMode, ObjectWithId } from '@naturalcycles/js-lib'
|
|
1
|
+
import { CommonLogger, ErrorMode, ObjectWithId, Saved } from '@naturalcycles/js-lib'
|
|
2
2
|
import {
|
|
3
3
|
AjvSchema,
|
|
4
4
|
AjvValidationError,
|
|
@@ -12,25 +12,24 @@ import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../
|
|
|
12
12
|
|
|
13
13
|
export interface CommonDaoHooks<
|
|
14
14
|
BM extends Partial<ObjectWithId<ID>>,
|
|
15
|
-
DBM extends
|
|
16
|
-
|
|
15
|
+
DBM extends ObjectWithId<ID>,
|
|
16
|
+
TM,
|
|
17
|
+
ID extends string | number,
|
|
17
18
|
> {
|
|
18
|
-
createRandomId()
|
|
19
|
+
createRandomId: () => ID
|
|
19
20
|
/**
|
|
20
21
|
* createNaturalId hook is called (tried) first.
|
|
21
22
|
* If it doesn't exist - createRandomId is called.
|
|
22
23
|
*/
|
|
23
|
-
createNaturalId(obj: DBM | BM)
|
|
24
|
-
parseNaturalId(id: ID)
|
|
25
|
-
beforeCreate(bm: Partial<BM>)
|
|
26
|
-
beforeDBMValidate(dbm: Partial<DBM>)
|
|
27
|
-
beforeDBMToBM(dbm: DBM)
|
|
28
|
-
beforeBMToDBM(bm: BM)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
*/
|
|
33
|
-
anonymize(dbm: DBM): DBM
|
|
24
|
+
createNaturalId: (obj: DBM | BM) => ID
|
|
25
|
+
parseNaturalId: (id: ID) => Partial<DBM>
|
|
26
|
+
beforeCreate: (bm: Partial<BM>) => Partial<BM>
|
|
27
|
+
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>
|
|
28
|
+
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>
|
|
29
|
+
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>
|
|
30
|
+
beforeTMToBM: (tm: TM) => Partial<BM>
|
|
31
|
+
beforeBMToTM: (bm: BM) => Partial<TM>
|
|
32
|
+
anonymize: (dbm: DBM) => DBM
|
|
34
33
|
|
|
35
34
|
/**
|
|
36
35
|
* If hook is defined - allows to prevent or modify the error thrown.
|
|
@@ -38,7 +37,7 @@ export interface CommonDaoHooks<
|
|
|
38
37
|
* Return original `err` to pass the error through (will be thrown in CommonDao).
|
|
39
38
|
* Return modified/new `Error` if needed.
|
|
40
39
|
*/
|
|
41
|
-
onValidationError(err: JoiValidationError | AjvValidationError)
|
|
40
|
+
onValidationError: (err: JoiValidationError | AjvValidationError) => Error | false
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
export enum CommonDaoLogLevel {
|
|
@@ -61,9 +60,10 @@ export enum CommonDaoLogLevel {
|
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
export interface CommonDaoCfg<
|
|
64
|
-
BM extends ObjectWithId<ID
|
|
65
|
-
DBM extends ObjectWithId<ID> = BM
|
|
66
|
-
|
|
63
|
+
BM extends Partial<ObjectWithId<ID>>,
|
|
64
|
+
DBM extends ObjectWithId<ID> = Saved<BM>,
|
|
65
|
+
TM = BM,
|
|
66
|
+
ID extends string | number = string,
|
|
67
67
|
> {
|
|
68
68
|
db: CommonDB
|
|
69
69
|
table: string
|
|
@@ -73,6 +73,7 @@ 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>
|
|
76
77
|
|
|
77
78
|
excludeFromIndexes?: (keyof DBM)[]
|
|
78
79
|
|
|
@@ -84,8 +85,6 @@ export interface CommonDaoCfg<
|
|
|
84
85
|
* `delete*` and `patch` will throw.
|
|
85
86
|
*
|
|
86
87
|
* 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
|
|
89
88
|
*/
|
|
90
89
|
immutable?: boolean
|
|
91
90
|
|
|
@@ -111,7 +110,7 @@ export interface CommonDaoCfg<
|
|
|
111
110
|
logStarted?: boolean
|
|
112
111
|
|
|
113
112
|
// Hooks are designed with inspiration from got/ky interface
|
|
114
|
-
hooks?: Partial<CommonDaoHooks<BM, DBM, ID>>
|
|
113
|
+
hooks?: Partial<CommonDaoHooks<BM, DBM, TM, ID>>
|
|
115
114
|
|
|
116
115
|
/**
|
|
117
116
|
* Defaults to 'string'
|
|
@@ -128,7 +127,6 @@ export interface CommonDaoCfg<
|
|
|
128
127
|
/**
|
|
129
128
|
* See the same option in CommonDB.
|
|
130
129
|
* Defaults to false normally.
|
|
131
|
-
* "Generated" means "generated by the underlying DB" (e.g MySQL autoincrement).
|
|
132
130
|
*/
|
|
133
131
|
assignGeneratedIds?: boolean
|
|
134
132
|
|
|
@@ -226,7 +224,7 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
226
224
|
/**
|
|
227
225
|
* All properties default to undefined.
|
|
228
226
|
*/
|
|
229
|
-
export interface CommonDaoSaveOptions<DBM extends
|
|
227
|
+
export interface CommonDaoSaveOptions<DBM extends ObjectWithId>
|
|
230
228
|
extends CommonDaoOptions,
|
|
231
229
|
CommonDBSaveOptions<DBM> {
|
|
232
230
|
/**
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
_since,
|
|
7
7
|
_truncate,
|
|
8
8
|
_uniqBy,
|
|
9
|
+
AnyObject,
|
|
9
10
|
AppError,
|
|
10
11
|
AsyncMapper,
|
|
11
12
|
ErrorMode,
|
|
@@ -62,13 +63,15 @@ const isCI = !!process.env['CI']
|
|
|
62
63
|
*
|
|
63
64
|
* DBM = Database model (how it's stored in DB)
|
|
64
65
|
* BM = Backend model (optimized for API access)
|
|
66
|
+
* TM = Transport model (optimized to be sent over the wire)
|
|
65
67
|
*/
|
|
66
68
|
export class CommonDao<
|
|
67
|
-
BM extends ObjectWithId<ID
|
|
68
|
-
DBM extends ObjectWithId<ID> = BM
|
|
69
|
-
|
|
69
|
+
BM extends Partial<ObjectWithId<ID>>,
|
|
70
|
+
DBM extends ObjectWithId<ID> = Saved<BM>,
|
|
71
|
+
TM extends AnyObject = BM,
|
|
72
|
+
ID extends string | number = NonNullable<BM['id']>,
|
|
70
73
|
> {
|
|
71
|
-
constructor(public cfg: CommonDaoCfg<BM, DBM, ID>) {
|
|
74
|
+
constructor(public cfg: CommonDaoCfg<BM, DBM, TM, ID>) {
|
|
72
75
|
this.cfg = {
|
|
73
76
|
// Default is to NOT log in AppEngine and in CI,
|
|
74
77
|
// otherwise to log Operations
|
|
@@ -87,6 +90,8 @@ export class CommonDao<
|
|
|
87
90
|
beforeDBMValidate: dbm => dbm,
|
|
88
91
|
beforeDBMToBM: dbm => dbm as any,
|
|
89
92
|
beforeBMToDBM: bm => bm as any,
|
|
93
|
+
beforeTMToBM: tm => tm as any,
|
|
94
|
+
beforeBMToTM: bm => bm as any,
|
|
90
95
|
anonymize: dbm => dbm,
|
|
91
96
|
onValidationError: err => err,
|
|
92
97
|
...cfg.hooks,
|
|
@@ -159,6 +164,24 @@ export class CommonDao<
|
|
|
159
164
|
return dbm || null
|
|
160
165
|
}
|
|
161
166
|
|
|
167
|
+
async getByIdAsTM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>
|
|
168
|
+
async getByIdAsTM(id?: ID | null, opt?: CommonDaoOptions): Promise<TM | null>
|
|
169
|
+
async getByIdAsTM(id?: ID | null, opt: CommonDaoOptions = {}): Promise<TM | null> {
|
|
170
|
+
if (!id) return null
|
|
171
|
+
const op = `getByIdAsTM(${id})`
|
|
172
|
+
const table = opt.table || this.cfg.table
|
|
173
|
+
const started = this.logStarted(op, table)
|
|
174
|
+
const [dbm] = await this.cfg.db.getByIds<DBM>(table, [id])
|
|
175
|
+
if (opt.raw) {
|
|
176
|
+
this.logResult(started, op, dbm, table)
|
|
177
|
+
return (dbm as any) || null
|
|
178
|
+
}
|
|
179
|
+
const bm = await this.dbmToBM(dbm, opt)
|
|
180
|
+
const tm = this.bmToTM(bm, opt)
|
|
181
|
+
this.logResult(started, op, tm, table)
|
|
182
|
+
return tm || null
|
|
183
|
+
}
|
|
184
|
+
|
|
162
185
|
async getByIds(ids: ID[], opt: CommonDaoOptions = {}): Promise<Saved<BM>[]> {
|
|
163
186
|
const op = `getByIds ${ids.length} id(s) (${_truncate(ids.slice(0, 10).join(', '), 50)})`
|
|
164
187
|
const table = opt.table || this.cfg.table
|
|
@@ -256,8 +279,8 @@ export class CommonDao<
|
|
|
256
279
|
/**
|
|
257
280
|
* Pass `table` to override table
|
|
258
281
|
*/
|
|
259
|
-
query(table?: string): RunnableDBQuery<BM, DBM, ID> {
|
|
260
|
-
return new RunnableDBQuery<BM, DBM, ID>(this, table)
|
|
282
|
+
query(table?: string): RunnableDBQuery<BM, DBM, TM, ID> {
|
|
283
|
+
return new RunnableDBQuery<BM, DBM, TM, ID>(this, table)
|
|
261
284
|
}
|
|
262
285
|
|
|
263
286
|
async runQuery(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<Saved<BM>[]> {
|
|
@@ -325,6 +348,29 @@ export class CommonDao<
|
|
|
325
348
|
return { rows: dbms, ...queryResult }
|
|
326
349
|
}
|
|
327
350
|
|
|
351
|
+
async runQueryAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<TM[]> {
|
|
352
|
+
const { rows } = await this.runQueryExtendedAsTM(q, opt)
|
|
353
|
+
return rows
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
async runQueryExtendedAsTM(
|
|
357
|
+
q: DBQuery<DBM>,
|
|
358
|
+
opt: CommonDaoOptions = {},
|
|
359
|
+
): Promise<RunQueryResult<TM>> {
|
|
360
|
+
q.table = opt.table || q.table
|
|
361
|
+
const op = `runQueryAsTM(${q.pretty()})`
|
|
362
|
+
const started = this.logStarted(op, q.table)
|
|
363
|
+
const { rows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
|
|
364
|
+
const partialQuery = !!q._selectedFieldNames
|
|
365
|
+
const tms =
|
|
366
|
+
partialQuery || opt.raw ? (rows as any[]) : this.bmsToTM(await this.dbmsToBM(rows, opt), opt)
|
|
367
|
+
this.logResult(started, op, tms, q.table)
|
|
368
|
+
return {
|
|
369
|
+
rows: tms,
|
|
370
|
+
...queryResult,
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
328
374
|
async runQueryCount(q: DBQuery<DBM>, opt: CommonDaoOptions = {}): Promise<number> {
|
|
329
375
|
q.table = opt.table || q.table
|
|
330
376
|
const op = `runQueryCount(${q.pretty()})`
|
|
@@ -979,6 +1025,50 @@ export class CommonDao<
|
|
|
979
1025
|
return entities.map(entity => this.anyToDBM(entity, opt))
|
|
980
1026
|
}
|
|
981
1027
|
|
|
1028
|
+
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined
|
|
1029
|
+
bmToTM(bm?: Saved<BM>, opt?: CommonDaoOptions): TM
|
|
1030
|
+
bmToTM(bm?: Saved<BM>, opt?: CommonDaoOptions): TM | undefined {
|
|
1031
|
+
if (bm === undefined) return
|
|
1032
|
+
|
|
1033
|
+
// optimization: 1 validation is enough
|
|
1034
|
+
// Validate/convert BM
|
|
1035
|
+
// bm gets assigned to the new reference
|
|
1036
|
+
// bm = this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
1037
|
+
|
|
1038
|
+
// BM > TM
|
|
1039
|
+
const tm = this.cfg.hooks!.beforeBMToTM!(bm as any)
|
|
1040
|
+
|
|
1041
|
+
// Validate/convert DBM
|
|
1042
|
+
return this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt)
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
bmsToTM(bms: Saved<BM>[], opt: CommonDaoOptions = {}): TM[] {
|
|
1046
|
+
// try/catch?
|
|
1047
|
+
return bms.map(bm => this.bmToTM(bm, opt))
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
tmToBM(tm: undefined, opt?: CommonDaoOptions): undefined
|
|
1051
|
+
tmToBM(tm?: TM, opt?: CommonDaoOptions): BM
|
|
1052
|
+
tmToBM(tm?: TM, opt: CommonDaoOptions = {}): BM | undefined {
|
|
1053
|
+
if (!tm) return
|
|
1054
|
+
|
|
1055
|
+
// optimization: 1 validation is enough
|
|
1056
|
+
// Validate/convert TM
|
|
1057
|
+
// bm gets assigned to the new reference
|
|
1058
|
+
// tm = this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt)
|
|
1059
|
+
|
|
1060
|
+
// TM > BM
|
|
1061
|
+
const bm = this.cfg.hooks!.beforeTMToBM!(tm) as BM
|
|
1062
|
+
|
|
1063
|
+
// Validate/convert BM
|
|
1064
|
+
return this.validateAndConvert<BM>(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
tmsToBM(tms: TM[], opt: CommonDaoOptions = {}): BM[] {
|
|
1068
|
+
// try/catch?
|
|
1069
|
+
return tms.map(tm => this.tmToBM(tm, opt))
|
|
1070
|
+
}
|
|
1071
|
+
|
|
982
1072
|
/**
|
|
983
1073
|
* Returns *converted value*.
|
|
984
1074
|
* Validates (unless `skipValidation=true` passed).
|
package/src/db.model.ts
CHANGED
|
@@ -12,28 +12,28 @@ export interface CommonKeyValueDB {
|
|
|
12
12
|
/**
|
|
13
13
|
* Check that DB connection is working properly.
|
|
14
14
|
*/
|
|
15
|
-
ping()
|
|
15
|
+
ping: () => Promise<void>
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Will do like `create table ...` for mysql.
|
|
19
19
|
* Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
|
|
20
20
|
*/
|
|
21
|
-
createTable(table: string, opt?: CommonDBCreateOptions)
|
|
21
|
+
createTable: (table: string, opt?: CommonDBCreateOptions) => Promise<void>
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* Returns an array of tuples [key, value]. Not found values are not returned (no error is thrown).
|
|
25
25
|
*
|
|
26
26
|
* Currently it is NOT required to maintain the same order as input `ids`.
|
|
27
27
|
*/
|
|
28
|
-
getByIds(table: string, ids: string[])
|
|
28
|
+
getByIds: (table: string, ids: string[]) => Promise<KeyValueDBTuple[]>
|
|
29
29
|
|
|
30
|
-
deleteByIds(table: string, ids: string[])
|
|
30
|
+
deleteByIds: (table: string, ids: string[]) => Promise<void>
|
|
31
31
|
|
|
32
|
-
saveBatch(table: string, entries: KeyValueDBTuple[])
|
|
32
|
+
saveBatch: (table: string, entries: KeyValueDBTuple[]) => Promise<void>
|
|
33
33
|
|
|
34
|
-
streamIds(table: string, limit?: number)
|
|
35
|
-
streamValues(table: string, limit?: number)
|
|
36
|
-
streamEntries(table: string, limit?: number)
|
|
34
|
+
streamIds: (table: string, limit?: number) => ReadableTyped<string>
|
|
35
|
+
streamValues: (table: string, limit?: number) => ReadableTyped<Buffer>
|
|
36
|
+
streamEntries: (table: string, limit?: number) => ReadableTyped<KeyValueDBTuple>
|
|
37
37
|
|
|
38
|
-
count(table: string)
|
|
38
|
+
count: (table: string) => Promise<number>
|
|
39
39
|
}
|
|
@@ -32,9 +32,9 @@ export interface CommonKeyValueDaoCfg<T> {
|
|
|
32
32
|
logStarted?: boolean
|
|
33
33
|
|
|
34
34
|
hooks?: {
|
|
35
|
-
mapValueToBuffer
|
|
36
|
-
mapBufferToValue
|
|
37
|
-
beforeCreate
|
|
35
|
+
mapValueToBuffer?: (v: T) => Promise<Buffer>
|
|
36
|
+
mapBufferToValue?: (b: Buffer) => Promise<T>
|
|
37
|
+
beforeCreate?: (v: Partial<T>) => Partial<T>
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
/**
|