@naturalcycles/db-lib 8.24.2 → 8.26.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.
@@ -17,4 +17,5 @@ export declare class InMemoryKeyValueDB implements CommonKeyValueDB {
17
17
  streamIds(table: string, limit?: number): ReadableTyped<string>;
18
18
  streamValues(table: string, limit?: number): ReadableTyped<Buffer>;
19
19
  streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple>;
20
+ count(table: string): Promise<number>;
20
21
  }
@@ -34,5 +34,10 @@ class InMemoryKeyValueDB {
34
34
  streamEntries(table, limit) {
35
35
  return stream_1.Readable.from(Object.entries(this.data[table] || {}).slice(0, limit));
36
36
  }
37
+ async count(table) {
38
+ var _a;
39
+ (_a = this.data)[table] || (_a[table] = {});
40
+ return Object.keys(this.data[table]).length;
41
+ }
37
42
  }
38
43
  exports.InMemoryKeyValueDB = InMemoryKeyValueDB;
@@ -137,7 +137,7 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
137
137
  *
138
138
  * Does NOT mutate the object.
139
139
  */
140
- validateAndConvert<IN, OUT = IN>(obj: IN, schema?: ObjectSchemaTyped<IN> | AjvSchema<IN>, modelType?: DBModelType, opt?: CommonDaoOptions): OUT;
140
+ validateAndConvert<IN, OUT = IN>(obj: Partial<IN>, schema: ObjectSchemaTyped<IN> | AjvSchema<IN> | undefined, modelType: DBModelType, opt?: CommonDaoOptions): OUT;
141
141
  getTableSchema(): Promise<JsonSchemaRootObject<DBM>>;
142
142
  createTable(schema: JsonSchemaObject<DBM>, opt?: CommonDaoCreateOptions): Promise<void>;
143
143
  /**
@@ -56,7 +56,17 @@ class CommonDao {
56
56
  const op = `getById(${id})`;
57
57
  const table = opt.table || this.cfg.table;
58
58
  const started = this.logStarted(op, table);
59
- const [dbm] = await this.cfg.db.getByIds(table, [id]);
59
+ let dbm;
60
+ if (opt.timeout) {
61
+ // todo: possibly remove it after debugging is done
62
+ dbm = (await (0, js_lib_1.pTimeout)(this.cfg.db.getByIds(table, [id]), {
63
+ timeout: opt.timeout,
64
+ name: `getById(${table})`,
65
+ }))[0];
66
+ }
67
+ else {
68
+ dbm = (await this.cfg.db.getByIds(table, [id]))[0];
69
+ }
60
70
  const bm = opt.raw ? dbm : await this.dbmToBM(dbm, opt);
61
71
  this.logResult(started, op, bm, table);
62
72
  return bm || null;
@@ -1,4 +1,4 @@
1
- import { CommonLogger, ErrorMode, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { CommonLogger, ErrorMode, ObjectWithId, Saved } from '@naturalcycles/js-lib';
2
2
  import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchemaTyped, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDB } from '../common.db';
4
4
  import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
@@ -47,7 +47,7 @@ export declare enum CommonDaoLogLevel {
47
47
  */
48
48
  DATA_FULL = 30
49
49
  }
50
- export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId, TM> {
50
+ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId = Saved<BM>, TM = BM> {
51
51
  db: CommonDB;
52
52
  table: string;
53
53
  /**
@@ -135,6 +135,13 @@ export interface CommonDaoOptions extends CommonDBOptions {
135
135
  * Useful e.g in AirtableDB where you can have one Dao to control multiple tables.
136
136
  */
137
137
  table?: string;
138
+ /**
139
+ * If set - wraps the method in `pTimeout` with a timeout of given number of milliseconds.
140
+ * Currently, it is only used to debug an ongoing GCP infra issue.
141
+ *
142
+ * @experimental
143
+ */
144
+ timeout?: number;
138
145
  }
139
146
  /**
140
147
  * All properties default to undefined.
@@ -28,4 +28,5 @@ export interface CommonKeyValueDB {
28
28
  streamIds(table: string, limit?: number): ReadableTyped<string>;
29
29
  streamValues(table: string, limit?: number): ReadableTyped<Buffer>;
30
30
  streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple>;
31
+ count(table: string): Promise<number>;
31
32
  }
@@ -78,7 +78,7 @@ async function dbPipelineRestore(opt) {
78
78
  ...opt,
79
79
  metric: table,
80
80
  }),
81
- (0, nodejs_lib_1.transformLimit)(limit),
81
+ (0, nodejs_lib_1.transformLimit)({ limit }),
82
82
  ...(sinceUpdated
83
83
  ? [(0, nodejs_lib_1.transformFilterSync)(r => r.updated >= sinceUpdated)]
84
84
  : []),
@@ -18,6 +18,11 @@ export interface CommonDBImplementationFeatures {
18
18
  streaming?: boolean;
19
19
  bufferSupport?: boolean;
20
20
  nullValues?: boolean;
21
+ /**
22
+ * Set false for SQL (relational) databases,
23
+ * they will return `null` for all missing properties.
24
+ */
25
+ documentDB?: boolean;
21
26
  }
