@naturalcycles/db-lib 10.0.2 → 10.1.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 (110) hide show
  1. package/dist/adapter/cachedb/cache.db.d.ts +5 -5
  2. package/dist/adapter/cachedb/cache.db.js +8 -12
  3. package/dist/adapter/cachedb/cache.db.model.d.ts +2 -2
  4. package/dist/adapter/cachedb/cache.db.model.js +1 -2
  5. package/dist/adapter/cachedb/index.d.ts +2 -2
  6. package/dist/adapter/cachedb/index.js +2 -5
  7. package/dist/adapter/file/file.db.d.ts +5 -6
  8. package/dist/adapter/file/file.db.js +25 -27
  9. package/dist/adapter/file/file.db.model.d.ts +2 -2
  10. package/dist/adapter/file/file.db.model.js +1 -2
  11. package/dist/adapter/file/inMemory.persistence.plugin.d.ts +2 -2
  12. package/dist/adapter/file/inMemory.persistence.plugin.js +3 -7
  13. package/dist/adapter/file/index.d.ts +3 -3
  14. package/dist/adapter/file/index.js +3 -7
  15. package/dist/adapter/file/localFile.persistence.plugin.d.ts +2 -2
  16. package/dist/adapter/file/localFile.persistence.plugin.js +11 -15
  17. package/dist/adapter/file/noop.persistence.plugin.d.ts +2 -2
  18. package/dist/adapter/file/noop.persistence.plugin.js +1 -5
  19. package/dist/adapter/inmemory/inMemory.db.d.ts +5 -4
  20. package/dist/adapter/inmemory/inMemory.db.js +43 -41
  21. package/dist/adapter/inmemory/inMemoryKeyValueDB.d.ts +2 -2
  22. package/dist/adapter/inmemory/inMemoryKeyValueDB.js +7 -11
  23. package/dist/adapter/inmemory/queryInMemory.d.ts +1 -1
  24. package/dist/adapter/inmemory/queryInMemory.js +4 -7
  25. package/dist/base.common.db.d.ts +6 -4
  26. package/dist/base.common.db.js +8 -9
  27. package/dist/cnst.js +2 -5
  28. package/dist/common.db.d.ts +8 -2
  29. package/dist/common.db.js +3 -6
  30. package/dist/commondao/common.dao.d.ts +11 -4
  31. package/dist/commondao/common.dao.js +120 -113
  32. package/dist/commondao/common.dao.model.d.ts +2 -2
  33. package/dist/commondao/common.dao.model.js +2 -5
  34. package/dist/db.model.d.ts +6 -1
  35. package/dist/db.model.js +4 -7
  36. package/dist/index.d.ts +17 -17
  37. package/dist/index.js +17 -20
  38. package/dist/kv/commonKeyValueDB.d.ts +1 -1
  39. package/dist/kv/commonKeyValueDB.js +1 -4
  40. package/dist/kv/commonKeyValueDao.d.ts +3 -3
  41. package/dist/kv/commonKeyValueDao.js +10 -14
  42. package/dist/kv/commonKeyValueDaoMemoCache.d.ts +1 -1
  43. package/dist/kv/commonKeyValueDaoMemoCache.js +4 -8
  44. package/dist/model.util.js +9 -15
  45. package/dist/pipeline/dbPipelineBackup.d.ts +2 -2
  46. package/dist/pipeline/dbPipelineBackup.js +27 -30
  47. package/dist/pipeline/dbPipelineCopy.d.ts +2 -2
  48. package/dist/pipeline/dbPipelineCopy.js +20 -23
  49. package/dist/pipeline/dbPipelineRestore.d.ts +2 -2
  50. package/dist/pipeline/dbPipelineRestore.js +27 -30
  51. package/dist/query/dbQuery.d.ts +3 -3
  52. package/dist/query/dbQuery.js +7 -12
  53. package/dist/testing/{dbTest.d.ts → commonDBTest.d.ts} +1 -1
  54. package/dist/testing/{dbTest.js → commonDBTest.js} +81 -62
  55. package/dist/testing/commonDaoTest.d.ts +3 -0
  56. package/dist/testing/{daoTest.js → commonDaoTest.js} +101 -38
  57. package/dist/testing/index.d.ts +7 -7
  58. package/dist/testing/index.js +6 -20
  59. package/dist/testing/keyValueDBTest.d.ts +1 -1
  60. package/dist/testing/keyValueDBTest.js +28 -31
  61. package/dist/testing/keyValueDaoTest.d.ts +1 -1
  62. package/dist/testing/keyValueDaoTest.js +8 -11
  63. package/dist/testing/test.model.js +30 -37
  64. package/dist/testing/timeSeriesTest.util.d.ts +1 -1
  65. package/dist/testing/timeSeriesTest.util.js +3 -6
  66. package/dist/timeseries/commonTimeSeriesDao.d.ts +1 -1
  67. package/dist/timeseries/commonTimeSeriesDao.js +5 -9
  68. package/dist/timeseries/timeSeries.model.d.ts +1 -1
  69. package/dist/timeseries/timeSeries.model.js +1 -2
  70. package/dist/transaction/dbTransaction.util.d.ts +3 -2
  71. package/dist/transaction/dbTransaction.util.js +2 -5
  72. package/dist/validation/index.d.ts +2 -2
  73. package/dist/validation/index.js +25 -28
  74. package/package.json +3 -3
  75. package/src/adapter/cachedb/cache.db.model.ts +2 -2
  76. package/src/adapter/cachedb/cache.db.ts +6 -6
  77. package/src/adapter/cachedb/index.ts +2 -2
  78. package/src/adapter/file/file.db.model.ts +2 -2
  79. package/src/adapter/file/file.db.ts +8 -6
  80. package/src/adapter/file/inMemory.persistence.plugin.ts +2 -2
  81. package/src/adapter/file/index.ts +3 -3
  82. package/src/adapter/file/localFile.persistence.plugin.ts +2 -2
  83. package/src/adapter/file/noop.persistence.plugin.ts +2 -2
  84. package/src/adapter/inmemory/inMemory.db.ts +15 -10
  85. package/src/adapter/inmemory/inMemoryKeyValueDB.ts +7 -3
  86. package/src/adapter/inmemory/queryInMemory.ts +1 -1
  87. package/src/base.common.db.ts +9 -5
  88. package/src/common.db.ts +10 -2
  89. package/src/commondao/common.dao.model.ts +2 -2
  90. package/src/commondao/common.dao.ts +20 -6
  91. package/src/db.model.ts +7 -1
  92. package/src/index.ts +17 -17
  93. package/src/kv/commonKeyValueDB.ts +1 -1
  94. package/src/kv/commonKeyValueDao.ts +3 -3
  95. package/src/kv/commonKeyValueDaoMemoCache.ts +1 -1
  96. package/src/pipeline/dbPipelineBackup.ts +2 -2
  97. package/src/pipeline/dbPipelineCopy.ts +3 -3
  98. package/src/pipeline/dbPipelineRestore.ts +2 -2
  99. package/src/query/dbQuery.ts +3 -3
  100. package/src/testing/{dbTest.ts → commonDBTest.ts} +34 -6
  101. package/src/testing/{daoTest.ts → commonDaoTest.ts} +89 -11
  102. package/src/testing/index.ts +7 -7
  103. package/src/testing/keyValueDBTest.ts +2 -2
  104. package/src/testing/keyValueDaoTest.ts +3 -3
  105. package/src/testing/timeSeriesTest.util.ts +1 -1
  106. package/src/timeseries/commonTimeSeriesDao.ts +2 -2
  107. package/src/timeseries/timeSeries.model.ts +1 -1
  108. package/src/transaction/dbTransaction.util.ts +3 -2
  109. package/src/validation/index.ts +8 -3
  110. package/dist/testing/daoTest.d.ts +0 -3
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CommonTimeSeriesDao = void 0;
4
- const js_lib_1 = require("@naturalcycles/js-lib");
5
- const dbQuery_1 = require("../query/dbQuery");
1
+ import { _isTruthy } from '@naturalcycles/js-lib';
2
+ import { DBQuery } from '../query/dbQuery.js';
6
3
  const _TIMESERIES_RAW = '_TIMESERIES_RAW';
