@naturalcycles/db-lib 8.24.3 → 8.27.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;
@@ -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;
@@ -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
  }
@@ -35,6 +35,7 @@ export declare class CommonKeyValueDao<T> {
35
35
  save(id: string, value: T): Promise<void>;
36
36
  saveBatch(entries: KeyValueTuple<string, T>[]): Promise<void>;
37
37
  deleteByIds(ids: string[]): Promise<void>;
38
+ deleteById(id: string): Promise<void>;
38
39
  streamIds(limit?: number): ReadableTyped<string>;
39
40
  streamValues(limit?: number): ReadableTyped<Buffer>;
40
41
  streamEntries(limit?: number): ReadableTyped<KeyValueTuple<string, T>>;
@@ -49,6 +49,9 @@ class CommonKeyValueDao {
49
49
  async deleteByIds(ids) {
50
50
  await this.cfg.db.deleteByIds(this.cfg.table, ids);
51
51
  }
52
+ async deleteById(id) {
53
+ await this.cfg.db.deleteByIds(this.cfg.table, [id]);
54
+ }
52
55
  streamIds(limit) {
53
56
  return this.cfg.db.streamIds(this.cfg.table, limit);
54
57
  }
@@ -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.3",
46
+ "version": "8.27.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
  }
@@ -168,6 +168,14 @@ export interface CommonDaoOptions extends CommonDBOptions {
168
168
  * Useful e.g in AirtableDB where you can have one Dao to control multiple tables.
169
169
  */
170
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
171
179
  }
172
180
 
173
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
@@ -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
  }
@@ -84,6 +84,10 @@ export class CommonKeyValueDao<T> {
84
84
  await this.cfg.db.deleteByIds(this.cfg.table, ids)
85
85
  }
86
86
 
87
+ async deleteById(id: string): Promise<void> {
88
+ await this.cfg.db.deleteByIds(this.cfg.table, [id])
89
+ }
90
+
87
91
  streamIds(limit?: number): ReadableTyped<string> {
88
92
  return this.cfg.db.streamIds(this.cfg.table, limit)
89
93
  }
@@ -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()