22
27
  /**
23
28
  * All options default to `false`.
@@ -12,7 +12,7 @@ const test_util_1 = require("./test.util");
12
12
  function runCommonDBTest(db, features = {}, quirks = {}) {
13
13
  const { querying = true, tableSchemas = true, createTable = true, dbQueryFilter = true,
14
14
  // dbQueryFilterIn = true,
15
- dbQueryOrder = true, dbQuerySelectFields = true, streaming = true, strongConsistency = true, bufferSupport = true, nullValues = true, } = features;
15
+ dbQueryOrder = true, dbQuerySelectFields = true, streaming = true, strongConsistency = true, bufferSupport = true, nullValues = true, documentDB = true, } = features;
16
16
  // const {
17
17
  // allowExtraPropertiesInResponse,
18
18
  // allowBooleansAsUndefined,
@@ -71,20 +71,22 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
71
71
  expect(item3Loaded.k2).toBe(null);
72
72
  });
73
73
  }
74
- test('undefined values should not be saved/loaded', async () => {
75
- const item3 = {
76
- ...(0, test_model_1.createTestItemDBM)(3),
77
- k2: undefined,
78
- };
79
- (0, test_util_1.deepFreeze)(item3);
80
- const expected = { ...item3 };
81
- delete expected.k2;
82
- await db.saveBatch(test_model_1.TEST_TABLE, [item3]);
83
- const item3Loaded = (await db.getByIds(test_model_1.TEST_TABLE, [item3.id]))[0];
84
- expectMatch([expected], [item3Loaded], quirks);
85
- expect(item3Loaded.k2).toBe(undefined);
86
- expect(Object.keys(item3Loaded)).not.toContain('k2');
87
- });
74
+ if (documentDB) {
75
+ test('undefined values should not be saved/loaded', async () => {
76
+ const item3 = {
77
+ ...(0, test_model_1.createTestItemDBM)(3),
78
+ k2: undefined,
79
+ };
80
+ (0, test_util_1.deepFreeze)(item3);
81
+ const expected = { ...item3 };
82
+ delete expected.k2;
83
+ await db.saveBatch(test_model_1.TEST_TABLE, [item3]);
84
+ const item3Loaded = (await db.getByIds(test_model_1.TEST_TABLE, [item3.id]))[0];
85
+ expectMatch([expected], [item3Loaded], quirks);
86
+ expect(item3Loaded.k2).toBe(undefined);
87
+ expect(Object.keys(item3Loaded)).not.toContain('k2');
88
+ });
89
+ }
88
90
  test('saveBatch test items', async () => {
89
91
  await db.saveBatch(test_model_1.TEST_TABLE, items);
90
92
  });
@@ -20,12 +20,18 @@ function runCommonKeyValueDBTest(db) {
20
20
  const results = await db.getByIds(test_model_1.TEST_TABLE, testIds);
21
21
  expect(results).toEqual([]);
22
22
  });
23
+ test('count should be 0', async () => {
24
+ expect(await db.count(test_model_1.TEST_TABLE)).toBe(0);
25
+ });
23
26
  test('saveBatch, then getByIds', async () => {
24
27
  await db.saveBatch(test_model_1.TEST_TABLE, testEntries);
25
28
  const entries = await db.getByIds(test_model_1.TEST_TABLE, testIds);
26
29
  (0, js_lib_1._sortBy)(entries, e => e[0], true);
27
30
  expect(entries).toEqual(testEntries);
28
31
  });
32
+ test('count should be 3', async () => {
33
+ expect(await db.count(test_model_1.TEST_TABLE)).toBe(3);
34
+ });
29
35
  test('streamIds', async () => {
30
36
  const ids = await (0, nodejs_lib_1.readableToArray)(db.streamIds(test_model_1.TEST_TABLE));
31
37
  ids.sort();
package/package.json CHANGED
@@ -12,8 +12,9 @@
12
12
  "devDependencies": {
13
13
  "@naturalcycles/bench-lib": "^1.0.0",
14
14
  "@naturalcycles/dev-lib": "^12.0.1",
15
- "@types/node": "^16.0.0",
16
- "jest": "^27.0.3"
15
+ "@types/node": "^17.0.0",
16
+ "jest": "^27.0.3",
17
+ "weak-napi": "^2.0.2"
17
18
  },
18
19
  "files": [
19
20
  "dist",
@@ -42,7 +43,7 @@
42
43
  "engines": {
43
44
  "node": ">=14.15"
44
45
  },
45
- "version": "8.24.2",
46
+ "version": "8.26.0",
46
47
  "description": "Lowest Common Denominator API to supported Databases",
47
48
  "keywords": [
48
49
  "db",
@@ -42,4 +42,9 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
42
42
  streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
43
43
  return Readable.from(Object.entries(this.data[table] || {}).slice(0, limit))
44
44
  }
45
+
46
+ async count(table: string): Promise<number> {
47
+ this.data[table] ||= {}
48
+ return Object.keys(this.data[table]!).length
49
+ }
45
50
  }
@@ -1,4 +1,4 @@
1
- import { CommonLogger, ErrorMode, ObjectWithId } from '@naturalcycles/js-lib'
1
+ import { CommonLogger, ErrorMode, ObjectWithId, Saved } from '@naturalcycles/js-lib'
2
2
  import {
3
3
  AjvSchema,
4
4
  AjvValidationError,
@@ -60,7 +60,11 @@ export enum CommonDaoLogLevel {
60
60
  DATA_FULL = 30,
61
61
  }
62
62
 
63
- export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId, TM> {
63
+ export interface CommonDaoCfg<
64
+ BM extends Partial<ObjectWithId>,
65
+ DBM extends ObjectWithId = Saved<BM>,
66
+ TM = BM,
67
+ > {
64
68
  db: CommonDB
65
69
  table: string
66
70
 
@@ -164,6 +168,14 @@ export interface CommonDaoOptions extends CommonDBOptions {
164
168
  * Useful e.g in AirtableDB where you can have one Dao to control multiple tables.
165
169
  */