7
4
  /**
8
5
  * TimeSeries DB implementation based on provided CommonDB database.
@@ -10,7 +7,7 @@ const _TIMESERIES_RAW = '_TIMESERIES_RAW';
10
7
  *
11
8
  * @experimental
12
9
  */
13
- class CommonTimeSeriesDao {
10
+ export class CommonTimeSeriesDao {
14
11
  cfg;
15
12
  constructor(cfg) {
16
13
  this.cfg = cfg;
@@ -21,7 +18,7 @@ class CommonTimeSeriesDao {
21
18
  async getSeries() {
22
19
  return (await this.cfg.db.getTables())
23
20
  .map(t => /^(.*)_TIMESERIES_RAW$/.exec(t)?.[1])
24
- .filter(js_lib_1._isTruthy);
21
+ .filter(_isTruthy);
25
22
  }
26
23
  // convenience method
27
24
  async save(series, tsMillis, value) {
@@ -62,7 +59,7 @@ class CommonTimeSeriesDao {
62
59
  await this.saveBatch(series, ids.map(id => [id, null]));
63
60
  }
64
61
  async query(q) {
65
- const dbq = dbQuery_1.DBQuery.create(`${q.series}${_TIMESERIES_RAW}`).order('ts');
62
+ const dbq = DBQuery.create(`${q.series}${_TIMESERIES_RAW}`).order('ts');
66
63
  if (q.fromIncl)
67
64
  dbq.filter('ts', '>=', q.fromIncl);
68
65
  if (q.toExcl)
@@ -77,4 +74,3 @@ class CommonTimeSeriesDao {
77
74
  // todo
78
75
  }
79
76
  }
80
- exports.CommonTimeSeriesDao = CommonTimeSeriesDao;
@@ -1,4 +1,4 @@
1
- import type { CommonDB } from '../common.db';
1
+ import type { CommonDB } from '../common.db.js';
2
2
  export interface CommonTimeSeriesDaoCfg {
3
3
  db: CommonDB;
4
4
  }
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,6 +1,6 @@
1
1
  import type { ObjectWithId } from '@naturalcycles/js-lib';
2
- import type { CommonDB } from '../common.db';
3
- import type { CommonDBOptions, CommonDBSaveOptions, DBTransaction } from '../db.model';
2
+ import type { CommonDB } from '../common.db.js';
3
+ import type { CommonDBOptions, CommonDBSaveOptions, DBTransaction } from '../db.model.js';
4
4
  /**
5
5
  * Optimizes the Transaction (list of DBOperations) to do less operations.
6
6
  * E.g if you save id1 first and then delete it - this function will turn it into a no-op (self-eliminate).
@@ -21,6 +21,7 @@ import type { CommonDBOptions, CommonDBSaveOptions, DBTransaction } from '../db.
21
21
  export declare class FakeDBTransaction implements DBTransaction {
22
22
  protected db: CommonDB;
23
23
  constructor(db: CommonDB);
24
+ commit(): Promise<void>;
24
25
  rollback(): Promise<void>;
25
26
  getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
26
27
  saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FakeDBTransaction = void 0;
4
1
  /**
5
2
  * Optimizes the Transaction (list of DBOperations) to do less operations.
6
3
  * E.g if you save id1 first and then delete it - this function will turn it into a no-op (self-eliminate).
@@ -90,12 +87,13 @@ export function mergeDBOperations(ops: DBOperation[]): DBOperation[] {
90
87
  * Fake implementation of DBTransactionContext,
91
88
  * which executes all operations instantly, without any Transaction involved.
92
89
  */
93
- class FakeDBTransaction {
90
+ export class FakeDBTransaction {
94
91
  db;
95
92
  constructor(db) {
96
93
  this.db = db;
97
94
  }
98
95
  // no-op
96
+ async commit() { }
99
97
  async rollback() { }
100
98
  async getByIds(table, ids, opt) {
101
99
  return await this.db.getByIds(table, ids, opt);
@@ -113,4 +111,3 @@ class FakeDBTransaction {
113
111
  return await this.db.deleteByIds(table, ids, opt);
114
112
  }
115
113
  }
116
- exports.FakeDBTransaction = FakeDBTransaction;
@@ -1,5 +1,5 @@
1
- import type { CommonDBOptions, CommonDBSaveOptions } from '../db.model';
2
- import type { DBQuery, DBQueryFilter, DBQueryFilterOperator, DBQueryOrder } from '../query/dbQuery';
1
+ import type { CommonDBOptions, CommonDBSaveOptions } from '../db.model.js';
2
+ import type { DBQuery, DBQueryFilter, DBQueryFilterOperator, DBQueryOrder } from '../query/dbQuery.js';
3
3
  export declare const commonDBOptionsSchema: import("joi").ObjectSchema<CommonDBOptions>;
4
4
  export declare const commonDBSaveOptionsSchema: import("joi").ObjectSchema<CommonDBSaveOptions<any>>;
5
5
  export declare const dbQueryFilterOperatorSchema: import("@naturalcycles/nodejs-lib").StringSchema<DBQueryFilterOperator>;
@@ -1,32 +1,29 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dbQuerySchema = exports.dbQueryOrderSchema = exports.dbQueryFilterSchema = exports.dbQueryFilterOperatorSchema = exports.commonDBSaveOptionsSchema = exports.commonDBOptionsSchema = void 0;
4
- const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
5
- const dbQuery_1 = require("../query/dbQuery");
6
- exports.commonDBOptionsSchema = (0, nodejs_lib_1.objectSchema)({
7
- ['onlyCache']: nodejs_lib_1.booleanSchema.optional(),
8
- ['skipCache']: nodejs_lib_1.booleanSchema.optional(),
1
+ import { anySchema, arraySchema, booleanSchema, integerSchema, Joi, objectSchema, stringSchema, } from '@naturalcycles/nodejs-lib';
2
+ import { dbQueryFilterOperatorValues } from '../query/dbQuery.js';
3
+ export const commonDBOptionsSchema = objectSchema({
4
+ ['onlyCache']: booleanSchema.optional(),
5
+ ['skipCache']: booleanSchema.optional(),
9
6
  });
10
- exports.commonDBSaveOptionsSchema = (0, nodejs_lib_1.objectSchema)({
11
- excludeFromIndexes: (0, nodejs_lib_1.arraySchema)(nodejs_lib_1.stringSchema).optional(),
12
- }).concat(exports.commonDBOptionsSchema);
13
- exports.dbQueryFilterOperatorSchema = nodejs_lib_1.Joi.string().valid(...dbQuery_1.dbQueryFilterOperatorValues);
14
- exports.dbQueryFilterSchema = (0, nodejs_lib_1.objectSchema)({
15
- name: nodejs_lib_1.stringSchema,
16
- op: exports.dbQueryFilterOperatorSchema,
17
- val: nodejs_lib_1.anySchema,
7
+ export const commonDBSaveOptionsSchema = objectSchema({
8
+ excludeFromIndexes: arraySchema(stringSchema).optional(),
9
+ }).concat(commonDBOptionsSchema);
10
+ export const dbQueryFilterOperatorSchema = Joi.string().valid(...dbQueryFilterOperatorValues);
11
+ export const dbQueryFilterSchema = objectSchema({
12
+ name: stringSchema,
13
+ op: dbQueryFilterOperatorSchema,
14
+ val: anySchema,
18
15
  });
19
- exports.dbQueryOrderSchema = (0, nodejs_lib_1.objectSchema)({
20
- name: nodejs_lib_1.stringSchema,
21
- descending: nodejs_lib_1.booleanSchema.optional(),
16
+ export const dbQueryOrderSchema = objectSchema({
17
+ name: stringSchema,
18
+ descending: booleanSchema.optional(),
22
19
  });
23
- exports.dbQuerySchema = (0, nodejs_lib_1.objectSchema)({
24
- table: nodejs_lib_1.stringSchema,
25
- _filters: (0, nodejs_lib_1.arraySchema)(exports.dbQueryFilterSchema).optional(),
26
- _limitValue: nodejs_lib_1.integerSchema.min(0).optional(),
27
- _offsetValue: nodejs_lib_1.integerSchema.min(0).optional(),
28
- _orders: (0, nodejs_lib_1.arraySchema)(exports.dbQueryOrderSchema).optional(),
29
- _startCursor: nodejs_lib_1.stringSchema.optional(),
30
- _endCursor: nodejs_lib_1.stringSchema.optional(),
31
- _selectedFieldNames: (0, nodejs_lib_1.arraySchema)(nodejs_lib_1.stringSchema).optional(),
20
+ export const dbQuerySchema = objectSchema({
21
+ table: stringSchema,
22
+ _filters: arraySchema(dbQueryFilterSchema).optional(),
23
+ _limitValue: integerSchema.min(0).optional(),
24
+ _offsetValue: integerSchema.min(0).optional(),
25
+ _orders: arraySchema(dbQueryOrderSchema).optional(),
26
+ _startCursor: stringSchema.optional(),
27
+ _endCursor: stringSchema.optional(),
28
+ _selectedFieldNames: arraySchema(stringSchema).optional(),
32
29
  });
package/package.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
+ "type": "module",
3
4
  "scripts": {
4
5
  "prepare": "husky",
5
6
  "build": "dev-lib build",
@@ -13,7 +14,7 @@
13
14
  "@naturalcycles/nodejs-lib": "^13"
14
15
  },
15
16
  "devDependencies": {
16
- "@naturalcycles/bench-lib": "^3",
17
+ "@naturalcycles/bench-lib": "^4",
17
18
  "@naturalcycles/dev-lib": "^17",
18
19
  "@types/node": "^22",
19
20
  "@vitest/coverage-v8": "^3",
@@ -47,7 +48,7 @@
47
48
  "engines": {
48
49
  "node": ">=22.12.0"
49
50
  },
50
- "version": "10.0.2",
51
+ "version": "10.1.0",
51
52
  "description": "Lowest Common Denominator API to supported Databases",
52
53
  "keywords": [
53
54
  "db",
@@ -61,7 +62,6 @@
61
62
  "firestore",
62
63
  "mysql"
63
64
  ],
64
- "type": "commonjs",
65
65
  "author": "Natural Cycles Team",
66
66
  "license": "MIT"
67
67
  }
@@ -1,11 +1,11 @@
1
1
  import type { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'
2
- import type { CommonDB } from '../../common.db'
2
+ import type { CommonDB } from '../../common.db.js'
3
3
  import type {
4
4
  CommonDBCreateOptions,
5
5
  CommonDBOptions,
6
6
  CommonDBSaveOptions,
7
7
  CommonDBStreamOptions,
8
- } from '../../db.model'
8
+ } from '../../db.model.js'
9
9
 
10
10
  export interface CacheDBCfg {
11
11
  name: string
@@ -7,18 +7,18 @@ import type {
7
7
  } from '@naturalcycles/js-lib'
8
8
  import { _isTruthy } from '@naturalcycles/js-lib'
9
9
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib'
10
- import { BaseCommonDB } from '../../base.common.db'
11
- import type { CommonDB, CommonDBSupport } from '../../common.db'
12
- import { commonDBFullSupport } from '../../common.db'
13
- import type { RunQueryResult } from '../../db.model'
14
- import type { DBQuery } from '../../query/dbQuery'
10
+ import { BaseCommonDB } from '../../base.common.db.js'
11
+ import type { CommonDB, CommonDBSupport } from '../../common.db.js'
12
+ import { commonDBFullSupport } from '../../common.db.js'
13
+ import type { RunQueryResult } from '../../db.model.js'
14
+ import type { DBQuery } from '../../query/dbQuery.js'
15
15
  import type {
16
16
  CacheDBCfg,
17
17
  CacheDBCreateOptions,
18
18
  CacheDBOptions,
19
19
  CacheDBSaveOptions,
20
20
  CacheDBStreamOptions,
21
- } from './cache.db.model'
21
+ } from './cache.db.model.js'
22
22
 
23
23
  /**
24
24
  * CommonDB implementation that proxies requests to downstream CommonDB
@@ -1,10 +1,10 @@
1
- import { CacheDB } from './cache.db'
1
+ import { CacheDB } from './cache.db.js'
2
2
  import type {
3
3
  CacheDBCfg,
4
4
  CacheDBCreateOptions,
5
5
  CacheDBOptions,
6
6
  CacheDBStreamOptions,
7
- } from './cache.db.model'
7
+ } from './cache.db.model.js'
8
8
 
9
9
  export type { CacheDBCfg, CacheDBCreateOptions, CacheDBOptions, CacheDBStreamOptions }
10
10
 
@@ -1,6 +1,6 @@
1
1
  import type { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'
2
- import type { DBSaveBatchOperation } from '../../db.model'
3
- import type { DBQueryOrder } from '../../query/dbQuery'
2
+ import type { DBSaveBatchOperation } from '../../db.model.js'
3
+ import type { DBQueryOrder } from '../../query/dbQuery.js'
4
4
 
5
5
  export interface FileDBPersistencePlugin {
6
6
  ping: () => Promise<void>
@@ -13,17 +13,19 @@ import {
13
13
  } from '@naturalcycles/js-lib'
14
14
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib'
15
15
  import { dimGrey, readableCreate } from '@naturalcycles/nodejs-lib'
16
- import type { CommonDBSupport, DBSaveBatchOperation } from '../..'
17
- import { BaseCommonDB, commonDBFullSupport, queryInMemory } from '../..'
18
- import type { CommonDB } from '../../common.db'
16
+ import { BaseCommonDB } from '../../base.common.db.js'
17
+ import type { CommonDB, CommonDBSupport } from '../../common.db.js'
18
+ import { commonDBFullSupport } from '../../common.db.js'
19
19
  import type {
20
20
  CommonDBOptions,
21
21
  CommonDBSaveOptions,
22
22
  CommonDBStreamOptions,
23
+ DBSaveBatchOperation,
23
24
  RunQueryResult,
24
- } from '../../db.model'
25
- import type { DBQuery } from '../../query/dbQuery'
26
- import type { FileDBCfg } from './file.db.model'
25
+ } from '../../db.model.js'
26
+ import type { DBQuery } from '../../query/dbQuery.js'
27
+ import { queryInMemory } from '../inmemory/queryInMemory.js'
28
+ import type { FileDBCfg } from './file.db.model.js'
27
29
 
28
30
  /**
29
31
  * Provides barebone implementation for "whole file" based CommonDB.
@@ -1,7 +1,7 @@
1
1
  import type { ObjectWithId, StringMap } from '@naturalcycles/js-lib'
2
2
  import { _by } from '@naturalcycles/js-lib'
3
- import type { DBSaveBatchOperation } from '../../db.model'
4
- import type { FileDBPersistencePlugin } from './file.db.model'
3
+ import type { DBSaveBatchOperation } from '../../db.model.js'
4
+ import type { FileDBPersistencePlugin } from './file.db.model.js'
5
5
 
6
6
  /**
7
7
  * Mostly useful for testing.
@@ -1,6 +1,6 @@
1
- import { FileDB } from './file.db'
2
- import type { FileDBCfg, FileDBPersistencePlugin } from './file.db.model'
3
- import { LocalFilePersistencePlugin } from './localFile.persistence.plugin'
1
+ import { FileDB } from './file.db.js'
2
+ import type { FileDBCfg, FileDBPersistencePlugin } from './file.db.model.js'
3
+ import { LocalFilePersistencePlugin } from './localFile.persistence.plugin.js'
4
4
 
5
5
  export type { FileDBCfg, FileDBPersistencePlugin }
6
6
  export { FileDB, LocalFilePersistencePlugin }
@@ -2,8 +2,8 @@ import { Readable } from 'node:stream'
2
2
  import type { ObjectWithId } from '@naturalcycles/js-lib'
3
3
  import { pMap } from '@naturalcycles/js-lib'
4
4
  import { _pipeline, fs2 } from '@naturalcycles/nodejs-lib'
5
- import type { DBSaveBatchOperation } from '../../db.model'
6
- import type { FileDBPersistencePlugin } from './file.db.model'
5
+ import type { DBSaveBatchOperation } from '../../db.model.js'
6
+ import type { FileDBPersistencePlugin } from './file.db.model.js'
7
7
 
8
8
  export interface LocalFilePersistencePluginCfg {
9
9
  /**
@@ -1,6 +1,6 @@
1
1
  import type { ObjectWithId } from '@naturalcycles/js-lib'
2
- import type { DBSaveBatchOperation } from '../../db.model'
3
- import type { FileDBPersistencePlugin } from './file.db.model'
2
+ import type { DBSaveBatchOperation } from '../../db.model.js'
3
+ import type { FileDBPersistencePlugin } from './file.db.model.js'
4
4
 
5
5
  export class NoopPersistencePlugin implements FileDBPersistencePlugin {
6
6
  async ping(): Promise<void> {}
@@ -22,22 +22,20 @@ import {
22
22
  } from '@naturalcycles/js-lib'
23
23
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib'
24
24
  import { _pipeline, bufferReviver, dimGrey, fs2, yellow } from '@naturalcycles/nodejs-lib'
25
- import type {
26
- CommonDB,
27
- CommonDBSupport,
28
- CommonDBTransactionOptions,
29
- DBOperation,
30
- DBTransactionFn,
31
- } from '../..'
32
- import { commonDBFullSupport, CommonDBType, queryInMemory } from '../..'
25
+ import type { CommonDB, CommonDBSupport } from '../../common.db.js'
26
+ import { commonDBFullSupport, CommonDBType } from '../../common.db.js'
33
27
  import type {
34
28
  CommonDBCreateOptions,
35
29
  CommonDBOptions,
36
30
  CommonDBSaveOptions,
31
+ CommonDBTransactionOptions,
32
+ DBOperation,
37
33
  DBTransaction,
34
+ DBTransactionFn,
38
35
  RunQueryResult,
39
- } from '../../db.model'
40
- import type { DBQuery } from '../../query/dbQuery'
36
+ } from '../../db.model.js'
37
+ import type { DBQuery } from '../../query/dbQuery.js'
38
+ import { queryInMemory } from './queryInMemory.js'
41
39
 
42
40
  export interface InMemoryDBCfg {
43
41
  /**
@@ -281,6 +279,13 @@ export class InMemoryDB implements CommonDB {
281
279
  }
282
280
  }
283
281
 
282
+ async createTransaction(opt: CommonDBTransactionOptions = {}): Promise<DBTransaction> {
283
+ return new InMemoryDBTransaction(this, {
284
+ readOnly: false,
285
+ ...opt,
286
+ })
287
+ }
288
+
284
289
  async incrementBatch(
285
290
  table: string,
286
291
  prop: string,
@@ -1,9 +1,13 @@
1
1
  import { Readable } from 'node:stream'
2
2
  import type { StringMap } from '@naturalcycles/js-lib'
3
3
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib'
4
- import type { CommonDBCreateOptions } from '../../db.model'
5
- import type { CommonKeyValueDB, IncrementTuple, KeyValueDBTuple } from '../../kv/commonKeyValueDB'
6
- import { commonKeyValueDBFullSupport } from '../../kv/commonKeyValueDB'
4
+ import type { CommonDBCreateOptions } from '../../db.model.js'
5
+ import type {
6
+ CommonKeyValueDB,
7
+ IncrementTuple,
8
+ KeyValueDBTuple,
9
+ } from '../../kv/commonKeyValueDB.js'
10
+ import { commonKeyValueDBFullSupport } from '../../kv/commonKeyValueDB.js'
7
11
 
8
12
  export interface InMemoryKeyValueDBCfg {}
9
13
 
@@ -1,6 +1,6 @@
1
1
  import type { ObjectWithId } from '@naturalcycles/js-lib'
2
2
  import { _get, _pick } from '@naturalcycles/js-lib'
3
- import type { DBQuery, DBQueryFilterOperator } from '../../query/dbQuery'
3
+ import type { DBQuery, DBQueryFilterOperator } from '../../query/dbQuery.js'
4
4
 
5
5
  type FilterFn = (v: any, val: any) => boolean
6
6
  const FILTER_FNS: Record<DBQueryFilterOperator, FilterFn> = {
@@ -5,17 +5,17 @@ import type {
5
5
  StringMap,
6
6
  } from '@naturalcycles/js-lib'
7
7
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib'
8
- import type { CommonDB, CommonDBSupport } from './common.db'
9
- import { CommonDBType } from './common.db'
8
+ import type { CommonDB, CommonDBSupport } from './common.db.js'
9
+ import { CommonDBType } from './common.db.js'
10
10
  import type {
11
11
  CommonDBOptions,
12
12
  CommonDBSaveOptions,
13
13
  CommonDBTransactionOptions,
14
14
  DBTransactionFn,
15
15
  RunQueryResult,
16
- } from './db.model'
17
- import type { DBQuery } from './query/dbQuery'
18
- import { FakeDBTransaction } from './transaction/dbTransaction.util'
16
+ } from './db.model.js'
17
+ import type { DBQuery } from './query/dbQuery.js'
18
+ import { FakeDBTransaction } from './transaction/dbTransaction.util.js'
19
19
 
20
20
  /**
21
21
  * No-op implementation of CommonDB interface.
@@ -93,6 +93,10 @@ export class BaseCommonDB implements CommonDB {
93
93
  // there's no try/catch and rollback, as there's nothing to rollback
94
94
  }
95
95
 
96
+ async createTransaction(_opt?: CommonDBTransactionOptions): Promise<FakeDBTransaction> {
97
+ return new FakeDBTransaction(this)
98
+ }
99
+
96
100
  async incrementBatch(
97
101
  _table: string,
98
102
  _prop: string,
package/src/common.db.ts CHANGED
@@ -12,10 +12,11 @@ import type {
12
12
  CommonDBSaveOptions,
13
13
  CommonDBStreamOptions,
14
14
  CommonDBTransactionOptions,
15
+ DBTransaction,
15
16
  DBTransactionFn,
16
17
  RunQueryResult,
17
- } from './db.model'
18
- import type { DBQuery } from './query/dbQuery'
18
+ } from './db.model.js'
19
+ import type { DBQuery } from './query/dbQuery.js'
19
20
 
20
21
  export enum CommonDBType {
21
22
  'document' = 'document',
@@ -151,6 +152,13 @@ export interface CommonDB {
151
152
  */
152
153
  runInTransaction: (fn: DBTransactionFn, opt?: CommonDBTransactionOptions) => Promise<void>
153
154
 
155
+ /**
156
+ * Experimental API to support more manual transaction control.
157
+ *
158
+ * @experimental
159
+ */
160
+ createTransaction: (opt?: CommonDBTransactionOptions) => Promise<DBTransaction>
161
+
154
162
  /**
155
163
  * Increments a value of a property by a given amount.
156
164
  * This is a batch operation, so it allows to increment multiple rows at once.
@@ -15,8 +15,8 @@ import type {
15
15
  TransformLogProgressOptions,
16
16
  TransformMapOptions,
17
17
  } from '@naturalcycles/nodejs-lib'
18
- import type { CommonDB } from '../common.db'
19
- import type { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model'
18
+ import type { CommonDB } from '../common.db.js'
19
+ import type { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model.js'
20
20
 
21
21
  export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID = BM['id']> {
22
22
  /**
@@ -47,10 +47,10 @@ import {
47
47
  transformNoOp,
48
48
  writableVoid,
49
49
  } from '@naturalcycles/nodejs-lib'
50
- import { DBLibError } from '../cnst'
51
- import type { CommonDBTransactionOptions, DBTransaction, RunQueryResult } from '../db.model'
52
- import type { DBQuery } from '../query/dbQuery'
53
- import { RunnableDBQuery } from '../query/dbQuery'
50
+ import { DBLibError } from '../cnst.js'
51
+ import type { CommonDBTransactionOptions, DBTransaction, RunQueryResult } from '../db.model.js'
52
+ import type { DBQuery } from '../query/dbQuery.js'
53
+ import { RunnableDBQuery } from '../query/dbQuery.js'
54
54
  import type {
55
55
  CommonDaoCfg,
56
56
  CommonDaoCreateOptions,
@@ -65,8 +65,8 @@ import type {
65
65
  CommonDaoStreamForEachOptions,
66
66
  CommonDaoStreamOptions,
67
67
  CommonDaoStreamSaveOptions,
68
- } from './common.dao.model'
69
- import { CommonDaoLogLevel } from './common.dao.model'
68
+ } from './common.dao.model.js'
69
+ import { CommonDaoLogLevel } from './common.dao.model.js'
70
70
 
71
71
  const isGAE = !!process.env['GAE_INSTANCE']
72
72
  const isCI = !!process.env['CI']
@@ -1307,6 +1307,11 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
1307
1307
  await this.cfg.db.ping()
1308
1308
  }
1309
1309
 
1310
+ async createTransaction(opt?: CommonDBTransactionOptions): Promise<CommonDaoTransaction> {
1311
+ const tx = await this.cfg.db.createTransaction(opt)
1312
+ return new CommonDaoTransaction(tx, this.cfg.logger!)
1313
+ }
1314
+
1310
1315
  async runInTransaction<T = void>(
1311
1316
  fn: CommonDaoTransactionFn<T>,
1312
1317
  opt?: CommonDBTransactionOptions,
@@ -1419,8 +1424,17 @@ export class CommonDaoTransaction {
1419
1424
  private logger: CommonLogger,
1420
1425
  ) {}
1421
1426
 
1427
+ /**
1428
+ * Commits the underlying DBTransaction.
1429
+ * May throw.
1430
+ */
1431
+ async commit(): Promise<void> {
1432
+ await this.tx.commit()
1433
+ }
1434
+
1422
1435
  /**
1423
1436
  * Perform a graceful rollback without throwing/re-throwing any error.
1437
+ * Never throws.
1424
1438
  */
1425
1439
  async rollback(): Promise<void> {
1426
1440
  try {
package/src/db.model.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ObjectWithId, UnixTimestamp } from '@naturalcycles/js-lib'
2
- import type { CommonDB } from './common.db'
2
+ import type { CommonDB } from './common.db.js'
3
3
 
4
4
  /**
5
5
  * Similar to SQL INSERT, UPDATE.
@@ -27,6 +27,12 @@ export interface DBTransaction {
27
27
  saveBatch: CommonDB['saveBatch']
28
28
  deleteByIds: CommonDB['deleteByIds']
29
29
 
30
+ /**
31
+ * Commit the transaction.
32
+ * May throw.
33
+ */
34
+ commit: () => Promise<void>
35
+
30
36
  /**
31
37
  * Perform a graceful rollback.
32
38
  * It'll rollback the transaction and won't throw/re-throw any errors.
package/src/index.ts CHANGED
@@ -1,17 +1,17 @@
1
- export * from './adapter/inmemory/inMemory.db'
2
- export * from './adapter/inmemory/inMemoryKeyValueDB'
3
- export * from './adapter/inmemory/queryInMemory'
4
- export * from './base.common.db'
5
- export * from './common.db'
6
- export * from './commondao/common.dao'
7
- export * from './commondao/common.dao.model'
8
- export * from './db.model'
9
- export * from './kv/commonKeyValueDao'
10
- export * from './kv/commonKeyValueDaoMemoCache'
11
- export * from './kv/commonKeyValueDB'
12
- export * from './model.util'
13
- export * from './pipeline/dbPipelineBackup'
14
- export * from './pipeline/dbPipelineCopy'
15
- export * from './pipeline/dbPipelineRestore'
16
- export * from './query/dbQuery'
17
- export * from './transaction/dbTransaction.util'
1
+ export * from './adapter/inmemory/inMemory.db.js'
2
+ export * from './adapter/inmemory/inMemoryKeyValueDB.js'
3
+ export * from './adapter/inmemory/queryInMemory.js'
4
+ export * from './base.common.db.js'
5
+ export * from './common.db.js'
6
+ export * from './commondao/common.dao.js'
7
+ export * from './commondao/common.dao.model.js'
8
+ export * from './db.model.js'
9
+ export * from './kv/commonKeyValueDao.js'
10
+ export * from './kv/commonKeyValueDaoMemoCache.js'
11
+ export * from './kv/commonKeyValueDB.js'
12
+ export * from './model.util.js'
13
+ export * from './pipeline/dbPipelineBackup.js'
14
+ export * from './pipeline/dbPipelineCopy.js'
15
+ export * from './pipeline/dbPipelineRestore.js'
16
+ export * from './query/dbQuery.js'
17
+ export * from './transaction/dbTransaction.util.js'
@@ -1,6 +1,6 @@
1
1
  import type { Integer, UnixTimestamp } from '@naturalcycles/js-lib'
2
2
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib'
3
- import type { CommonDBCreateOptions } from '../db.model'
3
+ import type { CommonDBCreateOptions } from '../db.model.js'
4
4
 
5
5
  /**
6
6
  * Common interface for Key-Value database implementations.
@@ -2,14 +2,14 @@ import type { CommonLogger, KeyValueTuple } from '@naturalcycles/js-lib'
2
2
  import { AppError, pMap } from '@naturalcycles/js-lib'
3
3
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib'
4
4
  import { deflateString, inflateToString } from '@naturalcycles/nodejs-lib'
5
- import type { CommonDaoLogLevel } from '../commondao/common.dao.model'
6
- import type { CommonDBCreateOptions } from '../db.model'
5
+ import type { CommonDaoLogLevel } from '../commondao/common.dao.model.js'
6
+ import type { CommonDBCreateOptions } from '../db.model.js'
7
7
  import type {
8
8
  CommonKeyValueDB,
9
9
  CommonKeyValueDBSaveBatchOptions,
10
10
  IncrementTuple,
11
11
  KeyValueDBTuple,
12
- } from './commonKeyValueDB'
12
+ } from './commonKeyValueDB.js'
13
13
 
14
14
  export interface CommonKeyValueDaoCfg<V> {
15
15
  db: CommonKeyValueDB
@@ -1,6 +1,6 @@
1
1
  import type { AsyncMemoCache, NumberOfSeconds, UnixTimestamp } from '@naturalcycles/js-lib'
2
2
  import { localTime, MISS } from '@naturalcycles/js-lib'
3
- import type { CommonKeyValueDao } from './commonKeyValueDao'
3
+ import type { CommonKeyValueDao } from './commonKeyValueDao.js'
4
4
 
5
5
  export interface CommonKeyValueDaoMemoCacheCfg<VALUE> {
6
6
  dao: CommonKeyValueDao<string, VALUE>