@naturalcycles/db-lib 8.21.3 → 8.24.1
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 +3 -5
- package/dist/adapter/cachedb/cache.db.js +16 -14
- package/dist/adapter/cachedb/cache.db.model.d.ts +9 -1
- package/dist/adapter/file/file.db.d.ts +2 -2
- package/dist/adapter/file/file.db.js +4 -3
- package/dist/adapter/file/file.db.model.d.ts +7 -1
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +2 -2
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +2 -1
- package/dist/adapter/file/noop.persistence.plugin.d.ts +2 -1
- package/dist/adapter/inmemory/inMemory.db.d.ts +7 -2
- package/dist/adapter/inmemory/inMemory.db.js +8 -7
- package/dist/adapter/inmemory/queryInMemory.d.ts +1 -1
- package/dist/base.common.db.d.ts +2 -2
- package/dist/common.db.d.ts +2 -2
- package/dist/commondao/common.dao.d.ts +10 -3
- package/dist/commondao/common.dao.js +49 -12
- package/dist/commondao/common.dao.model.d.ts +19 -2
- package/dist/db.model.d.ts +1 -13
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -2
- package/dist/model.util.d.ts +1 -1
- package/dist/pipeline/dbPipelineBackup.js +0 -1
- package/dist/query/dbQuery.d.ts +5 -3
- package/dist/transaction/dbTransaction.d.ts +2 -1
- package/dist/validation/index.d.ts +3 -3
- package/package.json +2 -2
- package/readme.md +1 -6
- package/src/adapter/cachedb/cache.db.model.ts +10 -6
- package/src/adapter/cachedb/cache.db.ts +29 -18
- package/src/adapter/file/file.db.model.ts +8 -1
- package/src/adapter/file/file.db.ts +8 -11
- package/src/adapter/file/inMemory.persistence.plugin.ts +2 -2
- package/src/adapter/file/localFile.persistence.plugin.ts +2 -2
- package/src/adapter/file/noop.persistence.plugin.ts +2 -1
- package/src/adapter/inmemory/inMemory.db.ts +22 -9
- package/src/adapter/inmemory/queryInMemory.ts +1 -2
- package/src/base.common.db.ts +2 -2
- package/src/common.db.ts +1 -2
- package/src/commondao/common.dao.model.ts +21 -7
- package/src/commondao/common.dao.ts +62 -16
- package/src/db.model.ts +1 -16
- package/src/index.ts +0 -10
- package/src/model.util.ts +1 -1
- package/src/pipeline/dbPipelineBackup.ts +0 -2
- package/src/query/dbQuery.ts +5 -3
- package/src/timeseries/commonTimeSeriesDao.ts +1 -1
- package/src/transaction/dbTransaction.ts +2 -1
- package/src/transaction/dbTransaction.util.ts +2 -2
- package/CHANGELOG.md +0 -1327
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CommonKeyValueDao = exports.commitDBTransactionSimple = exports.mergeDBOperations = exports.RunnableDBTransaction = exports.DBTransaction = exports.BaseCommonDB = exports.DBLibError = exports.getDB = exports.dbPipelineCopy = exports.dbPipelineRestore = exports.dbPipelineBackup = exports.deserializeJsonField = exports.serializeJsonField = exports.queryInMemory = exports.InMemoryKeyValueDB = exports.InMemoryDB = exports.
|
|
3
|
+
exports.CommonKeyValueDao = exports.commitDBTransactionSimple = exports.mergeDBOperations = exports.RunnableDBTransaction = exports.DBTransaction = exports.BaseCommonDB = exports.DBLibError = exports.getDB = exports.dbPipelineCopy = exports.dbPipelineRestore = exports.dbPipelineBackup = exports.deserializeJsonField = exports.serializeJsonField = exports.queryInMemory = exports.InMemoryKeyValueDB = exports.InMemoryDB = exports.createdUpdatedIdFields = exports.createdUpdatedFields = exports.CommonDao = exports.DBModelType = exports.DBRelation = exports.CommonDaoLogLevel = exports.RunnableDBQuery = exports.dbQueryFilterOperatorValues = exports.DBQuery = void 0;
|
|
4
4
|
const inMemory_db_1 = require("./adapter/inmemory/inMemory.db");
|
|
5
5
|
Object.defineProperty(exports, "InMemoryDB", { enumerable: true, get: function () { return inMemory_db_1.InMemoryDB; } });
|
|
6
6
|
const inMemoryKeyValueDB_1 = require("./adapter/inmemory/inMemoryKeyValueDB");
|
|
@@ -26,7 +26,6 @@ const model_util_1 = require("./model.util");
|
|
|
26
26
|
Object.defineProperty(exports, "createdUpdatedFields", { enumerable: true, get: function () { return model_util_1.createdUpdatedFields; } });
|
|
27
27
|
Object.defineProperty(exports, "createdUpdatedIdFields", { enumerable: true, get: function () { return model_util_1.createdUpdatedIdFields; } });
|
|
28
28
|
Object.defineProperty(exports, "deserializeJsonField", { enumerable: true, get: function () { return model_util_1.deserializeJsonField; } });
|
|
29
|
-
Object.defineProperty(exports, "idField", { enumerable: true, get: function () { return model_util_1.idField; } });
|
|
30
29
|
Object.defineProperty(exports, "serializeJsonField", { enumerable: true, get: function () { return model_util_1.serializeJsonField; } });
|
|
31
30
|
const dbPipelineBackup_1 = require("./pipeline/dbPipelineBackup");
|
|
32
31
|
Object.defineProperty(exports, "dbPipelineBackup", { enumerable: true, get: function () { return dbPipelineBackup_1.dbPipelineBackup; } });
|
package/dist/model.util.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CreatedUpdated, CreatedUpdatedId, ObjectWithId } from '
|
|
1
|
+
import { CreatedUpdated, CreatedUpdatedId, ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
2
|
export declare function createdUpdatedFields(existingObject?: Partial<CreatedUpdated> | null): CreatedUpdated;
|
|
3
3
|
export declare function createdUpdatedIdFields(existingObject?: Partial<CreatedUpdatedId> | null): CreatedUpdatedId;
|
|
4
4
|
export declare function idField(existingObject?: Partial<CreatedUpdatedId> | null): ObjectWithId;
|
|
@@ -8,7 +8,6 @@ const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
|
|
|
8
8
|
const time_lib_1 = require("@naturalcycles/time-lib");
|
|
9
9
|
const fs = require("fs-extra");
|
|
10
10
|
const index_1 = require("../index");
|
|
11
|
-
// const log = Debug('nc:db-lib:pipeline')
|
|
12
11
|
/**
|
|
13
12
|
* Pipeline from input stream(s) to a NDJSON file (optionally gzipped).
|
|
14
13
|
* File is overwritten (by default).
|
package/dist/query/dbQuery.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { AsyncMapper, Saved } from '@naturalcycles/js-lib';
|
|
1
|
+
import { AnyObjectWithId, ObjectWithId, AsyncMapper, Saved } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDaoOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions } from '..';
|
|
4
4
|
import { CommonDao } from '../commondao/common.dao';
|
|
5
|
-
import {
|
|
5
|
+
import { RunQueryResult } from '../db.model';
|
|
6
6
|
/**
|
|
7
7
|
* Modeled after Firestore operators (WhereFilterOp type)
|
|
8
8
|
*
|
|
@@ -99,5 +99,7 @@ export declare class RunnableDBQuery<BM extends Partial<ObjectWithId>, DBM exten
|
|
|
99
99
|
queryIds(opt?: CommonDaoOptions): Promise<string[]>;
|
|
100
100
|
streamQueryIds(opt?: CommonDaoStreamOptions): ReadableTyped<string>;
|
|
101
101
|
streamQueryIdsForEach(mapper: AsyncMapper<string, void>, opt?: CommonDaoStreamForEachOptions<string>): Promise<void>;
|
|
102
|
-
deleteByQuery(opt?:
|
|
102
|
+
deleteByQuery(opt?: CommonDaoStreamForEachOptions<DBM> & {
|
|
103
|
+
stream?: boolean;
|
|
104
|
+
}): Promise<number>;
|
|
103
105
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { AnyObjectWithId, ObjectWithId } from '@naturalcycles/js-lib';
|
|
1
2
|
import type { CommonDB } from '../common.db';
|
|
2
|
-
import type {
|
|
3
|
+
import type { CommonDBSaveOptions, DBOperation } from '../db.model';
|
|
3
4
|
/**
|
|
4
5
|
* Convenience class that stores the list of DBOperations and provides a fluent API to add them.
|
|
5
6
|
*/
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { CommonDBOptions, CommonDBSaveOptions } from '../db.model';
|
|
2
2
|
import { DBQuery, DBQueryFilter, DBQueryOrder } from '../query/dbQuery';
|
|
3
3
|
export declare const commonDBOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBOptions, CommonDBOptions>;
|
|
4
|
-
export declare const commonDBSaveOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBSaveOptions<import("
|
|
4
|
+
export declare const commonDBSaveOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBSaveOptions<import("@naturalcycles/js-lib").AnyObjectWithId>, CommonDBSaveOptions<import("@naturalcycles/js-lib").AnyObjectWithId>>;
|
|
5
5
|
export declare const dbQueryFilterOperatorSchema: import("@naturalcycles/nodejs-lib/dist/validation/joi/string.extensions").ExtendedStringSchema;
|
|
6
|
-
export declare const dbQueryFilterSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryFilter<import("
|
|
7
|
-
export declare const dbQueryOrderSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryOrder<import("
|
|
6
|
+
export declare const dbQueryFilterSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId>, DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId>>;
|
|
7
|
+
export declare const dbQueryOrderSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId>, DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId>>;
|
|
8
8
|
export declare const dbQuerySchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQuery<any>, DBQuery<any>>;
|
package/package.json
CHANGED
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"url": "https://github.com/NaturalCycles/db-lib"
|
|
41
41
|
},
|
|
42
42
|
"engines": {
|
|
43
|
-
"node": ">=
|
|
43
|
+
"node": ">=14.15"
|
|
44
44
|
},
|
|
45
|
-
"version": "8.
|
|
45
|
+
"version": "8.24.1",
|
|
46
46
|
"description": "Lowest Common Denominator API to supported Databases",
|
|
47
47
|
"keywords": [
|
|
48
48
|
"db",
|
package/readme.md
CHANGED
|
@@ -295,11 +295,6 @@ Passing empty array will actually return an array of empty objects (documented e
|
|
|
295
295
|
.select(['id']) //=> [ { id: 'id1' }, { id: 'id2' }, ... ]
|
|
296
296
|
```
|
|
297
297
|
|
|
298
|
-
# DEBUG namespaces
|
|
299
|
-
|
|
300
|
-
- `nc:db-lib:commondao`
|
|
301
|
-
- `nc:db-lib:inmemorydb`
|
|
302
|
-
|
|
303
298
|
# Exports
|
|
304
299
|
|
|
305
300
|
- `/` root
|
|
@@ -315,6 +310,6 @@ Passing empty array will actually return an array of empty objects (documented e
|
|
|
315
310
|
# Packaging
|
|
316
311
|
|
|
317
312
|
- `engines.node >= LTS`
|
|
318
|
-
- `main: dist/index.js`: commonjs,
|
|
313
|
+
- `main: dist/index.js`: commonjs, es2020
|
|
319
314
|
- `types: dist/index.d.ts`: typescript types
|
|
320
315
|
- `/src` folder with source `*.ts` files included
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
+
import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'
|
|
1
2
|
import { CommonDB } from '../../common.db'
|
|
2
|
-
import {
|
|
3
|
-
CommonDBCreateOptions,
|
|
4
|
-
CommonDBSaveOptions,
|
|
5
|
-
CommonDBStreamOptions,
|
|
6
|
-
ObjectWithId,
|
|
7
|
-
} from '../../db.model'
|
|
3
|
+
import { CommonDBCreateOptions, CommonDBSaveOptions, CommonDBStreamOptions } from '../../db.model'
|
|
8
4
|
|
|
9
5
|
export interface CacheDBCfg {
|
|
10
6
|
name: string
|
|
@@ -42,6 +38,14 @@ export interface CacheDBCfg {
|
|
|
42
38
|
* @default false
|
|
43
39
|
*/
|
|
44
40
|
logDownstream?: boolean
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Pass noopLogger (or undefined) to skip logging completely.
|
|
44
|
+
*
|
|
45
|
+
* Defaults to `console` in dev.
|
|
46
|
+
* Default to noop in AppEngine.
|
|
47
|
+
*/
|
|
48
|
+
logger?: CommonLogger
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
export interface CacheDBOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { Readable } from 'stream'
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
JsonSchemaObject,
|
|
4
|
+
JsonSchemaRootObject,
|
|
5
|
+
ObjectWithId,
|
|
6
|
+
StringMap,
|
|
7
|
+
} from '@naturalcycles/js-lib'
|
|
4
8
|
import { BaseCommonDB } from '../../base.common.db'
|
|
5
9
|
import { CommonDB } from '../../common.db'
|
|
6
|
-
import {
|
|
10
|
+
import { RunQueryResult } from '../../db.model'
|
|
7
11
|
import { DBQuery } from '../../query/dbQuery'
|
|
8
12
|
import {
|
|
9
13
|
CacheDBCfg,
|
|
@@ -12,6 +16,8 @@ import {
|
|
|
12
16
|
CacheDBStreamOptions,
|
|
13
17
|
} from './cache.db.model'
|
|
14
18
|
|
|
19
|
+
const isGAE = !!process.env['GAE_INSTANCE']
|
|
20
|
+
|
|
15
21
|
/**
|
|
16
22
|
* CommonDB implementation that proxies requests to downstream CommonDB
|
|
17
23
|
* and does in-memory caching.
|
|
@@ -19,12 +25,15 @@ import {
|
|
|
19
25
|
* Queries always hit downstream (unless `onlyCache` is passed)
|
|
20
26
|
*/
|
|
21
27
|
export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
22
|
-
constructor(
|
|
28
|
+
constructor(cfg: CacheDBCfg) {
|
|
23
29
|
super()
|
|
24
|
-
this.
|
|
30
|
+
this.cfg = {
|
|
31
|
+
logger: isGAE ? undefined : console,
|
|
32
|
+
...cfg,
|
|
33
|
+
}
|
|
25
34
|
}
|
|
26
35
|
|
|
27
|
-
|
|
36
|
+
cfg: CacheDBCfg
|
|
28
37
|
|
|
29
38
|
override async ping(): Promise<void> {
|
|
30
39
|
await Promise.all([this.cfg.cacheDB.ping(), this.cfg.downstreamDB.ping()])
|
|
@@ -68,7 +77,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
68
77
|
ids: string[],
|
|
69
78
|
opt: CacheDBOptions<ROW> = {},
|
|
70
79
|
): Promise<ROW[]> {
|
|
71
|
-
const resultMap:
|
|
80
|
+
const resultMap: StringMap<ROW> = {}
|
|
72
81
|
const missingIds: string[] = []
|
|
73
82
|
|
|
74
83
|
if (!opt.skipCache && !this.cfg.skipCache) {
|
|
@@ -79,7 +88,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
79
88
|
missingIds.push(...ids.filter(id => !resultMap[id]))
|
|
80
89
|
|
|
81
90
|
if (this.cfg.logCached) {
|
|
82
|
-
this.log(
|
|
91
|
+
this.cfg.logger?.log(
|
|
83
92
|
`${table}.getByIds ${results.length} rows from cache: [${results
|
|
84
93
|
.map(r => r.id)
|
|
85
94
|
.join(', ')}]`,
|
|
@@ -92,7 +101,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
92
101
|
results.forEach(r => (resultMap[r.id] = r))
|
|
93
102
|
|
|
94
103
|
if (this.cfg.logDownstream) {
|
|
95
|
-
this.log(
|
|
104
|
+
this.cfg.logger?.log(
|
|
96
105
|
`${table}.getByIds ${results.length} rows from downstream: [${results
|
|
97
106
|
.map(r => r.id)
|
|
98
107
|
.join(', ')}]`,
|
|
@@ -120,14 +129,14 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
120
129
|
deletedIds = await this.cfg.downstreamDB.deleteByIds(table, ids, opt)
|
|
121
130
|
|
|
122
131
|
if (this.cfg.logDownstream) {
|
|
123
|
-
this.log(`${table}.deleteByIds ${deletedIds} rows from downstream`)
|
|
132
|
+
this.cfg.logger?.log(`${table}.deleteByIds ${deletedIds} rows from downstream`)
|
|
124
133
|
}
|
|
125
134
|
}
|
|
126
135
|
|
|
127
136
|
if (!opt.skipCache && !this.cfg.skipCache) {
|
|
128
137
|
const cacheResult = this.cfg.cacheDB.deleteByIds(table, ids, opt).then(deletedFromCache => {
|
|
129
138
|
if (this.cfg.logCached) {
|
|
130
|
-
this.log(`${table}.deleteByIds ${deletedFromCache} rows from cache`)
|
|
139
|
+
this.cfg.logger?.log(`${table}.deleteByIds ${deletedFromCache} rows from cache`)
|
|
131
140
|
}
|
|
132
141
|
})
|
|
133
142
|
if (this.cfg.awaitCache) await cacheResult
|
|
@@ -145,7 +154,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
145
154
|
await this.cfg.downstreamDB.saveBatch(table, rows, opt)
|
|
146
155
|
|
|
147
156
|
if (this.cfg.logDownstream) {
|
|
148
|
-
this.log(
|
|
157
|
+
this.cfg.logger?.log(
|
|
149
158
|
`${table}.saveBatch ${rows.length} rows to downstream: [${rows
|
|
150
159
|
.map(r => r.id)
|
|
151
160
|
.join(', ')}]`,
|
|
@@ -156,7 +165,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
156
165
|
if (!opt.skipCache && !this.cfg.skipCache) {
|
|
157
166
|
const cacheResult = this.cfg.cacheDB.saveBatch(table, rows, opt).then(() => {
|
|
158
167
|
if (this.cfg.logCached) {
|
|
159
|
-
this.log(
|
|
168
|
+
this.cfg.logger?.log(
|
|
160
169
|
`${table}.saveBatch ${rows.length} rows to cache: [${rows.map(r => r.id).join(', ')}]`,
|
|
161
170
|
)
|
|
162
171
|
}
|
|
@@ -173,7 +182,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
173
182
|
const { rows, ...queryResult } = await this.cfg.downstreamDB.runQuery(q, opt)
|
|
174
183
|
|
|
175
184
|
if (this.cfg.logDownstream) {
|
|
176
|
-
this.log(`${q.table}.runQuery ${rows.length} rows from downstream`)
|
|
185
|
+
this.cfg.logger?.log(`${q.table}.runQuery ${rows.length} rows from downstream`)
|
|
177
186
|
}
|
|
178
187
|
|
|
179
188
|
// Don't save to cache if it was a projection query
|
|
@@ -189,7 +198,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
189
198
|
const { rows, ...queryResult } = await this.cfg.cacheDB.runQuery(q, opt)
|
|
190
199
|
|
|
191
200
|
if (this.cfg.logCached) {
|
|
192
|
-
this.log(`${q.table}.runQuery ${rows.length} rows from cache`)
|
|
201
|
+
this.cfg.logger?.log(`${q.table}.runQuery ${rows.length} rows from cache`)
|
|
193
202
|
}
|
|
194
203
|
|
|
195
204
|
return { rows, ...queryResult }
|
|
@@ -206,7 +215,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
206
215
|
const count = await this.cfg.cacheDB.runQueryCount(q, opt)
|
|
207
216
|
|
|
208
217
|
if (this.cfg.logCached) {
|
|
209
|
-
this.log(`${q.table}.runQueryCount ${count} rows from cache`)
|
|
218
|
+
this.cfg.logger?.log(`${q.table}.runQueryCount ${count} rows from cache`)
|
|
210
219
|
}
|
|
211
220
|
|
|
212
221
|
return count
|
|
@@ -259,7 +268,9 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
259
268
|
const deletedIds = await this.cfg.downstreamDB.deleteByQuery(q, opt)
|
|
260
269
|
|
|
261
270
|
if (this.cfg.logDownstream) {
|
|
262
|
-
this.log(
|
|
271
|
+
this.cfg.logger?.log(
|
|
272
|
+
`${q.table}.deleteByQuery ${deletedIds} rows from downstream and cache`,
|
|
273
|
+
)
|
|
263
274
|
}
|
|
264
275
|
|
|
265
276
|
if (!opt.skipCache && !this.cfg.skipCache) {
|
|
@@ -275,7 +286,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
275
286
|
const deletedIds = await this.cfg.cacheDB.deleteByQuery(q, opt)
|
|
276
287
|
|
|
277
288
|
if (this.cfg.logCached) {
|
|
278
|
-
this.log(`${q.table}.deleteByQuery ${deletedIds} rows from cache`)
|
|
289
|
+
this.cfg.logger?.log(`${q.table}.deleteByQuery ${deletedIds} rows from cache`)
|
|
279
290
|
}
|
|
280
291
|
|
|
281
292
|
return deletedIds
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
|
+
import { DBSaveBatchOperation } from '../../db.model'
|
|
2
3
|
import type { DBQueryOrder } from '../../query/dbQuery'
|
|
3
4
|
|
|
4
5
|
export interface FileDBPersistencePlugin {
|
|
@@ -22,6 +23,12 @@ export interface FileDBCfg {
|
|
|
22
23
|
*/
|
|
23
24
|
sortObjects?: boolean
|
|
24
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Defaults to `console` in dev.
|
|
28
|
+
* Default to noop in AppEngine.
|
|
29
|
+
*/
|
|
30
|
+
logger?: CommonLogger
|
|
31
|
+
|
|
25
32
|
/**
|
|
26
33
|
* @default false
|
|
27
34
|
*/
|
|
@@ -11,16 +11,12 @@ import {
|
|
|
11
11
|
_uniq,
|
|
12
12
|
JsonSchemaRootObject,
|
|
13
13
|
_filterUndefinedValues,
|
|
14
|
+
ObjectWithId,
|
|
15
|
+
AnyObjectWithId,
|
|
14
16
|
} from '@naturalcycles/js-lib'
|
|
15
|
-
import {
|
|
17
|
+
import { readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
16
18
|
import { dimGrey } from '@naturalcycles/nodejs-lib/dist/colors'
|
|
17
|
-
import {
|
|
18
|
-
AnyObjectWithId,
|
|
19
|
-
BaseCommonDB,
|
|
20
|
-
DBSaveBatchOperation,
|
|
21
|
-
ObjectWithId,
|
|
22
|
-
queryInMemory,
|
|
23
|
-
} from '../..'
|
|
19
|
+
import { BaseCommonDB, DBSaveBatchOperation, queryInMemory } from '../..'
|
|
24
20
|
import { CommonDB } from '../../common.db'
|
|
25
21
|
import {
|
|
26
22
|
CommonDBOptions,
|
|
@@ -32,7 +28,7 @@ import { DBQuery } from '../../query/dbQuery'
|
|
|
32
28
|
import { DBTransaction } from '../../transaction/dbTransaction'
|
|
33
29
|
import { FileDBCfg } from './file.db.model'
|
|
34
30
|
|
|
35
|
-
const
|
|
31
|
+
const isGAE = !!process.env['GAE_INSTANCE']
|
|
36
32
|
|
|
37
33
|
/**
|
|
38
34
|
* Provides barebone implementation for "whole file" based CommonDB.
|
|
@@ -50,6 +46,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
50
46
|
this.cfg = {
|
|
51
47
|
sortObjects: true,
|
|
52
48
|
logFinished: true,
|
|
49
|
+
logger: isGAE ? undefined : console,
|
|
53
50
|
...cfg,
|
|
54
51
|
}
|
|
55
52
|
}
|
|
@@ -275,13 +272,13 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
275
272
|
|
|
276
273
|
private logStarted(op: string): number {
|
|
277
274
|
if (this.cfg.logStarted) {
|
|
278
|
-
log(`>> ${op}`)
|
|
275
|
+
this.cfg.logger?.log(`>> ${op}`)
|
|
279
276
|
}
|
|
280
277
|
return Date.now()
|
|
281
278
|
}
|
|
282
279
|
|
|
283
280
|
private logFinished(started: number, op: string): void {
|
|
284
281
|
if (!this.cfg.logFinished) return
|
|
285
|
-
log(`<< ${op} ${dimGrey(`in ${_since(started)}`)}`)
|
|
282
|
+
this.cfg.logger?.log(`<< ${op} ${dimGrey(`in ${_since(started)}`)}`)
|
|
286
283
|
}
|
|
287
284
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { StringMap, _by } from '@naturalcycles/js-lib'
|
|
2
|
-
import { DBSaveBatchOperation
|
|
1
|
+
import { StringMap, _by, ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
|
+
import { DBSaveBatchOperation } from '../../db.model'
|
|
3
3
|
import { FileDBPersistencePlugin } from './file.db.model'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Readable } from 'stream'
|
|
2
2
|
import { createGzip, createUnzip } from 'zlib'
|
|
3
|
-
import { pMap } from '@naturalcycles/js-lib'
|
|
3
|
+
import { pMap, ObjectWithId } from '@naturalcycles/js-lib'
|
|
4
4
|
import {
|
|
5
5
|
transformJsonParse,
|
|
6
6
|
transformSplit,
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
_pipeline,
|
|
10
10
|
} from '@naturalcycles/nodejs-lib'
|
|
11
11
|
import * as fs from 'fs-extra'
|
|
12
|
-
import { DBSaveBatchOperation
|
|
12
|
+
import { DBSaveBatchOperation } from '../../db.model'
|
|
13
13
|
import { FileDBPersistencePlugin } from './file.db.model'
|
|
14
14
|
|
|
15
15
|
export interface LocalFilePersistencePluginCfg {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
|
+
import { DBSaveBatchOperation } from '../../db.model'
|
|
2
3
|
import { FileDBPersistencePlugin } from './file.db.model'
|
|
3
4
|
|
|
4
5
|
export class NoopPersistencePlugin implements FileDBPersistencePlugin {
|
|
@@ -9,10 +9,12 @@ import {
|
|
|
9
9
|
_since,
|
|
10
10
|
_sortObjectDeep,
|
|
11
11
|
JsonSchemaRootObject,
|
|
12
|
+
ObjectWithId,
|
|
13
|
+
_stringMapValues,
|
|
14
|
+
CommonLogger,
|
|
12
15
|
} from '@naturalcycles/js-lib'
|
|
13
16
|
import {
|
|
14
17
|
bufferReviver,
|
|
15
|
-
Debug,
|
|
16
18
|
ReadableTyped,
|
|
17
19
|
transformJsonParse,
|
|
18
20
|
transformSplit,
|
|
@@ -22,7 +24,7 @@ import {
|
|
|
22
24
|
} from '@naturalcycles/nodejs-lib'
|
|
23
25
|
import { dimGrey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
|
|
24
26
|
import * as fs from 'fs-extra'
|
|
25
|
-
import { CommonDB, DBTransaction,
|
|
27
|
+
import { CommonDB, DBTransaction, queryInMemory } from '../..'
|
|
26
28
|
import {
|
|
27
29
|
CommonDBCreateOptions,
|
|
28
30
|
CommonDBOptions,
|
|
@@ -61,9 +63,15 @@ export interface InMemoryDBCfg {
|
|
|
61
63
|
* @default true
|
|
62
64
|
*/
|
|
63
65
|
persistZip: boolean
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Defaults to `console` in dev.
|
|
69
|
+
* Default to noop in AppEngine.
|
|
70
|
+
*/
|
|
71
|
+
logger?: CommonLogger
|
|
64
72
|
}
|
|
65
73
|
|
|
66
|
-
const
|
|
74
|
+
const isGAE = !!process.env['GAE_INSTANCE']
|
|
67
75
|
|
|
68
76
|
export class InMemoryDB implements CommonDB {
|
|
69
77
|
constructor(cfg?: Partial<InMemoryDBCfg>) {
|
|
@@ -73,6 +81,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
73
81
|
persistenceEnabled: false,
|
|
74
82
|
persistZip: true,
|
|
75
83
|
persistentStoragePath: './tmp/inmemorydb',
|
|
84
|
+
logger: isGAE ? undefined : console,
|
|
76
85
|
...cfg,
|
|
77
86
|
}
|
|
78
87
|
}
|
|
@@ -98,13 +107,13 @@ export class InMemoryDB implements CommonDB {
|
|
|
98
107
|
async resetCache(_table?: string): Promise<void> {
|
|
99
108
|
if (_table) {
|
|
100
109
|
const table = this.cfg.tablesPrefix + _table
|
|
101
|
-
log(`reset ${table}`)
|
|
110
|
+
this.cfg.logger?.log(`reset ${table}`)
|
|
102
111
|
this.data[table] = {}
|
|
103
112
|
} else {
|
|
104
113
|
;(await this.getTables()).forEach(table => {
|
|
105
114
|
this.data[table] = {}
|
|
106
115
|
})
|
|
107
|
-
log('reset')
|
|
116
|
+
this.cfg.logger?.log('reset')
|
|
108
117
|
}
|
|
109
118
|
}
|
|
110
119
|
|
|
@@ -117,7 +126,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
117
126
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
118
127
|
const table = this.cfg.tablesPrefix + _table
|
|
119
128
|
return {
|
|
120
|
-
...generateJsonSchemaFromData(
|
|
129
|
+
...generateJsonSchemaFromData(_stringMapValues(this.data[table] || {})),
|
|
121
130
|
$id: `${table}.schema.json`,
|
|
122
131
|
}
|
|
123
132
|
}
|
|
@@ -155,7 +164,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
155
164
|
|
|
156
165
|
rows.forEach(r => {
|
|
157
166
|
if (!r.id) {
|
|
158
|
-
|
|
167
|
+
this.cfg.logger?.warn({ rows })
|
|
159
168
|
throw new Error(`InMemoryDB: id doesn't exist for row`)
|
|
160
169
|
}
|
|
161
170
|
|
|
@@ -264,7 +273,9 @@ export class InMemoryDB implements CommonDB {
|
|
|
264
273
|
])
|
|
265
274
|
})
|
|
266
275
|
|
|
267
|
-
log(
|
|
276
|
+
this.cfg.logger?.log(
|
|
277
|
+
`flushToDisk took ${dimGrey(_since(started))} to save ${yellow(tables)} tables`,
|
|
278
|
+
)
|
|
268
279
|
}
|
|
269
280
|
|
|
270
281
|
/**
|
|
@@ -304,6 +315,8 @@ export class InMemoryDB implements CommonDB {
|
|
|
304
315
|
this.data[table] = _by(rows, r => r.id)
|
|
305
316
|
})
|
|
306
317
|
|
|
307
|
-
log(
|
|
318
|
+
this.cfg.logger?.log(
|
|
319
|
+
`restoreFromDisk took ${dimGrey(_since(started))} to read ${yellow(files.length)} tables`,
|
|
320
|
+
)
|
|
308
321
|
}
|
|
309
322
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { _pick } from '@naturalcycles/js-lib'
|
|
2
|
-
import { ObjectWithId } from '../../db.model'
|
|
1
|
+
import { _pick, ObjectWithId } from '@naturalcycles/js-lib'
|
|
3
2
|
import { DBQuery, DBQueryFilterOperator } from '../../query/dbQuery'
|
|
4
3
|
|
|
5
4
|
type FilterFn = (v: any, val: any) => boolean
|
package/src/base.common.db.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Readable } from 'stream'
|
|
2
|
-
import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib'
|
|
2
|
+
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'
|
|
3
3
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
4
4
|
import { CommonDB } from './common.db'
|
|
5
|
-
import { CommonDBOptions, CommonDBSaveOptions,
|
|
5
|
+
import { CommonDBOptions, CommonDBSaveOptions, RunQueryResult } from './db.model'
|
|
6
6
|
import { DBQuery } from './query/dbQuery'
|
|
7
7
|
import { DBTransaction } from './transaction/dbTransaction'
|
|
8
8
|
import { commitDBTransactionSimple } from './transaction/dbTransaction.util'
|
package/src/common.db.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib'
|
|
1
|
+
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
3
3
|
import {
|
|
4
4
|
CommonDBCreateOptions,
|
|
5
5
|
CommonDBOptions,
|
|
6
6
|
CommonDBSaveOptions,
|
|
7
7
|
CommonDBStreamOptions,
|
|
8
|
-
ObjectWithId,
|
|
9
8
|
RunQueryResult,
|
|
10
9
|
} from './db.model'
|
|
11
10
|
import { DBQuery } from './query/dbQuery'
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorMode } from '@naturalcycles/js-lib'
|
|
1
|
+
import { CommonLogger, ErrorMode, ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
2
|
import {
|
|
3
3
|
AjvSchema,
|
|
4
4
|
AjvValidationError,
|
|
@@ -8,12 +8,7 @@ import {
|
|
|
8
8
|
TransformMapOptions,
|
|
9
9
|
} from '@naturalcycles/nodejs-lib'
|
|
10
10
|
import { CommonDB } from '../common.db'
|
|
11
|
-
import {
|
|
12
|
-
CommonDBCreateOptions,
|
|
13
|
-
CommonDBOptions,
|
|
14
|
-
CommonDBSaveOptions,
|
|
15
|
-
ObjectWithId,
|
|
16
|
-
} from '../db.model'
|
|
11
|
+
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model'
|
|
17
12
|
|
|
18
13
|
// Hook DBM, BM, TM types should follow this exact order
|
|
19
14
|
export type CommonDaoCreateIdHook<BM, DBM> = (obj: DBM | BM) => string
|
|
@@ -75,6 +70,14 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends Obje
|
|
|
75
70
|
*/
|
|
76
71
|
readOnly?: boolean
|
|
77
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Pass undefined (or noopLogger) to disable logging completely.
|
|
75
|
+
*
|
|
76
|
+
* Defaults to `console` in dev.
|
|
77
|
+
* Default to noop in AppEngine.
|
|
78
|
+
*/
|
|
79
|
+
logger?: CommonLogger
|
|
80
|
+
|
|
78
81
|
/**
|
|
79
82
|
* @default OPERATIONS
|
|
80
83
|
*/
|
|
@@ -93,6 +96,17 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends Obje
|
|
|
93
96
|
* Set to false to disable created/updated fields management.
|
|
94
97
|
*/
|
|
95
98
|
createdUpdated?: boolean
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Default is false.
|
|
102
|
+
* If true - will run `_filterNullishValues` inside `validateAndConvert` function
|
|
103
|
+
* (instead of `_filterUndefinedValues`).
|
|
104
|
+
* This was the old db-lib behavior.
|
|
105
|
+
* This option allows to keep backwards-compatible behavior.
|
|
106
|
+
*
|
|
107
|
+
* @deprecated
|
|
108
|
+
*/
|
|
109
|
+
filterNullishValues?: boolean
|
|
96
110
|
}
|
|
97
111
|
|
|
98
112
|
/**
|