@naturalcycles/db-lib 9.28.0 → 10.0.1

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.
@@ -1,3 +1,3 @@
1
1
  import { CommonDB } from '../common.db';
2
2
  import { CommonDBImplementationQuirks } from './dbTest';
3
- export declare function runCommonDaoTest(db: CommonDB, quirks?: CommonDBImplementationQuirks): void;
3
+ export declare function runCommonDaoTest(db: CommonDB, quirks?: CommonDBImplementationQuirks): Promise<void>;
@@ -6,9 +6,10 @@ const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
7
  const __1 = require("..");
8
8
  const common_dao_1 = require("../commondao/common.dao");
9
- const dbTest_1 = require("./dbTest");
10
9
  const test_model_1 = require("./test.model");
11
- function runCommonDaoTest(db, quirks = {}) {
10
+ async function runCommonDaoTest(db, quirks = {}) {
11
+ // this is because vitest cannot be "required" from cjs
12
+ const { test, expect } = await import('vitest');
12
13
  const { support } = db;
13
14
  const dao = new common_dao_1.CommonDao({
14
15
  table: test_model_1.TEST_TABLE,
@@ -76,7 +77,7 @@ function runCommonDaoTest(db, quirks = {}) {
76
77
  // deepFreeze(item3) // no, Dao is expected to mutate object to add id, created, updated
77
78
  await dao.save(item3);
78
79
  const item3Loaded = await dao.requireById(item3.id);
79
- (0, dbTest_1.expectMatch)([item3], [item3Loaded], quirks);
80
+ expectMatch([item3], [item3Loaded], quirks);
80
81
  expect(item3Loaded.k2).toBeNull();
81
82
  expect(Object.keys(item3)).toContain('k2');
82
83
  expect(item3.k2).toBeNull();
@@ -93,7 +94,7 @@ function runCommonDaoTest(db, quirks = {}) {
93
94
  await dao.save(item3);
94
95
  expected.updated = item3.updated; // as it's mutated
95
96
  const item3Loaded = await dao.requireById(item3.id);
96
- (0, dbTest_1.expectMatch)([expected], [item3Loaded], quirks);
97
+ expectMatch([expected], [item3Loaded], quirks);
97
98
  expect(item3Loaded.k2).toBeUndefined();
98
99
  expect(Object.keys(item3Loaded)).not.toContain('k2');
99
100
  expect(Object.keys(item3)).toContain('k2');
@@ -105,7 +106,7 @@ function runCommonDaoTest(db, quirks = {}) {
105
106
  // no unnecessary mutation
106
107
  const { updated: _, ...clone } = itemsClone[0];
107
108
  expect(items[0]).toMatchObject(clone);
108
- (0, dbTest_1.expectMatch)(expectedItems, itemsSaved, quirks);
109
+ expectMatch(expectedItems, itemsSaved, quirks);
109
110
  });
110
111
  if (support.increment) {
111
112
  test('increment', async () => {
@@ -127,7 +128,7 @@ function runCommonDaoTest(db, quirks = {}) {
127
128
  }
128
129
  return r;
129
130
  });
130
- (0, dbTest_1.expectMatch)(expected, rows, quirks);
131
+ expectMatch(expected, rows, quirks);
131
132
  // reset the changes
132
133
  await dao.increment('k3', 'id1', -1);
133
134
  await dao.increment('k3', 'id2', -2);
@@ -136,20 +137,20 @@ function runCommonDaoTest(db, quirks = {}) {
136
137
  // GET not empty
137
138
  test('getByIds all items', async () => {
138
139
  const rows = await dao.getByIds(items.map(i => i.id).concat('abcd'));
139
- (0, dbTest_1.expectMatch)(expectedItems, (0, js_lib_1._sortBy)(rows, r => r.id), quirks);
140
+ expectMatch(expectedItems, (0, js_lib_1._sortBy)(rows, r => r.id), quirks);
140
141
  });
141
142
  // QUERY
142
143
  if (support.queries) {
143
144
  test('runQuery(all) should return all items', async () => {
144
145
  let rows = await dao.query().runQuery();
145
146
  rows = (0, js_lib_1._sortBy)(rows, r => r.id);
146
- (0, dbTest_1.expectMatch)(expectedItems, rows, quirks);
147
+ expectMatch(expectedItems, rows, quirks);
147
148
  });
148
149
  if (support.dbQueryFilter) {
149
150
  test('query even=true', async () => {
150
151
  let rows = await dao.query().filter('even', '==', true).runQuery();
151
152
  rows = (0, js_lib_1._sortBy)(rows, r => r.id);
152
- (0, dbTest_1.expectMatch)(expectedItems.filter(i => i.even), rows, quirks);
153
+ expectMatch(expectedItems.filter(i => i.even), rows, quirks);
153
154
  });
154
155
  test('query nested property', async () => {
155
156
  let rows = await dao
@@ -157,20 +158,20 @@ function runCommonDaoTest(db, quirks = {}) {
157
158
  .filter('nested.foo', '==', 1)
158
159
  .runQuery();
159
160
  rows = (0, js_lib_1._sortBy)(rows, r => r.id);
160
- (0, dbTest_1.expectMatch)(expectedItems.filter(i => i.nested?.foo === 1), rows, quirks);
161
+ expectMatch(expectedItems.filter(i => i.nested?.foo === 1), rows, quirks);
161
162
  });
162
163
  }
163
164
  if (support.dbQueryOrder) {
164
165
  test('query order by k1 desc', async () => {
165
166
  const rows = await dao.query().order('k1', true).runQuery();
166
- (0, dbTest_1.expectMatch)([...expectedItems].reverse(), rows, quirks);
167
+ expectMatch([...expectedItems].reverse(), rows, quirks);
167
168
  });
168
169
  }
169
170
  if (support.dbQuerySelectFields) {
170
171
  test('projection query with only ids', async () => {
171
172
  let rows = await dao.query().select(['id']).runQuery();
172
173
  rows = (0, js_lib_1._sortBy)(rows, r => r.id);
173
- (0, dbTest_1.expectMatch)(expectedItems.map(item => (0, js_lib_1._pick)(item, ['id'])), rows, quirks);
174
+ expectMatch(expectedItems.map(item => (0, js_lib_1._pick)(item, ['id'])), rows, quirks);
174
175
  });
175
176
  }
176
177
  test('runQueryCount should return 3', async () => {
@@ -183,30 +184,30 @@ function runCommonDaoTest(db, quirks = {}) {
183
184
  let rows = [];
184
185
  await dao.query().streamQueryForEach(bm => void rows.push(bm));
185
186
  rows = (0, js_lib_1._sortBy)(rows, r => r.id);
186
- (0, dbTest_1.expectMatch)(expectedItems, rows, quirks);
187
+ expectMatch(expectedItems, rows, quirks);
187
188
  });
188
189
  test('streamQuery all', async () => {
189
190
  let rows = await dao.query().streamQuery().toArray();
190
191
  rows = (0, js_lib_1._sortBy)(rows, r => r.id);
191
- (0, dbTest_1.expectMatch)(expectedItems, rows, quirks);
192
+ expectMatch(expectedItems, rows, quirks);
192
193
  });
193
194
  test('streamQueryIdsForEach all', async () => {
194
195
  let ids = [];
195
196
  await dao.query().streamQueryIdsForEach(id => void ids.push(id));
196
197
  ids = ids.sort();
197
- (0, dbTest_1.expectMatch)(expectedItems.map(i => i.id), ids, quirks);
198
+ expectMatch(expectedItems.map(i => i.id), ids, quirks);
198
199
  });
199
200
  test('streamQueryIds all', async () => {
200
201
  let ids = await dao.query().streamQueryIds().toArray();
201
202
  ids = ids.sort();
202
- (0, dbTest_1.expectMatch)(expectedItems.map(i => i.id), ids, quirks);
203
+ expectMatch(expectedItems.map(i => i.id), ids, quirks);
203
204
  });
204
205
  test('streamSaveTransform', async () => {
205
206
  const items2 = (0, test_model_1.createTestItemsBM)(2).map(i => ({ ...i, id: i.id + '_str' }));
206
207
  const ids = items2.map(i => i.id);
207
208
  await (0, nodejs_lib_1._pipeline)([node_stream_1.Readable.from(items2), ...dao.streamSaveTransform()]);
208
209
  const items2Loaded = await dao.getByIds(ids);
209
- (0, dbTest_1.expectMatch)(items2, items2Loaded, quirks);
210
+ expectMatch(items2, items2Loaded, quirks);
210
211
  // cleanup
211
212
  await dao.query().filterIn('id', ids).deleteByQuery();
212
213
  });
@@ -252,7 +253,7 @@ function runCommonDaoTest(db, quirks = {}) {
252
253
  });
253
254
  const rows = await dao.query().runQuery();
254
255
  const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
255
- (0, dbTest_1.expectMatch)(expected, rows, quirks);
256
+ expectMatch(expected, rows, quirks);
256
257
  });
257
258
  test('transaction rollback', async () => {
258
259
  await expect(dao.runInTransaction(async (tx) => {
@@ -261,7 +262,7 @@ function runCommonDaoTest(db, quirks = {}) {
261
262
  })).rejects.toThrow();
262
263
  const rows = await dao.query().runQuery();
263
264
  const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
264
- (0, dbTest_1.expectMatch)(expected, rows, quirks);
265
+ expectMatch(expected, rows, quirks);
265
266
  });
266
267
  if (support.queries) {
267
268
  test('transaction cleanup', async () => {
@@ -269,4 +270,19 @@ function runCommonDaoTest(db, quirks = {}) {
269
270
  });
270
271
  }
271
272
  }
273
+ function expectMatch(expected, actual, quirks) {
274
+ // const expectedSorted = sortObjectDeep(expected)
275
+ // const actualSorted = sortObjectDeep(actual)
276
+ if (quirks.allowBooleansAsUndefined) {
277
+ expected = expected.map(r => {
278
+ return typeof r !== 'object' ? r : (0, js_lib_1._filterObject)(r, (_k, v) => v !== false);
279
+ });
280
+ }
281
+ if (quirks.allowExtraPropertiesInResponse) {
282
+ expect(actual).toMatchObject(expected);
283
+ }
284
+ else {
285
+ expect(actual).toEqual(expected);
286
+ }
287
+ }
272
288
  }
@@ -12,5 +12,4 @@ export interface CommonDBImplementationQuirks {
12
12
  */
13
13
  allowBooleansAsUndefined?: boolean;
14
14
  }
15
- export declare function runCommonDBTest(db: CommonDB, quirks?: CommonDBImplementationQuirks): void;
16
- export declare function expectMatch(expected: any[], actual: any[], quirks: CommonDBImplementationQuirks): void;
15
+ export declare function runCommonDBTest(db: CommonDB, quirks?: CommonDBImplementationQuirks): Promise<void>;
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runCommonDBTest = runCommonDBTest;
4
- exports.expectMatch = expectMatch;
5
4
  const js_lib_1 = require("@naturalcycles/js-lib");
6
5
  const common_db_1 = require("../common.db");
7
6
  const dbQuery_1 = require("../query/dbQuery");
8
7
  const test_model_1 = require("./test.model");
9
- function runCommonDBTest(db, quirks = {}) {
8
+ async function runCommonDBTest(db, quirks = {}) {
9
+ // this is because vitest cannot be "required" from cjs
10
+ const { test, expect } = await import('vitest');
10
11
  const { support } = db;
11
12
  const items = (0, test_model_1.createTestItemsDBM)(3);
12
13
  (0, js_lib_1._deepFreeze)(items);
@@ -288,19 +289,19 @@ function runCommonDBTest(db, quirks = {}) {
288
289
  await db.deleteByQuery(queryAll());
289
290
  });
290
291
  }
291
- }
292
- function expectMatch(expected, actual, quirks) {
293
- // const expectedSorted = sortObjectDeep(expected)
294
- // const actualSorted = sortObjectDeep(actual)
295
- if (quirks.allowBooleansAsUndefined) {
296
- expected = expected.map(r => {
297
- return typeof r !== 'object' ? r : (0, js_lib_1._filterObject)(r, (_k, v) => v !== false);
298
- });
299
- }
300
- if (quirks.allowExtraPropertiesInResponse) {
301
- expect(actual).toMatchObject(expected);
302
- }
303
- else {
304
- expect(actual).toEqual(expected);
292
+ function expectMatch(expected, actual, quirks) {
293
+ // const expectedSorted = sortObjectDeep(expected)
294
+ // const actualSorted = sortObjectDeep(actual)
295
+ if (quirks.allowBooleansAsUndefined) {
296
+ expected = expected.map(r => {
297
+ return typeof r !== 'object' ? r : (0, js_lib_1._filterObject)(r, (_k, v) => v !== false);
298
+ });
299
+ }
300
+ if (quirks.allowExtraPropertiesInResponse) {
301
+ expect(actual).toMatchObject(expected);
302
+ }
303
+ else {
304
+ expect(actual).toEqual(expected);
305
+ }
305
306
  }
306
307
  }
@@ -1,2 +1,2 @@
1
1
  import { CommonKeyValueDB } from '../kv/commonKeyValueDB';
2
- export declare function runCommonKeyValueDBTest(db: CommonKeyValueDB): void;
2
+ export declare function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<void>;
@@ -8,7 +8,8 @@ const testEntries = testIds.map(id => [
8
8
  id,
9
9
  Buffer.from(`${id}value`),
10
10
  ]);
11
- function runCommonKeyValueDBTest(db) {
11
+ async function runCommonKeyValueDBTest(db) {
12
+ const { afterAll, beforeAll, expect, test } = await import('vitest');
12
13
  beforeAll(async () => {
13
14
  // Tests in this suite are not isolated,
14
15
  // and failing tests can leave the DB in an unexpected state for other tests,
@@ -1,2 +1,2 @@
1
1
  import { CommonKeyValueDB } from '../kv/commonKeyValueDB';
2
- export declare function runCommonKeyValueDaoTest(db: CommonKeyValueDB): void;
2
+ export declare function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<void>;
@@ -7,7 +7,8 @@ const test_model_1 = require("./test.model");
7
7
  const testItems = (0, test_model_1.createTestItemsBM)(4);
8
8
  const testIds = testItems.map(e => e.id);
9
9
  const testEntries = testItems.map(e => [e.id, Buffer.from(`${e.id}value`)]);
10
- function runCommonKeyValueDaoTest(db) {
10
+ async function runCommonKeyValueDaoTest(db) {
11
+ const { afterAll, beforeAll, expect, test } = await import('vitest');
11
12
  const dao = new commonKeyValueDao_1.CommonKeyValueDao({
12
13
  db,
13
14
  table: test_model_1.TEST_TABLE,
package/package.json CHANGED
@@ -14,9 +14,10 @@
14
14
  },
15
15
  "devDependencies": {
16
16
  "@naturalcycles/bench-lib": "^3",
17
- "@naturalcycles/dev-lib": "^15",
17
+ "@naturalcycles/dev-lib": "^16",
18
18
  "@types/node": "^22",
19
- "jest": "^29"
19
+ "@vitest/coverage-v8": "^3",
20
+ "vitest": "^3"
20
21
  },
21
22
  "files": [
22
23
  "dist",
@@ -43,9 +44,9 @@
43
44
  "url": "https://github.com/NaturalCycles/db-lib"
44
45
  },
45
46
  "engines": {
46
- "node": ">=22.10.0"
47
+ "node": ">=22.12.0"
47
48
  },
48
- "version": "9.28.0",
49
+ "version": "10.0.1",
49
50
  "description": "Lowest Common Denominator API to supported Databases",
50
51
  "keywords": [
51
52
  "db",
@@ -1,11 +1,11 @@
1
1
  import { Readable } from 'node:stream'
2
- import { _deepCopy, _omit, _pick, _sortBy, localTime } from '@naturalcycles/js-lib'
2
+ import { _deepCopy, _filterObject, _omit, _pick, _sortBy, localTime } from '@naturalcycles/js-lib'
3
3
  import { _pipeline } from '@naturalcycles/nodejs-lib'
4
4
  import { CommonDaoLogLevel, DBQuery } from '..'
5
5
  import { CommonDB } from '../common.db'
6
6
  import { CommonDao } from '../commondao/common.dao'
7
7
  import { TestItemBM } from '.'
8
- import { CommonDBImplementationQuirks, expectMatch } from './dbTest'
8
+ import { CommonDBImplementationQuirks } from './dbTest'
9
9
  import {
10
10
  createTestItemBM,
11
11
  createTestItemsBM,
@@ -14,7 +14,13 @@ import {
14
14
  testItemBMSchema,
15
15
  } from './test.model'
16
16
 
17
- export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQuirks = {}): void {
17
+ export async function runCommonDaoTest(
18
+ db: CommonDB,
19
+ quirks: CommonDBImplementationQuirks = {},
20
+ ): Promise<void> {
21
+ // this is because vitest cannot be "required" from cjs
22
+ const { test, expect } = await import('vitest')
23
+
18
24
  const { support } = db
19
25
  const dao = new CommonDao({
20
26
  table: TEST_TABLE,
@@ -358,4 +364,21 @@ export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQui
358
364
  })
359
365
  }
360
366
  }
367
+
368
+ function expectMatch(expected: any[], actual: any[], quirks: CommonDBImplementationQuirks): void {
369
+ // const expectedSorted = sortObjectDeep(expected)
370
+ // const actualSorted = sortObjectDeep(actual)
371
+
372
+ if (quirks.allowBooleansAsUndefined) {
373
+ expected = expected.map(r => {
374
+ return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
375
+ })
376
+ }
377
+
378
+ if (quirks.allowExtraPropertiesInResponse) {
379
+ expect(actual).toMatchObject(expected)
380
+ } else {
381
+ expect(actual).toEqual(expected)
382
+ }
383
+ }
361
384
  }
@@ -24,7 +24,13 @@ export interface CommonDBImplementationQuirks {
24
24
  allowBooleansAsUndefined?: boolean
25
25
  }
26
26
 
27
- export function runCommonDBTest(db: CommonDB, quirks: CommonDBImplementationQuirks = {}): void {
27
+ export async function runCommonDBTest(
28
+ db: CommonDB,
29
+ quirks: CommonDBImplementationQuirks = {},
30
+ ): Promise<void> {
31
+ // this is because vitest cannot be "required" from cjs
32
+ const { test, expect } = await import('vitest')
33
+
28
34
  const { support } = db
29
35
  const items = createTestItemsDBM(3)
30
36
  _deepFreeze(items)
@@ -379,25 +385,21 @@ export function runCommonDBTest(db: CommonDB, quirks: CommonDBImplementationQuir
379
385
  await db.deleteByQuery(queryAll())
380
386
  })
381
387
  }
382
- }
383
388
 
384
- export function expectMatch(
385
- expected: any[],
386
- actual: any[],
387
- quirks: CommonDBImplementationQuirks,
388
- ): void {
389
- // const expectedSorted = sortObjectDeep(expected)
390
- // const actualSorted = sortObjectDeep(actual)
391
-
392
- if (quirks.allowBooleansAsUndefined) {
393
- expected = expected.map(r => {
394
- return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
395
- })
396
- }
389
+ function expectMatch(expected: any[], actual: any[], quirks: CommonDBImplementationQuirks): void {
390
+ // const expectedSorted = sortObjectDeep(expected)
391
+ // const actualSorted = sortObjectDeep(actual)
397
392
 
398
- if (quirks.allowExtraPropertiesInResponse) {
399
- expect(actual).toMatchObject(expected)
400
- } else {
401
- expect(actual).toEqual(expected)
393
+ if (quirks.allowBooleansAsUndefined) {
394
+ expected = expected.map(r => {
395
+ return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
396
+ })
397
+ }
398
+
399
+ if (quirks.allowExtraPropertiesInResponse) {
400
+ expect(actual).toMatchObject(expected)
401
+ } else {
402
+ expect(actual).toEqual(expected)
403
+ }
402
404
  }
403
405
  }
@@ -9,7 +9,9 @@ const testEntries: KeyValueTuple<string, Buffer>[] = testIds.map(id => [
9
9
  Buffer.from(`${id}value`),
10
10
  ])
11
11
 
12
- export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
12
+ export async function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<void> {
13
+ const { afterAll, beforeAll, expect, test } = await import('vitest')
14
+
13
15
  beforeAll(async () => {
14
16
  // Tests in this suite are not isolated,
15
17
  // and failing tests can leave the DB in an unexpected state for other tests,
@@ -7,7 +7,9 @@ const testItems = createTestItemsBM(4)
7
7
  const testIds = testItems.map(e => e.id)
8
8
  const testEntries: KeyValueDBTuple[] = testItems.map(e => [e.id, Buffer.from(`${e.id}value`)])
9
9
 
10
- export function runCommonKeyValueDaoTest(db: CommonKeyValueDB): void {
10
+ export async function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<void> {
11
+ const { afterAll, beforeAll, expect, test } = await import('vitest')
12
+
11
13
  const dao = new CommonKeyValueDao({
12
14
  db,
13
15
  table: TEST_TABLE,