@naturalcycles/db-lib 9.14.2 → 9.15.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 (38) hide show
  1. package/dist/adapter/cachedb/index.d.ts +1 -1
  2. package/dist/adapter/file/inMemory.persistence.plugin.d.ts +1 -1
  3. package/dist/adapter/inmemory/inMemory.db.d.ts +1 -1
  4. package/dist/commondao/common.dao.d.ts +3 -3
  5. package/dist/commondao/common.dao.js +2 -1
  6. package/dist/commondao/common.dao.model.d.ts +11 -1
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.js +1 -1
  9. package/dist/kv/commonKeyValueDaoMemoCache.js +1 -1
  10. package/dist/model.util.js +2 -2
  11. package/dist/pipeline/dbPipelineBackup.d.ts +1 -1
  12. package/dist/query/dbQuery.d.ts +3 -0
  13. package/dist/query/dbQuery.js +3 -0
  14. package/dist/testing/daoTest.js +2 -1
  15. package/dist/testing/index.d.ts +3 -3
  16. package/dist/testing/index.js +2 -2
  17. package/dist/testing/keyValueDBTest.js +1 -0
  18. package/dist/testing/keyValueDaoTest.js +1 -0
  19. package/package.json +10 -5
  20. package/src/adapter/cachedb/index.ts +1 -1
  21. package/src/adapter/file/file.db.ts +4 -4
  22. package/src/adapter/file/inMemory.persistence.plugin.ts +1 -1
  23. package/src/adapter/inmemory/inMemory.db.ts +11 -11
  24. package/src/commondao/common.dao.model.ts +13 -1
  25. package/src/commondao/common.dao.ts +6 -4
  26. package/src/index.ts +1 -1
  27. package/src/kv/commonKeyValueDaoMemoCache.ts +2 -2
  28. package/src/model.util.ts +3 -3
  29. package/src/pipeline/dbPipelineBackup.ts +8 -8
  30. package/src/pipeline/dbPipelineCopy.ts +7 -7
  31. package/src/pipeline/dbPipelineRestore.ts +10 -10
  32. package/src/query/dbQuery.ts +5 -2
  33. package/src/testing/daoTest.ts +6 -5
  34. package/src/testing/dbTest.ts +2 -0
  35. package/src/testing/index.ts +9 -9
  36. package/src/testing/keyValueDBTest.ts +2 -0
  37. package/src/testing/keyValueDaoTest.ts +3 -1
  38. package/src/testing/test.model.ts +1 -1
@@ -1,4 +1,4 @@
1
1
  import { CacheDB } from './cache.db';
2
2
  import { CacheDBCfg, CacheDBCreateOptions, CacheDBOptions, CacheDBStreamOptions } from './cache.db.model';
3
- export type { CacheDBCfg, CacheDBOptions, CacheDBCreateOptions, CacheDBStreamOptions };
3
+ export type { CacheDBCfg, CacheDBCreateOptions, CacheDBOptions, CacheDBStreamOptions };
4
4
  export { CacheDB };
@@ -1,4 +1,4 @@
1
- import { StringMap, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { ObjectWithId, StringMap } from '@naturalcycles/js-lib';
2
2
  import { DBSaveBatchOperation } from '../../db.model';
3
3
  import { FileDBPersistencePlugin } from './file.db.model';
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { JsonSchemaObject, StringMap, JsonSchemaRootObject, ObjectWithId, CommonLogger } from '@naturalcycles/js-lib';
1
+ import { CommonLogger, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, StringMap } from '@naturalcycles/js-lib';
2
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDB, CommonDBTransactionOptions, CommonDBType, DBOperation, DBPatch, DBTransactionFn } from '../..';
4
4
  import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, DBTransaction, RunQueryResult } from '../../db.model';
