@naturalcycles/db-lib 10.59.0 → 10.61.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.
- package/dist/adapter/cachedb/cache.db.d.ts +2 -2
- package/dist/adapter/cachedb/cache.db.js +2 -0
- package/dist/adapter/file/file.db.d.ts +6 -6
- package/dist/adapter/file/file.db.js +3 -2
- package/dist/adapter/file/file.db.model.d.ts +1 -1
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +1 -1
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +2 -2
- package/dist/adapter/file/noop.persistence.plugin.d.ts +1 -1
- package/dist/commondao/common.dao.d.ts +24 -18
- package/dist/commondao/common.dao.js +43 -28
- package/dist/commondao/commonDaoTransaction.d.ts +3 -3
- package/dist/commondb/base.common.db.d.ts +3 -3
- package/dist/commondb/common.db.d.ts +3 -3
- package/dist/db.model.d.ts +2 -2
- package/dist/inmemory/inMemory.db.d.ts +6 -6
- package/dist/inmemory/inMemoryKeyValueDB.d.ts +4 -4
- package/dist/inmemory/queryInMemory.js +1 -1
- package/dist/kv/commonKeyValueDB.d.ts +4 -4
- package/dist/kv/commonKeyValueDao.d.ts +5 -5
- package/dist/pipeline/dbPipelineBackup.js +1 -1
- package/dist/query/dbQuery.d.ts +4 -4
- package/dist/query/dbQuery.js +2 -2
- package/dist/testing/commonDBTest.js +68 -69
- package/dist/testing/commonDaoTest.js +59 -70
- package/dist/testing/keyValueDBTest.js +28 -21
- package/dist/testing/keyValueDaoTest.js +17 -17
- package/dist/timeseries/commonTimeSeriesDao.d.ts +3 -3
- package/dist/transaction/dbTransaction.util.d.ts +3 -3
- package/package.json +2 -2
- package/src/adapter/cachedb/cache.db.ts +4 -2
- package/src/adapter/file/file.db.model.ts +1 -1
- package/src/adapter/file/file.db.ts +10 -7
- package/src/adapter/file/inMemory.persistence.plugin.ts +1 -1
- package/src/adapter/file/localFile.persistence.plugin.ts +2 -2
- package/src/adapter/file/noop.persistence.plugin.ts +1 -1
- package/src/commondao/common.dao.ts +73 -48
- package/src/commondao/commonDaoTransaction.ts +3 -3
- package/src/commondb/base.common.db.ts +10 -3
- package/src/commondb/common.db.ts +7 -3
- package/src/db.model.ts +2 -2
- package/src/inmemory/inMemory.db.ts +10 -6
- package/src/inmemory/inMemoryKeyValueDB.ts +7 -4
- package/src/inmemory/queryInMemory.ts +5 -5
- package/src/kv/commonKeyValueDB.ts +4 -4
- package/src/kv/commonKeyValueDao.ts +5 -5
- package/src/pipeline/dbPipelineBackup.ts +1 -1
- package/src/query/dbQuery.ts +6 -6
- package/src/testing/commonDBTest.ts +92 -75
- package/src/testing/commonDaoTest.ts +78 -76
- package/src/testing/keyValueDBTest.ts +29 -21
- package/src/testing/keyValueDaoTest.ts +17 -17
- package/src/timeseries/commonTimeSeriesDao.ts +3 -3
- package/src/transaction/dbTransaction.util.ts +3 -3
|
@@ -14,7 +14,7 @@ export async function runCommonDaoTest(
|
|
|
14
14
|
quirks: CommonDBImplementationQuirks = {},
|
|
15
15
|
): Promise<void> {
|
|
16
16
|
// this is because vitest cannot be "required" from cjs
|
|
17
|
-
const {
|
|
17
|
+
const { afterAll, beforeAll, expect, it } = await import('vitest')
|
|
18
18
|
|
|
19
19
|
const { support } = db
|
|
20
20
|
const dao = new CommonDao({
|
|
@@ -33,20 +33,14 @@ export async function runCommonDaoTest(
|
|
|
33
33
|
updated: expect.any(Number),
|
|
34
34
|
}))
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// CREATE TABLE, DROP
|
|
41
|
-
if (support.createTable) {
|
|
42
|
-
test('createTable, dropIfExists=true', async () => {
|
|
36
|
+
beforeAll(async () => {
|
|
37
|
+
// Tests in this suite are not isolated: a failed run can leave rows behind for the next run
|
|
38
|
+
if (support.createTable) {
|
|
43
39
|
await dao.createTable(testItemBMSchema.build(), { dropIfExists: true })
|
|
44
|
-
}
|
|
45
|
-
}
|
|
40
|
+
}
|
|
46
41
|
|
|
47
|
-
if (support.queries) {
|
|
48
42
|
// DELETE ALL initially
|
|
49
|
-
|
|
43
|
+
if (support.queries) {
|
|
50
44
|
const rows = await dao.query().select(['id']).runQuery()
|
|
51
45
|
await db.deleteByQuery(
|
|
52
46
|
DBQuery.create(TEST_TABLE).filter(
|
|
@@ -55,44 +49,46 @@ export async function runCommonDaoTest(
|
|
|
55
49
|
rows.map(r => r.id),
|
|
56
50
|
),
|
|
57
51
|
)
|
|
58
|
-
}
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
afterAll(async () => {
|
|
56
|
+
// CLEAN UP
|
|
57
|
+
if (support.queries) {
|
|
58
|
+
await dao.query().deleteByQuery()
|
|
59
|
+
}
|
|
60
|
+
})
|
|
59
61
|
|
|
62
|
+
it('should respond to ping', async () => {
|
|
63
|
+
await dao.ping()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
if (support.queries) {
|
|
60
67
|
// QUERY empty
|
|
61
|
-
|
|
68
|
+
it('should return no rows and a count of 0 for an empty table', async () => {
|
|
62
69
|
expect(await dao.query().runQuery()).toEqual([])
|
|
63
70
|
expect(await dao.query().runQueryCount()).toBe(0)
|
|
64
71
|
})
|
|
65
72
|
}
|
|
66
73
|
|
|
67
74
|
// GET empty
|
|
68
|
-
|
|
75
|
+
it('should return no row for an id that is not saved yet', async () => {
|
|
69
76
|
const [item1Loaded] = await dao.getByIds([item1.id])
|
|
70
77
|
expect(item1Loaded).toBeUndefined()
|
|
71
78
|
expect(await dao.getById(item1.id)).toBeNull()
|
|
72
79
|
})
|
|
73
80
|
|
|
74
|
-
|
|
81
|
+
it('should return an empty array for getByIds without ids', async () => {
|
|
75
82
|
expect(await dao.getByIds([])).toEqual([])
|
|
76
83
|
})
|
|
77
84
|
|
|
78
|
-
|
|
85
|
+
it('should return an empty array for unknown ids', async () => {
|
|
79
86
|
expect(await dao.getByIds(['abc', 'abcd'])).toEqual([])
|
|
80
87
|
})
|
|
81
88
|
|
|
82
|
-
// TimeMachine
|
|
83
|
-
if (support.timeMachine) {
|
|
84
|
-
test('getByIds(...) 10 minutes ago should return []', async () => {
|
|
85
|
-
expect(
|
|
86
|
-
await dao.getByIds([item1.id, 'abc'], {
|
|
87
|
-
readAt: localTime.now().minus(10, 'minute').unix,
|
|
88
|
-
}),
|
|
89
|
-
).toEqual([])
|
|
90
|
-
})
|
|
91
|
-
}
|
|
92
|
-
|
|
93
89
|
// SAVE
|
|
94
90
|
if (support.nullValues) {
|
|
95
|
-
|
|
91
|
+
it('should allow to save and load null values', async () => {
|
|
96
92
|
const item3 = {
|
|
97
93
|
...createTestItemBM(3),
|
|
98
94
|
k2: null,
|
|
@@ -107,7 +103,7 @@ export async function runCommonDaoTest(
|
|
|
107
103
|
})
|
|
108
104
|
}
|
|
109
105
|
|
|
110
|
-
|
|
106
|
+
it('should not store undefined properties but keep them on the input object', async () => {
|
|
111
107
|
const item3 = {
|
|
112
108
|
...createTestItemBM(3),
|
|
113
109
|
k2: undefined,
|
|
@@ -128,7 +124,7 @@ export async function runCommonDaoTest(
|
|
|
128
124
|
expect(item3.k2).toBeUndefined()
|
|
129
125
|
})
|
|
130
126
|
|
|
131
|
-
|
|
127
|
+
it('should save the test items and return the same objects', async () => {
|
|
132
128
|
const itemsSaved = await dao.saveBatch(items)
|
|
133
129
|
expect(itemsSaved[0]).toBe(items[0]) // expect "same object" returned
|
|
134
130
|
|
|
@@ -140,7 +136,7 @@ export async function runCommonDaoTest(
|
|
|
140
136
|
})
|
|
141
137
|
|
|
142
138
|
if (support.increment) {
|
|
143
|
-
|
|
139
|
+
it('should increment a property by a different amount per id', async () => {
|
|
144
140
|
await dao.incrementBatch('k3', { id1: 1, id2: 2 })
|
|
145
141
|
let rows = await dao.query().runQuery()
|
|
146
142
|
rows = _sortBy(rows, r => r.id)
|
|
@@ -168,7 +164,7 @@ export async function runCommonDaoTest(
|
|
|
168
164
|
}
|
|
169
165
|
|
|
170
166
|
// GET not empty
|
|
171
|
-
|
|
167
|
+
it('should return all saved items by ids, skipping unknown ids', async () => {
|
|
172
168
|
const rows = await dao.getByIds(items.map(i => i.id).concat('abcd'))
|
|
173
169
|
expectMatch(
|
|
174
170
|
expectedItems,
|
|
@@ -177,16 +173,25 @@ export async function runCommonDaoTest(
|
|
|
177
173
|
)
|
|
178
174
|
})
|
|
179
175
|
|
|
176
|
+
// TimeMachine
|
|
177
|
+
if (support.timeMachine) {
|
|
178
|
+
it('should not return the saved items when reading 10 minutes back in time', async () => {
|
|
179
|
+
const ids = items.map(i => i.id)
|
|
180
|
+
const readAt = localTime.now().minus(10, 'minute').unix
|
|
181
|
+
expect(await dao.getByIds(ids, { readAt })).toEqual([])
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
180
185
|
// QUERY
|
|
181
186
|
if (support.queries) {
|
|
182
|
-
|
|
187
|
+
it('should return all items from a query without conditions', async () => {
|
|
183
188
|
let rows = await dao.query().runQuery()
|
|
184
189
|
rows = _sortBy(rows, r => r.id)
|
|
185
190
|
expectMatch(expectedItems, rows, quirks)
|
|
186
191
|
})
|
|
187
192
|
|
|
188
193
|
if (support.dbQueryFilter) {
|
|
189
|
-
|
|
194
|
+
it('should filter rows by equality', async () => {
|
|
190
195
|
let rows = await dao.query().filter('even', '==', true).runQuery()
|
|
191
196
|
rows = _sortBy(rows, r => r.id)
|
|
192
197
|
expectMatch(
|
|
@@ -196,7 +201,7 @@ export async function runCommonDaoTest(
|
|
|
196
201
|
)
|
|
197
202
|
})
|
|
198
203
|
|
|
199
|
-
|
|
204
|
+
it('should filter rows by a nested property', async () => {
|
|
200
205
|
let rows = await dao
|
|
201
206
|
.query()
|
|
202
207
|
.filter('nested.foo' as any, '==', 1)
|
|
@@ -211,14 +216,14 @@ export async function runCommonDaoTest(
|
|
|
211
216
|
}
|
|
212
217
|
|
|
213
218
|
if (support.dbQueryOrder) {
|
|
214
|
-
|
|
219
|
+
it('should order rows descending', async () => {
|
|
215
220
|
const rows = await dao.query().order('k1', true).runQuery()
|
|
216
221
|
expectMatch(expectedItems.toReversed(), rows, quirks)
|
|
217
222
|
})
|
|
218
223
|
}
|
|
219
224
|
|
|
220
225
|
if (support.dbQuerySelectFields) {
|
|
221
|
-
|
|
226
|
+
it('should select only the id', async () => {
|
|
222
227
|
let rows = await dao.query().select(['id']).runQuery()
|
|
223
228
|
rows = _sortBy(rows, r => r.id)
|
|
224
229
|
expectMatch(
|
|
@@ -229,14 +234,14 @@ export async function runCommonDaoTest(
|
|
|
229
234
|
})
|
|
230
235
|
}
|
|
231
236
|
|
|
232
|
-
|
|
237
|
+
it('should count all saved items', async () => {
|
|
233
238
|
expect(await dao.query().runQueryCount()).toBe(3)
|
|
234
239
|
})
|
|
235
240
|
}
|
|
236
241
|
|
|
237
242
|
// STREAM
|
|
238
243
|
if (support.streaming) {
|
|
239
|
-
|
|
244
|
+
it('should stream all items to forEachSync', async () => {
|
|
240
245
|
let rows: TestItemBM[] = []
|
|
241
246
|
await dao
|
|
242
247
|
.query()
|
|
@@ -247,14 +252,14 @@ export async function runCommonDaoTest(
|
|
|
247
252
|
expectMatch(expectedItems, rows, quirks)
|
|
248
253
|
})
|
|
249
254
|
|
|
250
|
-
|
|
255
|
+
it('should stream all items to an array', async () => {
|
|
251
256
|
let rows: TestItemBM[] = await dao.query().streamQuery().toArray()
|
|
252
257
|
|
|
253
258
|
rows = _sortBy(rows, r => r.id)
|
|
254
259
|
expectMatch(expectedItems, rows, quirks)
|
|
255
260
|
})
|
|
256
261
|
|
|
257
|
-
|
|
262
|
+
it('should stream all ids to forEachSync', async () => {
|
|
258
263
|
let ids: string[] = []
|
|
259
264
|
await dao
|
|
260
265
|
.query()
|
|
@@ -268,7 +273,7 @@ export async function runCommonDaoTest(
|
|
|
268
273
|
)
|
|
269
274
|
})
|
|
270
275
|
|
|
271
|
-
|
|
276
|
+
it('should stream all ids to an array', async () => {
|
|
272
277
|
let ids = await dao.query().streamQueryIds().toArray()
|
|
273
278
|
ids = ids.sort()
|
|
274
279
|
expectMatch(
|
|
@@ -278,7 +283,7 @@ export async function runCommonDaoTest(
|
|
|
278
283
|
)
|
|
279
284
|
})
|
|
280
285
|
|
|
281
|
-
|
|
286
|
+
it('should save the items of a stream', async () => {
|
|
282
287
|
const items2 = createTestItemsBM(2).map(i => ({ ...i, id: i.id + '_str' }))
|
|
283
288
|
const ids = items2.map(i => i.id)
|
|
284
289
|
|
|
@@ -294,16 +299,11 @@ export async function runCommonDaoTest(
|
|
|
294
299
|
|
|
295
300
|
// DELETE BY
|
|
296
301
|
if (support.queries) {
|
|
297
|
-
|
|
302
|
+
it('should delete rows by a filter and return the deleted count', async () => {
|
|
298
303
|
const deleted = await dao.query().filter('even', '==', false).deleteByQuery()
|
|
299
304
|
expect(deleted).toBe(items.filter(item => !item.even).length)
|
|
300
305
|
expect(await dao.query().runQueryCount()).toBe(1)
|
|
301
306
|
})
|
|
302
|
-
|
|
303
|
-
test('cleanup', async () => {
|
|
304
|
-
// CLEAN UP
|
|
305
|
-
await dao.query().deleteByQuery()
|
|
306
|
-
})
|
|
307
307
|
}
|
|
308
308
|
|
|
309
309
|
if (support.transactions) {
|
|
@@ -319,7 +319,7 @@ export async function runCommonDaoTest(
|
|
|
319
319
|
return itemsToSave
|
|
320
320
|
}
|
|
321
321
|
|
|
322
|
-
|
|
322
|
+
it('should commit the saves and deletes made in runInTransaction', async () => {
|
|
323
323
|
// cleanup
|
|
324
324
|
await dao.query().deleteByQuery()
|
|
325
325
|
|
|
@@ -353,11 +353,15 @@ export async function runCommonDaoTest(
|
|
|
353
353
|
|
|
354
354
|
const rows = await dao.query().runQuery()
|
|
355
355
|
const expected = [items[0], { ...items[2]!, k1: 'k1_mod' }]
|
|
356
|
-
expectMatch(
|
|
356
|
+
expectMatch(
|
|
357
|
+
expected,
|
|
358
|
+
_sortBy(rows, r => r.id),
|
|
359
|
+
quirks,
|
|
360
|
+
)
|
|
357
361
|
})
|
|
358
362
|
|
|
359
363
|
if (support.createTransaction) {
|
|
360
|
-
|
|
364
|
+
it('should commit the saves and deletes made in a created transaction', async () => {
|
|
361
365
|
// cleanup
|
|
362
366
|
await dao.query().deleteByQuery()
|
|
363
367
|
|
|
@@ -391,11 +395,15 @@ export async function runCommonDaoTest(
|
|
|
391
395
|
|
|
392
396
|
const rows = await dao.query().runQuery()
|
|
393
397
|
const expected = [items[0], { ...items[2]!, k1: 'k1_mod' }]
|
|
394
|
-
expectMatch(
|
|
398
|
+
expectMatch(
|
|
399
|
+
expected,
|
|
400
|
+
_sortBy(rows, r => r.id),
|
|
401
|
+
quirks,
|
|
402
|
+
)
|
|
395
403
|
})
|
|
396
404
|
}
|
|
397
405
|
|
|
398
|
-
|
|
406
|
+
it('should roll back runInTransaction when a save fails validation', async () => {
|
|
399
407
|
const expected = await prepare()
|
|
400
408
|
|
|
401
409
|
let err: any
|
|
@@ -413,34 +421,28 @@ export async function runCommonDaoTest(
|
|
|
413
421
|
expect(err).toBeInstanceOf(Error)
|
|
414
422
|
|
|
415
423
|
const rows = await dao.query().runQuery()
|
|
416
|
-
expectMatch(
|
|
424
|
+
expectMatch(
|
|
425
|
+
expected,
|
|
426
|
+
_sortBy(rows, r => r.id),
|
|
427
|
+
quirks,
|
|
428
|
+
)
|
|
417
429
|
})
|
|
418
430
|
|
|
419
431
|
if (support.createTransaction) {
|
|
420
|
-
|
|
432
|
+
it('should leave the data unchanged when a created transaction is rolled back after a failed save', async () => {
|
|
421
433
|
const expected = await prepare()
|
|
422
434
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
await tx.save(dao, { ...items[0]!, k1: 5 as any }) // it should fail here
|
|
428
|
-
await tx.commit()
|
|
429
|
-
} catch (err2) {
|
|
430
|
-
err = err2
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
expect(err).toBeDefined()
|
|
434
|
-
expect(err).toBeInstanceOf(Error)
|
|
435
|
+
const tx = await dao.createTransaction()
|
|
436
|
+
await tx.deleteById(dao, items[2]!.id)
|
|
437
|
+
await expect(tx.save(dao, { ...items[0]!, k1: 5 as any })).rejects.toThrow() // it should fail here
|
|
438
|
+
await tx.rollback()
|
|
435
439
|
|
|
436
440
|
const rows = await dao.query().runQuery()
|
|
437
|
-
expectMatch(
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
test('transaction cleanup', async () => {
|
|
443
|
-
await dao.query().deleteByQuery()
|
|
441
|
+
expectMatch(
|
|
442
|
+
expected,
|
|
443
|
+
_sortBy(rows, r => r.id),
|
|
444
|
+
quirks,
|
|
445
|
+
)
|
|
444
446
|
})
|
|
445
447
|
}
|
|
446
448
|
}
|
|
@@ -11,7 +11,7 @@ const testEntries: KeyValueTuple<string, Buffer>[] = testIds.map(id => [
|
|
|
11
11
|
])
|
|
12
12
|
|
|
13
13
|
export async function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<void> {
|
|
14
|
-
const { afterAll, beforeAll, expect,
|
|
14
|
+
const { afterAll, beforeAll, expect, it } = await import('vitest')
|
|
15
15
|
|
|
16
16
|
beforeAll(async () => {
|
|
17
17
|
// Tests in this suite are not isolated,
|
|
@@ -29,28 +29,30 @@ export async function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<voi
|
|
|
29
29
|
|
|
30
30
|
const { support } = db
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
it('should respond to ping', async () => {
|
|
33
33
|
await db.ping()
|
|
34
34
|
})
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
it('should create the table, dropping an existing one', async () => {
|
|
37
37
|
await db.createTable(TEST_TABLE, { dropIfExists: true })
|
|
38
38
|
})
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
it('should not throw when deleting ids that do not exist', async () => {
|
|
41
41
|
await db.deleteByIds(TEST_TABLE, testIds)
|
|
42
42
|
})
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
it('should return no entries for ids that do not exist', async () => {
|
|
45
45
|
const results = await db.getByIds(TEST_TABLE, testIds)
|
|
46
46
|
expect(results).toEqual([])
|
|
47
47
|
})
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
if (support.count) {
|
|
50
|
+
it('should count 0 entries in an empty table', async () => {
|
|
51
|
+
expect(await db.count(TEST_TABLE)).toBe(0)
|
|
52
|
+
})
|
|
53
|
+
}
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
it('should return the saved entries by ids', async () => {
|
|
54
56
|
await db.saveBatch(TEST_TABLE, testEntries)
|
|
55
57
|
|
|
56
58
|
const entries = await db.getByIds(TEST_TABLE, testIds)
|
|
@@ -59,18 +61,18 @@ export async function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<voi
|
|
|
59
61
|
})
|
|
60
62
|
|
|
61
63
|
if (support.count) {
|
|
62
|
-
|
|
64
|
+
it('should count the saved entries', async () => {
|
|
63
65
|
expect(await db.count(TEST_TABLE)).toBe(3)
|
|
64
66
|
})
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
|
|
69
|
+
it('should stream all ids', async () => {
|
|
68
70
|
const ids = await db.streamIds(TEST_TABLE).toArray()
|
|
69
71
|
ids.sort()
|
|
70
72
|
expect(ids).toEqual(testIds)
|
|
71
73
|
})
|
|
72
74
|
|
|
73
|
-
|
|
75
|
+
it('should stream as many ids as the limit', async () => {
|
|
74
76
|
const idsLimited = await db.streamIds(TEST_TABLE, 2).toArray()
|
|
75
77
|
// Order is non-deterministic, so, cannot compare values
|
|
76
78
|
// idsLimited.sort()
|
|
@@ -78,58 +80,64 @@ export async function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<voi
|
|
|
78
80
|
expect(idsLimited.length).toBe(2)
|
|
79
81
|
})
|
|
80
82
|
|
|
81
|
-
|
|
83
|
+
it('should stream all values', async () => {
|
|
82
84
|
const values = await db.streamValues(TEST_TABLE).toArray()
|
|
83
85
|
values.sort()
|
|
84
86
|
expect(values).toEqual(testEntries.map(e => e[1]))
|
|
85
87
|
})
|
|
86
88
|
|
|
87
|
-
|
|
89
|
+
it('should stream as many values as the limit', async () => {
|
|
88
90
|
const valuesLimited = await db.streamValues(TEST_TABLE, 2).toArray()
|
|
89
91
|
// valuesLimited.sort()
|
|
90
92
|
// expect(valuesLimited).toEqual(testEntries.map(e => e[1]).slice(0, 2))
|
|
91
93
|
expect(valuesLimited.length).toBe(2)
|
|
92
94
|
})
|
|
93
95
|
|
|
94
|
-
|
|
96
|
+
it('should stream all entries', async () => {
|
|
95
97
|
const entries = await db.streamEntries(TEST_TABLE).toArray()
|
|
96
98
|
entries.sort()
|
|
97
99
|
expect(entries).toEqual(testEntries)
|
|
98
100
|
})
|
|
99
101
|
|
|
100
|
-
|
|
102
|
+
it('should stream as many entries as the limit', async () => {
|
|
101
103
|
const entriesLimited = await db.streamEntries(TEST_TABLE, 2).toArray()
|
|
102
104
|
// entriesLimited.sort()
|
|
103
105
|
// expect(entriesLimited).toEqual(testEntries.slice(0, 2))
|
|
104
106
|
expect(entriesLimited.length).toBe(2)
|
|
105
107
|
})
|
|
106
108
|
|
|
107
|
-
|
|
109
|
+
it('should delete the entries by ids', async () => {
|
|
108
110
|
await db.deleteByIds(TEST_TABLE, testIds)
|
|
109
111
|
const results = await db.getByIds(TEST_TABLE, testIds)
|
|
110
112
|
expect(results).toEqual([])
|
|
111
113
|
})
|
|
112
114
|
|
|
115
|
+
if (support.count) {
|
|
116
|
+
it('should count 0 entries after deleting them', async () => {
|
|
117
|
+
expect(await db.count(TEST_TABLE)).toBe(0)
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
|
|
113
121
|
if (support.increment) {
|
|
114
122
|
const id = 'nonExistingField'
|
|
115
123
|
const id2 = 'nonExistingField2'
|
|
116
124
|
|
|
117
|
-
|
|
125
|
+
it('should increment a missing key from 0', async () => {
|
|
118
126
|
const result = await db.incrementBatch(TEST_TABLE, [[id, 1]])
|
|
119
127
|
expect(result).toEqual([[id, 1]])
|
|
120
128
|
})
|
|
121
129
|
|
|
122
|
-
|
|
130
|
+
it('should increment an existing key by 1', async () => {
|
|
123
131
|
const result = await db.incrementBatch(TEST_TABLE, [[id, 1]])
|
|
124
132
|
expect(result).toEqual([[id, 2]])
|
|
125
133
|
})
|
|
126
134
|
|
|
127
|
-
|
|
135
|
+
it('should increment an existing key by the given amount', async () => {
|
|
128
136
|
const result = await db.incrementBatch(TEST_TABLE, [[id, 2]])
|
|
129
137
|
expect(result).toEqual([[id, 4]])
|
|
130
138
|
})
|
|
131
139
|
|
|
132
|
-
|
|
140
|
+
it('should increment two keys in one batch', async () => {
|
|
133
141
|
const result = await db.incrementBatch(TEST_TABLE, [
|
|
134
142
|
[id, 1],
|
|
135
143
|
[id2, 2],
|
|
@@ -8,7 +8,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
10
|
export async function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<void> {
|
|
11
|
-
const { afterAll, beforeAll, expect,
|
|
11
|
+
const { afterAll, beforeAll, expect, it } = await import('vitest')
|
|
12
12
|
|
|
13
13
|
const dao = new CommonKeyValueDao({
|
|
14
14
|
db,
|
|
@@ -31,24 +31,24 @@ export async function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<vo
|
|
|
31
31
|
await dao.deleteByIds(ids)
|
|
32
32
|
})
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
it('should respond to ping', async () => {
|
|
35
35
|
await dao.ping()
|
|
36
36
|
})
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
it('should create the table, dropping an existing one', async () => {
|
|
39
39
|
await dao.createTable({ dropIfExists: true })
|
|
40
40
|
})
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
it('should not throw when deleting ids that do not exist', async () => {
|
|
43
43
|
await dao.deleteByIds(testIds)
|
|
44
44
|
})
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
it('should return no entries for ids that do not exist', async () => {
|
|
47
47
|
const results = await dao.getByIds(testIds)
|
|
48
48
|
expect(results).toEqual([])
|
|
49
49
|
})
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
it('should return the saved entries by ids', async () => {
|
|
52
52
|
await dao.saveBatch(testEntries)
|
|
53
53
|
|
|
54
54
|
const entries = await dao.getByIds(testIds)
|
|
@@ -60,13 +60,13 @@ export async function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<vo
|
|
|
60
60
|
// expect(entries.map(e => e[1].toString())).toEqual(testEntries.map(e => e[1].toString()))
|
|
61
61
|
})
|
|
62
62
|
|
|
63
|
-
|
|
63
|
+
it('should stream all ids', async () => {
|
|
64
64
|
const ids = await dao.streamIds().toArray()
|
|
65
65
|
ids.sort()
|
|
66
66
|
expect(ids).toEqual(testIds)
|
|
67
67
|
})
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
it('should stream as many ids as the limit', async () => {
|
|
70
70
|
const idsLimited = await dao.streamIds(2).toArray()
|
|
71
71
|
// Order is non-deterministic, so, cannot compare values
|
|
72
72
|
// idsLimited.sort()
|
|
@@ -74,33 +74,33 @@ export async function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<vo
|
|
|
74
74
|
expect(idsLimited.length).toBe(2)
|
|
75
75
|
})
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
it('should stream all values', async () => {
|
|
78
78
|
const values = await dao.streamValues().toArray()
|
|
79
79
|
values.sort()
|
|
80
80
|
expect(values).toEqual(testEntries.map(e => e[1]))
|
|
81
81
|
})
|
|
82
82
|
|
|
83
|
-
|
|
83
|
+
it('should stream as many values as the limit', async () => {
|
|
84
84
|
const valuesLimited = await dao.streamValues(2).toArray()
|
|
85
85
|
// valuesLimited.sort()
|
|
86
86
|
// expect(valuesLimited).toEqual(testEntries.map(e => e[1]).slice(0, 2))
|
|
87
87
|
expect(valuesLimited.length).toBe(2)
|
|
88
88
|
})
|
|
89
89
|
|
|
90
|
-
|
|
90
|
+
it('should stream all entries', async () => {
|
|
91
91
|
const entries = await dao.streamEntries().toArray()
|
|
92
92
|
entries.sort()
|
|
93
93
|
expect(entries).toEqual(testEntries)
|
|
94
94
|
})
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
it('should stream as many entries as the limit', async () => {
|
|
97
97
|
const entriesLimited = await dao.streamEntries(2).toArray()
|
|
98
98
|
// entriesLimited.sort()
|
|
99
99
|
// expect(entriesLimited).toEqual(testEntries.slice(0, 2))
|
|
100
100
|
expect(entriesLimited.length).toBe(2)
|
|
101
101
|
})
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
it('should delete the entries by ids', async () => {
|
|
104
104
|
await dao.deleteByIds(testIds)
|
|
105
105
|
const results = await dao.getByIds(testIds)
|
|
106
106
|
expect(results).toEqual([])
|
|
@@ -110,22 +110,22 @@ export async function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<vo
|
|
|
110
110
|
const id = 'nonExistingField'
|
|
111
111
|
const id2 = 'nonExistingField2'
|
|
112
112
|
|
|
113
|
-
|
|
113
|
+
it('should increment a missing key from 0', async () => {
|
|
114
114
|
const result = await dao.increment(id)
|
|
115
115
|
expect(result).toBe(1)
|
|
116
116
|
})
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
it('should increment an existing key by 1', async () => {
|
|
119
119
|
const result = await dao.increment(id)
|
|
120
120
|
expect(result).toBe(2)
|
|
121
121
|
})
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
it('should increment an existing key by the given amount', async () => {
|
|
124
124
|
const result = await dao.increment(id, 2)
|
|
125
125
|
expect(result).toBe(4)
|
|
126
126
|
})
|
|
127
127
|
|
|
128
|
-
|
|
128
|
+
it('should increment two keys in one batch', async () => {
|
|
129
129
|
const result = await dao.incrementBatch([
|
|
130
130
|
[id, 1],
|
|
131
131
|
[id2, 2],
|
|
@@ -35,7 +35,7 @@ export class CommonTimeSeriesDao {
|
|
|
35
35
|
await this.saveBatch(series, [[tsMillis, value]])
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
async saveBatch(series: string, dataPoints: TimeSeriesDataPoint[]): Promise<void> {
|
|
38
|
+
async saveBatch(series: string, dataPoints: readonly TimeSeriesDataPoint[]): Promise<void> {
|
|
39
39
|
if (!dataPoints.length) return
|
|
40
40
|
|
|
41
41
|
const rows: ObjectWithId[] = dataPoints.map(([ts, v]) => ({
|
|
@@ -50,7 +50,7 @@ export class CommonTimeSeriesDao {
|
|
|
50
50
|
/**
|
|
51
51
|
* All ops are executed as a single CommonDB Transaction.
|
|
52
52
|
*/
|
|
53
|
-
async commitTransaction(ops: TimeSeriesSaveBatchOp[]): Promise<void> {
|
|
53
|
+
async commitTransaction(ops: readonly TimeSeriesSaveBatchOp[]): Promise<void> {
|
|
54
54
|
if (!ops.length) return
|
|
55
55
|
|
|
56
56
|
await this.cfg.db.runInTransaction(async tx => {
|
|
@@ -70,7 +70,7 @@ export class CommonTimeSeriesDao {
|
|
|
70
70
|
await this.deleteByIds(series, [tsMillis])
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
async deleteByIds(series: string, ids: number[]): Promise<void> {
|
|
73
|
+
async deleteByIds(series: string, ids: readonly number[]): Promise<void> {
|
|
74
74
|
// Save to _RAW table with v=null
|
|
75
75
|
await this.saveBatch(
|
|
76
76
|
series,
|
|
@@ -103,7 +103,7 @@ export class FakeDBTransaction implements DBTransaction {
|
|
|
103
103
|
|
|
104
104
|
async getByIds<ROW extends ObjectWithId>(
|
|
105
105
|
table: string,
|
|
106
|
-
ids: string[],
|
|
106
|
+
ids: readonly string[],
|
|
107
107
|
opt?: CommonDBOptions,
|
|
108
108
|
): Promise<ROW[]> {
|
|
109
109
|
return await this.db.getByIds(table, ids, opt)
|
|
@@ -117,13 +117,13 @@ export class FakeDBTransaction implements DBTransaction {
|
|
|
117
117
|
// }
|
|
118
118
|
async saveBatch<ROW extends ObjectWithId>(
|
|
119
119
|
table: string,
|
|
120
|
-
rows: ROW[],
|
|
120
|
+
rows: readonly ROW[],
|
|
121
121
|
opt?: CommonDBSaveOptions<ROW>,
|
|
122
122
|
): Promise<void> {
|
|
123
123
|
await this.db.saveBatch(table, rows, opt)
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
async deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number> {
|
|
126
|
+
async deleteByIds(table: string, ids: readonly string[], opt?: CommonDBOptions): Promise<number> {
|
|
127
127
|
return await this.db.deleteByIds(table, ids, opt)
|
|
128
128
|
}
|
|
129
129
|
}
|