@naturalcycles/db-lib 8.31.0 → 8.32.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.
@@ -33,6 +33,7 @@ export declare class CommonKeyValueDao<T> {
33
33
  createTable(opt?: CommonDBCreateOptions): Promise<void>;
34
34
  create(input?: Partial<T>): T;
35
35
  getById(id?: string): Promise<T | null>;
36
+ requireById(id: string): Promise<T>;
36
37
  getByIdOrEmpty(id: string, part?: Partial<T>): Promise<T>;
37
38
  patch(id: string, patch: Partial<T>): Promise<T>;
38
39
  getByIds(ids: string[]): Promise<KeyValueTuple<string, T>[]>;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CommonKeyValueDao = void 0;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
6
+ const cnst_1 = require("../cnst");
6
7
  // todo: logging
7
8
  // todo: readonly
8
9
  class CommonKeyValueDao {
@@ -26,6 +27,18 @@ class CommonKeyValueDao {
26
27
  const [r] = await this.getByIds([id]);
27
28
  return r?.[1] || null;
28
29
  }
30
+ async requireById(id) {
31
+ const [r] = await this.getByIds([id]);
32
+ if (!r) {
33
+ const { table } = this.cfg;
34
+ throw new js_lib_1.AppError(`DB row required, but not found: ${table}.${id}`, {
35
+ code: cnst_1.DBLibError.DB_ROW_REQUIRED,
36
+ table,
37
+ id,
38
+ });
39
+ }
40
+ return r[1];
41
+ }
29
42
  async getByIdOrEmpty(id, part = {}) {
30
43
  const [r] = await this.getByIds([id]);
31
44
  if (r)
package/package.json CHANGED
@@ -43,7 +43,7 @@
43
43
  "engines": {
44
44
  "node": ">=14.15"
45
45
  },
46
- "version": "8.31.0",
46
+ "version": "8.32.0",
47
47
  "description": "Lowest Common Denominator API to supported Databases",
48
48
  "keywords": [
49
49
  "db",
@@ -1,5 +1,6 @@
1
- import { ErrorMode, KeyValueTuple, pMap } from '@naturalcycles/js-lib'
1
+ import { AppError, ErrorMode, KeyValueTuple, pMap } from '@naturalcycles/js-lib'
2
2
  import { ReadableTyped, transformMap } from '@naturalcycles/nodejs-lib'
3
+ import { DBLibError } from '../cnst'
3
4
  import { CommonDaoLogLevel } from '../commondao/common.dao.model'
4
5
  import { CommonDBCreateOptions } from '../db.model'
5
6
  import { CommonKeyValueDB, KeyValueDBTuple } from './commonKeyValueDB'
@@ -58,6 +59,21 @@ export class CommonKeyValueDao<T> {
58
59
  return r?.[1] || null
59
60
  }
60
61
 
62
+ async requireById(id: string): Promise<T> {
63
+ const [r] = await this.getByIds([id])
64
+
65
+ if (!r) {
66
+ const { table } = this.cfg
67
+ throw new AppError(`DB row required, but not found: ${table}.${id}`, {
68
+ code: DBLibError.DB_ROW_REQUIRED,
69
+ table,
70
+ id,
71
+ })
72
+ }
73
+
74
+ return r[1]
75
+ }
76
+
61
77
  async getByIdOrEmpty(id: string, part: Partial<T> = {}): Promise<T> {
62
78
  const [r] = await this.getByIds([id])
63
79
  if (r) return r[1]