@naturalcycles/db-lib 10.56.1 → 10.56.2
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,7 +170,6 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
170
170
|
* Mutates `dbm`.
|
|
171
171
|
*/
|
|
172
172
|
private decompress;
|
|
173
|
-
private assertNotCompressed;
|
|
174
173
|
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): null;
|
|
175
174
|
anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM;
|
|
176
175
|
anyToDBMs(rows: DBM[], opt?: CommonDaoOptions): DBM[];
|
|
@@ -218,8 +217,6 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
218
217
|
* Throws if readOnly is true
|
|
219
218
|
*/
|
|
220
219
|
private requireObjectMutability;
|
|
221
|
-
private validateQuery;
|
|
222
|
-
private validateQueryCompression;
|
|
223
220
|
/**
|
|
224
221
|
* Throws if query uses a property that is in `excludeFromIndexes` list.
|
|
225
222
|
*/
|
|
@@ -130,7 +130,6 @@ 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');
|
|
134
133
|
const col = q._selectedFieldNames[0];
|
|
135
134
|
const { rows } = await this.runQueryExtended(q, opt);
|
|
136
135
|
return rows.map((r) => r[col]);
|
|
@@ -145,7 +144,7 @@ export class CommonDao {
|
|
|
145
144
|
return _uniqBy(results, r => r.id);
|
|
146
145
|
}
|
|
147
146
|
async runQueryExtended(q, opt = {}) {
|
|
148
|
-
this.
|
|
147
|
+
this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
|
|
149
148
|
q.table = opt.table || q.table;
|
|
150
149
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery(q, opt);
|
|
151
150
|
const isPartialQuery = !!q._selectedFieldNames;
|
|
@@ -161,7 +160,7 @@ export class CommonDao {
|
|
|
161
160
|
return rows;
|
|
162
161
|
}
|
|
163
162
|
async runQueryExtendedAsDBM(q, opt = {}) {
|
|
164
|
-
this.
|
|
163
|
+
this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
|
|
165
164
|
q.table = opt.table || q.table;
|
|
166
165
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery(q, opt);
|
|
167
166
|
const isPartialQuery = !!q._selectedFieldNames;
|
|
@@ -170,12 +169,12 @@ export class CommonDao {
|
|
|
170
169
|
return { rows: dbms, ...queryResult };
|
|
171
170
|
}
|
|
172
171
|
async runQueryCount(q, opt = {}) {
|
|
173
|
-
this.
|
|
172
|
+
this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
|
|
174
173
|
q.table = opt.table || q.table;
|
|
175
174
|
return await this.cfg.db.runQueryCount(q, opt);
|
|
176
175
|
}
|
|
177
176
|
streamQueryAsDBM(q, opt = {}) {
|
|
178
|
-
this.
|
|
177
|
+
this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
|
|
179
178
|
q.table = opt.table || q.table;
|
|
180
179
|
let pipeline = this.cfg.db.streamQuery(q, opt);
|
|
181
180
|
if (this.cfg.compress?.keys.length) {
|
|
@@ -189,7 +188,7 @@ export class CommonDao {
|
|
|
189
188
|
return pipeline.mapSync(dbm => this.anyToDBM(dbm, opt), { errorMode: opt.errorMode });
|
|
190
189
|
}
|
|
191
190
|
streamQuery(q, opt = {}) {
|
|
192
|
-
this.
|
|
191
|
+
this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
|
|
193
192
|
q.table = opt.table || q.table;
|
|
194
193
|
let pipeline = this.cfg.db.streamQuery(q, opt);
|
|
195
194
|
if (this.cfg.compress?.keys.length) {
|
|
@@ -203,13 +202,13 @@ export class CommonDao {
|
|
|
203
202
|
return pipeline.mapSync(dbm => this.dbmToBM(dbm, opt), { errorMode: opt.errorMode });
|
|
204
203
|
}
|
|
205
204
|
async queryIds(q, opt = {}) {
|
|
206
|
-
this.
|
|
205
|
+
this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
|
|
207
206
|
q.table = opt.table || q.table;
|
|
208
207
|
const { rows } = await this.cfg.db.runQuery(q.select(['id']), opt);
|
|
209
208
|
return rows.map(r => r.id);
|
|
210
209
|
}
|
|
211
210
|
streamQueryIds(q, opt = {}) {
|
|
212
|
-
this.
|
|
211
|
+
this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
|
|
213
212
|
q.table = opt.table || q.table;
|
|
214
213
|
opt.errorMode ||= ErrorMode.SUPPRESS;
|
|
215
214
|
return this.cfg.db.streamQuery(q.select(['id']), opt).mapSync((r) => r.id);
|
|
@@ -491,7 +490,7 @@ export class CommonDao {
|
|
|
491
490
|
* This is expected to be more memory-efficient way of deleting large number of rows.
|
|
492
491
|
*/
|
|
493
492
|
async deleteByQuery(q, opt = {}) {
|
|
494
|
-
this.
|
|
493
|
+
this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
|
|
495
494
|
this.requireWriteAccess();
|
|
496
495
|
this.requireObjectMutability(opt);
|
|
497
496
|
q.table = opt.table || q.table;
|
|
@@ -527,12 +526,10 @@ export class CommonDao {
|
|
|
527
526
|
async patchByIds(ids, patch, opt = {}) {
|
|
528
527
|
if (!ids.length)
|
|
529
528
|
return 0;
|
|
530
|
-
this.assertNotCompressed(Object.keys(patch), 'patchByIds');
|
|
531
529
|
return await this.patchByQuery(this.query().filterIn('id', ids), patch, opt);
|
|
532
530
|
}
|
|
533
531
|
async patchByQuery(q, patch, opt = {}) {
|
|
534
|
-
this.
|
|
535
|
-
this.assertNotCompressed(Object.keys(patch), 'patchByQuery');
|
|
532
|
+
this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
|
|
536
533
|
this.requireWriteAccess();
|
|
537
534
|
this.requireObjectMutability(opt);
|
|
538
535
|
q.table = opt.table || q.table;
|
|
@@ -544,7 +541,6 @@ export class CommonDao {
|
|
|
544
541
|
* @experimental
|
|
545
542
|
*/
|
|
546
543
|
async increment(prop, id, by = 1, opt = {}) {
|
|
547
|
-
this.assertNotCompressed([prop], 'increment');
|
|
548
544
|
this.requireWriteAccess();
|
|
549
545
|
this.requireObjectMutability(opt);
|
|
550
546
|
const { table } = this.cfg;
|
|
@@ -559,7 +555,6 @@ export class CommonDao {
|
|
|
559
555
|
* @experimental
|
|
560
556
|
*/
|
|
561
557
|
async incrementBatch(prop, incrementMap, opt = {}) {
|
|
562
|
-
this.assertNotCompressed([prop], 'incrementBatch');
|
|
563
558
|
this.requireWriteAccess();
|
|
564
559
|
this.requireObjectMutability(opt);
|
|
565
560
|
const { table } = this.cfg;
|
|
@@ -684,16 +679,6 @@ export class CommonDao {
|
|
|
684
679
|
dbm.__compressed = undefined;
|
|
685
680
|
Object.assign(dbm, properties);
|
|
686
681
|
}
|
|
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
|
-
}
|
|
697
682
|
anyToDBM(dbm, _opt = {}) {
|
|
698
683
|
if (!dbm)
|
|
699
684
|
return null;
|
|
@@ -960,24 +945,6 @@ export class CommonDao {
|
|
|
960
945
|
table: this.cfg.table,
|
|
961
946
|
});
|
|
962
947
|
}
|
|
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
|
-
}
|
|
981
948
|
/**
|
|
982
949
|
* Throws if query uses a property that is in `excludeFromIndexes` list.
|
|
983
950
|
*/
|
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 type {
|
|
14
|
-
BaseDBEntity,
|
|
15
|
-
NonNegativeInteger,
|
|
16
|
-
ObjectWithId,
|
|
17
|
-
StringMap,
|
|
18
|
-
Unsaved,
|
|
19
|
-
} from '@naturalcycles/js-lib/types'
|
|
20
13
|
import {
|
|
21
14
|
_objectKeys,
|
|
22
15
|
_passthroughPredicate,
|
|
@@ -24,6 +17,13 @@ import {
|
|
|
24
17
|
_stringMapValues,
|
|
25
18
|
_typeCast,
|
|
26
19
|
} from '@naturalcycles/js-lib/types'
|
|
20
|
+
import type {
|
|
21
|
+
BaseDBEntity,
|
|
22
|
+
NonNegativeInteger,
|
|
23
|
+
ObjectWithId,
|
|
24
|
+
StringMap,
|
|
25
|
+
Unsaved,
|
|
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,7 +189,6 @@ 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')
|
|
193
192
|
|
|
194
193
|
const col = q._selectedFieldNames[0]!
|
|
195
194
|
|
|
@@ -213,7 +212,7 @@ export class CommonDao<
|
|
|
213
212
|
q: DBQuery<DBM>,
|
|
214
213
|
opt: CommonDaoReadOptions = {},
|
|
215
214
|
): Promise<RunQueryResult<BM>> {
|
|
216
|
-
this.
|
|
215
|
+
this.validateQueryIndexes(q) // throws if query uses `excludeFromIndexes` property
|
|
217
216
|
q.table = opt.table || q.table
|
|
218
217
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
|
|
219
218
|
const isPartialQuery = !!q._selectedFieldNames
|
|
@@ -234,7 +233,7 @@ export class CommonDao<
|
|
|
234
233
|
q: DBQuery<DBM>,
|
|
235
234
|
opt: CommonDaoReadOptions = {},
|
|
236
235
|
): Promise<RunQueryResult<DBM>> {
|
|
237
|
-
this.
|
|
236
|
+
this.validateQueryIndexes(q) // throws if query uses `excludeFromIndexes` property
|
|
238
237
|
q.table = opt.table || q.table
|
|
239
238
|
const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
|
|
240
239
|
const isPartialQuery = !!q._selectedFieldNames
|
|
@@ -244,13 +243,13 @@ export class CommonDao<
|
|
|
244
243
|
}
|
|
245
244
|
|
|
246
245
|
async runQueryCount(q: DBQuery<DBM>, opt: CommonDaoReadOptions = {}): Promise<number> {
|
|
247
|
-
this.
|
|
246
|
+
this.validateQueryIndexes(q) // throws if query uses `excludeFromIndexes` property
|
|
248
247
|
q.table = opt.table || q.table
|
|
249
248
|
return await this.cfg.db.runQueryCount(q, opt)
|
|
250
249
|
}
|
|
251
250
|
|
|
252
251
|
streamQueryAsDBM(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<DBM> = {}): Pipeline<DBM> {
|
|
253
|
-
this.
|
|
252
|
+
this.validateQueryIndexes(q) // throws if query uses `excludeFromIndexes` property
|
|
254
253
|
q.table = opt.table || q.table
|
|
255
254
|
let pipeline = this.cfg.db.streamQuery<DBM>(q, opt)
|
|
256
255
|
|
|
@@ -268,7 +267,7 @@ export class CommonDao<
|
|
|
268
267
|
}
|
|
269
268
|
|
|
270
269
|
streamQuery(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<BM> = {}): Pipeline<BM> {
|
|
271
|
-
this.
|
|
270
|
+
this.validateQueryIndexes(q) // throws if query uses `excludeFromIndexes` property
|
|
272
271
|
q.table = opt.table || q.table
|
|
273
272
|
let pipeline = this.cfg.db.streamQuery<DBM>(q, opt)
|
|
274
273
|
|
|
@@ -286,14 +285,14 @@ export class CommonDao<
|
|
|
286
285
|
}
|
|
287
286
|
|
|
288
287
|
async queryIds(q: DBQuery<DBM>, opt: CommonDaoReadOptions = {}): Promise<ID[]> {
|
|
289
|
-
this.
|
|
288
|
+
this.validateQueryIndexes(q) // throws if query uses `excludeFromIndexes` property
|
|
290
289
|
q.table = opt.table || q.table
|
|
291
290
|
const { rows } = await this.cfg.db.runQuery(q.select(['id']), opt)
|
|
292
291
|
return rows.map(r => r.id as ID)
|
|
293
292
|
}
|
|
294
293
|
|
|
295
294
|
streamQueryIds(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<ID> = {}): Pipeline<ID> {
|
|
296
|
-
this.
|
|
295
|
+
this.validateQueryIndexes(q) // throws if query uses `excludeFromIndexes` property
|
|
297
296
|
q.table = opt.table || q.table
|
|
298
297
|
opt.errorMode ||= ErrorMode.SUPPRESS
|
|
299
298
|
|
|
@@ -651,7 +650,7 @@ export class CommonDao<
|
|
|
651
650
|
q: DBQuery<DBM>,
|
|
652
651
|
opt: CommonDaoStreamDeleteOptions<DBM> = {},
|
|
653
652
|
): Promise<number> {
|
|
654
|
-
this.
|
|
653
|
+
this.validateQueryIndexes(q) // throws if query uses `excludeFromIndexes` property
|
|
655
654
|
this.requireWriteAccess()
|
|
656
655
|
this.requireObjectMutability(opt)
|
|
657
656
|
q.table = opt.table || q.table
|
|
@@ -692,7 +691,6 @@ export class CommonDao<
|
|
|
692
691
|
|
|
693
692
|
async patchByIds(ids: ID[], patch: Partial<DBM>, opt: CommonDaoOptions = {}): Promise<number> {
|
|
694
693
|
if (!ids.length) return 0
|
|
695
|
-
this.assertNotCompressed(Object.keys(patch), 'patchByIds')
|
|
696
694
|
return await this.patchByQuery(this.query().filterIn('id', ids), patch, opt)
|
|
697
695
|
}
|
|
698
696
|
|
|
@@ -701,8 +699,7 @@ export class CommonDao<
|
|
|
701
699
|
patch: Partial<DBM>,
|
|
702
700
|
opt: CommonDaoOptions = {},
|
|
703
701
|
): Promise<number> {
|
|
704
|
-
this.
|
|
705
|
-
this.assertNotCompressed(Object.keys(patch), 'patchByQuery')
|
|
702
|
+
this.validateQueryIndexes(q) // throws if query uses `excludeFromIndexes` property
|
|
706
703
|
this.requireWriteAccess()
|
|
707
704
|
this.requireObjectMutability(opt)
|
|
708
705
|
q.table = opt.table || q.table
|
|
@@ -715,7 +712,6 @@ export class CommonDao<
|
|
|
715
712
|
* @experimental
|
|
716
713
|
*/
|
|
717
714
|
async increment(prop: keyof DBM, id: ID, by = 1, opt: CommonDaoOptions = {}): Promise<number> {
|
|
718
|
-
this.assertNotCompressed([prop], 'increment')
|
|
719
715
|
this.requireWriteAccess()
|
|
720
716
|
this.requireObjectMutability(opt)
|
|
721
717
|
const { table } = this.cfg
|
|
@@ -735,7 +731,6 @@ export class CommonDao<
|
|
|
735
731
|
incrementMap: StringMap<number>,
|
|
736
732
|
opt: CommonDaoOptions = {},
|
|
737
733
|
): Promise<StringMap<number>> {
|
|
738
|
-
this.assertNotCompressed([prop], 'incrementBatch')
|
|
739
734
|
this.requireWriteAccess()
|
|
740
735
|
this.requireObjectMutability(opt)
|
|
741
736
|
const { table } = this.cfg
|
|
@@ -882,19 +877,6 @@ export class CommonDao<
|
|
|
882
877
|
Object.assign(dbm, properties)
|
|
883
878
|
}
|
|
884
879
|
|
|
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
|
-
|
|
898
880
|
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): null
|
|
899
881
|
anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM
|
|
900
882
|
anyToDBM(dbm?: DBM, _opt: CommonDaoOptions = {}): DBM | null {
|
|
@@ -1230,29 +1212,6 @@ export class CommonDao<
|
|
|
1230
1212
|
})
|
|
1231
1213
|
}
|
|
1232
1214
|
|
|
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
|
-
|
|
1256
1215
|
/**
|
|
1257
1216
|
* Throws if query uses a property that is in `excludeFromIndexes` list.
|
|
1258
1217
|
*/
|