@naturalcycles/db-lib 8.34.2 → 8.36.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) 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 +20 -20
  10. package/dist/commondao/common.dao.js +13 -9
  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/model.util.d.ts +2 -3
  16. package/dist/model.util.js +1 -7
  17. package/dist/pipeline/dbPipelineBackup.js +1 -2
  18. package/dist/pipeline/dbPipelineCopy.js +1 -2
  19. package/dist/pipeline/dbPipelineRestore.js +1 -2
  20. package/dist/query/dbQuery.d.ts +6 -6
  21. package/dist/validation/index.d.ts +3 -3
  22. package/package.json +1 -2
  23. package/src/adapter/cachedb/cache.db.model.ts +8 -7
  24. package/src/adapter/cachedb/cache.db.ts +20 -17
  25. package/src/adapter/file/file.db.ts +2 -2
  26. package/src/adapter/inmemory/inMemory.db.ts +9 -5
  27. package/src/base.common.db.ts +2 -2
  28. package/src/common.db.ts +7 -3
  29. package/src/commondao/common.dao.model.ts +31 -24
  30. package/src/commondao/common.dao.ts +51 -60
  31. package/src/db.model.ts +2 -18
  32. package/src/index.ts +2 -22
  33. package/src/model.util.ts +3 -9
  34. package/src/pipeline/dbPipelineBackup.ts +9 -3
  35. package/src/pipeline/dbPipelineCopy.ts +2 -3
  36. package/src/pipeline/dbPipelineRestore.ts +2 -2
  37. package/src/query/dbQuery.ts +8 -7
  38. package/dist/adapter/inmemory/index.d.ts +0 -3
  39. package/dist/adapter/inmemory/index.js +0 -8
  40. package/dist/getDB.d.ts +0 -19
  41. package/dist/getDB.js +0 -32
  42. package/src/adapter/inmemory/index.ts +0 -9
  43. package/src/getDB.ts +0 -50
package/src/index.ts CHANGED
@@ -6,25 +6,16 @@ import { DBLibError } from './cnst'
6
6
  import { CommonDB } from './common.db'
7
7
  import { CommonDao } from './commondao/common.dao'
8
8
  import {
9
- CommonDaoAnonymizeHook,
10
- CommonDaoBeforeBMToDBMHook,
11
- CommonDaoBeforeBMToTMHook,
12
- CommonDaoBeforeCreateHook,
13
- CommonDaoBeforeDBMToBMHook,
14
- CommonDaoBeforeDBMValidateHook,
15
- CommonDaoBeforeTMToBMHook,
16
9
  CommonDaoCfg,
17
- CommonDaoCreateIdHook,
18
10
  CommonDaoCreateOptions,
19
11
  CommonDaoLogLevel,
20
12
  CommonDaoOptions,
21
- CommonDaoParseNaturalIdHook,
22
13
  CommonDaoSaveOptions,
23
14
  CommonDaoStreamForEachOptions,
24
15
  CommonDaoStreamOptions,
16
+ CommonDaoHooks,
25
17
  } from './commondao/common.dao.model'
