@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
package/dist/query/dbQuery.js
CHANGED
|
@@ -80,11 +80,11 @@ export class DBQuery {
|
|
|
80
80
|
return this;
|
|
81
81
|
}
|
|
82
82
|
select(fieldNames) {
|
|
83
|
-
this._selectedFieldNames = fieldNames;
|
|
83
|
+
this._selectedFieldNames = fieldNames.slice();
|
|
84
84
|
return this;
|
|
85
85
|
}
|
|
86
86
|
groupBy(fieldNames) {
|
|
87
|
-
this._groupByFieldNames = fieldNames;
|
|
87
|
+
this._groupByFieldNames = fieldNames.slice();
|
|
88
88
|
return this;
|
|
89
89
|
}
|
|
90
90
|
distinct(distinct = true) {
|
|
@@ -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) {
|
|
@@ -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
|
}
|