@naturalcycles/db-lib 10.0.2 → 10.1.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 +5 -5
- package/dist/adapter/cachedb/cache.db.js +8 -12
- package/dist/adapter/cachedb/cache.db.model.d.ts +2 -2
- package/dist/adapter/cachedb/cache.db.model.js +1 -2
- package/dist/adapter/cachedb/index.d.ts +2 -2
- package/dist/adapter/cachedb/index.js +2 -5
- package/dist/adapter/file/file.db.d.ts +5 -6
- package/dist/adapter/file/file.db.js +25 -27
- package/dist/adapter/file/file.db.model.d.ts +2 -2
- package/dist/adapter/file/file.db.model.js +1 -2
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +2 -2
- package/dist/adapter/file/inMemory.persistence.plugin.js +3 -7
- package/dist/adapter/file/index.d.ts +3 -3
- package/dist/adapter/file/index.js +3 -7
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +2 -2
- package/dist/adapter/file/localFile.persistence.plugin.js +11 -15
- package/dist/adapter/file/noop.persistence.plugin.d.ts +2 -2
- package/dist/adapter/file/noop.persistence.plugin.js +1 -5
- package/dist/adapter/inmemory/inMemory.db.d.ts +5 -4
- package/dist/adapter/inmemory/inMemory.db.js +43 -41
- package/dist/adapter/inmemory/inMemoryKeyValueDB.d.ts +2 -2
- package/dist/adapter/inmemory/inMemoryKeyValueDB.js +7 -11
- package/dist/adapter/inmemory/queryInMemory.d.ts +1 -1
- package/dist/adapter/inmemory/queryInMemory.js +4 -7
- package/dist/base.common.db.d.ts +6 -4
- package/dist/base.common.db.js +8 -9
- package/dist/cnst.js +2 -5
- package/dist/common.db.d.ts +8 -2
- package/dist/common.db.js +3 -6
- package/dist/commondao/common.dao.d.ts +11 -4
- package/dist/commondao/common.dao.js +120 -113
- package/dist/commondao/common.dao.model.d.ts +2 -2
- package/dist/commondao/common.dao.model.js +2 -5
- package/dist/db.model.d.ts +6 -1
- package/dist/db.model.js +4 -7
- package/dist/index.d.ts +17 -17
- package/dist/index.js +17 -20
- package/dist/kv/commonKeyValueDB.d.ts +1 -1
- package/dist/kv/commonKeyValueDB.js +1 -4
- package/dist/kv/commonKeyValueDao.d.ts +3 -3
- package/dist/kv/commonKeyValueDao.js +10 -14
- package/dist/kv/commonKeyValueDaoMemoCache.d.ts +1 -1
- package/dist/kv/commonKeyValueDaoMemoCache.js +4 -8
- package/dist/model.util.js +9 -15
- package/dist/pipeline/dbPipelineBackup.d.ts +2 -2
- package/dist/pipeline/dbPipelineBackup.js +27 -30
- package/dist/pipeline/dbPipelineCopy.d.ts +2 -2
- package/dist/pipeline/dbPipelineCopy.js +20 -23
- package/dist/pipeline/dbPipelineRestore.d.ts +2 -2
- package/dist/pipeline/dbPipelineRestore.js +27 -30
- package/dist/query/dbQuery.d.ts +3 -3
- package/dist/query/dbQuery.js +7 -12
- package/dist/testing/{dbTest.d.ts → commonDBTest.d.ts} +1 -1
- package/dist/testing/{dbTest.js → commonDBTest.js} +81 -62
- package/dist/testing/commonDaoTest.d.ts +3 -0
- package/dist/testing/{daoTest.js → commonDaoTest.js} +101 -38
- package/dist/testing/index.d.ts +7 -7
- package/dist/testing/index.js +6 -20
- package/dist/testing/keyValueDBTest.d.ts +1 -1
- package/dist/testing/keyValueDBTest.js +28 -31
- package/dist/testing/keyValueDaoTest.d.ts +1 -1
- package/dist/testing/keyValueDaoTest.js +8 -11
- package/dist/testing/test.model.js +30 -37
- package/dist/testing/timeSeriesTest.util.d.ts +1 -1
- package/dist/testing/timeSeriesTest.util.js +3 -6
- package/dist/timeseries/commonTimeSeriesDao.d.ts +1 -1
- package/dist/timeseries/commonTimeSeriesDao.js +5 -9
- package/dist/timeseries/timeSeries.model.d.ts +1 -1
- package/dist/timeseries/timeSeries.model.js +1 -2
- package/dist/transaction/dbTransaction.util.d.ts +3 -2
- package/dist/transaction/dbTransaction.util.js +2 -5
- package/dist/validation/index.d.ts +2 -2
- package/dist/validation/index.js +25 -28
- package/package.json +3 -3
- package/src/adapter/cachedb/cache.db.model.ts +2 -2
- package/src/adapter/cachedb/cache.db.ts +6 -6
- package/src/adapter/cachedb/index.ts +2 -2
- package/src/adapter/file/file.db.model.ts +2 -2
- package/src/adapter/file/file.db.ts +8 -6
- package/src/adapter/file/inMemory.persistence.plugin.ts +2 -2
- package/src/adapter/file/index.ts +3 -3
- package/src/adapter/file/localFile.persistence.plugin.ts +2 -2
- package/src/adapter/file/noop.persistence.plugin.ts +2 -2
- package/src/adapter/inmemory/inMemory.db.ts +15 -10
- package/src/adapter/inmemory/inMemoryKeyValueDB.ts +7 -3
- package/src/adapter/inmemory/queryInMemory.ts +1 -1
- package/src/base.common.db.ts +9 -5
- package/src/common.db.ts +10 -2
- package/src/commondao/common.dao.model.ts +2 -2
- package/src/commondao/common.dao.ts +20 -6
- package/src/db.model.ts +7 -1
- package/src/index.ts +17 -17
- package/src/kv/commonKeyValueDB.ts +1 -1
- package/src/kv/commonKeyValueDao.ts +3 -3
- package/src/kv/commonKeyValueDaoMemoCache.ts +1 -1
- package/src/pipeline/dbPipelineBackup.ts +2 -2
- package/src/pipeline/dbPipelineCopy.ts +3 -3
- package/src/pipeline/dbPipelineRestore.ts +2 -2
- package/src/query/dbQuery.ts +3 -3
- package/src/testing/{dbTest.ts → commonDBTest.ts} +34 -6
- package/src/testing/{daoTest.ts → commonDaoTest.ts} +89 -11
- package/src/testing/index.ts +7 -7
- package/src/testing/keyValueDBTest.ts +2 -2
- package/src/testing/keyValueDaoTest.ts +3 -3
- package/src/testing/timeSeriesTest.util.ts +1 -1
- package/src/timeseries/commonTimeSeriesDao.ts +2 -2
- package/src/timeseries/timeSeries.model.ts +1 -1
- package/src/transaction/dbTransaction.util.ts +3 -2
- package/src/validation/index.ts +8 -3
- package/dist/testing/daoTest.d.ts +0 -3
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
2
|
import type { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
|
-
import { BaseCommonDB } from '../../base.common.db';
|
|
4
|
-
import type { CommonDB, CommonDBSupport } from '../../common.db';
|
|
5
|
-
import type { RunQueryResult } from '../../db.model';
|
|
6
|
-
import type { DBQuery } from '../../query/dbQuery';
|
|
7
|
-
import type { CacheDBCfg, CacheDBCreateOptions, CacheDBOptions, CacheDBSaveOptions, CacheDBStreamOptions } from './cache.db.model';
|
|
3
|
+
import { BaseCommonDB } from '../../base.common.db.js';
|
|
4
|
+
import type { CommonDB, CommonDBSupport } from '../../common.db.js';
|
|
5
|
+
import type { RunQueryResult } from '../../db.model.js';
|
|
6
|
+
import type { DBQuery } from '../../query/dbQuery.js';
|
|
7
|
+
import type { CacheDBCfg, CacheDBCreateOptions, CacheDBOptions, CacheDBSaveOptions, CacheDBStreamOptions } from './cache.db.model.js';
|
|
8
8
|
/**
|
|
9
9
|
* CommonDB implementation that proxies requests to downstream CommonDB
|
|
10
10
|
* and does in-memory caching.
|
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
|
-
const base_common_db_1 = require("../../base.common.db");
|
|
7
|
-
const common_db_1 = require("../../common.db");
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { _isTruthy } from '@naturalcycles/js-lib';
|
|
3
|
+
import { BaseCommonDB } from '../../base.common.db.js';
|
|
4
|
+
import { commonDBFullSupport } from '../../common.db.js';
|
|
8
5
|
/**
|
|
9
6
|
* CommonDB implementation that proxies requests to downstream CommonDB
|
|
10
7
|
* and does in-memory caching.
|
|
11
8
|
*
|
|
12
9
|
* Queries always hit downstream (unless `onlyCache` is passed)
|
|
13
10
|
*/
|
|
14
|
-
class CacheDB extends
|
|
11
|
+
export class CacheDB extends BaseCommonDB {
|
|
15
12
|
support = {
|
|
16
|
-
...
|
|
13
|
+
...commonDBFullSupport,
|
|
17
14
|
transactions: false,
|
|
18
15
|
increment: false,
|
|
19
16
|
};
|
|
@@ -78,7 +75,7 @@ class CacheDB extends base_common_db_1.BaseCommonDB {
|
|
|
78
75
|
}
|
|
79
76
|
}
|
|
80
77
|
// return in right order
|
|
81
|
-
return ids.map(id => resultMap[id]).filter(
|
|
78
|
+
return ids.map(id => resultMap[id]).filter(_isTruthy);
|
|
82
79
|
}
|
|
83
80
|
async saveBatch(table, rows, opt = {}) {
|
|
84
81
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
@@ -147,7 +144,7 @@ class CacheDB extends base_common_db_1.BaseCommonDB {
|
|
|
147
144
|
return stream;
|
|
148
145
|
}
|
|
149
146
|
if (opt.skipCache || this.cfg.skipCache)
|
|
150
|
-
return
|
|
147
|
+
return Readable.from([]);
|
|
151
148
|
const stream = this.cfg.cacheDB.streamQuery(q, opt);
|
|
152
149
|
// if (this.cfg.logCached) {
|
|
153
150
|
// let count = 0
|
|
@@ -195,4 +192,3 @@ class CacheDB extends base_common_db_1.BaseCommonDB {
|
|
|
195
192
|
return updated || 0;
|
|
196
193
|
}
|
|
197
194
|
}
|
|
198
|
-
exports.CacheDB = CacheDB;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
|
-
import type { CommonDB } from '../../common.db';
|
|
3
|
-
import type { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions } from '../../db.model';
|
|
2
|
+
import type { CommonDB } from '../../common.db.js';
|
|
3
|
+
import type { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions } from '../../db.model.js';
|
|
4
4
|
export interface CacheDBCfg {
|
|
5
5
|
name: string;
|
|
6
6
|
cacheDB: CommonDB;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CacheDB } from './cache.db';
|
|
2
|
-
import type { CacheDBCfg, CacheDBCreateOptions, CacheDBOptions, CacheDBStreamOptions } from './cache.db.model';
|
|
1
|
+
import { CacheDB } from './cache.db.js';
|
|
2
|
+
import type { CacheDBCfg, CacheDBCreateOptions, CacheDBOptions, CacheDBStreamOptions } from './cache.db.model.js';
|
|
3
3
|
export type { CacheDBCfg, CacheDBCreateOptions, CacheDBOptions, CacheDBStreamOptions };
|
|
4
4
|
export { CacheDB };
|
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.CacheDB = void 0;
|
|
4
|
-
const cache_db_1 = require("./cache.db");
|
|
5
|
-
Object.defineProperty(exports, "CacheDB", { enumerable: true, get: function () { return cache_db_1.CacheDB; } });
|
|
1
|
+
import { CacheDB } from './cache.db.js';
|
|
2
|
+
export { CacheDB };
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import type { JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
2
|
import type { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
7
|
-
import type {
|
|
8
|
-
import type { FileDBCfg } from './file.db.model';
|
|
3
|
+
import { BaseCommonDB } from '../../base.common.db.js';
|
|
4
|
+
import type { CommonDB, CommonDBSupport } from '../../common.db.js';
|
|
5
|
+
import type { CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, DBSaveBatchOperation, RunQueryResult } from '../../db.model.js';
|
|
6
|
+
import type { DBQuery } from '../../query/dbQuery.js';
|
|
7
|
+
import type { FileDBCfg } from './file.db.model.js';
|
|
9
8
|
/**
|
|
10
9
|
* Provides barebone implementation for "whole file" based CommonDB.
|
|
11
10
|
* "whole file" means that the persistence layer doesn't allow any querying,
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const __1 = require("../..");
|
|
1
|
+
import { _assert, _by, _deepEquals, _filterUndefinedValues, _since, _sortBy, _sortObjectDeep, _stringMapValues, generateJsonSchemaFromData, localTime, } from '@naturalcycles/js-lib';
|
|
2
|
+
import { dimGrey, readableCreate } from '@naturalcycles/nodejs-lib';
|
|
3
|
+
import { BaseCommonDB } from '../../base.common.db.js';
|
|
4
|
+
import { commonDBFullSupport } from '../../common.db.js';
|
|
5
|
+
import { queryInMemory } from '../inmemory/queryInMemory.js';
|
|
7
6
|
/**
|
|
8
7
|
* Provides barebone implementation for "whole file" based CommonDB.
|
|
9
8
|
* "whole file" means that the persistence layer doesn't allow any querying,
|
|
@@ -14,9 +13,9 @@ const __1 = require("../..");
|
|
|
14
13
|
* Each load/query operation loads *whole* file from the persitence layer.
|
|
15
14
|
* Each save operation saves *whole* file to the persistence layer.
|
|
16
15
|
*/
|
|
17
|
-
class FileDB extends
|
|
16
|
+
export class FileDB extends BaseCommonDB {
|
|
18
17
|
support = {
|
|
19
|
-
...
|
|
18
|
+
...commonDBFullSupport,
|
|
20
19
|
bufferValues: false, // todo: implement
|
|
21
20
|
insertSaveMethod: false,
|
|
22
21
|
updateSaveMethod: false,
|
|
@@ -45,19 +44,19 @@ class FileDB extends __1.BaseCommonDB {
|
|
|
45
44
|
return tables;
|
|
46
45
|
}
|
|
47
46
|
async getByIds(table, ids, _opt) {
|
|
48
|
-
const byId =
|
|
47
|
+
const byId = _by(await this.loadFile(table), r => r.id);
|
|
49
48
|
return ids.map(id => byId[id]).filter(Boolean);
|
|
50
49
|
}
|
|
51
50
|
async saveBatch(table, rows, _opt) {
|
|
52
51
|
if (!rows.length)
|
|
53
52
|
return; // save some api calls
|
|
54
53
|
// 1. Load the whole file
|
|
55
|
-
const byId =
|
|
54
|
+
const byId = _by(await this.loadFile(table), r => r.id);
|
|
56
55
|
// 2. Merge with new data (using ids)
|
|
57
56
|
let saved = 0;
|
|
58
57
|
rows.forEach(r => {
|
|
59
|
-
|
|
60
|
-
if (!
|
|
58
|
+
_assert(r.id, 'FileDB: row.id is required');
|
|
59
|
+
if (!_deepEquals(byId[r.id], r)) {
|
|
61
60
|
byId[r.id] = r;
|
|
62
61
|
saved++;
|
|
63
62
|
}
|
|
@@ -65,19 +64,19 @@ class FileDB extends __1.BaseCommonDB {
|
|
|
65
64
|
// Only save if there are changed rows
|
|
66
65
|
if (saved > 0) {
|
|
67
66
|
// 3. Save the whole file
|
|
68
|
-
await this.saveFile(table,
|
|
67
|
+
await this.saveFile(table, _stringMapValues(byId));
|
|
69
68
|
}
|
|
70
69
|
}
|
|
71
70
|
async runQuery(q, _opt) {
|
|
72
71
|
return {
|
|
73
|
-
rows:
|
|
72
|
+
rows: queryInMemory(q, await this.loadFile(q.table)),
|
|
74
73
|
};
|
|
75
74
|
}
|
|
76
75
|
async runQueryCount(q, _opt) {
|
|
77
76
|
return (await this.loadFile(q.table)).length;
|
|
78
77
|
}
|
|
79
78
|
streamQuery(q, opt) {
|
|
80
|
-
const readable =
|
|
79
|
+
const readable = readableCreate();
|
|
81
80
|
void this.runQuery(q, opt).then(({ rows }) => {
|
|
82
81
|
rows.forEach(r => readable.push(r));
|
|
83
82
|
readable.push(null); // done
|
|
@@ -85,19 +84,19 @@ class FileDB extends __1.BaseCommonDB {
|
|
|
85
84
|
return readable;
|
|
86
85
|
}
|
|
87
86
|
async deleteByQuery(q, _opt) {
|
|
88
|
-
const byId =
|
|
87
|
+
const byId = _by(await this.loadFile(q.table), r => r.id);
|
|
89
88
|
let deleted = 0;
|
|
90
|
-
|
|
89
|
+
queryInMemory(q, _stringMapValues(byId)).forEach(r => {
|
|
91
90
|
delete byId[r.id];
|
|
92
91
|
deleted++;
|
|
93
92
|
});
|
|
94
93
|
if (deleted > 0) {
|
|
95
|
-
await this.saveFile(q.table,
|
|
94
|
+
await this.saveFile(q.table, _stringMapValues(byId));
|
|
96
95
|
}
|
|
97
96
|
return deleted;
|
|
98
97
|
}
|
|
99
98
|
async deleteByIds(table, ids, _opt) {
|
|
100
|
-
const byId =
|
|
99
|
+
const byId = _by(await this.loadFile(table), r => r.id);
|
|
101
100
|
let deleted = 0;
|
|
102
101
|
ids.forEach(id => {
|
|
103
102
|
if (!byId[id])
|
|
@@ -106,14 +105,14 @@ class FileDB extends __1.BaseCommonDB {
|
|
|
106
105
|
deleted++;
|
|
107
106
|
});
|
|
108
107
|
if (deleted > 0) {
|
|
109
|
-
await this.saveFile(table,
|
|
108
|
+
await this.saveFile(table, _stringMapValues(byId));
|
|
110
109
|
}
|
|
111
110
|
return deleted;
|
|
112
111
|
}
|
|
113
112
|
async getTableSchema(table) {
|
|
114
113
|
const rows = await this.loadFile(table);
|
|
115
114
|
return {
|
|
116
|
-
...
|
|
115
|
+
...generateJsonSchemaFromData(rows),
|
|
117
116
|
$id: `${table}.schema.json`,
|
|
118
117
|
};
|
|
119
118
|
}
|
|
@@ -146,14 +145,14 @@ class FileDB extends __1.BaseCommonDB {
|
|
|
146
145
|
// return new FileDBTransaction(this)
|
|
147
146
|
// }
|
|
148
147
|
sortRows(rows) {
|
|
149
|
-
rows = rows.map(r =>
|
|
148
|
+
rows = rows.map(r => _filterUndefinedValues(r));
|
|
150
149
|
if (this.cfg.sortOnSave) {
|
|
151
|
-
|
|
150
|
+
_sortBy(rows, r => r[this.cfg.sortOnSave.name], true);
|
|
152
151
|
if (this.cfg.sortOnSave.descending)
|
|
153
152
|
rows.reverse(); // mutates
|
|
154
153
|
}
|
|
155
154
|
if (this.cfg.sortObjects) {
|
|
156
|
-
return
|
|
155
|
+
return _sortObjectDeep(rows);
|
|
157
156
|
}
|
|
158
157
|
return rows;
|
|
159
158
|
}
|
|
@@ -161,15 +160,14 @@ class FileDB extends __1.BaseCommonDB {
|
|
|
161
160
|
if (this.cfg.logStarted) {
|
|
162
161
|
this.cfg.logger?.log(`>> ${op}`);
|
|
163
162
|
}
|
|
164
|
-
return
|
|
163
|
+
return localTime.nowUnixMillis();
|
|
165
164
|
}
|
|
166
165
|
logFinished(started, op) {
|
|
167
166
|
if (!this.cfg.logFinished)
|
|
168
167
|
return;
|
|
169
|
-
this.cfg.logger?.log(`<< ${op} ${
|
|
168
|
+
this.cfg.logger?.log(`<< ${op} ${dimGrey(`in ${_since(started)}`)}`);
|
|
170
169
|
}
|
|
171
170
|
}
|
|
172
|
-
exports.FileDB = FileDB;
|
|
173
171
|
// todo: get back and fix it
|
|
174
172
|
// Implementation is optimized for loading/saving _whole files_.
|
|
175
173
|
/*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
|
-
import type { DBSaveBatchOperation } from '../../db.model';
|
|
3
|
-
import type { DBQueryOrder } from '../../query/dbQuery';
|
|
2
|
+
import type { DBSaveBatchOperation } from '../../db.model.js';
|
|
3
|
+
import type { DBQueryOrder } from '../../query/dbQuery.js';
|
|
4
4
|
export interface FileDBPersistencePlugin {
|
|
5
5
|
ping: () => Promise<void>;
|
|
6
6
|
getTables: () => Promise<string[]>;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ObjectWithId, StringMap } from '@naturalcycles/js-lib';
|
|
2
|
-
import type { DBSaveBatchOperation } from '../../db.model';
|
|
3
|
-
import type { FileDBPersistencePlugin } from './file.db.model';
|
|
2
|
+
import type { DBSaveBatchOperation } from '../../db.model.js';
|
|
3
|
+
import type { FileDBPersistencePlugin } from './file.db.model.js';
|
|
4
4
|
/**
|
|
5
5
|
* Mostly useful for testing.
|
|
6
6
|
*/
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InMemoryPersistencePlugin = void 0;
|
|
4
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
1
|
+
import { _by } from '@naturalcycles/js-lib';
|
|
5
2
|
/**
|
|
6
3
|
* Mostly useful for testing.
|
|
7
4
|
*/
|
|
8
|
-
class InMemoryPersistencePlugin {
|
|
5
|
+
export class InMemoryPersistencePlugin {
|
|
9
6
|
data = {};
|
|
10
7
|
async ping() { }
|
|
11
8
|
async getTables() {
|
|
@@ -16,8 +13,7 @@ class InMemoryPersistencePlugin {
|
|
|
16
13
|
}
|
|
17
14
|
async saveFiles(ops) {
|
|
18
15
|
ops.forEach(op => {
|
|
19
|
-
this.data[op.table] =
|
|
16
|
+
this.data[op.table] = _by(op.rows, r => r.id);
|
|
20
17
|
});
|
|
21
18
|
}
|
|
22
19
|
}
|
|
23
|
-
exports.InMemoryPersistencePlugin = InMemoryPersistencePlugin;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FileDB } from './file.db';
|
|
2
|
-
import type { FileDBCfg, FileDBPersistencePlugin } from './file.db.model';
|
|
3
|
-
import { LocalFilePersistencePlugin } from './localFile.persistence.plugin';
|
|
1
|
+
import { FileDB } from './file.db.js';
|
|
2
|
+
import type { FileDBCfg, FileDBPersistencePlugin } from './file.db.model.js';
|
|
3
|
+
import { LocalFilePersistencePlugin } from './localFile.persistence.plugin.js';
|
|
4
4
|
export type { FileDBCfg, FileDBPersistencePlugin };
|
|
5
5
|
export { FileDB, LocalFilePersistencePlugin };
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const file_db_1 = require("./file.db");
|
|
5
|
-
Object.defineProperty(exports, "FileDB", { enumerable: true, get: function () { return file_db_1.FileDB; } });
|
|
6
|
-
const localFile_persistence_plugin_1 = require("./localFile.persistence.plugin");
|
|
7
|
-
Object.defineProperty(exports, "LocalFilePersistencePlugin", { enumerable: true, get: function () { return localFile_persistence_plugin_1.LocalFilePersistencePlugin; } });
|
|
1
|
+
import { FileDB } from './file.db.js';
|
|
2
|
+
import { LocalFilePersistencePlugin } from './localFile.persistence.plugin.js';
|
|
3
|
+
export { FileDB, LocalFilePersistencePlugin };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
|
-
import type { DBSaveBatchOperation } from '../../db.model';
|
|
3
|
-
import type { FileDBPersistencePlugin } from './file.db.model';
|
|
2
|
+
import type { DBSaveBatchOperation } from '../../db.model.js';
|
|
3
|
+
import type { FileDBPersistencePlugin } from './file.db.model.js';
|
|
4
4
|
export interface LocalFilePersistencePluginCfg {
|
|
5
5
|
/**
|
|
6
6
|
* @default ./tmp/localdb
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const node_stream_1 = require("node:stream");
|
|
5
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
|
-
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { pMap } from '@naturalcycles/js-lib';
|
|
3
|
+
import { _pipeline, fs2 } from '@naturalcycles/nodejs-lib';
|
|
7
4
|
/**
|
|
8
5
|
* Persists in local filesystem as ndjson.
|
|
9
6
|
*/
|
|
10
|
-
class LocalFilePersistencePlugin {
|
|
7
|
+
export class LocalFilePersistencePlugin {
|
|
11
8
|
constructor(cfg = {}) {
|
|
12
9
|
this.cfg = {
|
|
13
10
|
storagePath: './tmp/localdb',
|
|
@@ -18,26 +15,25 @@ class LocalFilePersistencePlugin {
|
|
|
18
15
|
cfg;
|
|
19
16
|
async ping() { }
|
|
20
17
|
async getTables() {
|
|
21
|
-
return (await
|
|
18
|
+
return (await fs2.readdirAsync(this.cfg.storagePath))
|
|
22
19
|
.filter(f => f.includes('.ndjson'))
|
|
23
20
|
.map(f => f.split('.ndjson')[0]);
|
|
24
21
|
}
|
|
25
22
|
async loadFile(table) {
|
|
26
|
-
await
|
|
23
|
+
await fs2.ensureDirAsync(this.cfg.storagePath);
|
|
27
24
|
const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`;
|
|
28
25
|
const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
|
|
29
|
-
if (!(await
|
|
26
|
+
if (!(await fs2.pathExistsAsync(filePath)))
|
|
30
27
|
return [];
|
|
31
|
-
return await
|
|
28
|
+
return await fs2.createReadStreamAsNDJSON(filePath).toArray();
|
|
32
29
|
}
|
|
33
30
|
async saveFiles(ops) {
|
|
34
|
-
await
|
|
31
|
+
await pMap(ops, async (op) => await this.saveFile(op.table, op.rows), { concurrency: 32 });
|
|
35
32
|
}
|
|
36
33
|
async saveFile(table, rows) {
|
|
37
|
-
await
|
|
34
|
+
await fs2.ensureDirAsync(this.cfg.storagePath);
|
|
38
35
|
const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`;
|
|
39
36
|
const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
|
|
40
|
-
await
|
|
37
|
+
await _pipeline([Readable.from(rows), ...fs2.createWriteStreamAsNDJSON(filePath)]);
|
|
41
38
|
}
|
|
42
39
|
}
|
|
43
|
-
exports.LocalFilePersistencePlugin = LocalFilePersistencePlugin;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
|
-
import type { DBSaveBatchOperation } from '../../db.model';
|
|
3
|
-
import type { FileDBPersistencePlugin } from './file.db.model';
|
|
2
|
+
import type { DBSaveBatchOperation } from '../../db.model.js';
|
|
3
|
+
import type { FileDBPersistencePlugin } from './file.db.model.js';
|
|
4
4
|
export declare class NoopPersistencePlugin implements FileDBPersistencePlugin {
|
|
5
5
|
ping(): Promise<void>;
|
|
6
6
|
getTables(): Promise<string[]>;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NoopPersistencePlugin = void 0;
|
|
4
|
-
class NoopPersistencePlugin {
|
|
1
|
+
export class NoopPersistencePlugin {
|
|
5
2
|
async ping() { }
|
|
6
3
|
async getTables() {
|
|
7
4
|
return [];
|
|
@@ -11,4 +8,3 @@ class NoopPersistencePlugin {
|
|
|
11
8
|
}
|
|
12
9
|
async saveFiles(_ops) { }
|
|
13
10
|
}
|
|
14
|
-
exports.NoopPersistencePlugin = NoopPersistencePlugin;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { AnyObjectWithId, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, StringMap } from '@naturalcycles/js-lib';
|
|
2
2
|
import type { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
|
-
import type { CommonDB, CommonDBSupport
|
|
4
|
-
import { CommonDBType } from '
|
|
5
|
-
import type { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, DBTransaction, RunQueryResult } from '../../db.model';
|
|
6
|
-
import type { DBQuery } from '../../query/dbQuery';
|
|
3
|
+
import type { CommonDB, CommonDBSupport } from '../../common.db.js';
|
|
4
|
+
import { CommonDBType } from '../../common.db.js';
|
|
5
|
+
import type { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBTransactionOptions, DBOperation, DBTransaction, DBTransactionFn, RunQueryResult } from '../../db.model.js';
|
|
6
|
+
import type { DBQuery } from '../../query/dbQuery.js';
|
|
7
7
|
export interface InMemoryDBCfg {
|
|
8
8
|
/**
|
|
9
9
|
* @default ''
|
|
@@ -74,6 +74,7 @@ export declare class InMemoryDB implements CommonDB {
|
|
|
74
74
|
runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
75
75
|
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): ReadableTyped<ROW>;
|
|
76
76
|
runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
|
|
77
|
+
createTransaction(opt?: CommonDBTransactionOptions): Promise<DBTransaction>;
|
|
77
78
|
incrementBatch(table: string, prop: string, incrementMap: StringMap<number>, _opt?: CommonDBOptions): Promise<StringMap<number>>;
|
|
78
79
|
/**
|
|
79
80
|
* Flushes all tables (all namespaces) at once.
|