@naturalcycles/db-lib 10.64.0 → 10.65.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.
@@ -1,13 +1,15 @@
1
1
  import { comparators } from '@naturalcycles/js-lib/array';
2
2
  import { _get, _pick } from '@naturalcycles/js-lib/object/object.util.js';
3
+ // Datastore (and Firestore) treat an array-valued property as multi-valued:
4
+ // `==` matches when any element equals the value, `in` when any element is among the values.
3
5
  const FILTER_FNS = {
4
- '==': (v, val) => v === val,
6
+ '==': (v, val) => (Array.isArray(v) ? v.includes(val) : v === val),
5
7
  '!=': (v, val) => v !== val,
6
8
  '<': (v, val) => v < val,
7
9
  '<=': (v, val) => v <= val,
8
10
  '>': (v, val) => v > val,
9
11
  '>=': (v, val) => v >= val,
10
- in: (v, val) => (val || []).includes(v),
12
+ in: (v, val) => Array.isArray(v) ? v.some(item => (val || []).includes(item)) : (val || []).includes(v),
11
13
  'not-in': (v, val) => !(val || []).includes(v),
12
14
  'array-contains': (v, val) => (v || []).includes(val),
13
15
  'array-contains-any': (v, val) => (v && val && v.some(item => val.includes(item))) || false,
@@ -101,11 +101,11 @@ export class CommonKeyValueDao {
101
101
  async saveBatch(entries, opt) {
102
102
  const { transformer } = this.cfg;
103
103
  let rawEntries;
104
- if (!transformer) {
105
- rawEntries = entries;
104
+ if (transformer) {
105
+ rawEntries = entries.map(([id, v]) => [id, transformer.valueToBuffer(v)]);
106
106
  }
107
107
  else {
108
- rawEntries = entries.map(([id, v]) => [id, transformer.valueToBuffer(v)]);
108
+ rawEntries = entries;
109
109
  }
110
110
  await this.cfg.db.saveBatch(this.cfg.table, rawEntries, opt);
111
111
  }
@@ -315,7 +315,7 @@ export async function runCommonDBTest(db, quirks = {}) {
315
315
  // const actualSorted = sortObjectDeep(actual)
316
316
  if (quirks.allowBooleansAsUndefined) {
317
317
  expected = expected.map(r => {
318
- return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false);
318
+ return typeof r === 'object' ? _filterObject(r, (_k, v) => v !== false) : r;
319
319
  });
320
320
  }
321
321
  if (quirks.allowExtraPropertiesInResponse) {
@@ -335,7 +335,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
335
335
  // const actualSorted = sortObjectDeep(actual)
336
336
  if (quirks.allowBooleansAsUndefined) {
337
337
  expected = expected.map(r => {
338
- return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false);
338
+ return typeof r === 'object' ? _filterObject(r, (_k, v) => v !== false) : r;
339
339
  });
340
340
  }
341
341
  if (quirks.allowExtraPropertiesInResponse) {
@@ -1,4 +1,4 @@
1
- import { _range, _sortBy } from '@naturalcycles/js-lib/array';
1
+ import { _range, _sortBy, comparators } from '@naturalcycles/js-lib/array';
2
2
  import { TEST_TABLE } from './test.model.js';
3
3
  const testIds = _range(1, 4).map(n => `id${n}`);
4
4
  const testEntries = testIds.map(id => [
@@ -63,7 +63,7 @@ export async function runCommonKeyValueDBTest(db) {
63
63
  });
64
64
  it('should stream all values', async () => {
65
65
  const values = await db.streamValues(TEST_TABLE).toArray();
66
- values.sort();
66
+ values.sort(Buffer.compare);
67
67
  expect(values).toEqual(testEntries.map(e => e[1]));
68
68
  });
69
69
  it('should stream as many values as the limit', async () => {
@@ -74,7 +74,7 @@ export async function runCommonKeyValueDBTest(db) {
74
74
  });
75
75
  it('should stream all entries', async () => {
76
76
  const entries = await db.streamEntries(TEST_TABLE).toArray();
77
- entries.sort();
77
+ entries.sort(comparators.by(e => e[0]));
78
78
  expect(entries).toEqual(testEntries);
79
79
  });
80
80
  it('should stream as many entries as the limit', async () => {
@@ -1,4 +1,4 @@
1
- import { _sortBy } from '@naturalcycles/js-lib/array/sort.js';
1
+ import { _sortBy, comparators } from '@naturalcycles/js-lib/array/sort.js';
2
2
  import { CommonKeyValueDao } from '../kv/commonKeyValueDao.js';
3
3
  import { createTestItemsBM, TEST_TABLE } from './test.model.js';
4
4
  const testItems = createTestItemsBM(4);
@@ -60,7 +60,7 @@ export async function runCommonKeyValueDaoTest(db) {
60
60
  });
61
61
  it('should stream all values', async () => {
62
62
  const values = await dao.streamValues().toArray();
63
- values.sort();
63
+ values.sort(Buffer.compare);
64
64
  expect(values).toEqual(testEntries.map(e => e[1]));
65
65
  });
66
66
  it('should stream as many values as the limit', async () => {
@@ -71,7 +71,7 @@ export async function runCommonKeyValueDaoTest(db) {
71
71
  });
72
72
  it('should stream all entries', async () => {
73
73
  const entries = await dao.streamEntries().toArray();
74
- entries.sort();
74
+ entries.sort(comparators.by(e => e[0]));
75
75
  expect(entries).toEqual(testEntries);
76
76
  });
77
77
  it('should stream as many entries as the limit', async () => {
@@ -1,6 +1,7 @@
1
1
  import { j, JBuilder } from '@naturalcycles/nodejs-lib/ajv';
2
2
  import { dbQueryFilterOperatorValues } from '../query/dbQuery.js';
3
3
  // oxlint-disable typescript/explicit-function-return-type
4
+ // oxlint-disable typescript/no-deprecated -- castAs narrows string to keyof ROW, which a schema cannot check
4
5
  // DBTransaction schema - validates presence without deep validation
5
6
  const dbTransactionSchema = j.object.any().castAs();
6
7
  // Schema that accepts any value (string, number, boolean, object, array, null)
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.64.0",
4
+ "version": "10.65.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
8
8
  },
9
9
  "devDependencies": {
10
- "@naturalcycles/dev-lib": "0.0.0",
10
+ "@naturalcycles/dev-lib": "20.79.0",
11
11
  "typescript": "^7"
12
12
  },
13
13
  "files": [
@@ -4,14 +4,17 @@ import type { ObjectWithId } from '@naturalcycles/js-lib/types'
4
4
  import type { DBQuery, DBQueryFilterOperator } from '../query/dbQuery.js'
5
5
 
6
6
  type FilterFn = (v: any, val: any) => boolean
7
+ // Datastore (and Firestore) treat an array-valued property as multi-valued:
8
+ // `==` matches when any element equals the value, `in` when any element is among the values.
7
9
  const FILTER_FNS: Record<DBQueryFilterOperator, FilterFn> = {
8
- '==': (v, val) => v === val,
10
+ '==': (v, val) => (Array.isArray(v) ? v.includes(val) : v === val),
9
11
  '!=': (v, val) => v !== val,
10
12
  '<': (v, val) => v < val,
11
13
  '<=': (v, val) => v <= val,
12
14
  '>': (v, val) => v > val,
13
15
  '>=': (v, val) => v >= val,
14
- in: (v, val?: readonly any[]) => (val || []).includes(v),
16
+ in: (v, val?: readonly any[]) =>
17
+ Array.isArray(v) ? v.some(item => (val || []).includes(item)) : (val || []).includes(v),
15
18
  'not-in': (v, val?: readonly any[]) => !(val || []).includes(v),
16
19
  'array-contains': (v?: readonly any[], val?: any) => (v || []).includes(val),
17
20
  'array-contains-any': (v?: readonly any[], val?: readonly any[]) =>
@@ -177,10 +177,10 @@ export class CommonKeyValueDao<K extends string = string, V = Buffer> {
177
177
  const { transformer } = this.cfg
178
178
  let rawEntries: KeyValueDBTuple[]
179
179
 
180
- if (!transformer) {
181
- rawEntries = entries as any
182
- } else {
180
+ if (transformer) {
183
181
  rawEntries = entries.map(([id, v]) => [id, transformer.valueToBuffer(v)])
182
+ } else {
183
+ rawEntries = entries as any
184
184
  }
185
185
 
186
186
  await this.cfg.db.saveBatch(this.cfg.table, rawEntries, opt)
@@ -441,7 +441,7 @@ export async function runCommonDBTest(
441
441
 
442
442
  if (quirks.allowBooleansAsUndefined) {
443
443
  expected = expected.map(r => {
444
- return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
444
+ return typeof r === 'object' ? _filterObject(r, (_k, v) => v !== false) : r
445
445
  })
446
446
  }
447
447
 
@@ -453,7 +453,7 @@ export async function runCommonDaoTest(
453
453
 
454
454
  if (quirks.allowBooleansAsUndefined) {
455
455
  expected = expected.map(r => {
456
- return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
456
+ return typeof r === 'object' ? _filterObject(r, (_k, v) => v !== false) : r
457
457
  })
458
458
  }
459
459
 
@@ -1,4 +1,4 @@
1
- import { _range, _sortBy } from '@naturalcycles/js-lib/array'
1
+ import { _range, _sortBy, comparators } from '@naturalcycles/js-lib/array'
2
2
  import type { KeyValueTuple } from '@naturalcycles/js-lib/types'
3
3
  import type { CommonKeyValueDB } from '../kv/commonKeyValueDB.js'
4
4
  import { TEST_TABLE } from './test.model.js'
@@ -82,7 +82,7 @@ export async function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<voi
82
82
 
83
83
  it('should stream all values', async () => {
84
84
  const values = await db.streamValues(TEST_TABLE).toArray()
85
- values.sort()
85
+ values.sort(Buffer.compare)
86
86
  expect(values).toEqual(testEntries.map(e => e[1]))
87
87
  })
88
88
 
@@ -95,7 +95,7 @@ export async function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<voi
95
95
 
96
96
  it('should stream all entries', async () => {
97
97
  const entries = await db.streamEntries(TEST_TABLE).toArray()
98
- entries.sort()
98
+ entries.sort(comparators.by(e => e[0]))
99
99
  expect(entries).toEqual(testEntries)
100
100
  })
101
101
 
@@ -1,4 +1,4 @@
1
- import { _sortBy } from '@naturalcycles/js-lib/array/sort.js'
1
+ import { _sortBy, comparators } from '@naturalcycles/js-lib/array/sort.js'
2
2
  import { CommonKeyValueDao } from '../kv/commonKeyValueDao.js'
3
3
  import type { CommonKeyValueDB, KeyValueDBTuple } from '../kv/commonKeyValueDB.js'
4
4
  import { createTestItemsBM, TEST_TABLE } from './test.model.js'
@@ -76,7 +76,7 @@ export async function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<vo
76
76
 
77
77
  it('should stream all values', async () => {
78
78
  const values = await dao.streamValues().toArray()
79
- values.sort()
79
+ values.sort(Buffer.compare)
80
80
  expect(values).toEqual(testEntries.map(e => e[1]))
81
81
  })
82
82
 
@@ -89,7 +89,7 @@ export async function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<vo
89
89
 
90
90
  it('should stream all entries', async () => {
91
91
  const entries = await dao.streamEntries().toArray()
92
- entries.sort()
92
+ entries.sort(comparators.by(e => e[0]))
93
93
  expect(entries).toEqual(testEntries)
94
94
  })
95
95
 
@@ -5,6 +5,7 @@ import type { DBQueryFilter, DBQueryOrder } from '../query/dbQuery.js'
5
5
  import { dbQueryFilterOperatorValues } from '../query/dbQuery.js'
6
6
 
7
7
  // oxlint-disable typescript/explicit-function-return-type
8
+ // oxlint-disable typescript/no-deprecated -- castAs narrows string to keyof ROW, which a schema cannot check
8
9
 
9
10
  // DBTransaction schema - validates presence without deep validation
10
11
  const dbTransactionSchema = j.object.any().castAs<DBTransaction>()