166
170
  table?: string
171
+
172
+ /**
173
+ * If set - wraps the method in `pTimeout` with a timeout of given number of milliseconds.
174
+ * Currently, it is only used to debug an ongoing GCP infra issue.
175
+ *
176
+ * @experimental
177
+ */
178
+ timeout?: number
167
179
  }
168
180
 
169
181
  /**
@@ -12,6 +12,7 @@ import {
12
12
  JsonSchemaRootObject,
13
13
  ObjectWithId,
14
14
  pMap,
15
+ pTimeout,
15
16
  Saved,
16
17
  } from '@naturalcycles/js-lib'
17
18
  import {
@@ -103,7 +104,21 @@ export class CommonDao<
103
104
  const op = `getById(${id})`
104
105
  const table = opt.table || this.cfg.table
105
106
  const started = this.logStarted(op, table)
106
- const [dbm] = await this.cfg.db.getByIds<DBM>(table, [id])
107
+
108
+ let dbm: DBM | undefined
109
+
110
+ if (opt.timeout) {
111
+ // todo: possibly remove it after debugging is done
112
+ dbm = (
113
+ await pTimeout(this.cfg.db.getByIds<DBM>(table, [id]), {
114
+ timeout: opt.timeout,
115
+ name: `getById(${table})`,
116
+ })
117
+ )[0]
118
+ } else {
119
+ dbm = (await this.cfg.db.getByIds<DBM>(table, [id]))[0]
120
+ }
121
+
107
122
  const bm = opt.raw ? (dbm as any) : await this.dbmToBM(dbm, opt)
108
123
  this.logResult(started, op, bm, table)
109
124
  return bm || null
@@ -903,9 +918,9 @@ export class CommonDao<
903
918
  * Does NOT mutate the object.
904
919
  */
