@naturalcycles/db-lib 10.58.0 → 10.60.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.js +2 -0
- package/dist/adapter/file/file.db.js +1 -0
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +1 -0
- package/dist/adapter/file/inMemory.persistence.plugin.js +3 -0
- package/dist/inmemory/inMemory.db.d.ts +1 -0
- package/dist/inmemory/inMemory.db.js +3 -0
- package/dist/inmemory/inMemoryKeyValueDB.d.ts +1 -0
- package/dist/inmemory/inMemoryKeyValueDB.js +3 -0
- package/dist/pipeline/dbPipelineBackup.js +1 -1
- 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/package.json +2 -2
- package/src/adapter/cachedb/cache.db.ts +2 -0
- package/src/adapter/file/file.db.ts +1 -0
- package/src/adapter/file/inMemory.persistence.plugin.ts +4 -0
- package/src/inmemory/inMemory.db.ts +4 -0
- package/src/inmemory/inMemoryKeyValueDB.ts +4 -0
- package/src/pipeline/dbPipelineBackup.ts +1 -1
- 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
|
@@ -33,7 +33,7 @@ export async function runCommonDBTest(
|
|
|
33
33
|
quirks: CommonDBImplementationQuirks = {},
|
|
34
34
|
): Promise<void> {
|
|
35
35
|
// this is because vitest cannot be "required" from cjs
|
|
36
|
-
const {
|
|
36
|
+
const { afterAll, beforeAll, expect, it } = await import('vitest')
|
|
37
37
|
|
|
38
38
|
const { support } = db
|
|
39
39
|
const items = createTestItemsDBM(3)
|
|
@@ -42,20 +42,14 @@ export async function runCommonDBTest(
|
|
|
42
42
|
|
|
43
43
|
const queryAll = (): DBQuery<TestItemDBM> => DBQuery.create<TestItemDBM>(TEST_TABLE)
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// CREATE TABLE, DROP
|
|
50
|
-
if (support.createTable) {
|
|
51
|
-
test('createTable, dropIfExists=true', async () => {
|
|
45
|
+
beforeAll(async () => {
|
|
46
|
+
// Tests in this suite are not isolated: a failed run can leave rows behind for the next run
|
|
47
|
+
if (support.createTable) {
|
|
52
48
|
await db.createTable(TEST_TABLE, testItemBMSchema.build(), { dropIfExists: true })
|
|
53
|
-
}
|
|
54
|
-
}
|
|
49
|
+
}
|
|
55
50
|
|
|
56
|
-
if (support.queries) {
|
|
57
51
|
// DELETE ALL initially
|
|
58
|
-
|
|
52
|
+
if (support.queries) {
|
|
59
53
|
const { rows } = await db.runQuery(queryAll().select(['id']))
|
|
60
54
|
await db.deleteByQuery(
|
|
61
55
|
queryAll().filterIn(
|
|
@@ -63,44 +57,40 @@ export async function runCommonDBTest(
|
|
|
63
57
|
rows.map(i => i.id),
|
|
64
58
|
),
|
|
65
59
|
)
|
|
66
|
-
}
|
|
60
|
+
}
|
|
61
|
+
})
|
|
67
62
|
|
|
63
|
+
afterAll(async () => {
|
|
64
|
+
// CLEAN UP
|
|
65
|
+
if (support.queries) {
|
|
66
|
+
await db.deleteByQuery(queryAll())
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('should respond to ping', async () => {
|
|
71
|
+
await db.ping()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
if (support.queries) {
|
|
68
75
|
// QUERY empty
|
|
69
|
-
|
|
76
|
+
it('should return no rows and a count of 0 for an empty table', async () => {
|
|
70
77
|
expect((await db.runQuery(queryAll())).rows).toEqual([])
|
|
71
78
|
expect(await db.runQueryCount(queryAll())).toBe(0)
|
|
72
79
|
})
|
|
73
80
|
}
|
|
74
81
|
|
|
75
82
|
// GET empty
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
// console.log(a)
|
|
79
|
-
expect(item1Loaded).toBeUndefined()
|
|
83
|
+
it('should return an empty array for ids that are not saved', async () => {
|
|
84
|
+
expect(await db.getByIds(TEST_TABLE, [item1.id, 'abc', 'abcd'])).toEqual([])
|
|
80
85
|
})
|
|
81
86
|
|
|
82
|
-
|
|
87
|
+
it('should return an empty array for getByIds without ids', async () => {
|
|
83
88
|
expect(await db.getByIds(TEST_TABLE, [])).toEqual([])
|
|
84
89
|
})
|
|
85
90
|
|
|
86
|
-
test('getByIds(...) should return empty', async () => {
|
|
87
|
-
expect(await db.getByIds(TEST_TABLE, ['abc', 'abcd'])).toEqual([])
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
// TimeMachine
|
|
91
|
-
if (support.timeMachine) {
|
|
92
|
-
test('getByIds(...) 10 minutes ago should return []', async () => {
|
|
93
|
-
expect(
|
|
94
|
-
await db.getByIds(TEST_TABLE, [item1.id, 'abc'], {
|
|
95
|
-
readAt: localTime.now().minus(10, 'minute').unix,
|
|
96
|
-
}),
|
|
97
|
-
).toEqual([])
|
|
98
|
-
})
|
|
99
|
-
}
|
|
100
|
-
|
|
101
91
|
// SAVE
|
|
102
92
|
if (support.nullValues) {
|
|
103
|
-
|
|
93
|
+
it('should allow to save and load null values', async () => {
|
|
104
94
|
const item3 = {
|
|
105
95
|
...createTestItemDBM(3),
|
|
106
96
|
k2: null,
|
|
@@ -114,7 +104,7 @@ export async function runCommonDBTest(
|
|
|
114
104
|
}
|
|
115
105
|
|
|
116
106
|
if (db.dbType === CommonDBType.document) {
|
|
117
|
-
|
|
107
|
+
it('should not store properties with undefined values', async () => {
|
|
118
108
|
const item3 = {
|
|
119
109
|
...createTestItemDBM(3),
|
|
120
110
|
k2: undefined,
|
|
@@ -132,33 +122,33 @@ export async function runCommonDBTest(
|
|
|
132
122
|
}
|
|
133
123
|
|
|
134
124
|
if (support.updateSaveMethod) {
|
|
135
|
-
|
|
125
|
+
it('should throw on saveBatch with the update method when rows do not exist', async () => {
|
|
136
126
|
await expect(db.saveBatch(TEST_TABLE, items, { saveMethod: 'update' })).rejects.toThrow()
|
|
137
127
|
})
|
|
138
128
|
}
|
|
139
129
|
|
|
140
|
-
|
|
130
|
+
it('should save the test items', async () => {
|
|
141
131
|
await db.saveBatch(TEST_TABLE, items)
|
|
142
132
|
})
|
|
143
133
|
|
|
144
|
-
|
|
134
|
+
it('should throw on saveBatch of a row with a null id', async () => {
|
|
145
135
|
await expect(db.saveBatch(TEST_TABLE, [{ ...item1, id: null as any }])).rejects.toThrow()
|
|
146
136
|
})
|
|
147
137
|
|
|
148
138
|
if (support.insertSaveMethod) {
|
|
149
|
-
|
|
139
|
+
it('should throw on saveBatch with the insert method when rows exist', async () => {
|
|
150
140
|
await expect(db.saveBatch(TEST_TABLE, items, { saveMethod: 'insert' })).rejects.toThrow()
|
|
151
141
|
})
|
|
152
142
|
}
|
|
153
143
|
|
|
154
144
|
if (support.updateSaveMethod) {
|
|
155
|
-
|
|
145
|
+
it('should save with the update method when rows exist', async () => {
|
|
156
146
|
await db.saveBatch(TEST_TABLE, items, { saveMethod: 'update' })
|
|
157
147
|
})
|
|
158
148
|
}
|
|
159
149
|
|
|
160
150
|
// GET not empty
|
|
161
|
-
|
|
151
|
+
it('should return all saved items by ids, skipping unknown ids', async () => {
|
|
162
152
|
const rows = await db.getByIds<TestItemDBM>(TEST_TABLE, items.map(i => i.id).concat('abcd'))
|
|
163
153
|
expectMatch(
|
|
164
154
|
items,
|
|
@@ -167,16 +157,25 @@ export async function runCommonDBTest(
|
|
|
167
157
|
)
|
|
168
158
|
})
|
|
169
159
|
|
|
160
|
+
// TimeMachine
|
|
161
|
+
if (support.timeMachine) {
|
|
162
|
+
it('should not return the saved items when reading 10 minutes back in time', async () => {
|
|
163
|
+
const ids = items.map(i => i.id)
|
|
164
|
+
const readAt = localTime.now().minus(10, 'minute').unix
|
|
165
|
+
expect(await db.getByIds(TEST_TABLE, ids, { readAt })).toEqual([])
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
|
|
170
169
|
// QUERY
|
|
171
170
|
if (support.queries) {
|
|
172
|
-
|
|
171
|
+
it('should return all items from a query without conditions', async () => {
|
|
173
172
|
let { rows } = await db.runQuery(queryAll())
|
|
174
173
|
rows = _sortBy(rows, r => r.id) // because query doesn't specify order here
|
|
175
174
|
expectMatch(items, rows, quirks)
|
|
176
175
|
})
|
|
177
176
|
|
|
178
177
|
if (support.dbQueryFilter) {
|
|
179
|
-
|
|
178
|
+
it('should filter rows by equality', async () => {
|
|
180
179
|
const q = new DBQuery<TestItemDBM>(TEST_TABLE).filter('even', '==', true)
|
|
181
180
|
let { rows } = await db.runQuery(q)
|
|
182
181
|
if (!support.dbQueryOrder) rows = _sortBy(rows, r => r.id)
|
|
@@ -189,7 +188,7 @@ export async function runCommonDBTest(
|
|
|
189
188
|
}
|
|
190
189
|
|
|
191
190
|
if (support.dbQueryOrder) {
|
|
192
|
-
|
|
191
|
+
it('should order rows descending', async () => {
|
|
193
192
|
const q = new DBQuery<TestItemDBM>(TEST_TABLE).order('k1', true)
|
|
194
193
|
const { rows } = await db.runQuery(q)
|
|
195
194
|
expectMatch(items.toReversed(), rows, quirks)
|
|
@@ -197,7 +196,7 @@ export async function runCommonDBTest(
|
|
|
197
196
|
}
|
|
198
197
|
|
|
199
198
|
if (support.dbQuerySelectFields) {
|
|
200
|
-
|
|
199
|
+
it('should select only the id', async () => {
|
|
201
200
|
const q = new DBQuery<TestItemDBM>(TEST_TABLE).select(['id'])
|
|
202
201
|
let { rows } = await db.runQuery(q)
|
|
203
202
|
rows = _sortBy(rows, r => r.id) // cause order is not specified
|
|
@@ -208,7 +207,7 @@ export async function runCommonDBTest(
|
|
|
208
207
|
)
|
|
209
208
|
})
|
|
210
209
|
|
|
211
|
-
|
|
210
|
+
it('should select a property without the id', async () => {
|
|
212
211
|
const q = new DBQuery<TestItemDBM>(TEST_TABLE).select(['k1'])
|
|
213
212
|
let { rows } = await db.runQuery(q)
|
|
214
213
|
rows = _sortBy(rows, r => r.k1) // cause order is not specified
|
|
@@ -219,7 +218,7 @@ export async function runCommonDBTest(
|
|
|
219
218
|
)
|
|
220
219
|
})
|
|
221
220
|
|
|
222
|
-
|
|
221
|
+
it('should return empty rows when selecting no properties', async () => {
|
|
223
222
|
const q = new DBQuery<TestItemDBM>(TEST_TABLE).select([])
|
|
224
223
|
const { rows } = await db.runQuery(q)
|
|
225
224
|
expectMatch(
|
|
@@ -230,14 +229,14 @@ export async function runCommonDBTest(
|
|
|
230
229
|
})
|
|
231
230
|
}
|
|
232
231
|
|
|
233
|
-
|
|
232
|
+
it('should count all saved items', async () => {
|
|
234
233
|
expect(await db.runQueryCount(new DBQuery(TEST_TABLE))).toBe(3)
|
|
235
234
|
})
|
|
236
235
|
}
|
|
237
236
|
|
|
238
237
|
// STREAM
|
|
239
238
|
if (support.streaming) {
|
|
240
|
-
|
|
239
|
+
it('should stream all items', async () => {
|
|
241
240
|
let rows = await db.streamQuery(queryAll()).toArray()
|
|
242
241
|
|
|
243
242
|
rows = _sortBy(rows, r => r.id) // cause order is not specified in DBQuery
|
|
@@ -246,22 +245,27 @@ export async function runCommonDBTest(
|
|
|
246
245
|
}
|
|
247
246
|
|
|
248
247
|
// getTables
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
//
|
|
248
|
+
it('should resolve getTables to an array', async () => {
|
|
249
|
+
// Implementations differ in what they list (prefixed names, lazily updated stats, nothing),
|
|
250
|
+
// so only the shape is part of the shared contract
|
|
251
|
+
await expect(db.getTables()).resolves.toBeInstanceOf(Array)
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
if (support.tableSchemas) {
|
|
255
|
+
it('should return a schema $id matching each listed table', async () => {
|
|
256
|
+
const tables = await db.getTables()
|
|
252
257
|
|
|
253
|
-
if (support.tableSchemas) {
|
|
254
258
|
await pMap(tables, async table => {
|
|
255
259
|
const schema = await db.getTableSchema(table)
|
|
256
260
|
// console.log(schema)
|
|
257
261
|
expect(schema.$id).toBe(`${table}.schema.json`)
|
|
258
262
|
})
|
|
259
|
-
}
|
|
260
|
-
}
|
|
263
|
+
})
|
|
264
|
+
}
|
|
261
265
|
|
|
262
266
|
// DELETE BY
|
|
263
267
|
if (support.queries && support.dbQueryFilter) {
|
|
264
|
-
|
|
268
|
+
it('should delete rows by a filter and return the deleted count', async () => {
|
|
265
269
|
const q = new DBQuery<TestItemDBM>(TEST_TABLE).filter('even', '==', false)
|
|
266
270
|
const deleted = await db.deleteByQuery(q)
|
|
267
271
|
expect(deleted).toBe(items.filter(item => !item.even).length)
|
|
@@ -272,7 +276,7 @@ export async function runCommonDBTest(
|
|
|
272
276
|
|
|
273
277
|
// BUFFER
|
|
274
278
|
if (support.bufferValues) {
|
|
275
|
-
|
|
279
|
+
it('should save and load Buffer values', async () => {
|
|
276
280
|
const s = 'helloWorld 1'
|
|
277
281
|
const b1 = Buffer.from(s)
|
|
278
282
|
|
|
@@ -309,7 +313,7 @@ export async function runCommonDBTest(
|
|
|
309
313
|
return itemsToSave
|
|
310
314
|
}
|
|
311
315
|
|
|
312
|
-
|
|
316
|
+
it('should commit the saves and deletes made in runInTransaction', async () => {
|
|
313
317
|
// cleanup
|
|
314
318
|
await db.deleteByQuery(queryAll())
|
|
315
319
|
|
|
@@ -325,10 +329,14 @@ export async function runCommonDBTest(
|
|
|
325
329
|
|
|
326
330
|
const { rows } = await db.runQuery(queryAll())
|
|
327
331
|
const expected = [items[0], { ...items[2]!, k1: 'k1_mod' }]
|
|
328
|
-
expectMatch(
|
|
332
|
+
expectMatch(
|
|
333
|
+
expected,
|
|
334
|
+
_sortBy(rows, r => r.id),
|
|
335
|
+
quirks,
|
|
336
|
+
)
|
|
329
337
|
})
|
|
330
338
|
|
|
331
|
-
|
|
339
|
+
it('should commit the saves and deletes made in a created transaction', async () => {
|
|
332
340
|
// cleanup
|
|
333
341
|
await db.deleteByQuery(queryAll())
|
|
334
342
|
|
|
@@ -340,10 +348,14 @@ export async function runCommonDBTest(
|
|
|
340
348
|
|
|
341
349
|
const { rows } = await db.runQuery(queryAll())
|
|
342
350
|
const expected = [items[0], { ...items[2]!, k1: 'k1_mod' }]
|
|
343
|
-
expectMatch(
|
|
351
|
+
expectMatch(
|
|
352
|
+
expected,
|
|
353
|
+
_sortBy(rows, r => r.id),
|
|
354
|
+
quirks,
|
|
355
|
+
)
|
|
344
356
|
})
|
|
345
357
|
|
|
346
|
-
|
|
358
|
+
it('should roll back runInTransaction when an operation fails', async () => {
|
|
347
359
|
const expected = await prepare()
|
|
348
360
|
|
|
349
361
|
let err: any
|
|
@@ -361,12 +373,16 @@ export async function runCommonDBTest(
|
|
|
361
373
|
expect(err).toBeDefined()
|
|
362
374
|
|
|
363
375
|
const { rows } = await db.runQuery(queryAll())
|
|
364
|
-
expectMatch(
|
|
376
|
+
expectMatch(
|
|
377
|
+
expected,
|
|
378
|
+
_sortBy(rows, r => r.id),
|
|
379
|
+
quirks,
|
|
380
|
+
)
|
|
365
381
|
})
|
|
366
382
|
}
|
|
367
383
|
|
|
368
384
|
if (support.patchByQuery) {
|
|
369
|
-
|
|
385
|
+
it('should patch the rows matching a query', async () => {
|
|
370
386
|
// cleanup, reset initial data
|
|
371
387
|
await db.deleteByQuery(queryAll())
|
|
372
388
|
await db.saveBatch(TEST_TABLE, items)
|
|
@@ -385,12 +401,16 @@ export async function runCommonDBTest(
|
|
|
385
401
|
}
|
|
386
402
|
return r
|
|
387
403
|
})
|
|
388
|
-
expectMatch(
|
|
404
|
+
expectMatch(
|
|
405
|
+
expected,
|
|
406
|
+
_sortBy(rows, r => r.id),
|
|
407
|
+
quirks,
|
|
408
|
+
)
|
|
389
409
|
})
|
|
390
410
|
}
|
|
391
411
|
|
|
392
412
|
if (support.increment) {
|
|
393
|
-
|
|
413
|
+
it('should increment a property by a different amount per id', async () => {
|
|
394
414
|
// cleanup, reset initial data
|
|
395
415
|
await db.deleteByQuery(queryAll())
|
|
396
416
|
await db.saveBatch(TEST_TABLE, items)
|
|
@@ -407,14 +427,11 @@ export async function runCommonDBTest(
|
|
|
407
427
|
}
|
|
408
428
|
return r
|
|
409
429
|
})
|
|
410
|
-
expectMatch(
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
test('cleanup', async () => {
|
|
416
|
-
// CLEAN UP
|
|
417
|
-
await db.deleteByQuery(queryAll())
|
|
430
|
+
expectMatch(
|
|
431
|
+
expected,
|
|
432
|
+
_sortBy(rows, r => r.id),
|
|
433
|
+
quirks,
|
|
434
|
+
)
|
|
418
435
|
})
|
|
419
436
|
}
|
|
420
437
|
|