@naturalcycles/db-lib 8.34.1 → 8.36.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.
Files changed (40) hide show
  1. package/dist/adapter/cachedb/cache.db.d.ts +9 -9
  2. package/dist/adapter/cachedb/cache.db.js +3 -1
  3. package/dist/adapter/cachedb/cache.db.model.d.ts +5 -3
  4. package/dist/adapter/file/file.db.d.ts +2 -2
  5. package/dist/adapter/inmemory/inMemory.db.d.ts +2 -2
  6. package/dist/adapter/inmemory/inMemory.db.js +6 -3
  7. package/dist/base.common.db.d.ts +2 -2
  8. package/dist/common.db.d.ts +3 -3
  9. package/dist/commondao/common.dao.d.ts +21 -21
  10. package/dist/commondao/common.dao.js +24 -22
  11. package/dist/commondao/common.dao.model.d.ts +22 -22
  12. package/dist/db.model.d.ts +2 -17
  13. package/dist/index.d.ts +4 -5
  14. package/dist/index.js +2 -4
  15. package/dist/pipeline/dbPipelineBackup.js +1 -2
  16. package/dist/pipeline/dbPipelineCopy.js +1 -2
  17. package/dist/pipeline/dbPipelineRestore.js +1 -2
  18. package/dist/query/dbQuery.d.ts +6 -6
  19. package/dist/validation/index.d.ts +3 -3
  20. package/package.json +1 -2
  21. package/src/adapter/cachedb/cache.db.model.ts +8 -7
  22. package/src/adapter/cachedb/cache.db.ts +20 -17
  23. package/src/adapter/file/file.db.ts +2 -2
  24. package/src/adapter/inmemory/inMemory.db.ts +9 -5
  25. package/src/base.common.db.ts +2 -2
  26. package/src/common.db.ts +7 -3
  27. package/src/commondao/common.dao.model.ts +31 -24
  28. package/src/commondao/common.dao.ts +78 -76
  29. package/src/db.model.ts +2 -18
  30. package/src/index.ts +2 -22
  31. package/src/pipeline/dbPipelineBackup.ts +9 -3
  32. package/src/pipeline/dbPipelineCopy.ts +2 -3
  33. package/src/pipeline/dbPipelineRestore.ts +2 -2
  34. package/src/query/dbQuery.ts +8 -7
  35. package/dist/adapter/inmemory/index.d.ts +0 -3
  36. package/dist/adapter/inmemory/index.js +0 -8
  37. package/dist/getDB.d.ts +0 -19
  38. package/dist/getDB.js +0 -32
  39. package/src/adapter/inmemory/index.ts +0 -9
  40. package/src/getDB.ts +0 -50
@@ -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
@@ -6,7 +6,6 @@
6
6
  "dependencies": {
7
7
  "@naturalcycles/js-lib": "^14.0.0",
8
8
  "@naturalcycles/nodejs-lib": "^12.0.0",
9
- "@naturalcycles/time-lib": "^3.0.1",
10
9
  "fs-extra": "^10.0.0"
11
10
  },