905
920
  validateAndConvert<IN, OUT = IN>(
906
- obj: IN,
907
- schema?: ObjectSchemaTyped<IN> | AjvSchema<IN>,
908
- modelType?: DBModelType,
921
+ obj: Partial<IN>,
922
+ schema: ObjectSchemaTyped<IN> | AjvSchema<IN> | undefined,
923
+ modelType: DBModelType,
909
924
  opt: CommonDaoOptions = {},
910
925
  ): OUT {
911
926
  // `raw` option completely bypasses any processing
@@ -928,12 +943,12 @@ export class CommonDao<
928
943
 
929
944
  // Pre-validation hooks
930
945
  if (modelType === DBModelType.DBM) {
931
- obj = this.cfg.hooks!.beforeDBMValidate!(obj as any) as any
946
+ obj = this.cfg.hooks!.beforeDBMValidate!(obj as any) as IN
932
947
  }
933
948
 
934
949
  // Return as is if no schema is passed or if `skipConversion` is set
935
950
  if (!schema || opt.skipConversion) {
936
- return obj as any
951
+ return obj as OUT
937
952
  }
938
953
 
939
954
  // This will Convert and Validate
@@ -947,12 +962,12 @@ export class CommonDao<
947
962
  // Ajv schema
948
963
  convertedValue = obj // because Ajv mutates original object
949
964
 
950
- error = schema.getValidationError(obj, {
965
+ error = schema.getValidationError(obj as IN, {
951
966
  objectName,
952
967
  })
953
968
  } else {
954
969
  // Joi
955
- const vr = getValidationResult<IN, OUT>(obj, schema, objectName)
970
+ const vr = getValidationResult<IN, OUT>(obj as IN, schema, objectName)
956
971
  error = vr.error
957
972
  convertedValue = vr.value
958
973
  }
@@ -34,4 +34,6 @@ export interface CommonKeyValueDB {
34
34
  streamIds(table: string, limit?: number): ReadableTyped<string>
35
35
  streamValues(table: string, limit?: number): ReadableTyped<Buffer>
36
36
  streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple>
37
+
38
+ count(table: string): Promise<number>
37
39
  }
@@ -209,7 +209,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
209
209
  ...opt,
210
210
  metric: table,
211
211
  }),
212
- transformLimit(limit),
212
+ transformLimit({ limit }),
213
213
  ...(sinceUpdated
214
214
  ? [transformFilterSync<SavedDBEntity>(r => r.updated >= sinceUpdated)]
215
215
  : []),
@@ -35,6 +35,12 @@ export interface CommonDBImplementationFeatures {
35
35
 
36
36
  bufferSupport?: boolean
37
37
  nullValues?: boolean
38
+
39
+ /**
40
+ * Set false for SQL (relational) databases,
41
+ * they will return `null` for all missing properties.
42
+ */
43
+ documentDB?: boolean
38
44
  }
39
45
 
40
46
  /**
@@ -78,6 +84,7 @@ export function runCommonDBTest(
78
84
  strongConsistency = true,
79
85
  bufferSupport = true,
80
86
  nullValues = true,
87
+ documentDB = true,
81
88
  } = features
82
89
 
83
90
  // const {
@@ -151,21 +158,23 @@ export function runCommonDBTest(
151
158
  })
152
159
  }
153
160
 
154
- test('undefined values should not be saved/loaded', async () => {
155
- const item3 = {
156
- ...createTestItemDBM(3),
157
- k2: undefined,
158
- }
159
- deepFreeze(item3)
160
- const expected = { ...item3 }
161
- delete expected.k2
162
-
163
- await db.saveBatch(TEST_TABLE, [item3])
164
- const item3Loaded = (await db.getByIds<TestItemDBM>(TEST_TABLE, [item3.id]))[0]!
165
- expectMatch([expected], [item3Loaded], quirks)
166
- expect(item3Loaded.k2).toBe(undefined)
167
- expect(Object.keys(item3Loaded)).not.toContain('k2')
168
- })
161
+ if (documentDB) {
162
+ test('undefined values should not be saved/loaded', async () => {
163
+ const item3 = {
164
+ ...createTestItemDBM(3),
165
+ k2: undefined,
166
+ }
167
+ deepFreeze(item3)
168
+ const expected = { ...item3 }
169
+ delete expected.k2
170
+
171
+ await db.saveBatch(TEST_TABLE, [item3])
172
+ const item3Loaded = (await db.getByIds<TestItemDBM>(TEST_TABLE, [item3.id]))[0]!
173
+ expectMatch([expected], [item3Loaded], quirks)
174
+ expect(item3Loaded.k2).toBe(undefined)
175
+ expect(Object.keys(item3Loaded)).not.toContain('k2')
176
+ })
177
+ }
169
178
 
170
179
  test('saveBatch test items', async () => {
171
180
  await db.saveBatch(TEST_TABLE, items)
@@ -25,6 +25,10 @@ export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
25
25
  expect(results).toEqual([])
26
26
  })
27
27
 
28
+ test('count should be 0', async () => {
29
+ expect(await db.count(TEST_TABLE)).toBe(0)
30
+ })
31
+
28
32
  test('saveBatch, then getByIds', async () => {
29
33
  await db.saveBatch(TEST_TABLE, testEntries)
30
34
 
@@ -33,6 +37,10 @@ export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
33
37
  expect(entries).toEqual(testEntries)
34
38
  })
35
39
 
40
+ test('count should be 3', async () => {
41
+ expect(await db.count(TEST_TABLE)).toBe(3)
42
+ })
43
+
36
44
  test('streamIds', async () => {
37
45
  const ids = await readableToArray(db.streamIds(TEST_TABLE))
38
46
  ids.sort()