@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
|
@@ -7,7 +7,7 @@ import { DBQuery } from '../query/dbQuery.js';
|
|
|
7
7
|
import { createTestItemBM, createTestItemsBM, TEST_TABLE, testItemBMSchema } from './test.model.js';
|
|
8
8
|
export async function runCommonDaoTest(db, quirks = {}) {
|
|
9
9
|
// this is because vitest cannot be "required" from cjs
|
|
10
|
-
const {
|
|
10
|
+
const { afterAll, beforeAll, expect, it } = await import('vitest');
|
|
11
11
|
const { support } = db;
|
|
12
12
|
const dao = new CommonDao({
|
|
13
13
|
table: TEST_TABLE,
|
|
@@ -22,50 +22,48 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
22
22
|
...i,
|
|
23
23
|
updated: expect.any(Number),
|
|
24
24
|
}));
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// CREATE TABLE, DROP
|
|
29
|
-
if (support.createTable) {
|
|
30
|
-
test('createTable, dropIfExists=true', async () => {
|
|
25
|
+
beforeAll(async () => {
|
|
26
|
+
// Tests in this suite are not isolated: a failed run can leave rows behind for the next run
|
|
27
|
+
if (support.createTable) {
|
|
31
28
|
await dao.createTable(testItemBMSchema.build(), { dropIfExists: true });
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
if (support.queries) {
|
|
29
|
+
}
|
|
35
30
|
// DELETE ALL initially
|
|
36
|
-
|
|
31
|
+
if (support.queries) {
|
|
37
32
|
const rows = await dao.query().select(['id']).runQuery();
|
|
38
33
|
await db.deleteByQuery(DBQuery.create(TEST_TABLE).filter('id', 'in', rows.map(r => r.id)));
|
|
39
|
-
}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
afterAll(async () => {
|
|
37
|
+
// CLEAN UP
|
|
38
|
+
if (support.queries) {
|
|
39
|
+
await dao.query().deleteByQuery();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
it('should respond to ping', async () => {
|
|
43
|
+
await dao.ping();
|
|
44
|
+
});
|
|
45
|
+
if (support.queries) {
|
|
40
46
|
// QUERY empty
|
|
41
|
-
|
|
47
|
+
it('should return no rows and a count of 0 for an empty table', async () => {
|
|
42
48
|
expect(await dao.query().runQuery()).toEqual([]);
|
|
43
49
|
expect(await dao.query().runQueryCount()).toBe(0);
|
|
44
50
|
});
|
|
45
51
|
}
|
|
46
52
|
// GET empty
|
|
47
|
-
|
|
53
|
+
it('should return no row for an id that is not saved yet', async () => {
|
|
48
54
|
const [item1Loaded] = await dao.getByIds([item1.id]);
|
|
49
55
|
expect(item1Loaded).toBeUndefined();
|
|
50
56
|
expect(await dao.getById(item1.id)).toBeNull();
|
|
51
57
|
});
|
|
52
|
-
|
|
58
|
+
it('should return an empty array for getByIds without ids', async () => {
|
|
53
59
|
expect(await dao.getByIds([])).toEqual([]);
|
|
54
60
|
});
|
|
55
|
-
|
|
61
|
+
it('should return an empty array for unknown ids', async () => {
|
|
56
62
|
expect(await dao.getByIds(['abc', 'abcd'])).toEqual([]);
|
|
57
63
|
});
|
|
58
|
-
// TimeMachine
|
|
59
|
-
if (support.timeMachine) {
|
|
60
|
-
test('getByIds(...) 10 minutes ago should return []', async () => {
|
|
61
|
-
expect(await dao.getByIds([item1.id, 'abc'], {
|
|
62
|
-
readAt: localTime.now().minus(10, 'minute').unix,
|
|
63
|
-
})).toEqual([]);
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
64
|
// SAVE
|
|
67
65
|
if (support.nullValues) {
|
|
68
|
-
|
|
66
|
+
it('should allow to save and load null values', async () => {
|
|
69
67
|
const item3 = {
|
|
70
68
|
...createTestItemBM(3),
|
|
71
69
|
k2: null,
|
|
@@ -79,7 +77,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
79
77
|
expect(item3.k2).toBeNull();
|
|
80
78
|
});
|
|
81
79
|
}
|
|
82
|
-
|
|
80
|
+
it('should not store undefined properties but keep them on the input object', async () => {
|
|
83
81
|
const item3 = {
|
|
84
82
|
...createTestItemBM(3),
|
|
85
83
|
k2: undefined,
|
|
@@ -96,7 +94,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
96
94
|
expect(Object.keys(item3)).toContain('k2');
|
|
97
95
|
expect(item3.k2).toBeUndefined();
|
|
98
96
|
});
|
|
99
|
-
|
|
97
|
+
it('should save the test items and return the same objects', async () => {
|
|
100
98
|
const itemsSaved = await dao.saveBatch(items);
|
|
101
99
|
expect(itemsSaved[0]).toBe(items[0]); // expect "same object" returned
|
|
102
100
|
// no unnecessary mutation
|
|
@@ -105,7 +103,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
105
103
|
expectMatch(expectedItems, itemsSaved, quirks);
|
|
106
104
|
});
|
|
107
105
|
if (support.increment) {
|
|
108
|
-
|
|
106
|
+
it('should increment a property by a different amount per id', async () => {
|
|
109
107
|
await dao.incrementBatch('k3', { id1: 1, id2: 2 });
|
|
110
108
|
let rows = await dao.query().runQuery();
|
|
111
109
|
rows = _sortBy(rows, r => r.id);
|
|
@@ -131,24 +129,32 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
131
129
|
});
|
|
132
130
|
}
|
|
133
131
|
// GET not empty
|
|
134
|
-
|
|
132
|
+
it('should return all saved items by ids, skipping unknown ids', async () => {
|
|
135
133
|
const rows = await dao.getByIds(items.map(i => i.id).concat('abcd'));
|
|
136
134
|
expectMatch(expectedItems, _sortBy(rows, r => r.id), quirks);
|
|
137
135
|
});
|
|
136
|
+
// TimeMachine
|
|
137
|
+
if (support.timeMachine) {
|
|
138
|
+
it('should not return the saved items when reading 10 minutes back in time', async () => {
|
|
139
|
+
const ids = items.map(i => i.id);
|
|
140
|
+
const readAt = localTime.now().minus(10, 'minute').unix;
|
|
141
|
+
expect(await dao.getByIds(ids, { readAt })).toEqual([]);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
138
144
|
// QUERY
|
|
139
145
|
if (support.queries) {
|
|
140
|
-
|
|
146
|
+
it('should return all items from a query without conditions', async () => {
|
|
141
147
|
let rows = await dao.query().runQuery();
|
|
142
148
|
rows = _sortBy(rows, r => r.id);
|
|
143
149
|
expectMatch(expectedItems, rows, quirks);
|
|
144
150
|
});
|
|
145
151
|
if (support.dbQueryFilter) {
|
|
146
|
-
|
|
152
|
+
it('should filter rows by equality', async () => {
|
|
147
153
|
let rows = await dao.query().filter('even', '==', true).runQuery();
|
|
148
154
|
rows = _sortBy(rows, r => r.id);
|
|
149
155
|
expectMatch(expectedItems.filter(i => i.even), rows, quirks);
|
|
150
156
|
});
|
|
151
|
-
|
|
157
|
+
it('should filter rows by a nested property', async () => {
|
|
152
158
|
let rows = await dao
|
|
153
159
|
.query()
|
|
154
160
|
.filter('nested.foo', '==', 1)
|
|
@@ -158,25 +164,25 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
158
164
|
});
|
|
159
165
|
}
|
|
160
166
|
if (support.dbQueryOrder) {
|
|
161
|
-
|
|
167
|
+
it('should order rows descending', async () => {
|
|
162
168
|
const rows = await dao.query().order('k1', true).runQuery();
|
|
163
169
|
expectMatch(expectedItems.toReversed(), rows, quirks);
|
|
164
170
|
});
|
|
165
171
|
}
|
|
166
172
|
if (support.dbQuerySelectFields) {
|
|
167
|
-
|
|
173
|
+
it('should select only the id', async () => {
|
|
168
174
|
let rows = await dao.query().select(['id']).runQuery();
|
|
169
175
|
rows = _sortBy(rows, r => r.id);
|
|
170
176
|
expectMatch(expectedItems.map(item => _pick(item, ['id'])), rows, quirks);
|
|
171
177
|
});
|
|
172
178
|
}
|
|
173
|
-
|
|
179
|
+
it('should count all saved items', async () => {
|
|
174
180
|
expect(await dao.query().runQueryCount()).toBe(3);
|
|
175
181
|
});
|
|
176
182
|
}
|
|
177
183
|
// STREAM
|
|
178
184
|
if (support.streaming) {
|
|
179
|
-
|
|
185
|
+
it('should stream all items to forEachSync', async () => {
|
|
180
186
|
let rows = [];
|
|
181
187
|
await dao
|
|
182
188
|
.query()
|
|
@@ -185,12 +191,12 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
185
191
|
rows = _sortBy(rows, r => r.id);
|
|
186
192
|
expectMatch(expectedItems, rows, quirks);
|
|
187
193
|
});
|
|
188
|
-
|
|
194
|
+
it('should stream all items to an array', async () => {
|
|
189
195
|
let rows = await dao.query().streamQuery().toArray();
|
|
190
196
|
rows = _sortBy(rows, r => r.id);
|
|
191
197
|
expectMatch(expectedItems, rows, quirks);
|
|
192
198
|
});
|
|
193
|
-
|
|
199
|
+
it('should stream all ids to forEachSync', async () => {
|
|
194
200
|
let ids = [];
|
|
195
201
|
await dao
|
|
196
202
|
.query()
|
|
@@ -199,12 +205,12 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
199
205
|
ids = ids.sort();
|
|
200
206
|
expectMatch(expectedItems.map(i => i.id), ids, quirks);
|
|
201
207
|
});
|
|
202
|
-
|
|
208
|
+
it('should stream all ids to an array', async () => {
|
|
203
209
|
let ids = await dao.query().streamQueryIds().toArray();
|
|
204
210
|
ids = ids.sort();
|
|
205
211
|
expectMatch(expectedItems.map(i => i.id), ids, quirks);
|
|
206
212
|
});
|
|
207
|
-
|
|
213
|
+
it('should save the items of a stream', async () => {
|
|
208
214
|
const items2 = createTestItemsBM(2).map(i => ({ ...i, id: i.id + '_str' }));
|
|
209
215
|
const ids = items2.map(i => i.id);
|
|
210
216
|
await dao.streamSave(Pipeline.fromArray(items2));
|
|
@@ -216,15 +222,11 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
216
222
|
}
|
|
217
223
|
// DELETE BY
|
|
218
224
|
if (support.queries) {
|
|
219
|
-
|
|
225
|
+
it('should delete rows by a filter and return the deleted count', async () => {
|
|
220
226
|
const deleted = await dao.query().filter('even', '==', false).deleteByQuery();
|
|
221
227
|
expect(deleted).toBe(items.filter(item => !item.even).length);
|
|
222
228
|
expect(await dao.query().runQueryCount()).toBe(1);
|
|
223
229
|
});
|
|
224
|
-
test('cleanup', async () => {
|
|
225
|
-
// CLEAN UP
|
|
226
|
-
await dao.query().deleteByQuery();
|
|
227
|
-
});
|
|
228
230
|
}
|
|
229
231
|
if (support.transactions) {
|
|
230
232
|
/**
|
|
@@ -237,7 +239,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
237
239
|
await dao.saveBatch(itemsToSave);
|
|
238
240
|
return itemsToSave;
|
|
239
241
|
}
|
|
240
|
-
|
|
242
|
+
it('should commit the saves and deletes made in runInTransaction', async () => {
|
|
241
243
|
// cleanup
|
|
242
244
|
await dao.query().deleteByQuery();
|
|
243
245
|
// Test that id, created, updated are created
|
|
@@ -265,10 +267,10 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
265
267
|
});
|
|
266
268
|
const rows = await dao.query().runQuery();
|
|
267
269
|
const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
|
|
268
|
-
expectMatch(expected, rows, quirks);
|
|
270
|
+
expectMatch(expected, _sortBy(rows, r => r.id), quirks);
|
|
269
271
|
});
|
|
270
272
|
if (support.createTransaction) {
|
|
271
|
-
|
|
273
|
+
it('should commit the saves and deletes made in a created transaction', async () => {
|
|
272
274
|
// cleanup
|
|
273
275
|
await dao.query().deleteByQuery();
|
|
274
276
|
// Test that id, created, updated are created
|
|
@@ -296,10 +298,10 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
296
298
|
await tx.commit();
|
|
297
299
|
const rows = await dao.query().runQuery();
|
|
298
300
|
const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
|
|
299
|
-
expectMatch(expected, rows, quirks);
|
|
301
|
+
expectMatch(expected, _sortBy(rows, r => r.id), quirks);
|
|
300
302
|
});
|
|
301
303
|
}
|
|
302
|
-
|
|
304
|
+
it('should roll back runInTransaction when a save fails validation', async () => {
|
|
303
305
|
const expected = await prepare();
|
|
304
306
|
let err;
|
|
305
307
|
try {
|
|
@@ -314,30 +316,17 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
314
316
|
expect(err).toBeDefined();
|
|
315
317
|
expect(err).toBeInstanceOf(Error);
|
|
316
318
|
const rows = await dao.query().runQuery();
|
|
317
|
-
expectMatch(expected, rows, quirks);
|
|
319
|
+
expectMatch(expected, _sortBy(rows, r => r.id), quirks);
|
|
318
320
|
});
|
|
319
321
|
if (support.createTransaction) {
|
|
320
|
-
|
|
322
|
+
it('should leave the data unchanged when a created transaction is rolled back after a failed save', async () => {
|
|
321
323
|
const expected = await prepare();
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
await tx.save(dao, { ...items[0], k1: 5 }); // it should fail here
|
|
327
|
-
await tx.commit();
|
|
328
|
-
}
|
|
329
|
-
catch (err2) {
|
|
330
|
-
err = err2;
|
|
331
|
-
}
|
|
332
|
-
expect(err).toBeDefined();
|
|
333
|
-
expect(err).toBeInstanceOf(Error);
|
|
324
|
+
const tx = await dao.createTransaction();
|
|
325
|
+
await tx.deleteById(dao, items[2].id);
|
|
326
|
+
await expect(tx.save(dao, { ...items[0], k1: 5 })).rejects.toThrow(); // it should fail here
|
|
327
|
+
await tx.rollback();
|
|
334
328
|
const rows = await dao.query().runQuery();
|
|
335
|
-
expectMatch(expected, rows, quirks);
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
if (support.queries) {
|
|
339
|
-
test('transaction cleanup', async () => {
|
|
340
|
-
await dao.query().deleteByQuery();
|
|
329
|
+
expectMatch(expected, _sortBy(rows, r => r.id), quirks);
|
|
341
330
|
});
|
|
342
331
|
}
|
|
343
332
|
}
|
|
@@ -6,7 +6,7 @@ const testEntries = testIds.map(id => [
|
|
|
6
6
|
Buffer.from(`${id}value`),
|
|
7
7
|
]);
|
|
8
8
|
export async function runCommonKeyValueDBTest(db) {
|
|
9
|
-
const { afterAll, beforeAll, expect,
|
|
9
|
+
const { afterAll, beforeAll, expect, it } = await import('vitest');
|
|
10
10
|
beforeAll(async () => {
|
|
11
11
|
// Tests in this suite are not isolated,
|
|
12
12
|
// and failing tests can leave the DB in an unexpected state for other tests,
|
|
@@ -20,88 +20,95 @@ export async function runCommonKeyValueDBTest(db) {
|
|
|
20
20
|
await db.deleteByIds(TEST_TABLE, ids);
|
|
21
21
|
});
|
|
22
22
|
const { support } = db;
|
|
23
|
-
|
|
23
|
+
it('should respond to ping', async () => {
|
|
24
24
|
await db.ping();
|
|
25
25
|
});
|
|
26
|
-
|
|
26
|
+
it('should create the table, dropping an existing one', async () => {
|
|
27
27
|
await db.createTable(TEST_TABLE, { dropIfExists: true });
|
|
28
28
|
});
|
|
29
|
-
|
|
29
|
+
it('should not throw when deleting ids that do not exist', async () => {
|
|
30
30
|
await db.deleteByIds(TEST_TABLE, testIds);
|
|
31
31
|
});
|
|
32
|
-
|
|
32
|
+
it('should return no entries for ids that do not exist', async () => {
|
|
33
33
|
const results = await db.getByIds(TEST_TABLE, testIds);
|
|
34
34
|
expect(results).toEqual([]);
|
|
35
35
|
});
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
if (support.count) {
|
|
37
|
+
it('should count 0 entries in an empty table', async () => {
|
|
38
|
+
expect(await db.count(TEST_TABLE)).toBe(0);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
it('should return the saved entries by ids', async () => {
|
|
40
42
|
await db.saveBatch(TEST_TABLE, testEntries);
|
|
41
43
|
const entries = await db.getByIds(TEST_TABLE, testIds);
|
|
42
44
|
_sortBy(entries, e => e[0], { mutate: true });
|
|
43
45
|
expect(entries).toEqual(testEntries);
|
|
44
46
|
});
|
|
45
47
|
if (support.count) {
|
|
46
|
-
|
|
48
|
+
it('should count the saved entries', async () => {
|
|
47
49
|
expect(await db.count(TEST_TABLE)).toBe(3);
|
|
48
50
|
});
|
|
49
51
|
}
|
|
50
|
-
|
|
52
|
+
it('should stream all ids', async () => {
|
|
51
53
|
const ids = await db.streamIds(TEST_TABLE).toArray();
|
|
52
54
|
ids.sort();
|
|
53
55
|
expect(ids).toEqual(testIds);
|
|
54
56
|
});
|
|
55
|
-
|
|
57
|
+
it('should stream as many ids as the limit', async () => {
|
|
56
58
|
const idsLimited = await db.streamIds(TEST_TABLE, 2).toArray();
|
|
57
59
|
// Order is non-deterministic, so, cannot compare values
|
|
58
60
|
// idsLimited.sort()
|
|
59
61
|
// expect(idsLimited).toEqual(testIds.slice(0, 2))
|
|
60
62
|
expect(idsLimited.length).toBe(2);
|
|
61
63
|
});
|
|
62
|
-
|
|
64
|
+
it('should stream all values', async () => {
|
|
63
65
|
const values = await db.streamValues(TEST_TABLE).toArray();
|
|
64
66
|
values.sort();
|
|
65
67
|
expect(values).toEqual(testEntries.map(e => e[1]));
|
|
66
68
|
});
|
|
67
|
-
|
|
69
|
+
it('should stream as many values as the limit', async () => {
|
|
68
70
|
const valuesLimited = await db.streamValues(TEST_TABLE, 2).toArray();
|
|
69
71
|
// valuesLimited.sort()
|
|
70
72
|
// expect(valuesLimited).toEqual(testEntries.map(e => e[1]).slice(0, 2))
|
|
71
73
|
expect(valuesLimited.length).toBe(2);
|
|
72
74
|
});
|
|
73
|
-
|
|
75
|
+
it('should stream all entries', async () => {
|
|
74
76
|
const entries = await db.streamEntries(TEST_TABLE).toArray();
|
|
75
77
|
entries.sort();
|
|
76
78
|
expect(entries).toEqual(testEntries);
|
|
77
79
|
});
|
|
78
|
-
|
|
80
|
+
it('should stream as many entries as the limit', async () => {
|
|
79
81
|
const entriesLimited = await db.streamEntries(TEST_TABLE, 2).toArray();
|
|
80
82
|
// entriesLimited.sort()
|
|
81
83
|
// expect(entriesLimited).toEqual(testEntries.slice(0, 2))
|
|
82
84
|
expect(entriesLimited.length).toBe(2);
|
|
83
85
|
});
|
|
84
|
-
|
|
86
|
+
it('should delete the entries by ids', async () => {
|
|
85
87
|
await db.deleteByIds(TEST_TABLE, testIds);
|
|
86
88
|
const results = await db.getByIds(TEST_TABLE, testIds);
|
|
87
89
|
expect(results).toEqual([]);
|
|
88
90
|
});
|
|
91
|
+
if (support.count) {
|
|
92
|
+
it('should count 0 entries after deleting them', async () => {
|
|
93
|
+
expect(await db.count(TEST_TABLE)).toBe(0);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
89
96
|
if (support.increment) {
|
|
90
97
|
const id = 'nonExistingField';
|
|
91
98
|
const id2 = 'nonExistingField2';
|
|
92
|
-
|
|
99
|
+
it('should increment a missing key from 0', async () => {
|
|
93
100
|
const result = await db.incrementBatch(TEST_TABLE, [[id, 1]]);
|
|
94
101
|
expect(result).toEqual([[id, 1]]);
|
|
95
102
|
});
|
|
96
|
-
|
|
103
|
+
it('should increment an existing key by 1', async () => {
|
|
97
104
|
const result = await db.incrementBatch(TEST_TABLE, [[id, 1]]);
|
|
98
105
|
expect(result).toEqual([[id, 2]]);
|
|
99
106
|
});
|
|
100
|
-
|
|
107
|
+
it('should increment an existing key by the given amount', async () => {
|
|
101
108
|
const result = await db.incrementBatch(TEST_TABLE, [[id, 2]]);
|
|
102
109
|
expect(result).toEqual([[id, 4]]);
|
|
103
110
|
});
|
|
104
|
-
|
|
111
|
+
it('should increment two keys in one batch', async () => {
|
|
105
112
|
const result = await db.incrementBatch(TEST_TABLE, [
|
|
106
113
|
[id, 1],
|
|
107
114
|
[id2, 2],
|
|
@@ -5,7 +5,7 @@ const testItems = createTestItemsBM(4);
|
|
|
5
5
|
const testIds = testItems.map(e => e.id);
|
|
6
6
|
const testEntries = testItems.map(e => [e.id, Buffer.from(`${e.id}value`)]);
|
|
7
7
|
export async function runCommonKeyValueDaoTest(db) {
|
|
8
|
-
const { afterAll, beforeAll, expect,
|
|
8
|
+
const { afterAll, beforeAll, expect, it } = await import('vitest');
|
|
9
9
|
const dao = new CommonKeyValueDao({
|
|
10
10
|
db,
|
|
11
11
|
table: TEST_TABLE,
|
|
@@ -24,20 +24,20 @@ export async function runCommonKeyValueDaoTest(db) {
|
|
|
24
24
|
const ids = await dao.streamIds().toArray();
|
|
25
25
|
await dao.deleteByIds(ids);
|
|
26
26
|
});
|
|
27
|
-
|
|
27
|
+
it('should respond to ping', async () => {
|
|
28
28
|
await dao.ping();
|
|
29
29
|
});
|
|
30
|
-
|
|
30
|
+
it('should create the table, dropping an existing one', async () => {
|
|
31
31
|
await dao.createTable({ dropIfExists: true });
|
|
32
32
|
});
|
|
33
|
-
|
|
33
|
+
it('should not throw when deleting ids that do not exist', async () => {
|
|
34
34
|
await dao.deleteByIds(testIds);
|
|
35
35
|
});
|
|
36
|
-
|
|
36
|
+
it('should return no entries for ids that do not exist', async () => {
|
|
37
37
|
const results = await dao.getByIds(testIds);
|
|
38
38
|
expect(results).toEqual([]);
|
|
39
39
|
});
|
|
40
|
-
|
|
40
|
+
it('should return the saved entries by ids', async () => {
|
|
41
41
|
await dao.saveBatch(testEntries);
|
|
42
42
|
const entries = await dao.getByIds(testIds);
|
|
43
43
|
// console.log(typeof entries[0]![1], entries[0]![1])
|
|
@@ -46,41 +46,41 @@ export async function runCommonKeyValueDaoTest(db) {
|
|
|
46
46
|
// expect(entries.map(e => e[0])).toEqual(testEntries.map(e => e[0]))
|
|
47
47
|
// expect(entries.map(e => e[1].toString())).toEqual(testEntries.map(e => e[1].toString()))
|
|
48
48
|
});
|
|
49
|
-
|
|
49
|
+
it('should stream all ids', async () => {
|
|
50
50
|
const ids = await dao.streamIds().toArray();
|
|
51
51
|
ids.sort();
|
|
52
52
|
expect(ids).toEqual(testIds);
|
|
53
53
|
});
|
|
54
|
-
|
|
54
|
+
it('should stream as many ids as the limit', async () => {
|
|
55
55
|
const idsLimited = await dao.streamIds(2).toArray();
|
|
56
56
|
// Order is non-deterministic, so, cannot compare values
|
|
57
57
|
// idsLimited.sort()
|
|
58
58
|
// expect(idsLimited).toEqual(testIds.slice(0, 2))
|
|
59
59
|
expect(idsLimited.length).toBe(2);
|
|
60
60
|
});
|
|
61
|
-
|
|
61
|
+
it('should stream all values', async () => {
|
|
62
62
|
const values = await dao.streamValues().toArray();
|
|
63
63
|
values.sort();
|
|
64
64
|
expect(values).toEqual(testEntries.map(e => e[1]));
|
|
65
65
|
});
|
|
66
|
-
|
|
66
|
+
it('should stream as many values as the limit', async () => {
|
|
67
67
|
const valuesLimited = await dao.streamValues(2).toArray();
|
|
68
68
|
// valuesLimited.sort()
|
|
69
69
|
// expect(valuesLimited).toEqual(testEntries.map(e => e[1]).slice(0, 2))
|
|
70
70
|
expect(valuesLimited.length).toBe(2);
|
|
71
71
|
});
|
|
72
|
-
|
|
72
|
+
it('should stream all entries', async () => {
|
|
73
73
|
const entries = await dao.streamEntries().toArray();
|
|
74
74
|
entries.sort();
|
|
75
75
|
expect(entries).toEqual(testEntries);
|
|
76
76
|
});
|
|
77
|
-
|
|
77
|
+
it('should stream as many entries as the limit', async () => {
|
|
78
78
|
const entriesLimited = await dao.streamEntries(2).toArray();
|
|
79
79
|
// entriesLimited.sort()
|
|
80
80
|
// expect(entriesLimited).toEqual(testEntries.slice(0, 2))
|
|
81
81
|
expect(entriesLimited.length).toBe(2);
|
|
82
82
|
});
|
|
83
|
-
|
|
83
|
+
it('should delete the entries by ids', async () => {
|
|
84
84
|
await dao.deleteByIds(testIds);
|
|
85
85
|
const results = await dao.getByIds(testIds);
|
|
86
86
|
expect(results).toEqual([]);
|
|
@@ -88,19 +88,19 @@ export async function runCommonKeyValueDaoTest(db) {
|
|
|
88
88
|
if (support.increment) {
|
|
89
89
|
const id = 'nonExistingField';
|
|
90
90
|
const id2 = 'nonExistingField2';
|
|
91
|
-
|
|
91
|
+
it('should increment a missing key from 0', async () => {
|
|
92
92
|
const result = await dao.increment(id);
|
|
93
93
|
expect(result).toBe(1);
|
|
94
94
|
});
|
|
95
|
-
|
|
95
|
+
it('should increment an existing key by 1', async () => {
|
|
96
96
|
const result = await dao.increment(id);
|
|
97
97
|
expect(result).toBe(2);
|
|
98
98
|
});
|
|
99
|
-
|
|
99
|
+
it('should increment an existing key by the given amount', async () => {
|
|
100
100
|
const result = await dao.increment(id, 2);
|
|
101
101
|
expect(result).toBe(4);
|
|
102
102
|
});
|
|
103
|
-
|
|
103
|
+
it('should increment two keys in one batch', async () => {
|
|
104
104
|
const result = await dao.incrementBatch([
|
|
105
105
|
[id, 1],
|
|
106
106
|
[id2, 2],
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/db-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "10.
|
|
4
|
+
"version": "10.60.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@naturalcycles/js-lib": "^15",
|
|
7
7
|
"@naturalcycles/nodejs-lib": "^15"
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"directory": "packages/db-lib"
|
|
42
42
|
},
|
|
43
43
|
"engines": {
|
|
44
|
-
"node": ">=
|
|
44
|
+
"node": ">=26.12.1"
|
|
45
45
|
},
|
|
46
46
|
"description": "Lowest Common Denominator API to supported Databases",
|
|
47
47
|
"keywords": [
|
|
@@ -18,6 +18,10 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
|
18
18
|
|
|
19
19
|
async ping(): Promise<void> {}
|
|
20
20
|
|
|
21
|
+
clear(): void {
|
|
22
|
+
this.data = {}
|
|
23
|
+
}
|
|
24
|
+
|
|
21
25
|
async createTable(_table: string, _opt?: CommonDBCreateOptions): Promise<void> {}
|
|
22
26
|
|
|
23
27
|
async deleteByIds(table: string, ids: string[]): Promise<void> {
|
|
@@ -201,7 +201,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
|
|
|
201
201
|
|
|
202
202
|
if (emitSchemaFromDB) {
|
|
203
203
|
const schema = await db.getTableSchema(table)
|
|
204
|
-
await fs2.
|
|
204
|
+
await fs2.writePrettyJsonAsync(schemaFilePath, schema)
|
|
205
205
|
console.log(`>> ${grey(schemaFilePath)} saved (generated from DB)`)
|
|
206
206
|
}
|
|
207
207
|
|