@naturalcycles/db-lib 10.56.0 → 10.56.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.
|
@@ -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
|
*/
|
|
@@ -130,6 +130,7 @@ export class CommonDao {
|
|
|
130
130
|
}
|
|
131
131
|
async runQuerySingleColumn(q, opt) {
|
|
132
132
|
_assert(q._selectedFieldNames?.length === 1, `runQuerySingleColumn requires exactly 1 column to be selected: ${q.pretty()}`);
|
|
133
|
+
this.assertNotCompressed(q._selectedFieldNames, 'runQuerySingleColumn');
|
|
133
134
|
const col = q._selectedFieldNames[0];
|
|
134
135
|
const { rows } = await this.runQueryExtended(q, opt);
|
|
135
136
|
return rows.map((r) => r[col]);
|
|
@@ -144,7 +145,7 @@ export class CommonDao {
|
|
|
144
145
|
return _uniqBy(results, r => r.id);
|
|
145
146
|
}
|
|
146
147
|
async runQueryExtended(q, opt = {}) {
|
|
147
|
-
this.
|
|
148
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
148
149
|
q.table = opt.table || q.table;
|
|
149
150
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery(q, opt);
|
|
150
151
|
const isPartialQuery = !!q._selectedFieldNames;
|
|
@@ -160,7 +161,7 @@ export class CommonDao {
|
|
|
160
161
|
return rows;
|
|
161
162
|
}
|
|
162
163
|
async runQueryExtendedAsDBM(q, opt = {}) {
|
|
163
|
-
this.
|
|
164
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
164
165
|
q.table = opt.table || q.table;
|
|
165
166
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery(q, opt);
|
|
166
167
|
const isPartialQuery = !!q._selectedFieldNames;
|
|
@@ -169,12 +170,12 @@ export class CommonDao {
|
|
|
169
170
|
return { rows: dbms, ...queryResult };
|
|
170
171
|
}
|
|
171
172
|
async runQueryCount(q, opt = {}) {
|
|
172
|
-
this.
|
|
173
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
173
174
|
q.table = opt.table || q.table;
|
|
174
175
|
return await this.cfg.db.runQueryCount(q, opt);
|
|
175
176
|
}
|
|
176
177
|
streamQueryAsDBM(q, opt = {}) {
|
|
177
|
-
this.
|
|
178
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
178
179
|
q.table = opt.table || q.table;
|
|
179
180
|
let pipeline = this.cfg.db.streamQuery(q, opt);
|
|
180
181
|
if (this.cfg.compress?.keys.length) {
|
|
@@ -188,7 +189,7 @@ export class CommonDao {
|
|
|
188
189
|
return pipeline.mapSync(dbm => this.anyToDBM(dbm, opt), { errorMode: opt.errorMode });
|
|
189
190
|
}
|
|
190
191
|
streamQuery(q, opt = {}) {
|
|
191
|
-
this.
|
|
192
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
192
193
|
q.table = opt.table || q.table;
|
|
193
194
|
let pipeline = this.cfg.db.streamQuery(q, opt);
|
|
194
195
|
if (this.cfg.compress?.keys.length) {
|
|
@@ -202,13 +203,13 @@ export class CommonDao {
|
|
|
202
203
|
return pipeline.mapSync(dbm => this.dbmToBM(dbm, opt), { errorMode: opt.errorMode });
|
|
203
204
|
}
|
|
204
205
|
async queryIds(q, opt = {}) {
|
|
205
|
-
this.
|
|
206
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
206
207
|
q.table = opt.table || q.table;
|
|
207
208
|
const { rows } = await this.cfg.db.runQuery(q.select(['id']), opt);
|
|
208
209
|
return rows.map(r => r.id);
|
|
209
210
|
}
|
|
210
211
|
streamQueryIds(q, opt = {}) {
|
|
211
|
-
this.
|
|
212
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
212
213
|
q.table = opt.table || q.table;
|
|
213
214
|
opt.errorMode ||= ErrorMode.SUPPRESS;
|
|
214
215
|
return this.cfg.db.streamQuery(q.select(['id']), opt).mapSync((r) => r.id);
|
|
@@ -490,7 +491,7 @@ export class CommonDao {
|
|
|
490
491
|
* This is expected to be more memory-efficient way of deleting large number of rows.
|
|
491
492
|
*/
|
|
492
493
|
async deleteByQuery(q, opt = {}) {
|
|
493
|
-
this.
|
|
494
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
494
495
|
this.requireWriteAccess();
|
|
495
496
|
this.requireObjectMutability(opt);
|
|
496
497
|
q.table = opt.table || q.table;
|
|
@@ -526,10 +527,12 @@ export class CommonDao {
|
|
|
526
527
|
async patchByIds(ids, patch, opt = {}) {
|
|
527
528
|
if (!ids.length)
|
|
528
529
|
return 0;
|
|
530
|
+
this.assertNotCompressed(Object.keys(patch), 'patchByIds');
|
|
529
531
|
return await this.patchByQuery(this.query().filterIn('id', ids), patch, opt);
|
|
530
532
|
}
|
|
531
533
|
async patchByQuery(q, patch, opt = {}) {
|
|
532
|
-
this.
|
|
534
|
+
this.validateQuery(q); // throws if query uses `excludeFromIndexes` property
|
|
535
|
+
this.assertNotCompressed(Object.keys(patch), 'patchByQuery');
|
|
533
536
|
this.requireWriteAccess();
|
|
534
537
|
this.requireObjectMutability(opt);
|
|
535
538
|
q.table = opt.table || q.table;
|
|
@@ -541,6 +544,7 @@ export class CommonDao {
|
|
|
541
544
|
* @experimental
|
|
542
545
|
*/
|
|
543
546
|
async increment(prop, id, by = 1, opt = {}) {
|
|
547
|
+
this.assertNotCompressed([prop], 'increment');
|
|
544
548
|
this.requireWriteAccess();
|
|
545
549
|
this.requireObjectMutability(opt);
|
|
546
550
|
const { table } = this.cfg;
|
|
@@ -555,6 +559,7 @@ export class CommonDao {
|
|
|
555
559
|
* @experimental
|
|
556
560
|
*/
|
|
557
561
|
async incrementBatch(prop, incrementMap, opt = {}) {
|
|
562
|
+
this.assertNotCompressed([prop], 'incrementBatch');
|
|
558
563
|
this.requireWriteAccess();
|
|
559
564
|
this.requireObjectMutability(opt);
|
|
560
565
|
const { table } = this.cfg;
|
|
@@ -679,6 +684,16 @@ export class CommonDao {
|
|
|
679
684
|
dbm.__compressed = undefined;
|
|
680
685
|
Object.assign(dbm, properties);
|
|
681
686
|
}
|
|
687
|
+
assertNotCompressed(keys, op) {
|
|
688
|
+
if (!keys)
|
|
689
|
+
return;
|
|
690
|
+
const compressKeys = this.cfg.compress?.keys;
|
|
691
|
+
if (!compressKeys?.length)
|
|
692
|
+
return;
|
|
693
|
+
for (const k of keys) {
|
|
694
|
+
_assert(k !== '__compressed' && !compressKeys.includes(k), `${op} on ${this.cfg.table} cannot touch compressed property: ${k}`);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
682
697
|
anyToDBM(dbm, _opt = {}) {
|
|
683
698
|
if (!dbm)
|
|
684
699
|
return null;
|
|
@@ -945,6 +960,24 @@ export class CommonDao {
|
|
|
945
960
|
table: this.cfg.table,
|
|
946
961
|
});
|
|
947
962
|
}
|
|
963
|
+
validateQuery(q) {
|
|
964
|
+
this.validateQueryIndexes(q);
|
|
965
|
+
this.validateQueryCompression(q);
|
|
966
|
+
}
|
|
967
|
+
validateQueryCompression(q) {
|
|
968
|
+
const { _filters, _orders, _selectedFieldNames } = q;
|
|
969
|
+
if (_filters) {
|
|
970
|
+
const columnNames = _filters.map(cfg => cfg.name);
|
|
971
|
+
this.assertNotCompressed(columnNames, 'filter');
|
|
972
|
+
}
|
|
973
|
+
if (_orders) {
|
|
974
|
+
const columnNames = _orders.map(cfg => cfg.name);
|
|
975
|
+
this.assertNotCompressed(columnNames, 'order');
|
|
976
|
+
}
|
|
977
|
+
if (_selectedFieldNames) {
|
|
978
|
+
this.assertNotCompressed(_selectedFieldNames, 'select');
|
|
979
|
+
}
|
|
980
|
+
}
|
|
948
981
|
/**
|
|
949
982
|
* Throws if query uses a property that is in `excludeFromIndexes` list.
|
|
950
983
|
*/
|
package/package.json
CHANGED
|
@@ -10,13 +10,6 @@ import {
|
|
|
10
10
|
_pick,
|
|
11
11
|
} from '@naturalcycles/js-lib/object/object.util.js'
|
|
12
12
|
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
13
|
import type {
|
|
21
14
|
BaseDBEntity,
|
|
22
15
|
NonNegativeInteger,
|
|
@@ -24,6 +17,13 @@ import type {
|
|
|
24
17
|
StringMap,
|
|
25
18
|
Unsaved,
|
|
26
19
|
} from '@naturalcycles/js-lib/types'
|
|
20
|
+
import {
|
|
21
|
+
_objectKeys,
|
|
22
|
+
_passthroughPredicate,
|
|
23
|
+
_stringMapEntries,
|
|
24
|
+
_stringMapValues,
|
|
25
|
+
_typeCast,
|
|
26
|
+
} from '@naturalcycles/js-lib/types'
|
|
27
27
|
import { stringId } from '@naturalcycles/nodejs-lib'
|
|
28
28
|
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
29
29
|
import type { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
@@ -189,6 +189,7 @@ export class CommonDao<
|
|
|
189
189
|
q._selectedFieldNames?.length === 1,
|
|
190
190
|
`runQuerySingleColumn requires exactly 1 column to be selected: ${q.pretty()}`,
|
|
191
191
|
)
|
|
192
|
+
this.assertNotCompressed(q._selectedFieldNames, 'runQuerySingleColumn')
|
|
192
193
|
|
|
193
194
|
const col = q._selectedFieldNames[0]!
|
|
194
195
|
|
|
@@ -212,7 +213,7 @@ export class CommonDao<
|
|
|
212
213
|
q: DBQuery<DBM>,
|
|
213
214
|
opt: CommonDaoReadOptions = {},
|
|
214
215
|
): Promise<RunQueryResult<BM>> {
|
|
215
|
-
this.
|
|
216
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
216
217
|
q.table = opt.table || q.table
|
|
217
218
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
|
|
218
219
|
const isPartialQuery = !!q._selectedFieldNames
|
|
@@ -233,7 +234,7 @@ export class CommonDao<
|
|
|
233
234
|
q: DBQuery<DBM>,
|
|
234
235
|
opt: CommonDaoReadOptions = {},
|
|
235
236
|
): Promise<RunQueryResult<DBM>> {
|
|
236
|
-
this.
|
|
237
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
237
238
|
q.table = opt.table || q.table
|
|
238
239
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
|
|
239
240
|
const isPartialQuery = !!q._selectedFieldNames
|
|
@@ -243,13 +244,13 @@ export class CommonDao<
|
|
|
243
244
|
}
|
|
244
245
|
|
|
245
246
|
async runQueryCount(q: DBQuery<DBM>, opt: CommonDaoReadOptions = {}): Promise<number> {
|
|
246
|
-
this.
|
|
247
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
247
248
|
q.table = opt.table || q.table
|
|
248
249
|
return await this.cfg.db.runQueryCount(q, opt)
|
|
249
250
|
}
|
|
250
251
|
|
|
251
252
|
streamQueryAsDBM(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<DBM> = {}): Pipeline<DBM> {
|
|
252
|
-
this.
|
|
253
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
253
254
|
q.table = opt.table || q.table
|
|
254
255
|
let pipeline = this.cfg.db.streamQuery<DBM>(q, opt)
|
|
255
256
|
|
|
@@ -267,7 +268,7 @@ export class CommonDao<
|
|
|
267
268
|
}
|
|
268
269
|
|
|
269
270
|
streamQuery(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<BM> = {}): Pipeline<BM> {
|
|
270
|
-
this.
|
|
271
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
271
272
|
q.table = opt.table || q.table
|
|
272
273
|
let pipeline = this.cfg.db.streamQuery<DBM>(q, opt)
|
|
273
274
|
|
|
@@ -285,14 +286,14 @@ export class CommonDao<
|
|
|
285
286
|
}
|
|
286
287
|
|
|
287
288
|
async queryIds(q: DBQuery<DBM>, opt: CommonDaoReadOptions = {}): Promise<ID[]> {
|
|
288
|
-
this.
|
|
289
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
289
290
|
q.table = opt.table || q.table
|
|
290
291
|
const { rows } = await this.cfg.db.runQuery(q.select(['id']), opt)
|
|
291
292
|
return rows.map(r => r.id as ID)
|
|
292
293
|
}
|
|
293
294
|
|
|
294
295
|
streamQueryIds(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<ID> = {}): Pipeline<ID> {
|
|
295
|
-
this.
|
|
296
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
296
297
|
q.table = opt.table || q.table
|
|
297
298
|
opt.errorMode ||= ErrorMode.SUPPRESS
|
|
298
299
|
|
|
@@ -650,7 +651,7 @@ export class CommonDao<
|
|
|
650
651
|
q: DBQuery<DBM>,
|
|
651
652
|
opt: CommonDaoStreamDeleteOptions<DBM> = {},
|
|
652
653
|
): Promise<number> {
|
|
653
|
-
this.
|
|
654
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
654
655
|
this.requireWriteAccess()
|
|
655
656
|
this.requireObjectMutability(opt)
|
|
656
657
|
q.table = opt.table || q.table
|
|
@@ -691,6 +692,7 @@ export class CommonDao<
|
|
|
691
692
|
|
|
692
693
|
async patchByIds(ids: ID[], patch: Partial<DBM>, opt: CommonDaoOptions = {}): Promise<number> {
|
|
693
694
|
if (!ids.length) return 0
|
|
695
|
+
this.assertNotCompressed(Object.keys(patch), 'patchByIds')
|
|
694
696
|
return await this.patchByQuery(this.query().filterIn('id', ids), patch, opt)
|
|
695
697
|
}
|
|
696
698
|
|
|
@@ -699,7 +701,8 @@ export class CommonDao<
|
|
|
699
701
|
patch: Partial<DBM>,
|
|
700
702
|
opt: CommonDaoOptions = {},
|
|
701
703
|
): Promise<number> {
|
|
702
|
-
this.
|
|
704
|
+
this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
|
|
705
|
+
this.assertNotCompressed(Object.keys(patch), 'patchByQuery')
|
|
703
706
|
this.requireWriteAccess()
|
|
704
707
|
this.requireObjectMutability(opt)
|
|
705
708
|
q.table = opt.table || q.table
|
|
@@ -712,6 +715,7 @@ export class CommonDao<
|
|
|
712
715
|
* @experimental
|
|
713
716
|
*/
|
|
714
717
|
async increment(prop: keyof DBM, id: ID, by = 1, opt: CommonDaoOptions = {}): Promise<number> {
|
|
718
|
+
this.assertNotCompressed([prop], 'increment')
|
|
715
719
|
this.requireWriteAccess()
|
|
716
720
|
this.requireObjectMutability(opt)
|
|
717
721
|
const { table } = this.cfg
|
|
@@ -731,6 +735,7 @@ export class CommonDao<
|
|
|
731
735
|
incrementMap: StringMap<number>,
|
|
732
736
|
opt: CommonDaoOptions = {},
|
|
733
737
|
): Promise<StringMap<number>> {
|
|
738
|
+
this.assertNotCompressed([prop], 'incrementBatch')
|
|
734
739
|
this.requireWriteAccess()
|
|
735
740
|
this.requireObjectMutability(opt)
|
|
736
741
|
const { table } = this.cfg
|
|
@@ -877,6 +882,19 @@ export class CommonDao<
|
|
|
877
882
|
Object.assign(dbm, properties)
|
|
878
883
|
}
|
|
879
884
|
|
|
885
|
+
private assertNotCompressed(keys: (keyof DBM | string)[] | undefined, op: string): void {
|
|
886
|
+
if (!keys) return
|
|
887
|
+
|
|
888
|
+
const compressKeys = this.cfg.compress?.keys
|
|
889
|
+
if (!compressKeys?.length) return
|
|
890
|
+
for (const k of keys) {
|
|
891
|
+
_assert(
|
|
892
|
+
k !== '__compressed' && !compressKeys.includes(k as keyof DBM),
|
|
893
|
+
`${op} on ${this.cfg.table} cannot touch compressed property: ${k as string}`,
|
|
894
|
+
)
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
880
898
|
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): null
|
|
881
899
|
anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM
|
|
882
900
|
anyToDBM(dbm?: DBM, _opt: CommonDaoOptions = {}): DBM | null {
|
|
@@ -1212,6 +1230,29 @@ export class CommonDao<
|
|
|
1212
1230
|
})
|
|
1213
1231
|
}
|
|
1214
1232
|
|
|
1233
|
+
private validateQuery(q: DBQuery<DBM>): void {
|
|
1234
|
+
this.validateQueryIndexes(q)
|
|
1235
|
+
this.validateQueryCompression(q)
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
private validateQueryCompression(q: DBQuery<DBM>): void {
|
|
1239
|
+
const { _filters, _orders, _selectedFieldNames } = q
|
|
1240
|
+
|
|
1241
|
+
if (_filters) {
|
|
1242
|
+
const columnNames = _filters.map(cfg => cfg.name)
|
|
1243
|
+
this.assertNotCompressed(columnNames, 'filter')
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
if (_orders) {
|
|
1247
|
+
const columnNames = _orders.map(cfg => cfg.name)
|
|
1248
|
+
this.assertNotCompressed(columnNames, 'order')
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
if (_selectedFieldNames) {
|
|
1252
|
+
this.assertNotCompressed(_selectedFieldNames, 'select')
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1215
1256
|
/**
|
|
1216
1257
|
* Throws if query uses a property that is in `excludeFromIndexes` list.
|
|
1217
1258
|
*/
|