@naturalcycles/db-lib 10.56.2 → 10.57.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.js +2 -1
- package/dist/adapter/cachedb/cache.db.model.d.ts +1 -1
- package/dist/adapter/file/file.db.js +2 -1
- package/dist/adapter/file/file.db.model.d.ts +1 -1
- package/dist/commondao/common.dao.d.ts +3 -0
- package/dist/commondao/common.dao.js +44 -10
- package/dist/commondao/common.dao.model.d.ts +1 -1
- package/dist/commondao/commonDaoTransaction.js +1 -1
- package/dist/inmemory/inMemory.db.d.ts +1 -1
- package/dist/inmemory/inMemory.db.js +2 -1
- package/dist/kv/commonKeyValueDao.d.ts +1 -1
- package/dist/kv/commonKeyValueDao.js +4 -3
- package/package.json +1 -1
- package/src/adapter/cachedb/cache.db.model.ts +1 -1
- package/src/adapter/cachedb/cache.db.ts +2 -1
- package/src/adapter/file/file.db.model.ts +1 -1
- package/src/adapter/file/file.db.ts +2 -1
- package/src/commondao/common.dao.model.ts +1 -1
- package/src/commondao/common.dao.ts +59 -17
- package/src/commondao/commonDaoTransaction.ts +1 -1
- package/src/inmemory/inMemory.db.ts +3 -2
- package/src/kv/commonKeyValueDao.ts +5 -4
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { _isTruthy } from '@naturalcycles/js-lib';
|
|
2
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log';
|
|
2
3
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
3
4
|
import { BaseCommonDB } from '../../commondb/base.common.db.js';
|
|
4
5
|
import { commonDBFullSupport } from '../../commondb/common.db.js';
|
|
@@ -17,7 +18,7 @@ export class CacheDB extends BaseCommonDB {
|
|
|
17
18
|
constructor(cfg) {
|
|
18
19
|
super();
|
|
19
20
|
this.cfg = {
|
|
20
|
-
logger:
|
|
21
|
+
logger: consoleLogger,
|
|
21
22
|
...cfg,
|
|
22
23
|
};
|
|
23
24
|
}
|
|
@@ -2,6 +2,7 @@ import { Readable } from 'node:stream';
|
|
|
2
2
|
import { _by, _sortBy } from '@naturalcycles/js-lib/array';
|
|
3
3
|
import { _since, localTime } from '@naturalcycles/js-lib/datetime';
|
|
4
4
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
5
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log';
|
|
5
6
|
import { _deepEquals, _filterUndefinedValues, _sortObjectDeep } from '@naturalcycles/js-lib/object';
|
|
6
7
|
import { _stringMapValues } from '@naturalcycles/js-lib/types';
|
|
7
8
|
import { generateJsonSchemaFromData } from '@naturalcycles/nodejs-lib/ajv';
|
|
@@ -36,7 +37,7 @@ export class FileDB extends BaseCommonDB {
|
|
|
36
37
|
this.cfg = {
|
|
37
38
|
sortObjects: true,
|
|
38
39
|
logFinished: true,
|
|
39
|
-
logger:
|
|
40
|
+
logger: consoleLogger,
|
|
40
41
|
...cfg,
|
|
41
42
|
};
|
|
42
43
|
}
|
|
@@ -170,6 +170,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
170
170
|
* Mutates `dbm`.
|
|
171
171
|
*/
|
|
172
172
|
private decompress;
|
|
173
|
+
private assertNotCompressed;
|
|
173
174
|
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): null;
|
|
174
175
|
anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM;
|
|
175
176
|
anyToDBMs(rows: DBM[], opt?: CommonDaoOptions): DBM[];
|
|
@@ -217,6 +218,8 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
217
218
|
* Throws if readOnly is true
|
|
218
219
|
*/
|
|
219
220
|
private requireObjectMutability;
|
|
221
|
+
private validateQuery;
|
|
222
|
+
private validateQueryCompression;
|
|
220
223
|
/**
|
|
221
224
|
* Throws if query uses a property that is in `excludeFromIndexes` list.
|
|
222
225
|
*/
|
|
@@ -2,6 +2,7 @@ import { _isTruthy } from '@naturalcycles/js-lib';
|
|
|
2
2
|
import { _uniqBy } from '@naturalcycles/js-lib/array/array.util.js';
|
|
3
3
|
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js';
|
|
4
4
|
import { _assert, ErrorMode } from '@naturalcycles/js-lib/error';
|
|
5
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log';
|
|
5
6
|
import { _deepJsonEquals } from '@naturalcycles/js-lib/object/deepEquals.js';
|
|
6
7
|
import { _filterUndefinedValues, _objectAssignExact, _omitWithUndefined, _pick, } from '@naturalcycles/js-lib/object/object.util.js';
|
|
7
8
|
import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
|
|
@@ -32,7 +33,7 @@ export class CommonDao {
|
|
|
32
33
|
useUpdatedProperty: true,
|
|
33
34
|
validateOnLoad: true,
|
|
34
35
|
validateOnSave: true,
|
|
35
|
-
logger:
|
|
36
|
+
logger: consoleLogger,
|
|
36
37
|
...cfg,
|
|
37
38
|
hooks: {
|
|
38
39
|
parseNaturalId: () => ({}),
|
|
@@ -130,6 +131,7 @@ export class CommonDao {
|
|
|
130
131
|
}
|
|
131
132
|
async runQuerySingleColumn(q, opt) {
|
|
132
133
|
_assert(q._selectedFieldNames?.length === 1, `runQuerySingleColumn requires exactly 1 column to be selected: ${q.pretty()}`);
|
|
134
|
+
this.assertNotCompressed(q._selectedFieldNames, 'runQuerySingleColumn');
|
|
133
135
|
const col = q._selectedFieldNames[0];
|
|
134
136
|
const { rows } = await this.runQueryExtended(q, opt);
|
|
135
137
|
return rows.map((r) => r[col]);
|
|
@@ -144,7 +146,7 @@ export class CommonDao {
|
|
|
144
146
|
return _uniqBy(results, r => r.id);
|
|
145
147
|
}
|
|
146
148
|
async runQueryExtended(q, opt = {}) {
|
|
147
|
-
this.
|
|
149
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
148
150
|
q.table = opt.table || q.table;
|
|
149
151
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery(q, opt);
|
|
150
152
|
const isPartialQuery = !!q._selectedFieldNames;
|
|
@@ -160,7 +162,7 @@ export class CommonDao {
|
|
|
160
162
|
return rows;
|
|
161
163
|
}
|
|
162
164
|
async runQueryExtendedAsDBM(q, opt = {}) {
|
|
163
|
-
this.
|
|
165
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
164
166
|
q.table = opt.table || q.table;
|
|
165
167
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery(q, opt);
|
|
166
168
|
const isPartialQuery = !!q._selectedFieldNames;
|
|
@@ -169,12 +171,12 @@ export class CommonDao {
|
|
|
169
171
|
return { rows: dbms, ...queryResult };
|
|
170
172
|
}
|
|
171
173
|
async runQueryCount(q, opt = {}) {
|
|
172
|
-
this.
|
|
174
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
173
175
|
q.table = opt.table || q.table;
|
|
174
176
|
return await this.cfg.db.runQueryCount(q, opt);
|
|
175
177
|
}
|
|
176
178
|
streamQueryAsDBM(q, opt = {}) {
|
|
177
|
-
this.
|
|
179
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
178
180
|
q.table = opt.table || q.table;
|
|
179
181
|
let pipeline = this.cfg.db.streamQuery(q, opt);
|
|
180
182
|
if (this.cfg.compress?.keys.length) {
|
|
@@ -188,7 +190,7 @@ export class CommonDao {
|
|
|
188
190
|
return pipeline.mapSync(dbm => this.anyToDBM(dbm, opt), { errorMode: opt.errorMode });
|
|
189
191
|
}
|
|
190
192
|
streamQuery(q, opt = {}) {
|
|
191
|
-
this.
|
|
193
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
192
194
|
q.table = opt.table || q.table;
|
|
193
195
|
let pipeline = this.cfg.db.streamQuery(q, opt);
|
|
194
196
|
if (this.cfg.compress?.keys.length) {
|
|
@@ -202,13 +204,13 @@ export class CommonDao {
|
|
|
202
204
|
return pipeline.mapSync(dbm => this.dbmToBM(dbm, opt), { errorMode: opt.errorMode });
|
|
203
205
|
}
|
|
204
206
|
async queryIds(q, opt = {}) {
|
|
205
|
-
this.
|
|
207
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
206
208
|
q.table = opt.table || q.table;
|
|
207
209
|
const { rows } = await this.cfg.db.runQuery(q.select(['id']), opt);
|
|
208
210
|
return rows.map(r => r.id);
|
|
209
211
|
}
|
|
210
212
|
streamQueryIds(q, opt = {}) {
|
|
211
|
-
this.
|
|
213
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
212
214
|
q.table = opt.table || q.table;
|
|
213
215
|
opt.errorMode ||= ErrorMode.SUPPRESS;
|
|
214
216
|
return this.cfg.db.streamQuery(q.select(['id']), opt).mapSync((r) => r.id);
|
|
@@ -490,7 +492,7 @@ export class CommonDao {
|
|
|
490
492
|
* This is expected to be more memory-efficient way of deleting large number of rows.
|
|
491
493
|
*/
|
|
492
494
|
async deleteByQuery(q, opt = {}) {
|
|
493
|
-
this.
|
|
495
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
494
496
|
this.requireWriteAccess();
|
|
495
497
|
this.requireObjectMutability(opt);
|
|
496
498
|
q.table = opt.table || q.table;
|
|
@@ -526,10 +528,12 @@ export class CommonDao {
|
|
|
526
528
|
async patchByIds(ids, patch, opt = {}) {
|
|
527
529
|
if (!ids.length)
|
|
528
530
|
return 0;
|
|
531
|
+
this.assertNotCompressed(Object.keys(patch), 'patchByIds');
|
|
529
532
|
return await this.patchByQuery(this.query().filterIn('id', ids), patch, opt);
|
|
530
533
|
}
|
|
531
534
|
async patchByQuery(q, patch, opt = {}) {
|
|
532
|
-
this.
|
|
535
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
536
|
+
this.assertNotCompressed(Object.keys(patch), 'patchByQuery');
|
|
533
537
|
this.requireWriteAccess();
|
|
534
538
|
this.requireObjectMutability(opt);
|
|
535
539
|
q.table = opt.table || q.table;
|
|
@@ -541,6 +545,7 @@ export class CommonDao {
|
|
|
541
545
|
* @experimental
|
|
542
546
|
*/
|
|
543
547
|
async increment(prop, id, by = 1, opt = {}) {
|
|
548
|
+
this.assertNotCompressed([prop], 'increment');
|
|
544
549
|
this.requireWriteAccess();
|
|
545
550
|
this.requireObjectMutability(opt);
|
|
546
551
|
const { table } = this.cfg;
|
|
@@ -555,6 +560,7 @@ export class CommonDao {
|
|
|
555
560
|
* @experimental
|
|
556
561
|
*/
|
|
557
562
|
async incrementBatch(prop, incrementMap, opt = {}) {
|
|
563
|
+
this.assertNotCompressed([prop], 'incrementBatch');
|
|
558
564
|
this.requireWriteAccess();
|
|
559
565
|
this.requireObjectMutability(opt);
|
|
560
566
|
const { table } = this.cfg;
|
|
@@ -679,6 +685,16 @@ export class CommonDao {
|
|
|
679
685
|
dbm.__compressed = undefined;
|
|
680
686
|
Object.assign(dbm, properties);
|
|
681
687
|
}
|
|
688
|
+
assertNotCompressed(keys, op) {
|
|
689
|
+
if (!keys)
|
|
690
|
+
return;
|
|
691
|
+
const compressKeys = this.cfg.compress?.keys;
|
|
692
|
+
if (!compressKeys?.length)
|
|
693
|
+
return;
|
|
694
|
+
for (const k of keys) {
|
|
695
|
+
_assert(k !== '__compressed' && !compressKeys.includes(k), `${op} on ${this.cfg.table} cannot touch compressed property: ${k}`);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
682
698
|
anyToDBM(dbm, _opt = {}) {
|
|
683
699
|
if (!dbm)
|
|
684
700
|
return null;
|
|
@@ -945,6 +961,24 @@ export class CommonDao {
|
|
|
945
961
|
table: this.cfg.table,
|
|
946
962
|
});
|
|
947
963
|
}
|
|
964
|
+
validateQuery(q) {
|
|
965
|
+
this.validateQueryIndexes(q);
|
|
966
|
+
this.validateQueryCompression(q);
|
|
967
|
+
}
|
|
968
|
+
validateQueryCompression(q) {
|
|
969
|
+
const { _filters, _orders, _selectedFieldNames } = q;
|
|
970
|
+
if (_filters) {
|
|
971
|
+
const columnNames = _filters.map(cfg => cfg.name);
|
|
972
|
+
this.assertNotCompressed(columnNames, 'filter');
|
|
973
|
+
}
|
|
974
|
+
if (_orders) {
|
|
975
|
+
const columnNames = _orders.map(cfg => cfg.name);
|
|
976
|
+
this.assertNotCompressed(columnNames, 'order');
|
|
977
|
+
}
|
|
978
|
+
if (_selectedFieldNames) {
|
|
979
|
+
this.assertNotCompressed(_selectedFieldNames, 'select');
|
|
980
|
+
}
|
|
981
|
+
}
|
|
948
982
|
/**
|
|
949
983
|
* Throws if query uses a property that is in `excludeFromIndexes` list.
|
|
950
984
|
*/
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { _isEmptyObject } from '@naturalcycles/js-lib';
|
|
2
2
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
3
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log';
|
|
3
4
|
import { _deepCopy, _sortObjectDeep } from '@naturalcycles/js-lib/object';
|
|
4
5
|
import { _stringMapEntries, _stringMapValues } from '@naturalcycles/js-lib/types';
|
|
5
6
|
import { generateJsonSchemaFromData } from '@naturalcycles/nodejs-lib/ajv';
|
|
@@ -17,7 +18,7 @@ export class InMemoryDB {
|
|
|
17
18
|
// defaults
|
|
18
19
|
tablesPrefix: '',
|
|
19
20
|
forbidTransactionReadAfterWrite: true,
|
|
20
|
-
logger:
|
|
21
|
+
logger: consoleLogger,
|
|
21
22
|
...cfg,
|
|
22
23
|
};
|
|
23
24
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AppError } from '@naturalcycles/js-lib/error/error.util.js';
|
|
2
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log';
|
|
2
3
|
import { SKIP } from '@naturalcycles/js-lib/types';
|
|
3
4
|
import { zip2 } from '@naturalcycles/nodejs-lib/zip';
|
|
4
5
|
/**
|
|
@@ -31,7 +32,7 @@ export function commonKeyValueDaoCompressedTransformer() {
|
|
|
31
32
|
export class CommonKeyValueDao {
|
|
32
33
|
constructor(cfg) {
|
|
33
34
|
this.cfg = {
|
|
34
|
-
logger:
|
|
35
|
+
logger: consoleLogger,
|
|
35
36
|
...cfg,
|
|
36
37
|
};
|
|
37
38
|
}
|
|
@@ -118,7 +119,7 @@ export class CommonKeyValueDao {
|
|
|
118
119
|
return transformer.bufferToValue(buf);
|
|
119
120
|
}
|
|
120
121
|
catch (err) {
|
|
121
|
-
this.cfg.logger.error(err);
|
|
122
|
+
this.cfg.logger.error({ err });
|
|
122
123
|
return SKIP;
|
|
123
124
|
}
|
|
124
125
|
});
|
|
@@ -133,7 +134,7 @@ export class CommonKeyValueDao {
|
|
|
133
134
|
return [id, transformer.bufferToValue(buf)];
|
|
134
135
|
}
|
|
135
136
|
catch (err) {
|
|
136
|
-
this.cfg.logger.error(err);
|
|
137
|
+
this.cfg.logger.error({ err });
|
|
137
138
|
return SKIP;
|
|
138
139
|
}
|
|
139
140
|
});
|
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { _isTruthy } from '@naturalcycles/js-lib'
|
|
2
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log'
|
|
2
3
|
import type { ObjectWithId, StringMap } from '@naturalcycles/js-lib/types'
|
|
3
4
|
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
4
5
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
@@ -31,7 +32,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
31
32
|
constructor(cfg: CacheDBCfg) {
|
|
32
33
|
super()
|
|
33
34
|
this.cfg = {
|
|
34
|
-
logger:
|
|
35
|
+
logger: consoleLogger,
|
|
35
36
|
...cfg,
|
|
36
37
|
}
|
|
37
38
|
}
|
|
@@ -2,6 +2,7 @@ import { Readable } from 'node:stream'
|
|
|
2
2
|
import { _by, _sortBy } from '@naturalcycles/js-lib/array'
|
|
3
3
|
import { _since, localTime } from '@naturalcycles/js-lib/datetime'
|
|
4
4
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
5
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log'
|
|
5
6
|
import { _deepEquals, _filterUndefinedValues, _sortObjectDeep } from '@naturalcycles/js-lib/object'
|
|
6
7
|
import { _stringMapValues } from '@naturalcycles/js-lib/types'
|
|
7
8
|
import type { ObjectWithId, UnixTimestampMillis } from '@naturalcycles/js-lib/types'
|
|
@@ -50,7 +51,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
50
51
|
this.cfg = {
|
|
51
52
|
sortObjects: true,
|
|
52
53
|
logFinished: true,
|
|
53
|
-
logger:
|
|
54
|
+
logger: consoleLogger,
|
|
54
55
|
...cfg,
|
|
55
56
|
}
|
|
56
57
|
}
|
|
@@ -2,6 +2,7 @@ import { _isTruthy } from '@naturalcycles/js-lib'
|
|
|
2
2
|
import { _uniqBy } from '@naturalcycles/js-lib/array/array.util.js'
|
|
3
3
|
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
|
|
4
4
|
import { _assert, ErrorMode } from '@naturalcycles/js-lib/error'
|
|
5
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log'
|
|
5
6
|
import { _deepJsonEquals } from '@naturalcycles/js-lib/object/deepEquals.js'
|
|
6
7
|
import {
|
|
7
8
|
_filterUndefinedValues,
|
|
@@ -10,13 +11,6 @@ import {
|
|
|
10
11
|
_pick,
|
|
11
12
|
} from '@naturalcycles/js-lib/object/object.util.js'
|
|
12
13
|
import { pMap } from '@naturalcycles/js-lib/promise/pMap.js'
|
|
13
|
-
import {
|
|
14
|
-
_objectKeys,
|
|
15
|
-
_passthroughPredicate,
|
|
16
|
-
_stringMapEntries,
|
|
17
|
-
_stringMapValues,
|
|
18
|
-
_typeCast,
|
|
19
|
-
} from '@naturalcycles/js-lib/types'
|
|
20
14
|
import type {
|
|
21
15
|
BaseDBEntity,
|
|
22
16
|
NonNegativeInteger,
|
|
@@ -24,6 +18,13 @@ import type {
|
|
|
24
18
|
StringMap,
|
|
25
19
|
Unsaved,
|
|
26
20
|
} from '@naturalcycles/js-lib/types'
|
|
21
|
+
import {
|
|
22
|
+
_objectKeys,
|
|
23
|
+
_passthroughPredicate,
|
|
24
|
+
_stringMapEntries,
|
|
25
|
+
_stringMapValues,
|
|
26
|
+
_typeCast,
|
|
27
|
+
} from '@naturalcycles/js-lib/types'
|
|
27
28
|
import { stringId } from '@naturalcycles/nodejs-lib'
|
|
28
29
|
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
29
30
|
import type { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
@@ -75,7 +76,7 @@ export class CommonDao<
|
|
|
75
76
|
useUpdatedProperty: true,
|
|
76
77
|
validateOnLoad: true,
|
|
77
78
|
validateOnSave: true,
|
|
78
|
-
logger:
|
|
79
|
+
logger: consoleLogger,
|
|
79
80
|
...cfg,
|
|
80
81
|
hooks: {
|
|
81
82
|
parseNaturalId: () => ({}),
|
|
@@ -189,6 +190,7 @@ export class CommonDao<
|
|
|
189
190
|
q._selectedFieldNames?.length === 1,
|
|
190
191
|
`runQuerySingleColumn requires exactly 1 column to be selected: ${q.pretty()}`,
|
|
191
192
|
)
|
|
193
|
+
this.assertNotCompressed(q._selectedFieldNames, 'runQuerySingleColumn')
|
|
192
194
|
|
|
193
195
|
const col = q._selectedFieldNames[0]!
|
|
194
196
|
|
|
@@ -212,7 +214,7 @@ export class CommonDao<
|
|
|
212
214
|
q: DBQuery<DBM>,
|
|
213
215
|
opt: CommonDaoReadOptions = {},
|
|
214
216
|
): Promise<RunQueryResult<BM>> {
|
|
215
|
-
this.
|
|
217
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
216
218
|
q.table = opt.table || q.table
|
|
217
219
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
|
|
218
220
|
const isPartialQuery = !!q._selectedFieldNames
|
|
@@ -233,7 +235,7 @@ export class CommonDao<
|
|
|
233
235
|
q: DBQuery<DBM>,
|
|
234
236
|
opt: CommonDaoReadOptions = {},
|
|
235
237
|
): Promise<RunQueryResult<DBM>> {
|
|
236
|
-
this.
|
|
238
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
237
239
|
q.table = opt.table || q.table
|
|
238
240
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
|
|
239
241
|
const isPartialQuery = !!q._selectedFieldNames
|
|
@@ -243,13 +245,13 @@ export class CommonDao<
|
|
|
243
245
|
}
|
|
244
246
|
|
|
245
247
|
async runQueryCount(q: DBQuery<DBM>, opt: CommonDaoReadOptions = {}): Promise<number> {
|
|
246
|
-
this.
|
|
248
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
247
249
|
q.table = opt.table || q.table
|
|
248
250
|
return await this.cfg.db.runQueryCount(q, opt)
|
|
249
251
|
}
|
|
250
252
|
|
|
251
253
|
streamQueryAsDBM(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<DBM> = {}): Pipeline<DBM> {
|
|
252
|
-
this.
|
|
254
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
253
255
|
q.table = opt.table || q.table
|
|
254
256
|
let pipeline = this.cfg.db.streamQuery<DBM>(q, opt)
|
|
255
257
|
|
|
@@ -267,7 +269,7 @@ export class CommonDao<
|
|
|
267
269
|
}
|
|
268
270
|
|
|
269
271
|
streamQuery(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<BM> = {}): Pipeline<BM> {
|
|
270
|
-
this.
|
|
272
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
271
273
|
q.table = opt.table || q.table
|
|
272
274
|
let pipeline = this.cfg.db.streamQuery<DBM>(q, opt)
|
|
273
275
|
|
|
@@ -285,14 +287,14 @@ export class CommonDao<
|
|
|
285
287
|
}
|
|
286
288
|
|
|
287
289
|
async queryIds(q: DBQuery<DBM>, opt: CommonDaoReadOptions = {}): Promise<ID[]> {
|
|
288
|
-
this.
|
|
290
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
289
291
|
q.table = opt.table || q.table
|
|
290
292
|
const { rows } = await this.cfg.db.runQuery(q.select(['id']), opt)
|
|
291
293
|
return rows.map(r => r.id as ID)
|
|
292
294
|
}
|
|
293
295
|
|
|
294
296
|
streamQueryIds(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<ID> = {}): Pipeline<ID> {
|
|
295
|
-
this.
|
|
297
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
296
298
|
q.table = opt.table || q.table
|
|
297
299
|
opt.errorMode ||= ErrorMode.SUPPRESS
|
|
298
300
|
|
|
@@ -650,7 +652,7 @@ export class CommonDao<
|
|
|
650
652
|
q: DBQuery<DBM>,
|
|
651
653
|
opt: CommonDaoStreamDeleteOptions<DBM> = {},
|
|
652
654
|
): Promise<number> {
|
|
653
|
-
this.
|
|
655
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
654
656
|
this.requireWriteAccess()
|
|
655
657
|
this.requireObjectMutability(opt)
|
|
656
658
|
q.table = opt.table || q.table
|
|
@@ -691,6 +693,7 @@ export class CommonDao<
|
|
|
691
693
|
|
|
692
694
|
async patchByIds(ids: ID[], patch: Partial<DBM>, opt: CommonDaoOptions = {}): Promise<number> {
|
|
693
695
|
if (!ids.length) return 0
|
|
696
|
+
this.assertNotCompressed(Object.keys(patch), 'patchByIds')
|
|
694
697
|
return await this.patchByQuery(this.query().filterIn('id', ids), patch, opt)
|
|
695
698
|
}
|
|
696
699
|
|
|
@@ -699,7 +702,8 @@ export class CommonDao<
|
|
|
699
702
|
patch: Partial<DBM>,
|
|
700
703
|
opt: CommonDaoOptions = {},
|
|
701
704
|
): Promise<number> {
|
|
702
|
-
this.
|
|
705
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
706
|
+
this.assertNotCompressed(Object.keys(patch), 'patchByQuery')
|
|
703
707
|
this.requireWriteAccess()
|
|
704
708
|
this.requireObjectMutability(opt)
|
|
705
709
|
q.table = opt.table || q.table
|
|
@@ -712,6 +716,7 @@ export class CommonDao<
|
|
|
712
716
|
* @experimental
|
|
713
717
|
*/
|
|
714
718
|
async increment(prop: keyof DBM, id: ID, by = 1, opt: CommonDaoOptions = {}): Promise<number> {
|
|
719
|
+
this.assertNotCompressed([prop], 'increment')
|
|
715
720
|
this.requireWriteAccess()
|
|
716
721
|
this.requireObjectMutability(opt)
|
|
717
722
|
const { table } = this.cfg
|
|
@@ -731,6 +736,7 @@ export class CommonDao<
|
|
|
731
736
|
incrementMap: StringMap<number>,
|
|
732
737
|
opt: CommonDaoOptions = {},
|
|
733
738
|
): Promise<StringMap<number>> {
|
|
739
|
+
this.assertNotCompressed([prop], 'incrementBatch')
|
|
734
740
|
this.requireWriteAccess()
|
|
735
741
|
this.requireObjectMutability(opt)
|
|
736
742
|
const { table } = this.cfg
|
|
@@ -877,6 +883,19 @@ export class CommonDao<
|
|
|
877
883
|
Object.assign(dbm, properties)
|
|
878
884
|
}
|
|
879
885
|
|
|
886
|
+
private assertNotCompressed(keys: (keyof DBM | string)[] | undefined, op: string): void {
|
|
887
|
+
if (!keys) return
|
|
888
|
+
|
|
889
|
+
const compressKeys = this.cfg.compress?.keys
|
|
890
|
+
if (!compressKeys?.length) return
|
|
891
|
+
for (const k of keys) {
|
|
892
|
+
_assert(
|
|
893
|
+
k !== '__compressed' && !compressKeys.includes(k as keyof DBM),
|
|
894
|
+
`${op} on ${this.cfg.table} cannot touch compressed property: ${k as string}`,
|
|
895
|
+
)
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
880
899
|
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): null
|
|
881
900
|
anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM
|
|
882
901
|
anyToDBM(dbm?: DBM, _opt: CommonDaoOptions = {}): DBM | null {
|
|
@@ -1212,6 +1231,29 @@ export class CommonDao<
|
|
|
1212
1231
|
})
|
|
1213
1232
|
}
|
|
1214
1233
|
|
|
1234
|
+
private validateQuery(q: DBQuery<DBM>): void {
|
|
1235
|
+
this.validateQueryIndexes(q)
|
|
1236
|
+
this.validateQueryCompression(q)
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
private validateQueryCompression(q: DBQuery<DBM>): void {
|
|
1240
|
+
const { _filters, _orders, _selectedFieldNames } = q
|
|
1241
|
+
|
|
1242
|
+
if (_filters) {
|
|
1243
|
+
const columnNames = _filters.map(cfg => cfg.name)
|
|
1244
|
+
this.assertNotCompressed(columnNames, 'filter')
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
if (_orders) {
|
|
1248
|
+
const columnNames = _orders.map(cfg => cfg.name)
|
|
1249
|
+
this.assertNotCompressed(columnNames, 'order')
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
if (_selectedFieldNames) {
|
|
1253
|
+
this.assertNotCompressed(_selectedFieldNames, 'select')
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1215
1257
|
/**
|
|
1216
1258
|
* Throws if query uses a property that is in `excludeFromIndexes` list.
|
|
1217
1259
|
*/
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { _isEmptyObject } from '@naturalcycles/js-lib'
|
|
2
2
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
3
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log'
|
|
3
4
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
4
5
|
import { _deepCopy, _sortObjectDeep } from '@naturalcycles/js-lib/object'
|
|
5
6
|
import { _stringMapEntries, _stringMapValues } from '@naturalcycles/js-lib/types'
|
|
@@ -49,7 +50,7 @@ export interface InMemoryDBCfg {
|
|
|
49
50
|
forbidTransactionReadAfterWrite?: boolean
|
|
50
51
|
|
|
51
52
|
/**
|
|
52
|
-
* Defaults to `
|
|
53
|
+
* Defaults to `consoleLogger`.
|
|
53
54
|
*/
|
|
54
55
|
logger?: CommonLogger
|
|
55
56
|
}
|
|
@@ -67,7 +68,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
67
68
|
// defaults
|
|
68
69
|
tablesPrefix: '',
|
|
69
70
|
forbidTransactionReadAfterWrite: true,
|
|
70
|
-
logger:
|
|
71
|
+
logger: consoleLogger,
|
|
71
72
|
...cfg,
|
|
72
73
|
}
|
|
73
74
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AppError } from '@naturalcycles/js-lib/error/error.util.js'
|
|
2
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log'
|
|
2
3
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
3
4
|
import { SKIP } from '@naturalcycles/js-lib/types'
|
|
4
5
|
import type { Integer, KeyValueTuple } from '@naturalcycles/js-lib/types'
|
|
@@ -25,7 +26,7 @@ export interface CommonKeyValueDaoCfg<V> {
|
|
|
25
26
|
readOnly?: boolean
|
|
26
27
|
|
|
27
28
|
/**
|
|
28
|
-
* Default to
|
|
29
|
+
* Default to consoleLogger
|
|
29
30
|
*/
|
|
30
31
|
logger?: CommonLogger
|
|
31
32
|
|
|
@@ -87,7 +88,7 @@ export function commonKeyValueDaoCompressedTransformer<T = any>(): CommonKeyValu
|
|
|
87
88
|
export class CommonKeyValueDao<K extends string = string, V = Buffer> {
|
|
88
89
|
constructor(cfg: CommonKeyValueDaoCfg<V>) {
|
|
89
90
|
this.cfg = {
|
|
90
|
-
logger:
|
|
91
|
+
logger: consoleLogger,
|
|
91
92
|
...cfg,
|
|
92
93
|
}
|
|
93
94
|
}
|
|
@@ -198,7 +199,7 @@ export class CommonKeyValueDao<K extends string = string, V = Buffer> {
|
|
|
198
199
|
try {
|
|
199
200
|
return transformer.bufferToValue(buf)
|
|
200
201
|
} catch (err) {
|
|
201
|
-
this.cfg.logger.error(err)
|
|
202
|
+
this.cfg.logger.error({ err })
|
|
202
203
|
return SKIP
|
|
203
204
|
}
|
|
204
205
|
})
|
|
@@ -215,7 +216,7 @@ export class CommonKeyValueDao<K extends string = string, V = Buffer> {
|
|
|
215
216
|
try {
|
|
216
217
|
return [id as K, transformer.bufferToValue(buf)]
|
|
217
218
|
} catch (err) {
|
|
218
|
-
this.cfg.logger.error(err)
|
|
219
|
+
this.cfg.logger.error({ err })
|
|
219
220
|
return SKIP
|
|
220
221
|
}
|
|
221
222
|
})
|