@naturalcycles/db-lib 9.14.3 → 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.
@@ -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.
@@ -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.
@@ -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({
@@ -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.3",
48
+ "version": "9.15.0",
44
49
  "description": "Lowest Common Denominator API to supported Databases",
45
50
  "keywords": [
46
51
  "db",
@@ -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
  /**
@@ -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,
@@ -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 })
@@ -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
@@ -14,6 +14,7 @@ import {
14
14
  testItemBMSchema,
15
15
  } from './test.model'
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({
@@ -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
  */
@@ -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`)])
@@ -2,6 +2,8 @@ import { _range, _sortBy } from '@naturalcycles/js-lib'
2
2
  import { CommonKeyValueDao } from '../kv/commonKeyValueDao'
3
3
  import { KeyValueDBTuple } from '../kv/commonKeyValueDB'
4
4
 
5
+ /* eslint-disable jest/no-export */
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`)])
7
9