@naturalcycles/db-lib 8.25.0 → 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;
@@ -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
  }
@@ -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
@@ -13,7 +13,8 @@
13
13
  "@naturalcycles/bench-lib": "^1.0.0",
14
14
  "@naturalcycles/dev-lib": "^12.0.1",
15
15
  "@types/node": "^17.0.0",
16
- "jest": "^27.0.3"
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.25.0",
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
  }
@@ -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
  }
@@ -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()