@naturalcycles/db-lib 8.35.0 → 8.37.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 +9 -9
- package/dist/adapter/cachedb/cache.db.js +3 -1
- package/dist/adapter/cachedb/cache.db.model.d.ts +5 -3
- package/dist/adapter/file/file.db.d.ts +3 -3
- package/dist/adapter/file/file.db.js +1 -0
- package/dist/adapter/inmemory/inMemory.db.d.ts +3 -3
- package/dist/adapter/inmemory/inMemory.db.js +14 -11
- package/dist/base.common.db.d.ts +3 -3
- package/dist/common.db.d.ts +7 -4
- package/dist/commondao/common.dao.d.ts +24 -23
- package/dist/commondao/common.dao.js +13 -9
- package/dist/commondao/common.dao.model.d.ts +23 -23
- package/dist/db.model.d.ts +16 -18
- package/dist/index.d.ts +3 -3
- package/dist/model.util.d.ts +2 -3
- package/dist/model.util.js +1 -7
- package/dist/query/dbQuery.d.ts +6 -6
- package/dist/testing/dbTest.d.ts +2 -0
- package/dist/testing/dbTest.js +16 -1
- package/dist/validation/index.d.ts +3 -3
- package/package.json +1 -1
- package/src/adapter/cachedb/cache.db.model.ts +8 -7
- package/src/adapter/cachedb/cache.db.ts +21 -18
- package/src/adapter/file/file.db.ts +8 -5
- package/src/adapter/inmemory/inMemory.db.ts +22 -15
- package/src/base.common.db.ts +3 -3
- package/src/common.db.ts +11 -4
- package/src/commondao/common.dao.model.ts +32 -25
- package/src/commondao/common.dao.ts +58 -65
- package/src/db.model.ts +18 -19
- package/src/index.ts +4 -20
- package/src/model.util.ts +3 -9
- package/src/query/dbQuery.ts +8 -7
- package/src/testing/dbTest.ts +22 -0
- package/dist/adapter/inmemory/index.d.ts +0 -3
- package/dist/adapter/inmemory/index.js +0 -8
- package/src/adapter/inmemory/index.ts +0 -9
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { CommonDBOptions, CommonDBSaveOptions } from '../db.model';
|
|
2
2
|
import { DBQuery, DBQueryFilter, DBQueryOrder } from '../query/dbQuery';
|
|
3
3
|
export declare const commonDBOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBOptions, CommonDBOptions>;
|
|
4
|
-
export declare const commonDBSaveOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBSaveOptions<import("@naturalcycles/js-lib").AnyObjectWithId<string>>, CommonDBSaveOptions<import("@naturalcycles/js-lib").AnyObjectWithId<string>>>;
|
|
4
|
+
export declare const commonDBSaveOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBSaveOptions<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>, CommonDBSaveOptions<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>>;
|
|
5
5
|
export declare const dbQueryFilterOperatorSchema: import("@naturalcycles/nodejs-lib/dist/validation/joi/string.extensions").ExtendedStringSchema;
|
|
6
|
-
export declare const dbQueryFilterSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId<string>>, DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId<string>>>;
|
|
7
|
-
export declare const dbQueryOrderSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId<string>>, DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId<string>>>;
|
|
6
|
+
export declare const dbQueryFilterSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>, DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>>;
|
|
7
|
+
export declare const dbQueryOrderSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>, DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>>;
|
|
8
8
|
export declare const dbQuerySchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQuery<any>, DBQuery<any>>;
|
package/package.json
CHANGED
|
@@ -45,7 +45,7 @@ export interface CacheDBCfg {
|
|
|
45
45
|
logger?: CommonLogger
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export interface CacheDBOptions
|
|
48
|
+
export interface CacheDBOptions {
|
|
49
49
|
/**
|
|
50
50
|
* @default false
|
|
51
51
|
*/
|
|
@@ -57,9 +57,10 @@ export interface CacheDBOptions<ROW extends ObjectWithId> extends CommonDBSaveOp
|
|
|
57
57
|
onlyCache?: boolean
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
export interface
|
|
61
|
-
extends CacheDBOptions
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
60
|
+
export interface CacheDBSaveOptions<ROW extends Partial<ObjectWithId>>
|
|
61
|
+
extends CacheDBOptions,
|
|
62
|
+
CommonDBSaveOptions<ROW> {}
|
|
63
|
+
|
|
64
|
+
export interface CacheDBStreamOptions extends CacheDBOptions, CommonDBStreamOptions {}
|
|
65
|
+
|
|
66
|
+
export interface CacheDBCreateOptions extends CacheDBOptions, CommonDBCreateOptions {}
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
CacheDBCfg,
|
|
14
14
|
CacheDBCreateOptions,
|
|
15
15
|
CacheDBOptions,
|
|
16
|
+
CacheDBSaveOptions,
|
|
16
17
|
CacheDBStreamOptions,
|
|
17
18
|
} from './cache.db.model'
|
|
18
19
|
|
|
@@ -59,7 +60,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
59
60
|
override async createTable<ROW extends ObjectWithId>(
|
|
60
61
|
table: string,
|
|
61
62
|
schema: JsonSchemaObject<ROW>,
|
|
62
|
-
opt: CacheDBCreateOptions
|
|
63
|
+
opt: CacheDBCreateOptions = {},
|
|
63
64
|
): Promise<void> {
|
|
64
65
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
65
66
|
await this.cfg.downstreamDB.createTable(table, schema, opt)
|
|
@@ -72,11 +73,11 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
72
73
|
|
|
73
74
|
override async getByIds<ROW extends ObjectWithId>(
|
|
74
75
|
table: string,
|
|
75
|
-
ids:
|
|
76
|
-
opt:
|
|
76
|
+
ids: ROW['id'][],
|
|
77
|
+
opt: CacheDBSaveOptions<ROW> = {},
|
|
77
78
|
): Promise<ROW[]> {
|
|
78
79
|
const resultMap: StringMap<ROW> = {}
|
|
79
|
-
const missingIds:
|
|
80
|
+
const missingIds: ROW['id'][] = []
|
|
80
81
|
|
|
81
82
|
if (!opt.skipCache && !this.cfg.skipCache) {
|
|
82
83
|
const results = await this.cfg.cacheDB.getByIds<ROW>(table, ids, opt)
|
|
@@ -118,13 +119,13 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
118
119
|
|
|
119
120
|
override async deleteByIds<ROW extends ObjectWithId>(
|
|
120
121
|
table: string,
|
|
121
|
-
ids:
|
|
122
|
-
opt: CacheDBOptions
|
|
122
|
+
ids: ROW['id'][],
|
|
123
|
+
opt: CacheDBOptions = {},
|
|
123
124
|
): Promise<number> {
|
|
124
125
|
let deletedIds = 0
|
|
125
126
|
|
|
126
127
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
127
|
-
deletedIds = await this.cfg.downstreamDB.deleteByIds(table, ids, opt)
|
|
128
|
+
deletedIds = await this.cfg.downstreamDB.deleteByIds<ROW>(table, ids, opt)
|
|
128
129
|
|
|
129
130
|
if (this.cfg.logDownstream) {
|
|
130
131
|
this.cfg.logger?.log(`${table}.deleteByIds ${deletedIds} rows from downstream`)
|
|
@@ -132,21 +133,23 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
if (!opt.skipCache && !this.cfg.skipCache) {
|
|
135
|
-
const cacheResult = this.cfg.cacheDB
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
const cacheResult = this.cfg.cacheDB
|
|
137
|
+
.deleteByIds<ROW>(table, ids, opt)
|
|
138
|
+
.then(deletedFromCache => {
|
|
139
|
+
if (this.cfg.logCached) {
|
|
140
|
+
this.cfg.logger?.log(`${table}.deleteByIds ${deletedFromCache} rows from cache`)
|
|
141
|
+
}
|
|
142
|
+
})
|
|
140
143
|
if (this.cfg.awaitCache) await cacheResult
|
|
141
144
|
}
|
|
142
145
|
|
|
143
146
|
return deletedIds
|
|
144
147
|
}
|
|
145
148
|
|
|
146
|
-
override async saveBatch<ROW extends ObjectWithId
|
|
149
|
+
override async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
147
150
|
table: string,
|
|
148
151
|
rows: ROW[],
|
|
149
|
-
opt:
|
|
152
|
+
opt: CacheDBSaveOptions<ROW> = {},
|
|
150
153
|
): Promise<void> {
|
|
151
154
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
152
155
|
await this.cfg.downstreamDB.saveBatch(table, rows, opt)
|
|
@@ -174,7 +177,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
174
177
|
|
|
175
178
|
override async runQuery<ROW extends ObjectWithId>(
|
|
176
179
|
q: DBQuery<ROW>,
|
|
177
|
-
opt:
|
|
180
|
+
opt: CacheDBSaveOptions<ROW> = {},
|
|
178
181
|
): Promise<RunQueryResult<ROW>> {
|
|
179
182
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
180
183
|
const { rows, ...queryResult } = await this.cfg.downstreamDB.runQuery(q, opt)
|
|
@@ -204,7 +207,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
204
207
|
|
|
205
208
|
override async runQueryCount<ROW extends ObjectWithId>(
|
|
206
209
|
q: DBQuery<ROW>,
|
|
207
|
-
opt: CacheDBOptions
|
|
210
|
+
opt: CacheDBOptions = {},
|
|
208
211
|
): Promise<number> {
|
|
209
212
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
210
213
|
return await this.cfg.downstreamDB.runQueryCount(q, opt)
|
|
@@ -221,7 +224,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
221
224
|
|
|
222
225
|
override streamQuery<ROW extends ObjectWithId>(
|
|
223
226
|
q: DBQuery<ROW>,
|
|
224
|
-
opt: CacheDBStreamOptions
|
|
227
|
+
opt: CacheDBStreamOptions = {},
|
|
225
228
|
): Readable {
|
|
226
229
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
227
230
|
const stream = this.cfg.downstreamDB.streamQuery<ROW>(q, opt)
|
|
@@ -260,7 +263,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
260
263
|
|
|
261
264
|
override async deleteByQuery<ROW extends ObjectWithId>(
|
|
262
265
|
q: DBQuery<ROW>,
|
|
263
|
-
opt: CacheDBOptions
|
|
266
|
+
opt: CacheDBOptions = {},
|
|
264
267
|
): Promise<number> {
|
|
265
268
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
266
269
|
const deletedIds = await this.cfg.downstreamDB.deleteByQuery(q, opt)
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
_filterUndefinedValues,
|
|
14
14
|
ObjectWithId,
|
|
15
15
|
AnyObjectWithId,
|
|
16
|
+
_assert,
|
|
16
17
|
} from '@naturalcycles/js-lib'
|
|
17
18
|
import { readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
18
19
|
import { dimGrey } from '@naturalcycles/nodejs-lib/dist/colors'
|
|
@@ -64,14 +65,14 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
64
65
|
|
|
65
66
|
override async getByIds<ROW extends ObjectWithId>(
|
|
66
67
|
table: string,
|
|
67
|
-
ids:
|
|
68
|
+
ids: ROW['id'][],
|
|
68
69
|
_opt?: CommonDBOptions,
|
|
69
70
|
): Promise<ROW[]> {
|
|
70
71
|
const byId = _by(await this.loadFile<ROW>(table), r => r.id)
|
|
71
72
|
return ids.map(id => byId[id]!).filter(Boolean)
|
|
72
73
|
}
|
|
73
74
|
|
|
74
|
-
override async saveBatch<ROW extends ObjectWithId
|
|
75
|
+
override async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
75
76
|
table: string,
|
|
76
77
|
rows: ROW[],
|
|
77
78
|
_opt?: CommonDBSaveOptions<ROW>,
|
|
@@ -79,13 +80,15 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
79
80
|
if (!rows.length) return // save some api calls
|
|
80
81
|
|
|
81
82
|
// 1. Load the whole file
|
|
82
|
-
const byId = _by(await this.loadFile<ROW>(table), r => r.id)
|
|
83
|
+
const byId = _by(await this.loadFile<ROW & ObjectWithId>(table), r => r.id)
|
|
83
84
|
|
|
84
85
|
// 2. Merge with new data (using ids)
|
|
85
86
|
let saved = 0
|
|
86
87
|
rows.forEach(r => {
|
|
88
|
+
_assert(r.id, 'FileDB: row.id is required')
|
|
89
|
+
|
|
87
90
|
if (!_deepEquals(byId[r.id], r)) {
|
|
88
|
-
byId[r.id] = r
|
|
91
|
+
byId[r.id] = r as any
|
|
89
92
|
saved++
|
|
90
93
|
}
|
|
91
94
|
})
|
|
@@ -173,7 +176,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
173
176
|
|
|
174
177
|
override async deleteByIds<ROW extends ObjectWithId>(
|
|
175
178
|
table: string,
|
|
176
|
-
ids:
|
|
179
|
+
ids: ROW['id'][],
|
|
177
180
|
_opt?: CommonDBOptions,
|
|
178
181
|
): Promise<number> {
|
|
179
182
|
if (!ids.length) return 0
|
|
@@ -137,49 +137,56 @@ export class InMemoryDB implements CommonDB {
|
|
|
137
137
|
if (opt.dropIfExists) {
|
|
138
138
|
this.data[table] = {}
|
|
139
139
|
} else {
|
|
140
|
-
this.data[table]
|
|
140
|
+
this.data[table] ||= {}
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
async getByIds<ROW extends ObjectWithId>(
|
|
145
145
|
_table: string,
|
|
146
|
-
ids:
|
|
146
|
+
ids: ROW['id'][],
|
|
147
147
|
_opt?: CommonDBOptions,
|
|
148
148
|
): Promise<ROW[]> {
|
|
149
149
|
const table = this.cfg.tablesPrefix + _table
|
|
150
|
-
this.data[table]
|
|
150
|
+
this.data[table] ||= {}
|
|
151
151
|
return ids.map(id => this.data[table]![id]).filter(Boolean) as ROW[]
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
async saveBatch<ROW extends ObjectWithId
|
|
154
|
+
async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
155
155
|
_table: string,
|
|
156
156
|
rows: ROW[],
|
|
157
|
-
|
|
157
|
+
opt: CommonDBSaveOptions<ROW> = {},
|
|
158
158
|
): Promise<void> {
|
|
159
159
|
const table = this.cfg.tablesPrefix + _table
|
|
160
|
-
this.data[table]
|
|
160
|
+
this.data[table] ||= {}
|
|
161
161
|
|
|
162
162
|
rows.forEach(r => {
|
|
163
163
|
if (!r.id) {
|
|
164
164
|
this.cfg.logger?.warn({ rows })
|
|
165
|
-
throw new Error(
|
|
165
|
+
throw new Error(
|
|
166
|
+
`InMemoryDB doesn't support id auto-generation in saveBatch, row without id was given`,
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (opt.saveMethod === 'insert' && this.data[table]![r.id]) {
|
|
171
|
+
throw new Error(`InMemoryDB: INSERT failed, entity exists: ${table}.${r.id}`)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (opt.saveMethod === 'update' && !this.data[table]![r.id]) {
|
|
175
|
+
throw new Error(`InMemoryDB: UPDATE failed, entity doesn't exist: ${table}.${r.id}`)
|
|
166
176
|
}
|
|
167
177
|
|
|
168
178
|
// JSON parse/stringify (deep clone) is to:
|
|
169
179
|
// 1. Not store values "by reference" (avoid mutation bugs)
|
|
170
180
|
// 2. Simulate real DB that would do something like that in a transport layer anyway
|
|
171
181
|
this.data[table]![r.id] = JSON.parse(JSON.stringify(r), bufferReviver)
|
|
172
|
-
|
|
173
|
-
// special treatment for Buffers (assign them raw, without JSON parse/stringify)
|
|
174
|
-
// Object.entries(r).forEach(([k, v]) => {
|
|
175
|
-
// if (Buffer.isBuffer(v)) {
|
|
176
|
-
// this.data[table]![r.id]![k] = v
|
|
177
|
-
// }
|
|
178
|
-
// })
|
|
179
182
|
})
|
|
180
183
|
}
|
|
181
184
|
|
|
182
|
-
async deleteByIds
|
|
185
|
+
async deleteByIds<ROW extends ObjectWithId>(
|
|
186
|
+
_table: string,
|
|
187
|
+
ids: ROW['id'][],
|
|
188
|
+
_opt?: CommonDBOptions,
|
|
189
|
+
): Promise<number> {
|
|
183
190
|
const table = this.cfg.tablesPrefix + _table
|
|
184
191
|
this.data[table] = this.data[table] || {}
|
|
185
192
|
|
package/src/base.common.db.ts
CHANGED
|
@@ -35,7 +35,7 @@ export class BaseCommonDB implements CommonDB {
|
|
|
35
35
|
_schema: JsonSchemaObject<ROW>,
|
|
36
36
|
): Promise<void> {}
|
|
37
37
|
|
|
38
|
-
async deleteByIds(_table: string, _ids:
|
|
38
|
+
async deleteByIds<ROW extends ObjectWithId>(_table: string, _ids: ROW['id'][]): Promise<number> {
|
|
39
39
|
return 0
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -43,7 +43,7 @@ export class BaseCommonDB implements CommonDB {
|
|
|
43
43
|
return 0
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
async getByIds<ROW extends ObjectWithId>(_table: string, _ids:
|
|
46
|
+
async getByIds<ROW extends ObjectWithId>(_table: string, _ids: ROW['id'][]): Promise<ROW[]> {
|
|
47
47
|
return []
|
|
48
48
|
}
|
|
49
49
|
|
|
@@ -55,7 +55,7 @@ export class BaseCommonDB implements CommonDB {
|
|
|
55
55
|
return 0
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
async saveBatch<ROW extends ObjectWithId
|
|
58
|
+
async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
59
59
|
_table: string,
|
|
60
60
|
_rows: ROW[],
|
|
61
61
|
_opt?: CommonDBSaveOptions<ROW>,
|
package/src/common.db.ts
CHANGED
|
@@ -49,7 +49,7 @@ export interface CommonDB {
|
|
|
49
49
|
*/
|
|
50
50
|
getByIds<ROW extends ObjectWithId>(
|
|
51
51
|
table: string,
|
|
52
|
-
ids:
|
|
52
|
+
ids: ROW['id'][],
|
|
53
53
|
opt?: CommonDBOptions,
|
|
54
54
|
): Promise<ROW[]>
|
|
55
55
|
|
|
@@ -70,7 +70,10 @@ export interface CommonDB {
|
|
|
70
70
|
): ReadableTyped<ROW>
|
|
71
71
|
|
|
72
72
|
// SAVE
|
|
73
|
-
|
|
73
|
+
/**
|
|
74
|
+
* rows can have missing ids only if DB supports auto-generating them (like mysql auto_increment).
|
|
75
|
+
*/
|
|
76
|
+
saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
74
77
|
table: string,
|
|
75
78
|
rows: ROW[],
|
|
76
79
|
opt?: CommonDBSaveOptions<ROW>,
|
|
@@ -78,10 +81,14 @@ export interface CommonDB {
|
|
|
78
81
|
|
|
79
82
|
// DELETE
|
|
80
83
|
/**
|
|
81
|
-
*
|
|
84
|
+
* Returns number of deleted items.
|
|
82
85
|
* Not supported by all implementations (e.g Datastore will always return same number as number of ids).
|
|
83
86
|
*/
|
|
84
|
-
deleteByIds
|
|
87
|
+
deleteByIds<ROW extends ObjectWithId>(
|
|
88
|
+
table: string,
|
|
89
|
+
ids: ROW['id'][],
|
|
90
|
+
opt?: CommonDBOptions,
|
|
91
|
+
): Promise<number>
|
|
85
92
|
|
|
86
93
|
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions): Promise<number>
|
|
87
94
|
|
|
@@ -10,27 +10,21 @@ import {
|
|
|
10
10
|
import { CommonDB } from '../common.db'
|
|
11
11
|
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model'
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
beforeDBMValidate: CommonDaoBeforeDBMValidateHook<DBM>
|
|
29
|
-
beforeDBMToBM: CommonDaoBeforeDBMToBMHook<BM, DBM>
|
|
30
|
-
beforeBMToDBM: CommonDaoBeforeBMToDBMHook<BM, DBM>
|
|
31
|
-
beforeTMToBM: CommonDaoBeforeTMToBMHook<BM, TM>
|
|
32
|
-
beforeBMToTM: CommonDaoBeforeBMToTMHook<BM, TM>
|
|
33
|
-
anonymize: CommonDaoAnonymizeHook<DBM>
|
|
13
|
+
export interface CommonDaoHooks<
|
|
14
|
+
BM extends Partial<ObjectWithId<ID>>,
|
|
15
|
+
DBM extends ObjectWithId<ID>,
|
|
16
|
+
TM,
|
|
17
|
+
ID extends string | number,
|
|
18
|
+
> {
|
|
19
|
+
createId: (obj: DBM | BM) => ID
|
|
20
|
+
parseNaturalId: (id: ID) => Partial<DBM>
|
|
21
|
+
beforeCreate: (bm: Partial<BM>) => Partial<BM>
|
|
22
|
+
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>
|
|
23
|
+
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>
|
|
24
|
+
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>
|
|
25
|
+
beforeTMToBM: (tm: TM) => Partial<BM>
|
|
26
|
+
beforeBMToTM: (bm: BM) => Partial<TM>
|
|
27
|
+
anonymize: (dbm: DBM) => DBM
|
|
34
28
|
|
|
35
29
|
/**
|
|
36
30
|
* If hook is defined - allows to prevent or modify the error thrown.
|
|
@@ -61,9 +55,10 @@ export enum CommonDaoLogLevel {
|
|
|
61
55
|
}
|
|
62
56
|
|
|
63
57
|
export interface CommonDaoCfg<
|
|
64
|
-
BM extends Partial<ObjectWithId
|
|
65
|
-
DBM extends ObjectWithId = Saved<BM>,
|
|
58
|
+
BM extends Partial<ObjectWithId<ID>>,
|
|
59
|
+
DBM extends ObjectWithId<ID> = Saved<BM>,
|
|
66
60
|
TM = BM,
|
|
61
|
+
ID extends string | number = DBM['id'],
|
|
67
62
|
> {
|
|
68
63
|
db: CommonDB
|
|
69
64
|
table: string
|
|
@@ -109,7 +104,19 @@ export interface CommonDaoCfg<
|
|
|
109
104
|
logStarted?: boolean
|
|
110
105
|
|
|
111
106
|
// Hooks are designed with inspiration from got/ky interface
|
|
112
|
-
hooks?: Partial<CommonDaoHooks<BM, DBM, TM>>
|
|
107
|
+
hooks?: Partial<CommonDaoHooks<BM, DBM, TM, ID>>
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Defaults to 'string'
|
|
111
|
+
*/
|
|
112
|
+
idType?: 'string' | 'number'
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Defaults to true.
|
|
116
|
+
* Set to false to disable auto-generation of `id`.
|
|
117
|
+
* Useful e.g when your DB is generating ids by itself (e.g mysql auto_increment).
|
|
118
|
+
*/
|
|
119
|
+
createId?: boolean
|
|
113
120
|
|
|
114
121
|
/**
|
|
115
122
|
* Defaults to true
|
|
@@ -202,7 +209,7 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
202
209
|
/**
|
|
203
210
|
* All properties default to undefined.
|
|
204
211
|
*/
|
|
205
|
-
export interface CommonDaoSaveOptions<DBM extends ObjectWithId
|
|
212
|
+
export interface CommonDaoSaveOptions<DBM extends Partial<ObjectWithId>>
|
|
206
213
|
extends CommonDaoOptions,
|
|
207
214
|
CommonDBSaveOptions<DBM> {
|
|
208
215
|
/**
|