@naturalcycles/db-lib 9.21.0 → 9.22.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.
Files changed (43) hide show
  1. package/dist/adapter/cachedb/cache.db.d.ts +2 -2
  2. package/dist/adapter/cachedb/cache.db.js +4 -3
  3. package/dist/adapter/file/file.db.js +2 -1
  4. package/dist/adapter/inmemory/inMemory.db.d.ts +7 -6
  5. package/dist/adapter/inmemory/inMemory.db.js +15 -13
  6. package/dist/adapter/inmemory/inMemoryKeyValueDB.d.ts +8 -8
  7. package/dist/adapter/inmemory/inMemoryKeyValueDB.js +8 -7
  8. package/dist/base.common.db.d.ts +4 -3
  9. package/dist/base.common.db.js +5 -2
  10. package/dist/common.db.d.ts +23 -14
  11. package/dist/common.db.js +2 -2
  12. package/dist/commondao/common.dao.d.ts +16 -5
  13. package/dist/commondao/common.dao.js +37 -8
  14. package/dist/db.model.d.ts +0 -13
  15. package/dist/db.model.js +1 -17
  16. package/dist/kv/commonKeyValueDB.d.ts +18 -10
  17. package/dist/kv/commonKeyValueDao.d.ts +17 -25
  18. package/dist/kv/commonKeyValueDao.js +32 -54
  19. package/dist/kv/commonKeyValueDaoMemoCache.d.ts +2 -2
  20. package/dist/query/dbQuery.d.ts +2 -2
  21. package/dist/query/dbQuery.js +2 -2
  22. package/dist/testing/daoTest.js +26 -0
  23. package/dist/testing/dbTest.js +21 -23
  24. package/dist/testing/keyValueDBTest.js +22 -7
  25. package/dist/testing/keyValueDaoTest.d.ts +2 -2
  26. package/dist/testing/keyValueDaoTest.js +31 -8
  27. package/package.json +1 -1
  28. package/src/adapter/cachedb/cache.db.ts +6 -5
  29. package/src/adapter/file/file.db.ts +2 -1
  30. package/src/adapter/inmemory/inMemory.db.ts +29 -18
  31. package/src/adapter/inmemory/inMemoryKeyValueDB.ts +16 -16
  32. package/src/base.common.db.ts +18 -5
  33. package/src/common.db.ts +36 -17
  34. package/src/commondao/common.dao.ts +46 -11
  35. package/src/db.model.ts +0 -19
  36. package/src/kv/commonKeyValueDB.ts +19 -11
  37. package/src/kv/commonKeyValueDao.ts +53 -86
  38. package/src/kv/commonKeyValueDaoMemoCache.ts +2 -2
  39. package/src/query/dbQuery.ts +2 -3
  40. package/src/testing/daoTest.ts +28 -0
  41. package/src/testing/dbTest.ts +22 -28
  42. package/src/testing/keyValueDBTest.ts +31 -14
  43. package/src/testing/keyValueDaoTest.ts +37 -12
@@ -11,7 +11,6 @@ import {
11
11
  CommonDaoStreamDeleteOptions,
12
12
  CommonDaoStreamForEachOptions,
13
13
  CommonDaoStreamOptions,
14
- DBPatch,
15
14
  } from '..'
16
15
  import { CommonDao } from '../commondao/common.dao'
17
16
  import { RunQueryResult } from '../db.model'
@@ -276,8 +275,8 @@ export class RunnableDBQuery<
276
275
  return await this.dao.runQueryCount(this, opt)
277
276
  }
278
277
 
