@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,6 +7,7 @@ import type { FileDBPersistencePlugin } from './file.db.model.js';
|
|
|
7
7
|
export declare class InMemoryPersistencePlugin implements FileDBPersistencePlugin {
|
|
8
8
|
data: StringMap<StringMap<ObjectWithId>>;
|
|
9
9
|
ping(): Promise<void>;
|
|
10
|
+
clear(): void;
|
|
10
11
|
getTables(): Promise<string[]>;
|
|
11
12
|
loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>;
|
|
12
13
|
saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void>;
|
|
@@ -13,6 +13,7 @@ export declare class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
|
13
13
|
};
|
|
14
14
|
data: StringMap<StringMap<any>>;
|
|
15
15
|
ping(): Promise<void>;
|
|
16
|
+
clear(): void;
|
|
16
17
|
createTable(_table: string, _opt?: CommonDBCreateOptions): Promise<void>;
|
|
17
18
|
deleteByIds(table: string, ids: string[]): Promise<void>;
|
|
18
19
|
getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]>;
|
|
@@ -11,6 +11,9 @@ export class InMemoryKeyValueDB {
|
|
|
11
11
|
// data[table][id] => any (can be Buffer, or number)
|
|
12
12
|
data = {};
|
|
13
13
|
async ping() { }
|
|
14
|
+
clear() {
|
|
15
|
+
this.data = {};
|
|
16
|
+
}
|
|
14
17
|
async createTable(_table, _opt) { }
|
|
15
18
|
async deleteByIds(table, ids) {
|
|
16
19
|
this.data[table] ||= {};
|
|
@@ -51,7 +51,7 @@ export async function dbPipelineBackup(opt) {
|
|
|
51
51
|
// console.log(`>> ${grey(filePath)} started...`)
|
|
52
52
|
if (emitSchemaFromDB) {
|
|
53
53
|
const schema = await db.getTableSchema(table);
|
|
54
|
-
await fs2.
|
|
54
|
+
await fs2.writePrettyJsonAsync(schemaFilePath, schema);
|
|
55
55
|
console.log(`>> ${grey(schemaFilePath)} saved (generated from DB)`);
|
|
56
56
|
}
|
|
57
57
|
await db
|
|
@@ -7,56 +7,49 @@ import { DBQuery } from '../query/dbQuery.js';
|
|
|
7
7
|
import { createTestItemDBM, createTestItemsDBM, TEST_TABLE, testItemBMSchema, } from './test.model.js';
|
|
8
8
|
export async function runCommonDBTest(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 items = createTestItemsDBM(3);
|
|
13
13
|
_deepFreeze(items);
|
|
14
14
|
const item1 = items[0];
|
|
15
15
|
const queryAll = () => DBQuery.create(TEST_TABLE);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// CREATE TABLE, DROP
|
|
20
|
-
if (support.createTable) {
|
|
21
|
-
test('createTable, dropIfExists=true', async () => {
|
|
16
|
+
beforeAll(async () => {
|
|
17
|
+
// Tests in this suite are not isolated: a failed run can leave rows behind for the next run
|
|
18
|
+
if (support.createTable) {
|
|
22
19
|
await db.createTable(TEST_TABLE, testItemBMSchema.build(), { dropIfExists: true });
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
if (support.queries) {
|
|
20
|
+
}
|
|
26
21
|
// DELETE ALL initially
|
|
27
|
-
|
|
22
|
+
if (support.queries) {
|
|
28
23
|
const { rows } = await db.runQuery(queryAll().select(['id']));
|
|
29
24
|
await db.deleteByQuery(queryAll().filterIn('id', rows.map(i => i.id)));
|
|
30
|
-
}
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
afterAll(async () => {
|
|
28
|
+
// CLEAN UP
|
|
29
|
+
if (support.queries) {
|
|
30
|
+
await db.deleteByQuery(queryAll());
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
it('should respond to ping', async () => {
|
|
34
|
+
await db.ping();
|
|
35
|
+
});
|
|
36
|
+
if (support.queries) {
|
|
31
37
|
// QUERY empty
|
|
32
|
-
|
|
38
|
+
it('should return no rows and a count of 0 for an empty table', async () => {
|
|
33
39
|
expect((await db.runQuery(queryAll())).rows).toEqual([]);
|
|
34
40
|
expect(await db.runQueryCount(queryAll())).toBe(0);
|
|
35
41
|
});
|
|
36
42
|
}
|
|
37
43
|
// GET empty
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// console.log(a)
|
|
41
|
-
expect(item1Loaded).toBeUndefined();
|
|
44
|
+
it('should return an empty array for ids that are not saved', async () => {
|
|
45
|
+
expect(await db.getByIds(TEST_TABLE, [item1.id, 'abc', 'abcd'])).toEqual([]);
|
|
42
46
|
});
|
|
43
|
-
|
|
47
|
+
it('should return an empty array for getByIds without ids', async () => {
|
|
44
48
|
expect(await db.getByIds(TEST_TABLE, [])).toEqual([]);
|
|
45
49
|
});
|
|
46
|
-
test('getByIds(...) should return empty', async () => {
|
|
47
|
-
expect(await db.getByIds(TEST_TABLE, ['abc', 'abcd'])).toEqual([]);
|
|
48
|
-
});
|
|
49
|
-
// TimeMachine
|
|
50
|
-
if (support.timeMachine) {
|
|
51
|
-
test('getByIds(...) 10 minutes ago should return []', async () => {
|
|
52
|
-
expect(await db.getByIds(TEST_TABLE, [item1.id, 'abc'], {
|
|
53
|
-
readAt: localTime.now().minus(10, 'minute').unix,
|
|
54
|
-
})).toEqual([]);
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
50
|
// SAVE
|
|
58
51
|
if (support.nullValues) {
|
|
59
|
-
|
|
52
|
+
it('should allow to save and load null values', async () => {
|
|
60
53
|
const item3 = {
|
|
61
54
|
...createTestItemDBM(3),
|
|
62
55
|
k2: null,
|
|
@@ -69,7 +62,7 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
69
62
|
});
|
|
70
63
|
}
|
|
71
64
|
if (db.dbType === CommonDBType.document) {
|
|
72
|
-
|
|
65
|
+
it('should not store properties with undefined values', async () => {
|
|
73
66
|
const item3 = {
|
|
74
67
|
...createTestItemDBM(3),
|
|
75
68
|
k2: undefined,
|
|
@@ -85,40 +78,48 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
85
78
|
});
|
|
86
79
|
}
|
|
87
80
|
if (support.updateSaveMethod) {
|
|
88
|
-
|
|
81
|
+
it('should throw on saveBatch with the update method when rows do not exist', async () => {
|
|
89
82
|
await expect(db.saveBatch(TEST_TABLE, items, { saveMethod: 'update' })).rejects.toThrow();
|
|
90
83
|
});
|
|
91
84
|
}
|
|
92
|
-
|
|
85
|
+
it('should save the test items', async () => {
|
|
93
86
|
await db.saveBatch(TEST_TABLE, items);
|
|
94
87
|
});
|
|
95
|
-
|
|
88
|
+
it('should throw on saveBatch of a row with a null id', async () => {
|
|
96
89
|
await expect(db.saveBatch(TEST_TABLE, [{ ...item1, id: null }])).rejects.toThrow();
|
|
97
90
|
});
|
|
98
91
|
if (support.insertSaveMethod) {
|
|
99
|
-
|
|
92
|
+
it('should throw on saveBatch with the insert method when rows exist', async () => {
|
|
100
93
|
await expect(db.saveBatch(TEST_TABLE, items, { saveMethod: 'insert' })).rejects.toThrow();
|
|
101
94
|
});
|
|
102
95
|
}
|
|
103
96
|
if (support.updateSaveMethod) {
|
|
104
|
-
|
|
97
|
+
it('should save with the update method when rows exist', async () => {
|
|
105
98
|
await db.saveBatch(TEST_TABLE, items, { saveMethod: 'update' });
|
|
106
99
|
});
|
|
107
100
|
}
|
|
108
101
|
// GET not empty
|
|
109
|
-
|
|
102
|
+
it('should return all saved items by ids, skipping unknown ids', async () => {
|
|
110
103
|
const rows = await db.getByIds(TEST_TABLE, items.map(i => i.id).concat('abcd'));
|
|
111
104
|
expectMatch(items, _sortBy(rows, r => r.id), quirks);
|
|
112
105
|
});
|
|
106
|
+
// TimeMachine
|
|
107
|
+
if (support.timeMachine) {
|
|
108
|
+
it('should not return the saved items when reading 10 minutes back in time', async () => {
|
|
109
|
+
const ids = items.map(i => i.id);
|
|
110
|
+
const readAt = localTime.now().minus(10, 'minute').unix;
|
|
111
|
+
expect(await db.getByIds(TEST_TABLE, ids, { readAt })).toEqual([]);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
113
114
|
// QUERY
|
|
114
115
|
if (support.queries) {
|
|
115
|
-
|
|
116
|
+
it('should return all items from a query without conditions', async () => {
|
|
116
117
|
let { rows } = await db.runQuery(queryAll());
|
|
117
118
|
rows = _sortBy(rows, r => r.id); // because query doesn't specify order here
|
|
118
119
|
expectMatch(items, rows, quirks);
|
|
119
120
|
});
|
|
120
121
|
if (support.dbQueryFilter) {
|
|
121
|
-
|
|
122
|
+
it('should filter rows by equality', async () => {
|
|
122
123
|
const q = new DBQuery(TEST_TABLE).filter('even', '==', true);
|
|
123
124
|
let { rows } = await db.runQuery(q);
|
|
124
125
|
if (!support.dbQueryOrder)
|
|
@@ -127,58 +128,62 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
127
128
|
});
|
|
128
129
|
}
|
|
129
130
|
if (support.dbQueryOrder) {
|
|
130
|
-
|
|
131
|
+
it('should order rows descending', async () => {
|
|
131
132
|
const q = new DBQuery(TEST_TABLE).order('k1', true);
|
|
132
133
|
const { rows } = await db.runQuery(q);
|
|
133
134
|
expectMatch(items.toReversed(), rows, quirks);
|
|
134
135
|
});
|
|
135
136
|
}
|
|
136
137
|
if (support.dbQuerySelectFields) {
|
|
137
|
-
|
|
138
|
+
it('should select only the id', async () => {
|
|
138
139
|
const q = new DBQuery(TEST_TABLE).select(['id']);
|
|
139
140
|
let { rows } = await db.runQuery(q);
|
|
140
141
|
rows = _sortBy(rows, r => r.id); // cause order is not specified
|
|
141
142
|
expectMatch(items.map(item => _pick(item, ['id'])), rows, quirks);
|
|
142
143
|
});
|
|
143
|
-
|
|
144
|
+
it('should select a property without the id', async () => {
|
|
144
145
|
const q = new DBQuery(TEST_TABLE).select(['k1']);
|
|
145
146
|
let { rows } = await db.runQuery(q);
|
|
146
147
|
rows = _sortBy(rows, r => r.k1); // cause order is not specified
|
|
147
148
|
expectMatch(items.map(item => _pick(item, ['k1'])), rows, quirks);
|
|
148
149
|
});
|
|
149
|
-
|
|
150
|
+
it('should return empty rows when selecting no properties', async () => {
|
|
150
151
|
const q = new DBQuery(TEST_TABLE).select([]);
|
|
151
152
|
const { rows } = await db.runQuery(q);
|
|
152
153
|
expectMatch(items.map(() => ({})), rows, quirks);
|
|
153
154
|
});
|
|
154
155
|
}
|
|
155
|
-
|
|
156
|
+
it('should count all saved items', async () => {
|
|
156
157
|
expect(await db.runQueryCount(new DBQuery(TEST_TABLE))).toBe(3);
|
|
157
158
|
});
|
|
158
159
|
}
|
|
159
160
|
// STREAM
|
|
160
161
|
if (support.streaming) {
|
|
161
|
-
|
|
162
|
+
it('should stream all items', async () => {
|
|
162
163
|
let rows = await db.streamQuery(queryAll()).toArray();
|
|
163
164
|
rows = _sortBy(rows, r => r.id); // cause order is not specified in DBQuery
|
|
164
165
|
expectMatch(items, rows, quirks);
|
|
165
166
|
});
|
|
166
167
|
}
|
|
167
168
|
// getTables
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
//
|
|
171
|
-
|
|
169
|
+
it('should resolve getTables to an array', async () => {
|
|
170
|
+
// Implementations differ in what they list (prefixed names, lazily updated stats, nothing),
|
|
171
|
+
// so only the shape is part of the shared contract
|
|
172
|
+
await expect(db.getTables()).resolves.toBeInstanceOf(Array);
|
|
173
|
+
});
|
|
174
|
+
if (support.tableSchemas) {
|
|
175
|
+
it('should return a schema $id matching each listed table', async () => {
|
|
176
|
+
const tables = await db.getTables();
|
|
172
177
|
await pMap(tables, async (table) => {
|
|
173
178
|
const schema = await db.getTableSchema(table);
|
|
174
179
|
// console.log(schema)
|
|
175
180
|
expect(schema.$id).toBe(`${table}.schema.json`);
|
|
176
181
|
});
|
|
177
|
-
}
|
|
178
|
-
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
179
184
|
// DELETE BY
|
|
180
185
|
if (support.queries && support.dbQueryFilter) {
|
|
181
|
-
|
|
186
|
+
it('should delete rows by a filter and return the deleted count', async () => {
|
|
182
187
|
const q = new DBQuery(TEST_TABLE).filter('even', '==', false);
|
|
183
188
|
const deleted = await db.deleteByQuery(q);
|
|
184
189
|
expect(deleted).toBe(items.filter(item => !item.even).length);
|
|
@@ -187,7 +192,7 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
187
192
|
}
|
|
188
193
|
// BUFFER
|
|
189
194
|
if (support.bufferValues) {
|
|
190
|
-
|
|
195
|
+
it('should save and load Buffer values', async () => {
|
|
191
196
|
const s = 'helloWorld 1';
|
|
192
197
|
const b1 = Buffer.from(s);
|
|
193
198
|
const item = {
|
|
@@ -220,7 +225,7 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
220
225
|
await db.saveBatch(TEST_TABLE, itemsToSave);
|
|
221
226
|
return itemsToSave;
|
|
222
227
|
}
|
|
223
|
-
|
|
228
|
+
it('should commit the saves and deletes made in runInTransaction', async () => {
|
|
224
229
|
// cleanup
|
|
225
230
|
await db.deleteByQuery(queryAll());
|
|
226
231
|
// saveBatch [item1, 2, 3]
|
|
@@ -234,9 +239,9 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
234
239
|
});
|
|
235
240
|
const { rows } = await db.runQuery(queryAll());
|
|
236
241
|
const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
|
|
237
|
-
expectMatch(expected, rows, quirks);
|
|
242
|
+
expectMatch(expected, _sortBy(rows, r => r.id), quirks);
|
|
238
243
|
});
|
|
239
|
-
|
|
244
|
+
it('should commit the saves and deletes made in a created transaction', async () => {
|
|
240
245
|
// cleanup
|
|
241
246
|
await db.deleteByQuery(queryAll());
|
|
242
247
|
const tx = await db.createTransaction();
|
|
@@ -246,9 +251,9 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
246
251
|
await tx.commit();
|
|
247
252
|
const { rows } = await db.runQuery(queryAll());
|
|
248
253
|
const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
|
|
249
|
-
expectMatch(expected, rows, quirks);
|
|
254
|
+
expectMatch(expected, _sortBy(rows, r => r.id), quirks);
|
|
250
255
|
});
|
|
251
|
-
|
|
256
|
+
it('should roll back runInTransaction when an operation fails', async () => {
|
|
252
257
|
const expected = await prepare();
|
|
253
258
|
let err;
|
|
254
259
|
try {
|
|
@@ -263,11 +268,11 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
263
268
|
}
|
|
264
269
|
expect(err).toBeDefined();
|
|
265
270
|
const { rows } = await db.runQuery(queryAll());
|
|
266
|
-
expectMatch(expected, rows, quirks);
|
|
271
|
+
expectMatch(expected, _sortBy(rows, r => r.id), quirks);
|
|
267
272
|
});
|
|
268
273
|
}
|
|
269
274
|
if (support.patchByQuery) {
|
|
270
|
-
|
|
275
|
+
it('should patch the rows matching a query', async () => {
|
|
271
276
|
// cleanup, reset initial data
|
|
272
277
|
await db.deleteByQuery(queryAll());
|
|
273
278
|
await db.saveBatch(TEST_TABLE, items);
|
|
@@ -283,11 +288,11 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
283
288
|
}
|
|
284
289
|
return r;
|
|
285
290
|
});
|
|
286
|
-
expectMatch(expected, rows, quirks);
|
|
291
|
+
expectMatch(expected, _sortBy(rows, r => r.id), quirks);
|
|
287
292
|
});
|
|
288
293
|
}
|
|
289
294
|
if (support.increment) {
|
|
290
|
-
|
|
295
|
+
it('should increment a property by a different amount per id', async () => {
|
|
291
296
|
// cleanup, reset initial data
|
|
292
297
|
await db.deleteByQuery(queryAll());
|
|
293
298
|
await db.saveBatch(TEST_TABLE, items);
|
|
@@ -302,13 +307,7 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
302
307
|
}
|
|
303
308
|
return r;
|
|
304
309
|
});
|
|
305
|
-
expectMatch(expected, rows, quirks);
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
if (support.queries) {
|
|
309
|
-
test('cleanup', async () => {
|
|
310
|
-
// CLEAN UP
|
|
311
|
-
await db.deleteByQuery(queryAll());
|
|
310
|
+
expectMatch(expected, _sortBy(rows, r => r.id), quirks);
|
|
312
311
|
});
|
|
313
312
|
}
|
|
314
313
|
function expectMatch(expected, actual, quirks) {
|