12
11
  "devDependencies": {
@@ -43,7 +42,7 @@
43
42
  "engines": {
44
43
  "node": ">=14.15"
45
44
  },
46
- "version": "8.34.1",
45
+ "version": "8.36.0",
47
46
  "description": "Lowest Common Denominator API to supported Databases",
48
47
  "keywords": [
49
48
  "db",
@@ -45,7 +45,7 @@ export interface CacheDBCfg {
45
45
  logger?: CommonLogger
46
46
  }
47
47
 
48
- export interface CacheDBOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {
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 CacheDBStreamOptions<ROW extends ObjectWithId>
61
- extends CacheDBOptions<ROW>,
62
- CommonDBStreamOptions {}
63
- export interface CacheDBCreateOptions<ROW extends ObjectWithId>
64
- extends CacheDBOptions<ROW>,
65
- CommonDBCreateOptions {}
60
+ export interface CacheDBSaveOptions<ROW extends 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<ROW> = {},
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: string[],
76
- opt: CacheDBOptions<ROW> = {},
76
+ ids: ROW['id'][],
77
+ opt: CacheDBSaveOptions<ROW> = {},
77
78
  ): Promise<ROW[]> {
78
79
  const resultMap: StringMap<ROW> = {}
79
- const missingIds: string[] = []
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: string[],
122
- opt: CacheDBOptions<ROW> = {},
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,11 +133,13 @@ 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.deleteByIds(table, ids, opt).then(deletedFromCache => {
136
- if (this.cfg.logCached) {
137
- this.cfg.logger?.log(`${table}.deleteByIds ${deletedFromCache} rows from cache`)
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
 
@@ -146,7 +149,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
146
149
  override async saveBatch<ROW extends ObjectWithId>(
147
150
  table: string,
148
151
  rows: ROW[],
149
- opt: CacheDBOptions<ROW> = {},
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: CacheDBOptions<ROW> = {},
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<ROW> = {},
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<ROW> = {},
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<ROW> = {},
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)
@@ -64,7 +64,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
64
64
 
65
65
  override async getByIds<ROW extends ObjectWithId>(
66
66
  table: string,
67
- ids: string[],
67
+ ids: ROW['id'][],
68
68
  _opt?: CommonDBOptions,
69
69
  ): Promise<ROW[]> {
70
70
  const byId = _by(await this.loadFile<ROW>(table), r => r.id)
@@ -173,7 +173,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
173
173
 
174
174
  override async deleteByIds<ROW extends ObjectWithId>(
175
175
  table: string,
176
- ids: string[],
176
+ ids: ROW['id'][],
177
177
  _opt?: CommonDBOptions,
178
178
  ): Promise<number> {
179
179
  if (!ids.length) return 0
@@ -137,17 +137,17 @@ export class InMemoryDB implements CommonDB {
137
137
  if (opt.dropIfExists) {
138
138
  this.data[table] = {}
139
139
  } else {
140
- this.data[table] = 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: string[],
146
+ ids: ROW['id'][],
147
147
  _opt?: CommonDBOptions,
148
148
  ): Promise<ROW[]> {
149
149
  const table = this.cfg.tablesPrefix + _table
150
- this.data[table] = 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
 
@@ -157,7 +157,7 @@ export class InMemoryDB implements CommonDB {
157
157
  _opt?: CommonDBSaveOptions<ROW>,
158
158
  ): Promise<void> {
159
159
  const table = this.cfg.tablesPrefix + _table
160
- this.data[table] = this.data[table] || {}
160
+ this.data[table] ||= {}
161
161
 
162
162
  rows.forEach(r => {
163
163
  if (!r.id) {
@@ -179,7 +179,11 @@ export class InMemoryDB implements CommonDB {
179
179
  })
180
180
  }
181
181
 
182
- async deleteByIds(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<number> {
182
+ async deleteByIds<ROW extends ObjectWithId>(
183
+ _table: string,
184
+ ids: ROW['id'][],
185
+ _opt?: CommonDBOptions,
186
+ ): Promise<number> {
183
187
  const table = this.cfg.tablesPrefix + _table
184
188
  this.data[table] = this.data[table] || {}
185
189
 
@@ -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: string[]): Promise<number> {
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: string[]): Promise<ROW[]> {
46
+ async getByIds<ROW extends ObjectWithId>(_table: string, _ids: ROW['id'][]): Promise<ROW[]> {
47
47
  return []
48
48
  }
49
49
 
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: string[],
52
+ ids: ROW['id'][],
53
53
  opt?: CommonDBOptions,
54
54
  ): Promise<ROW[]>
55
55
 
@@ -78,10 +78,14 @@ export interface CommonDB {
78
78
 
79
79
  // DELETE
80
80
  /**
81
- * @returns number of deleted items.
81
+ * Returns number of deleted items.
82
82
  * Not supported by all implementations (e.g Datastore will always return same number as number of ids).
83
83
  */
84
- deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>
84
+ deleteByIds<ROW extends ObjectWithId>(
85
+ table: string,
86
+ ids: ROW['id'][],
87
+ opt?: CommonDBOptions,
88
+ ): Promise<number>
85
89
 
86
90
  deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions): Promise<number>
87
91
 
@@ -10,27 +10,21 @@ import {
10
10
  import { CommonDB } from '../common.db'
11
11
  import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model'
12
12
 
13
- // Hook DBM, BM, TM types should follow this exact order
14
- export type CommonDaoCreateIdHook<BM, DBM> = (obj: DBM | BM) => string
15
- export type CommonDaoParseNaturalIdHook<DBM> = (id: string) => Partial<DBM>
16
- export type CommonDaoBeforeCreateHook<BM> = (bm: Partial<BM>) => Partial<BM>
17
- export type CommonDaoBeforeDBMValidateHook<DBM> = (dbm: Partial<DBM>) => Partial<DBM>
18
- export type CommonDaoBeforeDBMToBMHook<BM, DBM> = (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>
19
- export type CommonDaoBeforeBMToDBMHook<BM, DBM> = (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>
20
- export type CommonDaoBeforeTMToBMHook<BM, TM> = (tm: TM) => Partial<BM>
21
- export type CommonDaoBeforeBMToTMHook<BM, TM> = (bm: BM) => Partial<TM>
22
- export type CommonDaoAnonymizeHook<DBM> = (dbm: DBM) => DBM
23
-
24
- interface CommonDaoHooks<BM, DBM, TM> {
25
- createId: CommonDaoCreateIdHook<BM, DBM>
26
- parseNaturalId: CommonDaoParseNaturalIdHook<DBM>
27
- beforeCreate: CommonDaoBeforeCreateHook<BM>
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