@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
|
@@ -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],
|
|
@@ -11,13 +11,13 @@ export declare class CommonTimeSeriesDao {
|
|
|
11
11
|
ping(): Promise<void>;
|
|
12
12
|
getSeries(): Promise<string[]>;
|
|
13
13
|
save(series: string, tsMillis: number, value: number): Promise<void>;
|
|
14
|
-
saveBatch(series: string, dataPoints: TimeSeriesDataPoint[]): Promise<void>;
|
|
14
|
+
saveBatch(series: string, dataPoints: readonly TimeSeriesDataPoint[]): Promise<void>;
|
|
15
15
|
/**
|
|
16
16
|
* All ops are executed as a single CommonDB Transaction.
|
|
17
17
|
*/
|
|
18
|
-
commitTransaction(ops: TimeSeriesSaveBatchOp[]): Promise<void>;
|
|
18
|
+
commitTransaction(ops: readonly TimeSeriesSaveBatchOp[]): Promise<void>;
|
|
19
19
|
deleteById(series: string, tsMillis: number): Promise<void>;
|
|
20
|
-
deleteByIds(series: string, ids: number[]): Promise<void>;
|
|
20
|
+
deleteByIds(series: string, ids: readonly number[]): Promise<void>;
|
|
21
21
|
query(q: TimeSeriesQuery): Promise<TimeSeriesDataPoint[]>;
|
|
22
22
|
optimize(): Promise<void>;
|
|
23
23
|
}
|
|
@@ -23,7 +23,7 @@ export declare class FakeDBTransaction implements DBTransaction {
|
|
|
23
23
|
constructor(db: CommonDB);
|
|
24
24
|
commit(): Promise<void>;
|
|
25
25
|
rollback(): Promise<void>;
|
|
26
|
-
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
|
|
27
|
-
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
|
|
28
|
-
deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>;
|
|
26
|
+
getByIds<ROW extends ObjectWithId>(table: string, ids: readonly string[], opt?: CommonDBOptions): Promise<ROW[]>;
|
|
27
|
+
saveBatch<ROW extends ObjectWithId>(table: string, rows: readonly ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
|
|
28
|
+
deleteByIds(table: string, ids: readonly string[], opt?: CommonDBOptions): Promise<number>;
|
|
29
29
|
}
|
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.61.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": [
|
|
@@ -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) {
|
|
@@ -76,7 +78,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
76
78
|
|
|
77
79
|
override async getByIds<ROW extends ObjectWithId>(
|
|
78
80
|
table: string,
|
|
79
|
-
ids: string[],
|
|
81
|
+
ids: readonly string[],
|
|
80
82
|
opt: CacheDBSaveOptions<ROW> = {},
|
|
81
83
|
): Promise<ROW[]> {
|
|
82
84
|
const resultMap: StringMap<ROW> = {}
|
|
@@ -122,7 +124,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
122
124
|
|
|
123
125
|
override async saveBatch<ROW extends ObjectWithId>(
|
|
124
126
|
table: string,
|
|
125
|
-
rows: ROW[],
|
|
127
|
+
rows: readonly ROW[],
|
|
126
128
|
opt: CacheDBSaveOptions<ROW> = {},
|
|
127
129
|
): Promise<void> {
|
|
128
130
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
@@ -7,7 +7,7 @@ export interface FileDBPersistencePlugin {
|
|
|
7
7
|
ping: () => Promise<void>
|
|
8
8
|
getTables: () => Promise<string[]>
|
|
9
9
|
loadFile: <ROW extends ObjectWithId>(table: string) => Promise<ROW[]>
|
|
10
|
-
saveFiles: (ops: DBSaveBatchOperation<any>[]) => Promise<void>
|
|
10
|
+
saveFiles: (ops: readonly DBSaveBatchOperation<any>[]) => Promise<void>
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export interface FileDBCfg {
|
|
@@ -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) {
|
|
@@ -71,7 +72,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
71
72
|
|
|
72
73
|
override async getByIds<ROW extends ObjectWithId>(
|
|
73
74
|
table: string,
|
|
74
|
-
ids: string[],
|
|
75
|
+
ids: readonly string[],
|
|
75
76
|
_opt?: CommonDBOptions,
|
|
76
77
|
): Promise<ROW[]> {
|
|
77
78
|
const byId = _by(await this.loadFile<ROW>(table), r => r.id)
|
|
@@ -80,7 +81,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
80
81
|
|
|
81
82
|
override async saveBatch<ROW extends ObjectWithId>(
|
|
82
83
|
table: string,
|
|
83
|
-
rows: ROW[],
|
|
84
|
+
rows: readonly ROW[],
|
|
84
85
|
_opt?: CommonDBSaveOptions<ROW>,
|
|
85
86
|
): Promise<void> {
|
|
86
87
|
if (!rows.length) return // save some api calls
|
|
@@ -153,7 +154,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
153
154
|
|
|
154
155
|
override async deleteByIds(
|
|
155
156
|
table: string,
|
|
156
|
-
ids: string[],
|
|
157
|
+
ids: readonly string[],
|
|
157
158
|
_opt?: CommonDBOptions,
|
|
158
159
|
): Promise<number> {
|
|
159
160
|
const byId = _by(await this.loadFile(table), r => r.id)
|
|
@@ -189,7 +190,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
189
190
|
}
|
|
190
191
|
|
|
191
192
|
// wrapper, to handle logging, sorting rows before saving
|
|
192
|
-
async saveFile<ROW extends ObjectWithId>(table: string, _rows: ROW[]): Promise<void> {
|
|
193
|
+
async saveFile<ROW extends ObjectWithId>(table: string, _rows: readonly ROW[]): Promise<void> {
|
|
193
194
|
// if (!_rows.length) return // NO, it should be able to save file with 0 rows!
|
|
194
195
|
|
|
195
196
|
// Sort the rows, if needed
|
|
@@ -201,7 +202,9 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
201
202
|
this.logFinished(started, op)
|
|
202
203
|
}
|
|
203
204
|
|
|
204
|
-
async saveFiles<ROW extends ObjectWithId>(
|
|
205
|
+
async saveFiles<ROW extends ObjectWithId>(
|
|
206
|
+
ops: readonly DBSaveBatchOperation<ROW>[],
|
|
207
|
+
): Promise<void> {
|
|
205
208
|
if (!ops.length) return
|
|
206
209
|
const op =
|
|
207
210
|
`saveFiles ${ops.length} op(s):\n` + ops.map(o => `${o.table} (${o.rows.length})`).join('\n')
|
|
@@ -214,8 +217,8 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
214
217
|
// return new FileDBTransaction(this)
|
|
215
218
|
// }
|
|
216
219
|
|
|
217
|
-
sortRows<ROW extends ObjectWithId>(
|
|
218
|
-
rows =
|
|
220
|
+
sortRows<ROW extends ObjectWithId>(inputRows: readonly ROW[]): ROW[] {
|
|
221
|
+
const rows = inputRows.map(r => _filterUndefinedValues(r))
|
|
219
222
|
|
|
220
223
|
if (this.cfg.sortOnSave) {
|
|
221
224
|
_sortBy(rows, r => r[this.cfg.sortOnSave!.name as keyof ROW] as string, { mutate: true })
|
|
@@ -23,7 +23,7 @@ export class InMemoryPersistencePlugin implements FileDBPersistencePlugin {
|
|
|
23
23
|
return Object.values(this.data[table] || ({} as any))
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
async saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void> {
|
|
26
|
+
async saveFiles(ops: readonly DBSaveBatchOperation<any>[]): Promise<void> {
|
|
27
27
|
ops.forEach(op => {
|
|
28
28
|
this.data[op.table] = _by(op.rows, r => r.id)
|
|
29
29
|
})
|
|
@@ -49,11 +49,11 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
|
|
|
49
49
|
return await Pipeline.fromNDJsonFile<ROW>(filePath).toArray()
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
async saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void> {
|
|
52
|
+
async saveFiles(ops: readonly DBSaveBatchOperation<any>[]): Promise<void> {
|
|
53
53
|
await pMap(ops, async op => await this.saveFile(op.table, op.rows), { concurrency: 32 })
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
async saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void> {
|
|
56
|
+
async saveFile<ROW extends ObjectWithId>(table: string, rows: readonly ROW[]): Promise<void> {
|
|
57
57
|
await fs2.ensureDirAsync(this.cfg.storagePath)
|
|
58
58
|
const ext = `ndjson${this.cfg.zst ? '.zst' : ''}`
|
|
59
59
|
const filePath = `${this.cfg.storagePath}/${table}.${ext}`
|