@@ -3,7 +3,7 @@ import { AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRo
3
3
  import { AjvSchema, ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';
4
4
  import { CommonDBTransactionOptions, DBPatch, DBTransaction, RunQueryResult } from '../db.model';
5
5
  import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
6
- import { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoPatchOptions, CommonDaoSaveBatchOptions, CommonDaoSaveOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDaoStreamSaveOptions } from './common.dao.model';
6
+ import { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoPatchByIdOptions, CommonDaoPatchOptions, CommonDaoSaveBatchOptions, CommonDaoSaveOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDaoStreamSaveOptions } from './common.dao.model';
7
7
  /**
8
8
  * Lowest common denominator API between supported Databases.
9
9
  *
@@ -86,11 +86,11 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
86
86
  * 2. Applies the patch on top of loaded data.
87
87
  * 3. Saves (as fast as possible since the read) with the Patch applied, but only if the data has changed.
88
88
  */
89
- patchById(id: string, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
89
+ patchById(id: string, patch: Partial<BM>, opt?: CommonDaoPatchByIdOptions<DBM>): Promise<BM>;
90
90
  /**
91
91
  * Like patchById, but runs all operations within a Transaction.
92
92
  */
93
- patchByIdInTransaction(id: string, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
93
+ patchByIdInTransaction(id: string, patch: Partial<BM>, opt?: CommonDaoPatchByIdOptions<DBM>): Promise<BM>;
94
94
  /**
95
95
  * Same as patchById, but takes the whole object as input.
96
96
  * This "whole object" is mutated with the patch and returned.
@@ -447,7 +447,7 @@ class CommonDao {
447
447
  * "Returns", just to have a type of "Saved"
448
448
  */
449
449
  assignIdCreatedUpdated(obj, opt = {}) {
450
- const now = (0, js_lib_1.nowUnix)();
450
+ const now = js_lib_1.localTime.nowUnix();
451
451
  if (this.cfg.useCreatedProperty) {
452
452
  obj.created ||= obj.updated || now;
453
453
  }
@@ -486,6 +486,7 @@ class CommonDao {
486
486
  }
487
487
  }
488
488
  else {
489
+ (0, js_lib_1._assert)(!opt.requireToExist, `${this.cfg.table}.patchById(${id}) is required, but missing`);
489
490
  patched = this.create({ ...patch, id }, opt);
490
491
  }
491
492
  return await this.save(patched, opt);
@@ -162,7 +162,7 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
162
162
  useUpdatedProperty?: boolean;
163
163
  /**
164
164
  * Defaults to false.
165
- * If true - run patch operations (patch, patchById) in a Transaction.
165
+ * If true - run patch operations (patch, patchById, patchByIdOrCreate) in a Transaction.
166
166
  *
167
167
  * @experimental
168
168
  */
@@ -211,6 +211,16 @@ export interface CommonDaoSaveOptions<BM extends BaseDBEntity, DBM extends BaseD
211
211
  */
212
212
  skipIfEquals?: BM;
213
213
  }
214
+ export interface CommonDaoPatchByIdOptions<DBM extends BaseDBEntity> extends CommonDaoSaveBatchOptions<DBM> {
215
+ /**
216
+ * Defaults to false.
217
+ * With false, if the row doesn't exist - it will be auto-created with `dao.create`.
218
+ * With true, if the row doesn't exist - it will throw an error.
219
+ *
220
+ * Use true when you expect the row to exist and it would be an error if it doesn't.
221
+ */
222
+ requireToExist?: boolean;
223
+ }
214
224
  export interface CommonDaoPatchOptions<DBM extends BaseDBEntity> extends CommonDaoSaveBatchOptions<DBM> {
215
225
  /**
216
226
  * If true - patch will skip loading from DB, and will just optimistically patch passed object.
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export * from './commondao/common.dao';
7
7
  export * from './commondao/common.dao.model';
8
8
  export * from './db.model';
9
9
  export * from './kv/commonKeyValueDao';
10
+ export * from './kv/commonKeyValueDaoMemoCache';
10
11
  export * from './kv/commonKeyValueDB';
11
12
  export * from './model.util';
12
13
  export * from './pipeline/dbPipelineBackup';
@@ -14,4 +15,3 @@ export * from './pipeline/dbPipelineCopy';
14
15
  export * from './pipeline/dbPipelineRestore';
15
16
  export * from './query/dbQuery';
16
17
  export * from './transaction/dbTransaction.util';
17
- export * from './kv/commonKeyValueDaoMemoCache';
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ tslib_1.__exportStar(require("./commondao/common.dao"), exports);
10
10
  tslib_1.__exportStar(require("./commondao/common.dao.model"), exports);
11
11
  tslib_1.__exportStar(require("./db.model"), exports);
12
12
  tslib_1.__exportStar(require("./kv/commonKeyValueDao"), exports);
13
+ tslib_1.__exportStar(require("./kv/commonKeyValueDaoMemoCache"), exports);
13
14
  tslib_1.__exportStar(require("./kv/commonKeyValueDB"), exports);
14
15
  tslib_1.__exportStar(require("./model.util"), exports);
15
16
  tslib_1.__exportStar(require("./pipeline/dbPipelineBackup"), exports);
@@ -17,4 +18,3 @@ tslib_1.__exportStar(require("./pipeline/dbPipelineCopy"), exports);
17
18
  tslib_1.__exportStar(require("./pipeline/dbPipelineRestore"), exports);
18
19
  tslib_1.__exportStar(require("./query/dbQuery"), exports);
19
20
  tslib_1.__exportStar(require("./transaction/dbTransaction.util"), exports);
20
- tslib_1.__exportStar(require("./kv/commonKeyValueDaoMemoCache"), exports);
@@ -18,7 +18,7 @@ class CommonKeyValueDaoMemoCache {
18
18
  return (await this.cfg.dao.getById(k)) || js_lib_1.MISS;
19
19
  }
20
20
  async set(k, v) {
21
- const opt = this.cfg.ttl ? { expireAt: (0, js_lib_1.nowUnix)() + this.cfg.ttl } : undefined;
21
+ const opt = this.cfg.ttl ? { expireAt: js_lib_1.localTime.nowUnix() + this.cfg.ttl } : undefined;
22
22
  await this.cfg.dao.save(k, v, opt);
23
23
  }
24
24
  async clear() {
@@ -7,14 +7,14 @@ exports.serializeJsonField = serializeJsonField;
7
7
  const js_lib_1 = require("@naturalcycles/js-lib");
8
8
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
9
9
  function createdUpdatedFields(existingObject) {
10
- const now = (0, js_lib_1.nowUnix)();
10
+ const now = js_lib_1.localTime.nowUnix();
11
11
  return {
12
12
  created: existingObject?.created || now,
13
13
  updated: now,
14
14
  };
15
15
  }
16
16
  function createdUpdatedIdFields(existingObject) {
17
- const now = (0, js_lib_1.nowUnix)();
17
+ const now = js_lib_1.localTime.nowUnix();
18
18
  return {
19
19
  created: existingObject?.created || now,
20
20
  id: existingObject?.id || (0, nodejs_lib_1.stringId)(),
@@ -1,4 +1,4 @@
1
- import { AsyncMapper, ErrorMode, UnixTimestampNumber, StringMap } from '@naturalcycles/js-lib';
1
+ import { AsyncMapper, ErrorMode, StringMap, UnixTimestampNumber } from '@naturalcycles/js-lib';
2
2
  import { NDJsonStats, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDB } from '../common.db';
4
4
  import { DBQuery } from '../index';
@@ -69,6 +69,9 @@ export declare class DBQuery<ROW extends ObjectWithId> {
69
69
  filter(name: keyof ROW, op: DBQueryFilterOperator, val: any): this;
70
70
  filterEq(name: keyof ROW, val: any): this;
71
71
  filterIn(name: keyof ROW, val: any[]): this;
72
+ /**
73
+ * Passing 0 means "no limit".
74
+ */
72
75
  limit(limit: number): this;
73
76
  offset(offset: number): this;
74
77
  order(name: keyof ROW, descending?: boolean): this;
@@ -54,6 +54,9 @@ class DBQuery {
54
54
  this._filters.push({ name, op: 'in', val });
55
55
  return this;
56
56
  }
57
+ /**
58
+ * Passing 0 means "no limit".
59
+ */
57
60
  limit(limit) {
58
61
  this._limitValue = limit;
59
62
  return this;
@@ -8,6 +8,7 @@ const __1 = require("..");
8
8
  const common_dao_1 = require("../commondao/common.dao");
9
9
  const dbTest_1 = require("./dbTest");
10
10
  const test_model_1 = require("./test.model");
11
+ // eslint-disable-next-line jest/no-export
11
12
  function runCommonDaoTest(db, quirks = {}) {
12
13
  const { support } = db;
13
14
  const dao = new common_dao_1.CommonDao({
@@ -186,7 +187,7 @@ function runCommonDaoTest(db, quirks = {}) {
186
187
  // cleanup
187
188
  await dao.query().deleteByQuery();
188
189
  // Test that id, created, updated are created
189
- const now = (0, js_lib_1.nowUnix)();
190
+ const now = js_lib_1.localTime.nowUnix();
190
191
  await dao.runInTransaction(async (tx) => {
191
192
  const row = (0, js_lib_1._omit)(item1, ['id', 'created', 'updated']);
192
193
  await tx.save(dao, row);
@@ -1,6 +1,6 @@
1
1
  import { runCommonDaoTest } from './daoTest';
2
2
  import { CommonDBImplementationQuirks, runCommonDBTest } from './dbTest';
3
3
  import { runCommonKeyValueDBTest } from './keyValueDBTest';
4
- import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TestItemBM, testItemBMJsonSchema, testItemBMSchema, TestItemDBM, TestItemTM, testItemTMSchema, TEST_TABLE } from './test.model';
5
- export type { TestItemDBM, TestItemBM, TestItemTM, CommonDBImplementationQuirks };
6
- export { TEST_TABLE, createTestItemDBM, createTestItemBM, createTestItemsDBM, createTestItemsBM, testItemBMSchema, testItemTMSchema, testItemBMJsonSchema, runCommonDBTest, runCommonDaoTest, runCommonKeyValueDBTest, };
4
+ import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TEST_TABLE, TestItemBM, testItemBMJsonSchema, testItemBMSchema, TestItemDBM, TestItemTM, testItemTMSchema } from './test.model';
5
+ export type { CommonDBImplementationQuirks, TestItemBM, TestItemDBM, TestItemTM };
6
+ export { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, runCommonDaoTest, runCommonDBTest, runCommonKeyValueDBTest, TEST_TABLE, testItemBMJsonSchema, testItemBMSchema, testItemTMSchema, };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.runCommonKeyValueDBTest = exports.runCommonDaoTest = exports.runCommonDBTest = exports.testItemBMJsonSchema = exports.testItemTMSchema = exports.testItemBMSchema = exports.createTestItemsBM = exports.createTestItemsDBM = exports.createTestItemBM = exports.createTestItemDBM = exports.TEST_TABLE = void 0;
3
+ exports.testItemTMSchema = exports.testItemBMSchema = exports.testItemBMJsonSchema = exports.TEST_TABLE = exports.runCommonKeyValueDBTest = exports.runCommonDBTest = exports.runCommonDaoTest = exports.createTestItemsDBM = exports.createTestItemsBM = exports.createTestItemDBM = exports.createTestItemBM = void 0;
4
4
  const daoTest_1 = require("./daoTest");
5
5
  Object.defineProperty(exports, "runCommonDaoTest", { enumerable: true, get: function () { return daoTest_1.runCommonDaoTest; } });
6
6
  const dbTest_1 = require("./dbTest");
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "createTestItemBM", { enumerable: true, get: func
12
12
  Object.defineProperty(exports, "createTestItemDBM", { enumerable: true, get: function () { return test_model_1.createTestItemDBM; } });
13
13
  Object.defineProperty(exports, "createTestItemsBM", { enumerable: true, get: function () { return test_model_1.createTestItemsBM; } });
14
14
  Object.defineProperty(exports, "createTestItemsDBM", { enumerable: true, get: function () { return test_model_1.createTestItemsDBM; } });
15
+ Object.defineProperty(exports, "TEST_TABLE", { enumerable: true, get: function () { return test_model_1.TEST_TABLE; } });
15
16
  Object.defineProperty(exports, "testItemBMJsonSchema", { enumerable: true, get: function () { return test_model_1.testItemBMJsonSchema; } });
16
17
  Object.defineProperty(exports, "testItemBMSchema", { enumerable: true, get: function () { return test_model_1.testItemBMSchema; } });
17
18
  Object.defineProperty(exports, "testItemTMSchema", { enumerable: true, get: function () { return test_model_1.testItemTMSchema; } });
18
- Object.defineProperty(exports, "TEST_TABLE", { enumerable: true, get: function () { return test_model_1.TEST_TABLE; } });
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runCommonKeyValueDBTest = runCommonKeyValueDBTest;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const test_model_1 = require("./test.model");
6
+ /* eslint-disable jest/no-export */
6
7
  const testIds = (0, js_lib_1._range)(1, 4).map(n => `id${n}`);
7
8
  const testEntries = testIds.map(id => [id, Buffer.from(`${id}value`)]);
8
9
  function runCommonKeyValueDBTest(db) {
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runCommonKeyValueDaoTest = runCommonKeyValueDaoTest;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
+ /* eslint-disable jest/no-export */
5
6
  const testIds = (0, js_lib_1._range)(1, 4).map(n => `id${n}`);
6
7
  const testEntries = testIds.map(id => [id, Buffer.from(`${id}value`)]);
7
8
  function runCommonKeyValueDaoTest(dao) {
package/package.json CHANGED
@@ -1,7 +1,12 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "scripts": {
4
- "prepare": "husky"
4
+ "prepare": "husky",
5
+ "build": "dev-lib build",
6
+ "test": "dev-lib test",
7
+ "lint": "dev-lib lint",
8
+ "bt": "dev-lib bt",
9
+ "lbt": "dev-lib lbt"
5
10
  },
6
11
  "dependencies": {
7
12
  "@naturalcycles/js-lib": "^14.116.0",
@@ -9,8 +14,8 @@
9
14
  },
10
15
  "devDependencies": {
11
16
  "@naturalcycles/bench-lib": "^3.0.0",
12
- "@naturalcycles/dev-lib": "^13.0.0",
13
- "@types/node": "^20.2.1",
17
+ "@naturalcycles/dev-lib": "^15.4.1",
18
+ "@types/node": "^22.1.0",
14
19
  "jest": "^29.0.0"
15
20
  },
16
21
  "files": [
@@ -38,9 +43,9 @@
38
43
  "url": "https://github.com/NaturalCycles/db-lib"
39
44
  },
40
45
  "engines": {
41
- "node": ">=18.12"
46
+ "node": ">=20.13"
42
47
  },
43
- "version": "9.14.2",
48
+ "version": "9.15.0",
44
49
  "description": "Lowest Common Denominator API to supported Databases",
45
50
  "keywords": [
46
51
  "db",
@@ -6,6 +6,6 @@ import {
6
6
  CacheDBStreamOptions,
7
7
  } from './cache.db.model'
8
8
 
9
- export type { CacheDBCfg, CacheDBOptions, CacheDBCreateOptions, CacheDBStreamOptions }
9
+ export type { CacheDBCfg, CacheDBCreateOptions, CacheDBOptions, CacheDBStreamOptions }
10
10
 
11
11
  export { CacheDB }
@@ -1,17 +1,17 @@
1
1
  import {
2
- generateJsonSchemaFromData,
2
+ _assert,
3
3
  _by,
4
4
  _deepEquals,
5
+ _filterUndefinedValues,
5
6
  _since,
6
7
  _sortBy,
7
8
  _sortObjectDeep,
8
9
  _stringMapValues,
10
+ generateJsonSchemaFromData,
9
11
  JsonSchemaRootObject,
10
- _filterUndefinedValues,
11
- _assert,
12
12
  ObjectWithId,
13
13
  } from '@naturalcycles/js-lib'
14
- import { readableCreate, ReadableTyped, dimGrey } from '@naturalcycles/nodejs-lib'
14
+ import { dimGrey, readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib'
15
15
  import {
16
16
  BaseCommonDB,
17
17
  commonDBFullSupport,
@@ -1,4 +1,4 @@
1
- import { StringMap, _by, ObjectWithId } from '@naturalcycles/js-lib'
1
+ import { _by, ObjectWithId, StringMap } from '@naturalcycles/js-lib'
2
2
  import { DBSaveBatchOperation } from '../../db.model'
3
3
  import { FileDBPersistencePlugin } from './file.db.model'
4
4
 
@@ -1,26 +1,26 @@
1
1
  import { Readable } from 'node:stream'
2
2
  import {
3
- generateJsonSchemaFromData,
4
- JsonSchemaObject,
5
- pMap,
6
- StringMap,
3
+ _assert,
7
4
  _by,
5
+ _deepCopy,
8
6
  _since,
9
7
  _sortObjectDeep,
10
- JsonSchemaRootObject,
11
- ObjectWithId,
12
8
  _stringMapValues,
13
9
  CommonLogger,
14
- _deepCopy,
15
- _assert,
10
+ generateJsonSchemaFromData,
11
+ JsonSchemaObject,
12
+ JsonSchemaRootObject,
13
+ ObjectWithId,
14
+ pMap,
15
+ StringMap,
16
16
  } from '@naturalcycles/js-lib'
17
17
  import {
18
- bufferReviver,
19
- ReadableTyped,
20
18
  _pipeline,
19
+ bufferReviver,
21
20
  dimGrey,
22
- yellow,
23
21
  fs2,
22
+ ReadableTyped,
23
+ yellow,
24
24
  } from '@naturalcycles/nodejs-lib'
25
25
  import {
26
26
  CommonDB,
@@ -203,7 +203,7 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
203
203
 
204
204
  /**
205
205
  * Defaults to false.
206
- * If true - run patch operations (patch, patchById) in a Transaction.
206
+ * If true - run patch operations (patch, patchById, patchByIdOrCreate) in a Transaction.
207
207
  *
208
208
  * @experimental
209
209
  */
@@ -260,6 +260,18 @@ export interface CommonDaoSaveOptions<BM extends BaseDBEntity, DBM extends BaseD
260
260
  skipIfEquals?: BM
261
261
  }
262
262
 
263
+ export interface CommonDaoPatchByIdOptions<DBM extends BaseDBEntity>
264
+ extends CommonDaoSaveBatchOptions<DBM> {
265
+ /**
266
+ * Defaults to false.
267
+ * With false, if the row doesn't exist - it will be auto-created with `dao.create`.
268
+ * With true, if the row doesn't exist - it will throw an error.
269
+ *
270
+ * Use true when you expect the row to exist and it would be an error if it doesn't.
271
+ */
272
+ requireToExist?: boolean
273
+ }
274
+
263
275
  export interface CommonDaoPatchOptions<DBM extends BaseDBEntity>
264
276
  extends CommonDaoSaveBatchOptions<DBM> {
265
277
  /**
@@ -18,7 +18,7 @@ import {
18
18
  ErrorMode,
19
19
  JsonSchemaObject,
20
20
  JsonSchemaRootObject,
21
- nowUnix,
21
+ localTime,
22
22
  ObjectWithId,
23
23
  pMap,
24
24
  SKIP,
@@ -52,6 +52,7 @@ import {
52
52
  CommonDaoHooks,
53
53
  CommonDaoLogLevel,
54
54
  CommonDaoOptions,
55
+ CommonDaoPatchByIdOptions,
55
56
  CommonDaoPatchOptions,
56
57
  CommonDaoSaveBatchOptions,
57
58
  CommonDaoSaveOptions,
@@ -605,7 +606,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
605
606
  * "Returns", just to have a type of "Saved"
606
607
  */
607
608
  assignIdCreatedUpdated<T extends BaseDBEntity>(obj: Partial<T>, opt: CommonDaoOptions = {}): T {
608
- const now = nowUnix()
609
+ const now = localTime.nowUnix()
609
610
 
610
611
  if (this.cfg.useCreatedProperty) {
611
612
  obj.created ||= obj.updated || now
@@ -635,7 +636,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
635
636
  async patchById(
636
637
  id: string,
637
638
  patch: Partial<BM>,
638
- opt: CommonDaoSaveBatchOptions<DBM> = {},
639
+ opt: CommonDaoPatchByIdOptions<DBM> = {},
639
640
  ): Promise<BM> {
640
641
  if (this.cfg.patchInTransaction && !opt.tx) {
641
642
  // patchInTransaction means that we should run this op in Transaction
@@ -655,6 +656,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
655
656
  return patched
656
657
  }
657
658
  } else {
659
+ _assert(!opt.requireToExist, `${this.cfg.table}.patchById(${id}) is required, but missing`)
658
660
  patched = this.create({ ...patch, id }, opt)
659
661
  }
660
662
 
@@ -667,7 +669,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
667
669
  async patchByIdInTransaction(
668
670
  id: string,
669
671
  patch: Partial<BM>,
670
- opt?: CommonDaoSaveBatchOptions<DBM>,
672
+ opt?: CommonDaoPatchByIdOptions<DBM>,
671
673
  ): Promise<BM> {
672
674
  return await this.runInTransaction(async daoTx => {
673
675
  return await this.patchById(id, patch, { ...opt, tx: daoTx.tx })
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ export * from './commondao/common.dao'
7
7
  export * from './commondao/common.dao.model'
8
8
  export * from './db.model'
9
9
  export * from './kv/commonKeyValueDao'
10
+ export * from './kv/commonKeyValueDaoMemoCache'
10
11
  export * from './kv/commonKeyValueDB'
11
12
  export * from './model.util'
12
13
  export * from './pipeline/dbPipelineBackup'
@@ -14,4 +15,3 @@ export * from './pipeline/dbPipelineCopy'
14
15
  export * from './pipeline/dbPipelineRestore'
15
16
  export * from './query/dbQuery'
16
17
  export * from './transaction/dbTransaction.util'
17
- export * from './kv/commonKeyValueDaoMemoCache'
@@ -1,4 +1,4 @@
1
- import { AsyncMemoCache, MISS, nowUnix, NumberOfSeconds } from '@naturalcycles/js-lib'
1
+ import { AsyncMemoCache, localTime, MISS, NumberOfSeconds } from '@naturalcycles/js-lib'
2
2
  import { CommonKeyValueDao } from './commonKeyValueDao'
3
3
 
4
4
  export interface CommonKeyValueDaoMemoCacheCfg<VALUE> {
@@ -26,7 +26,7 @@ export class CommonKeyValueDaoMemoCache<VALUE = any> implements AsyncMemoCache<s
26
26
  }
27
27
 
28
28
  async set(k: string, v: VALUE): Promise<void> {
29
- const opt = this.cfg.ttl ? { expireAt: nowUnix() + this.cfg.ttl } : undefined
29
+ const opt = this.cfg.ttl ? { expireAt: localTime.nowUnix() + this.cfg.ttl } : undefined
30
30
 
31
31
  await this.cfg.dao.save(k, v, opt)
32
32
  }
package/src/model.util.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { CreatedUpdated, CreatedUpdatedId, nowUnix } from '@naturalcycles/js-lib'
1
+ import { CreatedUpdated, CreatedUpdatedId, localTime } from '@naturalcycles/js-lib'
2
2
  import { stringId } from '@naturalcycles/nodejs-lib'
3
3
 
4
4
  export function createdUpdatedFields(
5
5
  existingObject?: Partial<CreatedUpdated> | null,
6
6
  ): CreatedUpdated {
7
- const now = nowUnix()
7
+ const now = localTime.nowUnix()
8
8
  return {
9
9
  created: existingObject?.created || now,
10
10
  updated: now,
@@ -14,7 +14,7 @@ export function createdUpdatedFields(
14
14
  export function createdUpdatedIdFields(
15
15
  existingObject?: Partial<CreatedUpdatedId> | null,
16
16
  ): CreatedUpdatedId {
17
- const now = nowUnix()
17
+ const now = localTime.nowUnix()
18
18
  return {
19
19
  created: existingObject?.created || now,
20
20
  id: existingObject?.id || stringId(),
@@ -1,26 +1,26 @@
1
1
  import {
2
+ _passthroughMapper,
2
3
  AppError,
3
4
  AsyncMapper,
4
5
  ErrorMode,
5
- pMap,
6
- _passthroughMapper,
7
6
  localTime,
8
- UnixTimestampNumber,
7
+ pMap,
9
8
  StringMap,
9
+ UnixTimestampNumber,
10
10
  } from '@naturalcycles/js-lib'
11
11
  import {
12
+ _pipeline,
13
+ boldWhite,
14
+ dimWhite,
15
+ fs2,
16
+ grey,
12
17
  NDJsonStats,
13
18
  transformLogProgress,
14
19
  TransformLogProgressOptions,
15
20
  transformMap,
16
21
  TransformMapOptions,
17
22
  transformTap,
18
- _pipeline,
19
- boldWhite,
20
- dimWhite,
21
- grey,
22
23
  yellow,
23
- fs2,
24
24
  } from '@naturalcycles/nodejs-lib'
25
25
  import { CommonDB } from '../common.db'
26
26
  import { DBQuery } from '../index'
@@ -1,12 +1,16 @@
1
1
  import {
2
+ _passthroughMapper,
2
3
  AsyncMapper,
4
+ BaseDBEntity,
3
5
  ErrorMode,
4
- pMap,
5
- _passthroughMapper,
6
6
  localTime,
7
- BaseDBEntity,
7
+ pMap,
8
8
  } from '@naturalcycles/js-lib'
9
9
  import {
10
+ _pipeline,
11
+ boldWhite,
12
+ dimWhite,
13
+ grey,
10
14
  NDJsonStats,
11
15
  transformChunk,
12
16
  transformLogProgress,
@@ -15,10 +19,6 @@ import {
15
19
  TransformMapOptions,
16
20
  transformTap,
17
21
  writableForEach,
18
- _pipeline,
19
- boldWhite,
20
- dimWhite,
21
- grey,
22
22
  yellow,
23
23
  } from '@naturalcycles/nodejs-lib'
24
24
  import { CommonDB } from '../common.db'
@@ -1,15 +1,20 @@
1
1
  import {
2
- AsyncMapper,
3
- ErrorMode,
4
- pMap,
5
2
  _hb,
6
3
  _mapValues,
7
4
  _passthroughMapper,
8
- localTime,
9
- JsonSchemaObject,
5
+ AsyncMapper,
10
6
  BaseDBEntity,
7
+ ErrorMode,
8
+ JsonSchemaObject,
9
+ localTime,
10
+ pMap,
11
11
  } from '@naturalcycles/js-lib'
12
12
  import {
13
+ _pipeline,
14
+ boldWhite,
15
+ dimWhite,
16
+ fs2,
17
+ grey,
13
18
  NDJsonStats,
14
19
  transformChunk,
15
20
  transformFilterSync,
@@ -19,12 +24,7 @@ import {
19
24
  TransformMapOptions,
20
25
  transformTap,
21
26
  writableForEach,
22
- _pipeline,
23
- boldWhite,
24
- dimWhite,
25
- grey,
26
27
  yellow,
27
- fs2,
28
28
  } from '@naturalcycles/nodejs-lib'
29
29
  import { CommonDB } from '../common.db'
30
30
  import { CommonDBSaveOptions } from '../index'
@@ -1,7 +1,7 @@
1
1
  import {
2
- AsyncMapper,
3
- _truncate,
4
2
  _objectAssign,
3
+ _truncate,
4
+ AsyncMapper,
5
5
  BaseDBEntity,
6
6
  ObjectWithId,
7
7
  } from '@naturalcycles/js-lib'
@@ -128,6 +128,9 @@ export class DBQuery<ROW extends ObjectWithId> {
128
128
  return this
129
129
  }
130
130
 
131
+ /**
132
+ * Passing 0 means "no limit".
133
+ */
131
134
  limit(limit: number): this {
132
135
  this._limitValue = limit
133
136
  return this
@@ -1,19 +1,20 @@
1
1
  import { Readable } from 'node:stream'
2
- import { _deepCopy, _pick, _sortBy, _omit, nowUnix } from '@naturalcycles/js-lib'
2
+ import { _deepCopy, _omit, _pick, _sortBy, localTime } from '@naturalcycles/js-lib'
3
3
  import { _pipeline } from '@naturalcycles/nodejs-lib'
4
4
  import { CommonDaoLogLevel, DBQuery } from '..'
5
5
  import { CommonDB } from '../common.db'
6
6
  import { CommonDao } from '../commondao/common.dao'
7
+ import { TestItemBM } from '.'
7
8
  import { CommonDBImplementationQuirks, expectMatch } from './dbTest'
8
9
  import {
10
+ createTestItemBM,
9
11
  createTestItemsBM,
10
- testItemBMSchema,
11
12
  TEST_TABLE,
12
- createTestItemBM,
13
13
  testItemBMJsonSchema,
14
+ testItemBMSchema,
14
15
  } from './test.model'
15
- import { TestItemBM } from '.'
16
16
 
17
+ // eslint-disable-next-line jest/no-export
17
18
  export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQuirks = {}): void {
18
19
  const { support } = db
19
20
  const dao = new CommonDao({
@@ -255,7 +256,7 @@ export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQui
255
256
  await dao.query().deleteByQuery()
256
257
 
257
258
  // Test that id, created, updated are created
258
- const now = nowUnix()
259
+ const now = localTime.nowUnix()
259
260
 
260
261
  await dao.runInTransaction(async tx => {
261
262
  const row = _omit(item1, ['id', 'created', 'updated'])
@@ -11,6 +11,8 @@ import {
11
11
  } from './test.model'
12
12
  import { deepFreeze } from './test.util'
13
13
 
14
+ /* eslint-disable jest/no-export */
15
+
14
16
  /**
15
17
  * All options default to `false`.
16
18
  */
@@ -6,27 +6,27 @@ import {
6
6
  createTestItemDBM,
7
7
  createTestItemsBM,
8
8
  createTestItemsDBM,
9
+ TEST_TABLE,
9
10
  TestItemBM,
10
11
  testItemBMJsonSchema,
11
12
  testItemBMSchema,
12
13
  TestItemDBM,
13
14
  TestItemTM,
14
15
  testItemTMSchema,
15
- TEST_TABLE,
16
16
  } from './test.model'
17
17
 
18
- export type { TestItemDBM, TestItemBM, TestItemTM, CommonDBImplementationQuirks }
18
+ export type { CommonDBImplementationQuirks, TestItemBM, TestItemDBM, TestItemTM }
19
19
 
20
20
  export {
21
- TEST_TABLE,
22
- createTestItemDBM,
23
21
  createTestItemBM,
24
- createTestItemsDBM,
22
+ createTestItemDBM,
25
23
  createTestItemsBM,
26
- testItemBMSchema,
27
- testItemTMSchema,
28
- testItemBMJsonSchema,
29
- runCommonDBTest,
24
+ createTestItemsDBM,
30
25
  runCommonDaoTest,
26
+ runCommonDBTest,
31
27
  runCommonKeyValueDBTest,
28
+ TEST_TABLE,
29
+ testItemBMJsonSchema,
30
+ testItemBMSchema,
31
+ testItemTMSchema,
32
32
  }
@@ -2,6 +2,8 @@ import { _range, _sortBy } from '@naturalcycles/js-lib'
2
2
  import { CommonKeyValueDB, KeyValueDBTuple } from '../kv/commonKeyValueDB'
3
3
  import { TEST_TABLE } from './test.model'
4
4
 
5
+ /* eslint-disable jest/no-export */
6
+
5
7
  const testIds = _range(1, 4).map(n => `id${n}`)
6
8
 
7
9
  const testEntries: KeyValueDBTuple[] = testIds.map(id => [id, Buffer.from(`${id}value`)])
@@ -1,6 +1,8 @@
1
1
  import { _range, _sortBy } from '@naturalcycles/js-lib'
2
- import { KeyValueDBTuple } from '../kv/commonKeyValueDB'
3
2
  import { CommonKeyValueDao } from '../kv/commonKeyValueDao'
3
+ import { KeyValueDBTuple } from '../kv/commonKeyValueDB'
4
+
5
+ /* eslint-disable jest/no-export */
4
6
 
5
7
  const testIds = _range(1, 4).map(n => `id${n}`)
6
8
  const testEntries: KeyValueDBTuple[] = testIds.map(id => [id, Buffer.from(`${id}value`)])
@@ -1,4 +1,4 @@
1
- import { jsonSchema, _range, BaseDBEntity, JsonSchemaObject } from '@naturalcycles/js-lib'
1
+ import { _range, BaseDBEntity, jsonSchema, JsonSchemaObject } from '@naturalcycles/js-lib'
2
2
  import {
3
3
  baseDBEntitySchema,
4
4
  binarySchema,