279
- async updateByQuery(patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number> {
280
- return await this.dao.updateByQuery(this, patch, opt)
278
+ async patchByQuery(patch: Partial<DBM>, opt?: CommonDaoOptions): Promise<number> {
279
+ return await this.dao.patchByQuery(this, patch, opt)
281
280
  }
282
281
 
283
282
  async streamQueryForEach(
@@ -129,6 +129,34 @@ export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQui
129
129
  expectMatch(expectedItems, itemsSaved, quirks)
130
130
  })
131
131
 
132
+ if (support.increment) {
133
+ test('increment', async () => {
134
+ await dao.incrementBatch('k3', { id1: 1, id2: 2 })
135
+ let rows = await dao.query().runQuery()
136
+ rows = _sortBy(rows, r => r.id)
137
+ const expected = expectedItems.map(r => {
138
+ if (r.id === 'id1') {
139
+ return {
140
+ ...r,
141
+ k3: r.k3! + 1,
142
+ }
143
+ }
144
+ if (r.id === 'id2') {
145
+ return {
146
+ ...r,
147
+ k3: r.k3! + 2,
148
+ }
149
+ }
150
+ return r
151
+ })
152
+ expectMatch(expected, rows, quirks)
153
+
154
+ // reset the changes
155
+ await dao.increment('k3', 'id1', -1)
156
+ await dao.increment('k3', 'id2', -2)
157
+ })
158
+ }
159
+
132
160
  // GET not empty
133
161
  test('getByIds all items', async () => {
134
162
  const rows = await dao.getByIds(items.map(i => i.id).concat('abcd'))
@@ -1,6 +1,5 @@
1
1
  import { _deepFreeze, _filterObject, _pick, _sortBy, pMap } from '@naturalcycles/js-lib'
2
2
  import { CommonDB, CommonDBType } from '../common.db'
3
- import { DBIncrement, DBPatch } from '../db.model'
4
3
  import { DBQuery } from '../query/dbQuery'
5
4
  import {
6
5
  createTestItemDBM,
@@ -317,18 +316,18 @@ export function runCommonDBTest(db: CommonDB, quirks: CommonDBImplementationQuir
317
316
  })
318
317
  }
319
318
 
320
- if (support.updateByQuery) {
321
- test('updateByQuery simple', async () => {
319
+ if (support.patchByQuery) {
320
+ test('patchByQuery', async () => {
322
321
  // cleanup, reset initial data
323
322
  await db.deleteByQuery(queryAll())
324
323
  await db.saveBatch(TEST_TABLE, items)
325
324
 
326
- const patch: DBPatch<TestItemDBM> = {
325
+ const patch: Partial<TestItemDBM> = {
327
326
  k3: 5,
328
327
  k2: 'abc',
329
328
  }
330
329
 
331
- await db.updateByQuery(DBQuery.create<TestItemDBM>(TEST_TABLE).filterEq('even', true), patch)
330
+ await db.patchByQuery(DBQuery.create<TestItemDBM>(TEST_TABLE).filterEq('even', true), patch)
332
331
 
333
332
  const { rows } = await db.runQuery(queryAll())
334
333
  const expected = items.map(r => {
@@ -339,33 +338,28 @@ export function runCommonDBTest(db: CommonDB, quirks: CommonDBImplementationQuir
339
338
  })
340
339
  expectMatch(expected, rows, quirks)
341
340
  })
341
+ }
342
342
 
343
- if (support.dbIncrement) {
344
- test('updateByQuery DBIncrement', async () => {
345
- // cleanup, reset initial data
346
- await db.deleteByQuery(queryAll())
347
- await db.saveBatch(TEST_TABLE, items)
348
-
349
- const patch: DBPatch<TestItemDBM> = {
350
- k3: DBIncrement.of(1),
351
- k2: 'abcd',
352
- }
343
+ if (support.increment) {
344
+ test('incrementBatch', async () => {
345
+ // cleanup, reset initial data
346
+ await db.deleteByQuery(queryAll())
347
+ await db.saveBatch(TEST_TABLE, items)
353
348
 
354
- await db.updateByQuery(
355
- DBQuery.create<TestItemDBM>(TEST_TABLE).filterEq('even', true),
356
- patch,
357
- )
349
+ await db.incrementBatch(TEST_TABLE, 'k3', { id1: 1, id2: 2 })
358
350
 
359
- const { rows } = await db.runQuery(queryAll())
360
- const expected = items.map(r => {
361
- if (r.even) {
362
- return { ...r, ...patch, k3: (r.k3 || 0) + 1 }
363
- }
364
- return r
365
- })
366
- expectMatch(expected, rows, quirks)
351
+ const { rows } = await db.runQuery(queryAll())
352
+ const expected = items.map(r => {
353
+ if (r.id === 'id1') {
354
+ return { ...r, k3: 2 }
355
+ }
356
+ if (r.id === 'id2') {
357
+ return { ...r, k3: 4 }
358
+ }
359
+ return r
367
360
  })
368
- }
361
+ expectMatch(expected, rows, quirks)
362
+ })
369
363
  }
370
364
 
371
365
  if (support.queries) {
@@ -1,10 +1,13 @@
1
- import { _range, _sortBy } from '@naturalcycles/js-lib'
2
- import { CommonKeyValueDB, KeyValueDBTuple } from '../kv/commonKeyValueDB'
1
+ import { _range, _sortBy, KeyValueTuple } from '@naturalcycles/js-lib'
2
+ import { CommonKeyValueDB } from '../kv/commonKeyValueDB'
3
3
  import { TEST_TABLE } from './test.model'
4
4
 
5
5
  const testIds = _range(1, 4).map(n => `id${n}`)
6
6
 
7
- const testEntries: KeyValueDBTuple[] = testIds.map(id => [id, Buffer.from(`${id}value`)])
7
+ const testEntries: KeyValueTuple<string, Buffer>[] = testIds.map(id => [
8
+ id,
9
+ Buffer.from(`${id}value`),
10
+ ])
8
11
 
9
12
  export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
10
13
  beforeAll(async () => {
@@ -47,7 +50,7 @@ export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
47
50
  test('saveBatch, then getByIds', async () => {
48
51
  await db.saveBatch(TEST_TABLE, testEntries)
49
52
 
50
- const entries = await db.getByIds(TEST_TABLE, testIds)
53
+ const entries = await db.getByIds<Buffer>(TEST_TABLE, testIds)
51
54
  _sortBy(entries, e => e[0], true)
52
55
  expect(entries).toEqual(testEntries)
53
56
  })
@@ -73,26 +76,26 @@ export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
73
76
  })
74
77
 
75
78
  test('streamValues', async () => {
76
- const values = await db.streamValues(TEST_TABLE).toArray()
79
+ const values = await db.streamValues<Buffer>(TEST_TABLE).toArray()
77
80
  values.sort()
78
81
  expect(values).toEqual(testEntries.map(e => e[1]))
79
82
  })
80
83
 
81
84
  test('streamValues limited', async () => {
82
- const valuesLimited = await db.streamValues(TEST_TABLE, 2).toArray()
85
+ const valuesLimited = await db.streamValues<Buffer>(TEST_TABLE, 2).toArray()
83
86
  // valuesLimited.sort()
84
87
  // expect(valuesLimited).toEqual(testEntries.map(e => e[1]).slice(0, 2))
85
88
  expect(valuesLimited.length).toBe(2)
86
89
  })
87
90
 
88
91
  test('streamEntries', async () => {
89
- const entries = await db.streamEntries(TEST_TABLE).toArray()
92
+ const entries = await db.streamEntries<Buffer>(TEST_TABLE).toArray()
90
93
  entries.sort()
91
94
  expect(entries).toEqual(testEntries)
92
95
  })
93
96
 
94
97
  test('streamEntries limited', async () => {
95
- const entriesLimited = await db.streamEntries(TEST_TABLE, 2).toArray()
98
+ const entriesLimited = await db.streamEntries<Buffer>(TEST_TABLE, 2).toArray()
96
99
  // entriesLimited.sort()
97
100
  // expect(entriesLimited).toEqual(testEntries.slice(0, 2))
98
101
  expect(entriesLimited.length).toBe(2)
@@ -105,19 +108,33 @@ export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
105
108
  })
106
109
 
107
110
  if (support.increment) {
111
+ const id = 'nonExistingField'
112
+ const id2 = 'nonExistingField2'
113
+
108
114
  test('increment on a non-existing field should set the value to 1', async () => {
109
- const result = await db.increment(TEST_TABLE, 'nonExistingField')
110
- expect(result).toBe(1)
115
+ const result = await db.incrementBatch(TEST_TABLE, [[id, 1]])
116
+ expect(result).toEqual([[id, 1]])
111
117
  })
112
118
 
113
119
  test('increment on a existing field should increase the value by one', async () => {
114
- const result = await db.increment(TEST_TABLE, 'nonExistingField')
115
- expect(result).toBe(2)
120
+ const result = await db.incrementBatch(TEST_TABLE, [[id, 1]])
121
+ expect(result).toEqual([[id, 2]])
116
122
  })
117
123
 
118
124
  test('increment should increase the value by the specified amount', async () => {
119
- const result = await db.increment(TEST_TABLE, 'nonExistingField', 2)
120
- expect(result).toBe(4)
125
+ const result = await db.incrementBatch(TEST_TABLE, [[id, 2]])
126
+ expect(result).toEqual([[id, 4]])
127
+ })
128
+
129
+ test('increment 2 ids at the same time', async () => {
130
+ const result = await db.incrementBatch(TEST_TABLE, [
131
+ [id, 1],
132
+ [id2, 2],
133
+ ])
134
+ expect(Object.fromEntries(result)).toEqual({
135
+ [id]: 5,
136
+ [id2]: 2,
137
+ })
121
138
  })
122
139
  }
123
140
  }
@@ -1,11 +1,20 @@
1
- import { _range, _sortBy } from '@naturalcycles/js-lib'
1
+ import { _sortBy, KeyValueTuple } from '@naturalcycles/js-lib'
2
2
  import { CommonKeyValueDao } from '../kv/commonKeyValueDao'
3
- import { KeyValueDBTuple } from '../kv/commonKeyValueDB'
4
-
5
- const testIds = _range(1, 4).map(n => `id${n}`)
6
- const testEntries: KeyValueDBTuple[] = testIds.map(id => [id, Buffer.from(`${id}value`)])
3
+ import { CommonKeyValueDB } from '../kv/commonKeyValueDB'
4
+ import { createTestItemsBM, TEST_TABLE, TestItemBM } from './test.model'
5
+
6
+ const testItems = createTestItemsBM(4)
7
+ const testIds = testItems.map(e => e.id)
8
+ const testEntries: KeyValueTuple<string, TestItemBM>[] = testItems.map(e => [e.id, e])
9
+
10
+ export function runCommonKeyValueDaoTest(db: CommonKeyValueDB): void {
11
+ const dao = new CommonKeyValueDao<string, TestItemBM>({
12
+ db,
13
+ table: TEST_TABLE,
14
+ // todo: make this test support "deflatedJson" transformer
15
+ })
16
+ const { support } = db
7
17
 
8
- export function runCommonKeyValueDaoTest(dao: CommonKeyValueDao<Buffer>): void {
9
18
  beforeAll(async () => {
10
19
  // Tests in this suite are not isolated,
11
20
  // and failing tests can leave the DB in an unexpected state for other tests,
@@ -20,8 +29,6 @@ export function runCommonKeyValueDaoTest(dao: CommonKeyValueDao<Buffer>): void {
20
29
  await dao.deleteByIds(ids)
21
30
  })
22
31
 
23
- const { support } = dao.cfg.db
24
-
25
32
  test('ping', async () => {
26
33
  await dao.ping()
27
34
  })
@@ -43,8 +50,12 @@ export function runCommonKeyValueDaoTest(dao: CommonKeyValueDao<Buffer>): void {
43
50
  await dao.saveBatch(testEntries)
44
51
 
45
52
  const entries = await dao.getByIds(testIds)
53
+ // console.log(typeof entries[0]![1], entries[0]![1])
54
+
46
55
  _sortBy(entries, e => e[0], true)
47
- expect(entries).toEqual(testEntries)
56
+ expect(entries).toEqual(testEntries) // Jest doesn't allow to compare Buffers directly
57
+ // expect(entries.map(e => e[0])).toEqual(testEntries.map(e => e[0]))
58
+ // expect(entries.map(e => e[1].toString())).toEqual(testEntries.map(e => e[1].toString()))
48
59
  })
49
60
 
50
61
  test('streamIds', async () => {
@@ -94,19 +105,33 @@ export function runCommonKeyValueDaoTest(dao: CommonKeyValueDao<Buffer>): void {
94
105
  })
95
106
 
96
107
  if (support.increment) {
108
+ const id = 'nonExistingField'
109
+ const id2 = 'nonExistingField2'
110
+
97
111
  test('increment on a non-existing field should set the value to 1', async () => {
98
- const result = await dao.increment('nonExistingField')
112
+ const result = await dao.increment(id)
99
113
  expect(result).toBe(1)
100
114
  })
101
115
 
102
116
  test('increment on a existing field should increase the value by one', async () => {
103
- const result = await dao.increment('nonExistingField')
117
+ const result = await dao.increment(id)
104
118
  expect(result).toBe(2)
105
119
  })
106
120
 
107
121
  test('increment should increase the value by the specified amount', async () => {
108
- const result = await dao.increment('nonExistingField', 2)
122
+ const result = await dao.increment(id, 2)
109
123
  expect(result).toBe(4)
110
124
  })
125
+
126
+ test('increment 2 ids at the same time', async () => {
127
+ const result = await dao.incrementBatch([
128
+ [id, 1],
129
+ [id2, 2],
130
+ ])
131
+ expect(Object.fromEntries(result)).toEqual({
132
+ [id]: 5,
133
+ [id2]: 2,
134
+ })
135
+ })
111
136
  }
112
137
  }