@naturalcycles/db-lib 10.59.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.
@@ -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, test } = await import('vitest');
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
- test('ping', async () => {
23
+ it('should respond to ping', async () => {
24
24
  await db.ping();
25
25
  });
26
- test('createTable', async () => {
26
+ it('should create the table, dropping an existing one', async () => {
27
27
  await db.createTable(TEST_TABLE, { dropIfExists: true });
28
28
  });
29
- test('deleteByIds non existing', async () => {
29
+ it('should not throw when deleting ids that do not exist', async () => {
30
30
  await db.deleteByIds(TEST_TABLE, testIds);
31
31
  });
32
- test('getByIds should return empty', async () => {
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
- test('count should be 0', async () => {
37
- expect(await db.count(TEST_TABLE)).toBe(0);
38
- });
39
- test('saveBatch, then getByIds', async () => {
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
- test('count should be 3', async () => {
48
+ it('should count the saved entries', async () => {
47
49
  expect(await db.count(TEST_TABLE)).toBe(3);
48
50
  });
49
51
  }
50
- test('streamIds', async () => {
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
- test('streamIds limited', async () => {
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
- test('streamValues', async () => {
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
- test('streamValues limited', async () => {
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
- test('streamEntries', async () => {
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
- test('streamEntries limited', async () => {
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
- test('deleteByIds should clear', async () => {
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
- test('increment on a non-existing field should set the value to 1', async () => {
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
- test('increment on a existing field should increase the value by one', async () => {
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
- test('increment should increase the value by the specified amount', async () => {
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
- test('increment 2 ids at the same time', async () => {
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, test } = await import('vitest');
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
- test('ping', async () => {
27
+ it('should respond to ping', async () => {
28
28
  await dao.ping();
29
29
  });
30
- test('createTable', async () => {
30
+ it('should create the table, dropping an existing one', async () => {
31
31
  await dao.createTable({ dropIfExists: true });
32
32
  });
33
- test('deleteByIds non existing', async () => {
33
+ it('should not throw when deleting ids that do not exist', async () => {
34
34
  await dao.deleteByIds(testIds);
35
35
  });
36
- test('getByIds should return empty', async () => {
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
- test('saveBatch, then getByIds', async () => {
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
- test('streamIds', async () => {
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
- test('streamIds limited', async () => {
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
- test('streamValues', async () => {
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
- test('streamValues limited', async () => {
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
- test('streamEntries', async () => {
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
- test('streamEntries limited', async () => {
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
- test('deleteByIds should clear', async () => {
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
- test('increment on a non-existing field should set the value to 1', async () => {
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
- test('increment on a existing field should increase the value by one', async () => {
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
- test('increment should increase the value by the specified amount', async () => {
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
- test('increment 2 ids at the same time', async () => {
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.59.0",
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": ">=24.10.0"
44
+ "node": ">=26.12.1"
45
45
  },
46
46
  "description": "Lowest Common Denominator API to supported Databases",
47
47
  "keywords": [
@@ -27,6 +27,8 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
27
27
  ...commonDBFullSupport,
28
28
  transactions: false,
29
29
  increment: false,
30
+ // Cache hits are served regardless of readAt
31
+ timeMachine: false,
30
32
  }
31
33
 
32
34
  constructor(cfg: CacheDBCfg) {
@@ -44,6 +44,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
44
44
  createTable: false,
45
45
  transactions: false, // todo
46
46
  increment: false,
47
+ timeMachine: false,
47
48
  }
48
49
 
49
50
  constructor(cfg: FileDBCfg) {
@@ -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.writeJsonAsync(schemaFilePath, schema, { spaces: 2 })
204
+ await fs2.writePrettyJsonAsync(schemaFilePath, schema)
205
205
  console.log(`>> ${grey(schemaFilePath)} saved (generated from DB)`)
206
206
  }
207
207
 
@@ -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 { test, expect } = await import('vitest')
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
- test('ping', async () => {
46
- await db.ping()
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
- test('deleteByIds test items', async () => {
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
- test('runQuery(all), runQueryCount should return empty', async () => {
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
- test('getByIds(item1.id) should return empty', async () => {
77
- const [item1Loaded] = await db.getByIds(TEST_TABLE, [item1.id])
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
- test('getByIds([]) should return []', async () => {
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
- test('should allow to save and load null values', async () => {
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
- test('undefined values should not be saved/loaded', async () => {
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
- test('saveBatch UPDATE method should throw', async () => {
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
- test('saveBatch test items', async () => {
130
+ it('should save the test items', async () => {
141
131
  await db.saveBatch(TEST_TABLE, items)
142
132
  })
143
133
 
144
- test('saveBatch should throw on null id', async () => {
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
- test('saveBatch INSERT method should throw', async () => {
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
- test('saveBatch UPDATE method should pass', async () => {
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
- test('getByIds all items', async () => {
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
- test('runQuery(all) should return all items', async () => {
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
- test('query even=true', async () => {
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
- test('query order by k1 desc', async () => {
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
- test('projection query with only ids', async () => {
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
- test('projection query without ids', async () => {
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
- test('projection query empty fields (edge case)', async () => {
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
- test('runQueryCount should return 3', async () => {
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
- test('streamQuery all', async () => {
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
- test('getTables, getTableSchema (if supported)', async () => {
250
- const tables = await db.getTables()
251
- // console.log({ tables })
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
- test('deleteByQuery even=false', async () => {
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
- test('buffer values', async () => {
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
- test('transaction happy path', async () => {
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(expected, rows, quirks)
332
+ expectMatch(
333
+ expected,
334
+ _sortBy(rows, r => r.id),
335
+ quirks,
336
+ )
329
337
  })
330
338
 
331
- test('createTransaction happy path', async () => {
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(expected, rows, quirks)
351
+ expectMatch(
352
+ expected,
353
+ _sortBy(rows, r => r.id),
354
+ quirks,
355
+ )
344
356
  })
345
357
 
346
- test('transaction rollback', async () => {
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(expected, rows, quirks)
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
- test('patchByQuery', async () => {
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(expected, rows, quirks)
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
- test('incrementBatch', async () => {
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(expected, rows, quirks)
411
- })
412
- }
413
-
414
- if (support.queries) {
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