26
18
  import {
27
- CommonDBAdapter,
28
19
  CommonDBCreateOptions,
29
20
  CommonDBOptions,
30
21
  CommonDBSaveOptions,
@@ -36,7 +27,6 @@ import {
36
27
  DBSaveBatchOperation,
37
28
  RunQueryResult,
38
29
  } from './db.model'
39
- import { getDB } from './getDB'
40
30
  import { CommonKeyValueDao, CommonKeyValueDaoCfg } from './kv/commonKeyValueDao'
41
31
  import { CommonKeyValueDB, KeyValueDBTuple } from './kv/commonKeyValueDB'
42
32
  import {
@@ -69,6 +59,7 @@ export type {
69
59
  CommonDaoSaveOptions,
70
60
  CommonDaoStreamForEachOptions,
71
61
  CommonDaoStreamOptions,
62
+ CommonDaoHooks,
72
63
  CommonDBOptions,
73
64
  CommonDBSaveOptions,
74
65
  CommonDBStreamOptions,
@@ -76,21 +67,11 @@ export type {
76
67
  CommonDB,
77
68
  RunQueryResult,
78
69
  CommonDaoCfg,
79
- CommonDaoCreateIdHook,
80
- CommonDaoParseNaturalIdHook,
81
- CommonDaoBeforeCreateHook,
82
- CommonDaoBeforeDBMValidateHook,
83
- CommonDaoBeforeDBMToBMHook,
84
- CommonDaoBeforeBMToDBMHook,
85
- CommonDaoBeforeTMToBMHook,
86
- CommonDaoBeforeBMToTMHook,
87
- CommonDaoAnonymizeHook,
88
70
  InMemoryDBCfg,
89
71
  InMemoryKeyValueDBCfg,
90
72
  DBPipelineBackupOptions,
91
73
  DBPipelineRestoreOptions,
92
74
  DBPipelineCopyOptions,
93
- CommonDBAdapter,
94
75
  DBOperation,
95
76
  DBSaveBatchOperation,
96
77
  DBDeleteByIdsOperation,
@@ -117,7 +98,6 @@ export {
117
98
  dbPipelineBackup,
118
99
  dbPipelineRestore,
119
100
  dbPipelineCopy,
120
- getDB,
121
101
  DBLibError,
122
102
  BaseCommonDB,
123
103
  DBTransaction,
package/src/model.util.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { stringId } from '@naturalcycles/nodejs-lib'
2
- import { CreatedUpdated, CreatedUpdatedId, ObjectWithId } from '@naturalcycles/js-lib'
2
+ import { CreatedUpdated, CreatedUpdatedId } from '@naturalcycles/js-lib'
3
3
 
4
4
  export function createdUpdatedFields(
5
5
  existingObject?: Partial<CreatedUpdated> | null,
@@ -12,8 +12,8 @@ export function createdUpdatedFields(
12
12
  }
13
13
 
14
14
  export function createdUpdatedIdFields(
15
- existingObject?: Partial<CreatedUpdatedId> | null,
16
- ): CreatedUpdatedId {
15
+ existingObject?: Partial<CreatedUpdatedId<string>> | null,
16
+ ): CreatedUpdatedId<string> {
17
17
  const now = Math.floor(Date.now() / 1000)
18
18
  return {
19
19
  created: existingObject?.created || now,
@@ -22,12 +22,6 @@ export function createdUpdatedIdFields(
22
22
  }
23
23
  }
24
24
 
25
- export function idField(existingObject?: Partial<CreatedUpdatedId> | null): ObjectWithId {
26
- return {
27
- id: existingObject?.id || stringId(),
28
- }
29
- }
30
-
31
25
  export function deserializeJsonField<T = any>(f?: string): T {
32
26
  return JSON.parse(f || '{}')
33
27
  }
@@ -1,5 +1,12 @@
1
1
  import { createGzip, ZlibOptions } from 'zlib'
2
- import { AppError, AsyncMapper, ErrorMode, pMap, _passthroughMapper } from '@naturalcycles/js-lib'
2
+ import {
3
+ AppError,
4
+ AsyncMapper,
5
+ ErrorMode,
6
+ pMap,
7
+ _passthroughMapper,
8
+ localTime,
9
+ } from '@naturalcycles/js-lib'
3
10
  import {
4
11
  NDJsonStats,
5
12
  transformLogProgress,
@@ -11,7 +18,6 @@ import {
11
18
  _pipeline,
12
19
  } from '@naturalcycles/nodejs-lib'
13
20
  import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
14
- import { dayjs } from '@naturalcycles/time-lib'
15
21
  import * as fs from 'fs-extra'
16
22
  import { CommonDB } from '../common.db'
17
23
  import { DBQuery } from '../index'
@@ -144,7 +150,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
144
150
 
145
151
  let { tables } = opt
146
152
 
147
- const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(dayjs.unix(sinceUpdated).toPretty()) : ''
153
+ const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(localTime(sinceUpdated).toPretty()) : ''
148
154
 
149
155
  console.log(
150
156
  `>> ${dimWhite('dbPipelineBackup')} started in ${grey(outputDirPath)}...${sinceUpdatedStr}`,
@@ -1,4 +1,4 @@
1
- import { AsyncMapper, ErrorMode, pMap, _passthroughMapper } from '@naturalcycles/js-lib'
1
+ import { AsyncMapper, ErrorMode, pMap, _passthroughMapper, localTime } from '@naturalcycles/js-lib'
2
2
  import {
3
3
  NDJsonStats,
4
4
  transformBuffer,
@@ -11,7 +11,6 @@ import {
11
11
  _pipeline,
12
12
  } from '@naturalcycles/nodejs-lib'
13
13
  import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
14
- import { dayjs } from '@naturalcycles/time-lib'
15
14
  import { CommonDB } from '../common.db'
16
15
  import { CommonDBSaveOptions } from '../db.model'
17
16
  import { DBQuery } from '../query/dbQuery'
@@ -103,7 +102,7 @@ export async function dbPipelineCopy(opt: DBPipelineCopyOptions): Promise<NDJson
103
102
 
104
103
  let { tables } = opt
105
104
 
106
- const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(dayjs.unix(sinceUpdated).toPretty()) : ''
105
+ const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(localTime(sinceUpdated).toPretty()) : ''
107
106
 
108
107
  console.log(`>> ${dimWhite('dbPipelineCopy')} started...${sinceUpdatedStr}`)
109
108
 
@@ -7,6 +7,7 @@ import {
7
7
  _mapValues,
8
8
  _passthroughMapper,
9
9
  SavedDBEntity,
10
+ localTime,
10
11
  } from '@naturalcycles/js-lib'
11
12
  import {
12
13
  NDJsonStats,
@@ -24,7 +25,6 @@ import {
24
25
  _pipeline,
25
26
  } from '@naturalcycles/nodejs-lib'
26
27
  import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
27
- import { dayjs } from '@naturalcycles/time-lib'
28
28
  import * as fs from 'fs-extra'
29
29
  import { CommonDB } from '../common.db'
30
30
  import { CommonDBSaveOptions } from '../index'
@@ -133,7 +133,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
133
133
  const strict = errorMode !== ErrorMode.SUPPRESS
134
134
  const onlyTables = opt.tables && new Set(opt.tables)
135
135
 
136
- const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(dayjs.unix(sinceUpdated).toPretty()) : ''
136
+ const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(localTime(sinceUpdated).toPretty()) : ''
137
137
 
138
138
  console.log(
139
139
  `>> ${dimWhite('dbPipelineRestore')} started in ${grey(inputDirPath)}...${sinceUpdatedStr}`,
@@ -216,14 +216,15 @@ export class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
216
216
  * DBQuery that has additional method to support Fluent API style.
217
217
  */
218
218
  export class RunnableDBQuery<
219
- BM extends Partial<ObjectWithId>,
220
- DBM extends ObjectWithId,
219
+ BM extends Partial<ObjectWithId<ID>>,
220
+ DBM extends ObjectWithId<ID>,
221
221
  TM,
222
+ ID extends string | number,
222
223
  > extends DBQuery<DBM> {
223
224
  /**
224
225
  * Pass `table` to override table.
225
226
  */
226
- constructor(public dao: CommonDao<BM, DBM, TM>, table?: string) {
227
+ constructor(public dao: CommonDao<BM, DBM, TM, ID>, table?: string) {
227
228
  super(table || dao.cfg.table)
228
229
  }
229
230
 
@@ -281,17 +282,17 @@ export class RunnableDBQuery<
281
282
  return this.dao.streamQueryAsDBM(this, opt)
282
283
  }
283
284
 
284
- async queryIds(opt?: CommonDaoOptions): Promise<string[]> {
285
+ async queryIds(opt?: CommonDaoOptions): Promise<ID[]> {
285
286
  return await this.dao.queryIds(this, opt)
286
287
  }
287
288
 
288
- streamQueryIds(opt?: CommonDaoStreamOptions): ReadableTyped<string> {
289
+ streamQueryIds(opt?: CommonDaoStreamOptions): ReadableTyped<ID> {
289
290
  return this.dao.streamQueryIds(this, opt)
290
291
  }
291
292
 
292
293
  async streamQueryIdsForEach(
293
- mapper: AsyncMapper<string, void>,
294
- opt?: CommonDaoStreamForEachOptions<string>,
294
+ mapper: AsyncMapper<ID, void>,
295
+ opt?: CommonDaoStreamForEachOptions<ID>,
295
296
  ): Promise<void> {
296
297
  await this.dao.streamQueryIdsForEach(this, mapper, opt)
297
298
  }
@@ -1,3 +0,0 @@
1
- import { CommonDB } from '../../common.db';
2
- export declare function getDBAdapter(): CommonDB;
3
- export {};
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDBAdapter = void 0;
4
- const inMemory_db_1 = require("./inMemory.db");
5
- function getDBAdapter() {
6
- return new inMemory_db_1.InMemoryDB();
7
- }
8
- exports.getDBAdapter = getDBAdapter;
package/dist/getDB.d.ts DELETED
@@ -1,19 +0,0 @@
1
- import { CommonDB } from './common.db';
2
- /**
3
- * Returns pre-configured DB
4
- *
5
- * @param index defaults to 1
6
- *
7
- * Requires process.env.DB${index} to be set to the name of the adapter, e.g `mysql-lib` or `db-lib/adapter/inmemory`.
8
- * Requires (by most adapters) process.env.SECRET_DB${index} to contain a "connection string" to that DB. Usually a JSON.stringified object,
9
- * but depends on the adapter.
10
- */
11
- export declare function getDB(index?: number): CommonDB;
12
- declare global {
13
- namespace NodeJS {
14
- interface ProcessEnv {
15
- DB1?: string;
16
- DB2?: string;
17
- }
18
- }
19
- }
package/dist/getDB.js DELETED
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDB = void 0;
4
- const js_lib_1 = require("@naturalcycles/js-lib");
5
- const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
6
- const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
7
- /**
8
- * Returns pre-configured DB
9
- *
10
- * @param index defaults to 1
11
- *
12
- * Requires process.env.DB${index} to be set to the name of the adapter, e.g `mysql-lib` or `db-lib/adapter/inmemory`.
13
- * Requires (by most adapters) process.env.SECRET_DB${index} to contain a "connection string" to that DB. Usually a JSON.stringified object,
14
- * but depends on the adapter.
15
- */
16
- function getDB(index = 1) {
17
- return _getDB(index);
18
- }
19
- exports.getDB = getDB;
20
- // Extra function to provide index=1 as default (since memo doesn't work well with default arguments)
21
- const _getDB = (0, js_lib_1._memoFn)((index) => {
22
- const libName = process.env[`DB${index}`];
23
- if (!libName) {
24
- throw new Error(`getDB(${(0, colors_1.yellow)(index)}), but process.env.${(0, colors_1.white)('DB' + index)} is not defined!`);
25
- }
26
- const lib = require(libName);
27
- if (!lib.getDBAdapter) {
28
- throw new Error(`DB${index}=${libName}, but require('${libName}').getDBAdapter() is not defined`);
29
- }
30
- const cfg = (0, nodejs_lib_1.secretOptional)(`SECRET_DB${index}`);
31
- return lib.getDBAdapter(cfg);
32
- });
@@ -1,9 +0,0 @@
1
- import { CommonDB } from '../../common.db'
2
- import { InMemoryDB } from './inMemory.db'
3
-
4
- export function getDBAdapter(): CommonDB {
5
- return new InMemoryDB()
6
- }
7
-
8
- export // InMemoryDB, // no, otherwise it's double-exported, which can confuse IDEs
9
- {}
package/src/getDB.ts DELETED
@@ -1,50 +0,0 @@
1
- import { _memoFn } from '@naturalcycles/js-lib'
2
- import { secretOptional } from '@naturalcycles/nodejs-lib'
3
- import { white, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
4
- import { CommonDB } from './common.db'
5
- import { CommonDBAdapter } from './db.model'
6
-
7
- /**
8
- * Returns pre-configured DB
9
- *
10
- * @param index defaults to 1
11
- *
12
- * Requires process.env.DB${index} to be set to the name of the adapter, e.g `mysql-lib` or `db-lib/adapter/inmemory`.
13
- * Requires (by most adapters) process.env.SECRET_DB${index} to contain a "connection string" to that DB. Usually a JSON.stringified object,
14
- * but depends on the adapter.
15
- */
16
- export function getDB(index = 1): CommonDB {
17
- return _getDB(index)
18
- }
19
-
20
- // Extra function to provide index=1 as default (since memo doesn't work well with default arguments)
21
- const _getDB = _memoFn((index: number) => {
22
- const libName = process.env[`DB${index}`]
23
-
24
- if (!libName) {
25
- throw new Error(
26
- `getDB(${yellow(index)}), but process.env.${white('DB' + index)} is not defined!`,
27
- )
28
- }
29
-
30
- const lib: CommonDBAdapter = require(libName)
31
-
32
- if (!lib.getDBAdapter) {
33
- throw new Error(
34
- `DB${index}=${libName}, but require('${libName}').getDBAdapter() is not defined`,
35
- )
36
- }
37
-
38
- const cfg = secretOptional(`SECRET_DB${index}`)
39
-
40
- return lib.getDBAdapter(cfg)
41
- })
42
-
43
- declare global {
44
- namespace NodeJS {
45
- interface ProcessEnv {
46
- DB1?: string
47
- DB2?: string
48
- }
49
- }
50
- }