@samet-it/be-db-common 1.1.7 → 1.1.9

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/README.md CHANGED
@@ -35,6 +35,12 @@ npm i @samet-it/be-db-common
35
35
  - [function dbLines()](./src/line/db-lines.impl.ts)
36
36
  - [abstract class DbRepo](./src/repo/db.repo.ts)
37
37
 
38
+ ## Errors
39
+ - `base error` [DbError](./src/error/db.error.ts)
40
+ - `execute error` [DbExecuteError](./src/error/db-execute.error.ts)
41
+ - `invalid value error` [DbInvalidValueError](./src/error/db-invalid-value.error.ts)
42
+ - `not supported error` [DbNotSupportedError](./src/error/db-not-supported.error.ts)
43
+
38
44
  ## Development
39
45
  > You can start to develop on it
40
46
  >
@@ -157,3 +157,14 @@ export type DbEvent = 'connected' | 'disconnected' | 'first-connected';
157
157
  * - It uses ASL
158
158
  * */
159
159
  export type UserFetcherLambda = () => Promise<KeyValue>;
160
+ /**
161
+ * Db direct connection option
162
+ * */
163
+ export interface DbConnDirectOpt extends DbConnOpt {
164
+ /**
165
+ * logger name
166
+ *
167
+ * @type {string}
168
+ * */
169
+ name?: string;
170
+ }
@@ -42,19 +42,63 @@ export declare abstract class DbRepo<CONN extends DbConnectionLike, OPT extends
42
42
  * @param {DbRepoOpt} opt - options
43
43
  * */
44
44
  protected constructor(conn: CONN, opt?: DbRepoOpt<ENT, ID>);
45
+ /**
46
+ * Check given options
47
+ * */
48
+ private _checkOptions;
49
+ /**
50
+ * Generate time value by configuration
51
+ *
52
+ * - hasIsoDate: true => string iso datetime
53
+ * - hasIsoDate: false => number timestamp
54
+ *
55
+ * @return {(string|number)}
56
+ * */
45
57
  protected get _now(): string | number;
58
+ /**
59
+ * Generate random index name
60
+ *
61
+ * @return {string}
62
+ * */
46
63
  protected get _randomIndexName(): string;
64
+ /**
65
+ * Check & validate index name
66
+ *
67
+ * @param {string} name - given index name
68
+ * @return {string} - validated index name
69
+ * */
47
70
  protected _indexName(name: string): string;
48
71
  /**
49
72
  * Fetch urn from doc as doc.urn
50
73
  * */
51
74
  protected _urnFromDoc(doc: Partial<ENT>, method: string): string;
75
+ /**
76
+ * Check key, understand it urn, id or portion
77
+ *
78
+ * @param {any} value - given id value
79
+ * @return {DbCheckKeysTuple} - key tuple as [value, field]
80
+ * */
52
81
  protected _checkKey(value: unknown): DbCheckKeysTuple<ID>;
53
82
  /**
54
83
  * Check array keys as id list or urn list
84
+ *
85
+ * @param {Array} keys - given keys
86
+ * @return {DbCheckKeysResult} - list of key tuple
87
+ * */
88
+ protected _checkKeys(keys: Array<unknown>): DbCheckKeysResult<ID>;
89
+ /**
90
+ * Is key urn or id
91
+ *
92
+ * @param {KeyValue} key - given key value
93
+ * @return boolean - is urn or not
55
94
  * */
56
- protected _checkKeys(keys: Array<KeyValue>): DbCheckKeysResult<ID>;
57
95
  protected _isUrn(key: KeyValue): boolean;
96
+ /**
97
+ * Build key to urn value with prefix
98
+ *
99
+ * @param {KeyValue} key - given key value
100
+ * @return {string} - urn value
101
+ * */
58
102
  protected _keyToUrn(key: KeyValue): string;
59
103
  /** @inheritDoc */
60
104
  get props(): Readonly<DbRepoProps<CONN, ENT, ID>>;
@@ -145,7 +189,7 @@ export declare abstract class DbRepo<CONN extends DbConnectionLike, OPT extends
145
189
  /** @inheritDoc */
146
190
  $toUrnTuple(urnRec: URN): UrnTuple;
147
191
  /** @inheritDoc */
148
- get(k1: KeyValue, p1?: OPT | string): Promise<ENT | undefined>;
192
+ get(keyLike: PORTION | KeyValue, p1?: OPT | string): Promise<ENT | undefined>;
149
193
  /** @inheritDoc */
150
194
  getByPrimary(key: string, p1?: OPT | string, ignoreCheck?: boolean): Promise<ENT | undefined>;
151
195
  /** @inheritDoc */
@@ -155,7 +199,7 @@ export declare abstract class DbRepo<CONN extends DbConnectionLike, OPT extends
155
199
  /** @inheritDoc */
156
200
  abstract $getBySecondary(key: KeyValue, p1?: OPT | string, ignoreCheck?: boolean): Promise<ENT | undefined>;
157
201
  /** @inheritDoc */
158
- exists(k1: KeyValue, p1?: OPT | string): Promise<boolean>;
202
+ exists(keyLike: PORTION | KeyValue, p1?: OPT | string): Promise<boolean>;
159
203
  /** @inheritDoc */
160
204
  existsByPrimary(key: string, p1?: OPT | string, ignoreCheck?: boolean): Promise<boolean>;
161
205
  /** @inheritDoc */
@@ -165,7 +209,7 @@ export declare abstract class DbRepo<CONN extends DbConnectionLike, OPT extends
165
209
  /** @inheritDoc */
166
210
  abstract $existsBySecondary(key: KeyValue, p1?: OPT | string, ignoreCheck?: boolean): Promise<boolean>;
167
211
  /** @inheritDoc */
168
- list(keys: Array<KeyValue>, p1?: OPT | string, ignoreCheck?: boolean): Promise<Array<ENT>>;
212
+ list(keyLikes: Array<PORTION | KeyValue>, p1?: OPT | string, ignoreCheck?: boolean): Promise<Array<ENT>>;
169
213
  /** @inheritDoc */
170
214
  abstract $listByPrimary(keys: string[], p1?: OPT | string, ignoreCheck?: boolean): Promise<Array<ENT>>;
171
215
  /** @inheritDoc */
@@ -189,7 +233,7 @@ export declare abstract class DbRepo<CONN extends DbConnectionLike, OPT extends
189
233
  /** @inheritDoc */
190
234
  abstract $upsert(doc: ENT, opt?: OPT | string, ignoreCheck?: boolean): Promise<string>;
191
235
  /** @inheritDoc */
192
- update(k1: KeyValue, doc: Partial<ENT>, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
236
+ update(k1: PORTION | KeyValue, doc: Partial<ENT>, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
193
237
  /** @inheritDoc */
194
238
  updateByPrimary(key: string, doc: Partial<ENT>, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
195
239
  /** @inheritDoc */
@@ -199,11 +243,11 @@ export declare abstract class DbRepo<CONN extends DbConnectionLike, OPT extends
199
243
  /** @inheritDoc */
200
244
  abstract $updateBySecondary(key: KeyValue, doc: Partial<ENT>, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
201
245
  /** @inheritDoc */
202
- set(key: KeyValue, doc: Partial<ENT>, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
246
+ set(key: PORTION | KeyValue, doc: Partial<ENT>, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
203
247
  /** @inheritDoc */
204
- unset(key: KeyValue, fields: Array<keyof ENT | KEYS | string>, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
248
+ unset(key: PORTION | KeyValue, fields: Array<keyof ENT | KEYS | string>, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
205
249
  /** @inheritDoc */
206
- remove(k1: KeyValue, p1?: OPT | string): Promise<string>;
250
+ remove(keyLike: PORTION | KeyValue, p1?: OPT | string): Promise<string>;
207
251
  /** @inheritDoc */
208
252
  removeByPrimary(key: string, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
209
253
  /** @inheritDoc */
@@ -213,7 +257,7 @@ export declare abstract class DbRepo<CONN extends DbConnectionLike, OPT extends
213
257
  /** @inheritDoc */
214
258
  abstract $removeBySecondary(key: KeyValue, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
215
259
  /** @inheritDoc */
216
- trash(k1: KeyValue, p1?: OPT | string): Promise<string>;
260
+ trash(keyLike: PORTION | KeyValue, p1?: OPT | string): Promise<string>;
217
261
  /** @inheritDoc */
218
262
  trashByPrimary(key: string, p1?: OPT | string, ignoreCheck?: boolean): Promise<string>;
219
263
  /** @inheritDoc */
@@ -225,24 +269,19 @@ export declare abstract class DbRepo<CONN extends DbConnectionLike, OPT extends
225
269
  /** @inheritDoc */
226
270
  $toDim<R extends IdDocLike<ID>>(doc: ENT, dim?: DIMS): Promise<R>;
227
271
  /** @inheritDoc */
228
- getDim<R extends IdDocLike<ID>>(key: KeyValue, dim?: DIMS): Promise<R>;
229
- /** @inheritDoc */
230
- listDims<R extends IdDocLike<ID>>(keys: Array<KeyValue>, dim?: DIMS, ignoreCheck?: boolean): Promise<Array<R>>;
231
- /** @inheritDoc */
232
- $toPair(doc: ENT): Promise<PAIR>;
272
+ getDim<R extends IdDocLike<ID>>(key: PORTION | KeyValue, dim?: DIMS): Promise<R>;
233
273
  /** @inheritDoc */
234
- getPair(key: KeyValue): Promise<PAIR>;
274
+ listDims<R extends IdDocLike<ID>>(keys: Array<PORTION | KeyValue>, dim?: DIMS, ignoreCheck?: boolean): Promise<Array<R>>;
235
275
  /** @inheritDoc */
236
- listPairs(keys: Array<KeyValue>, ignoreCheck?: boolean): Promise<Array<PAIR>>;
237
- $toView(doc: ENT): Promise<VIEW>;
276
+ getPair(key: PORTION | KeyValue): Promise<PAIR>;
238
277
  /** @inheritDoc */
239
- getView(identifier: string): Promise<VIEW>;
278
+ listPairs(keys: Array<PORTION | KeyValue>, ignoreCheck?: boolean): Promise<Array<PAIR>>;
240
279
  /** @inheritDoc */
241
- listViews(identifiers: Array<string>, ignoreCheck?: boolean): Promise<Array<VIEW>>;
280
+ getView(key: PORTION | KeyValue): Promise<VIEW>;
242
281
  /** @inheritDoc */
243
- $toPortion(doc: ENT): Promise<PORTION>;
282
+ listViews(keys: Array<PORTION | KeyValue>, ignoreCheck?: boolean): Promise<Array<VIEW>>;
244
283
  /** @inheritDoc */
245
- getPortion(identifier: string): Promise<PORTION>;
284
+ getPortion(key: PORTION | KeyValue): Promise<PORTION>;
246
285
  /** @inheritDoc */
247
- listPortions(identifiers: Array<string>, ignoreCheck?: boolean): Promise<Array<PORTION>>;
286
+ listPortions(keys: Array<PORTION | KeyValue>, ignoreCheck?: boolean): Promise<Array<PORTION>>;
248
287
  }
@@ -56,9 +56,18 @@ class DbRepo {
56
56
  if ((0, type_1.isObjectBare)(opt)) {
57
57
  this._opt = Object.assign(Object.assign({}, this._opt), opt);
58
58
  }
59
+ this._checkOptions();
60
+ this._props = Object.assign({ conn }, this._opt);
61
+ delete this._opt;
62
+ }
63
+ // region protected-method
64
+ /**
65
+ * Check given options
66
+ * */
67
+ _checkOptions() {
59
68
  // handle invalid cache
60
69
  if (!this._opt.cache) {
61
- this._opt.cache = { isConnected: false };
70
+ this._opt.cache = { isEnabled: false, isConnected: false };
62
71
  }
63
72
  if (this._opt.useVersion) {
64
73
  if (typeof this._opt.version !== 'number') {
@@ -99,18 +108,46 @@ class DbRepo {
99
108
  }
100
109
  });
101
110
  }
102
- this._props = Object.assign({ conn }, this._opt);
103
- delete this._opt;
111
+ const directOpt = this._opt;
112
+ if (typeof directOpt.$toUrnTuple === 'function') {
113
+ this.$toUrnTuple = directOpt.$toUrnTuple;
114
+ }
115
+ if (typeof directOpt.$toDim === 'function') {
116
+ this.$toDim = (doc, dim) => __awaiter(this, void 0, void 0, function* () {
117
+ return (yield directOpt.$toDim(doc, dim));
118
+ });
119
+ }
120
+ if (!Array.isArray(directOpt.indexedFields)) {
121
+ directOpt.indexedFields = [];
122
+ }
104
123
  }
105
- // region protected-method
124
+ /**
125
+ * Generate time value by configuration
126
+ *
127
+ * - hasIsoDate: true => string iso datetime
128
+ * - hasIsoDate: false => number timestamp
129
+ *
130
+ * @return {(string|number)}
131
+ * */
106
132
  get _now() {
107
133
  return this.hasIsoDate ? new Date().toISOString() : Date.now();
108
134
  }
135
+ /**
136
+ * Generate random index name
137
+ *
138
+ * @return {string}
139
+ * */
109
140
  get _randomIndexName() {
110
141
  return 'idx_' + (0, node_crypto_1.randomUUID)().match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
111
142
  .map(s => s.toLowerCase())
112
143
  .join('_');
113
144
  }
145
+ /**
146
+ * Check & validate index name
147
+ *
148
+ * @param {string} name - given index name
149
+ * @return {string} - validated index name
150
+ * */
114
151
  _indexName(name) {
115
152
  if (typeof name !== 'string') {
116
153
  return this._randomIndexName;
@@ -143,6 +180,12 @@ class DbRepo {
143
180
  (0, type_1.assertText)(doc.urn, Object.assign({ field: 'urn' }, param));
144
181
  return doc.urn;
145
182
  }
183
+ /**
184
+ * Check key, understand it urn, id or portion
185
+ *
186
+ * @param {any} value - given id value
187
+ * @return {DbCheckKeysTuple} - key tuple as [value, field]
188
+ * */
146
189
  _checkKey(value) {
147
190
  let id;
148
191
  switch (typeof value) {
@@ -182,18 +225,21 @@ class DbRepo {
182
225
  return [undefined, undefined];
183
226
  }
184
227
  const obj = value;
185
- if (obj.id !== undefined) {
186
- return this._checkKey(obj.id);
187
- }
188
228
  if (obj.urn !== undefined) {
189
229
  return this._checkKey(obj.urn);
190
230
  }
231
+ if (obj.id !== undefined) {
232
+ return this._checkKey(obj.id);
233
+ }
191
234
  break;
192
235
  }
193
236
  return [undefined, undefined];
194
237
  }
195
238
  /**
196
239
  * Check array keys as id list or urn list
240
+ *
241
+ * @param {Array} keys - given keys
242
+ * @return {DbCheckKeysResult} - list of key tuple
197
243
  * */
198
244
  _checkKeys(keys) {
199
245
  const result = { urns: [], ids: [], ordered: [] };
@@ -215,9 +261,21 @@ class DbRepo {
215
261
  }
216
262
  return result;
217
263
  }
264
+ /**
265
+ * Is key urn or id
266
+ *
267
+ * @param {KeyValue} key - given key value
268
+ * @return boolean - is urn or not
269
+ * */
218
270
  _isUrn(key) {
219
271
  return (typeof key === 'string') && key.startsWith(this.urnPrefix + ':');
220
272
  }
273
+ /**
274
+ * Build key to urn value with prefix
275
+ *
276
+ * @param {KeyValue} key - given key value
277
+ * @return {string} - urn value
278
+ * */
221
279
  _keyToUrn(key) {
222
280
  return `${this.urnPrefix}:${key}`;
223
281
  }
@@ -345,6 +403,10 @@ class DbRepo {
345
403
  }
346
404
  /** @inheritDoc */
347
405
  get urnLength() {
406
+ var _a, _b;
407
+ if (!this._props.urnLength) {
408
+ this._props.urnLength = (_b = (_a = this._props.urnPrefix) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
409
+ }
348
410
  return this._props.urnLength;
349
411
  }
350
412
  // endregion getter
@@ -443,17 +505,17 @@ class DbRepo {
443
505
  // endregion urn
444
506
  // region get
445
507
  /** @inheritDoc */
446
- get(k1, p1) {
508
+ get(keyLike, p1) {
447
509
  return __awaiter(this, void 0, void 0, function* () {
448
510
  const opt = this.buildOpt(p1, 'get');
449
- const [key, field] = this._checkKey(k1);
511
+ const [key, field] = this._checkKey(keyLike);
450
512
  switch (field) {
451
513
  case "urn":
452
514
  return this.getByPrimary(key, opt, true);
453
515
  case "id":
454
516
  return this.getBySecondary(key, opt, true);
455
517
  default:
456
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'get', queryName: opt.name });
518
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'get', queryName: opt.name });
457
519
  }
458
520
  });
459
521
  }
@@ -524,17 +586,17 @@ class DbRepo {
524
586
  // endregion get
525
587
  // region exists
526
588
  /** @inheritDoc */
527
- exists(k1, p1) {
589
+ exists(keyLike, p1) {
528
590
  return __awaiter(this, void 0, void 0, function* () {
529
591
  const opt = this.buildOpt(p1, 'exists');
530
- const [key, field] = this._checkKey(k1);
592
+ const [key, field] = this._checkKey(keyLike);
531
593
  switch (field) {
532
594
  case "urn":
533
595
  return this.existsByPrimary(key, opt, true);
534
596
  case "id":
535
597
  return this.existsBySecondary(key, opt, true);
536
598
  default:
537
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'exists', queryName: opt.name });
599
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'exists', queryName: opt.name });
538
600
  break;
539
601
  }
540
602
  });
@@ -592,13 +654,13 @@ class DbRepo {
592
654
  // endregion exists
593
655
  // region list
594
656
  /** @inheritDoc */
595
- list(keys, p1, ignoreCheck) {
657
+ list(keyLikes, p1, ignoreCheck) {
596
658
  return __awaiter(this, void 0, void 0, function* () {
597
659
  const opt = this.buildOpt(p1, 'list');
598
660
  if (!ignoreCheck) {
599
- (0, type_1.assertArray)(keys, { field: 'keys', where: 'DbRepo', method: 'list', queryName: opt.name });
661
+ (0, type_1.assertArray)(keyLikes, { field: 'keys', where: 'DbRepo', method: 'list', queryName: opt.name });
600
662
  }
601
- const result = this._checkKeys(keys);
663
+ const result = this._checkKeys(keyLikes);
602
664
  if (result.ordered.length < 1) {
603
665
  return [];
604
666
  }
@@ -944,17 +1006,17 @@ class DbRepo {
944
1006
  // endregion set
945
1007
  // region remove
946
1008
  /** @inheritDoc */
947
- remove(k1, p1) {
1009
+ remove(keyLike, p1) {
948
1010
  return __awaiter(this, void 0, void 0, function* () {
949
1011
  const opt = this.buildOpt(p1, 'remove');
950
- const [key, field] = this._checkKey(k1);
1012
+ const [key, field] = this._checkKey(keyLike);
951
1013
  switch (field) {
952
1014
  case "urn":
953
1015
  return this.removeByPrimary(key, opt, true);
954
1016
  case "id":
955
1017
  return this.removeBySecondary(key, opt, true);
956
1018
  default:
957
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'remove', queryName: opt.name });
1019
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'remove', queryName: opt.name });
958
1020
  }
959
1021
  });
960
1022
  }
@@ -1007,17 +1069,17 @@ class DbRepo {
1007
1069
  // endregion remove
1008
1070
  // region trash
1009
1071
  /** @inheritDoc */
1010
- trash(k1, p1) {
1072
+ trash(keyLike, p1) {
1011
1073
  return __awaiter(this, void 0, void 0, function* () {
1012
1074
  const opt = this.buildOpt(p1, 'trash');
1013
- const [key, field] = this._checkKey(k1);
1075
+ const [key, field] = this._checkKey(keyLike);
1014
1076
  switch (field) {
1015
1077
  case "urn":
1016
1078
  return this.trashByPrimary(key, opt, true);
1017
1079
  case "id":
1018
1080
  return this.trashBySecondary(key, opt, true);
1019
1081
  default:
1020
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'trash', queryName: opt.name });
1082
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'trash', queryName: opt.name });
1021
1083
  }
1022
1084
  });
1023
1085
  }
@@ -1088,14 +1150,17 @@ class DbRepo {
1088
1150
  return undefined;
1089
1151
  }
1090
1152
  switch (dim) {
1091
- case "portion":
1092
- return yield this.$toPortion(doc);
1093
1153
  case "view":
1094
- return yield this.$toView(doc);
1154
+ return doc;
1095
1155
  case "pair":
1096
- return yield this.$toPair(doc);
1097
- default:
1098
- return yield this.$toPortion(doc);
1156
+ return { id: doc.id, name: doc.name };
1157
+ default: // portion
1158
+ if (this.isIdSame) {
1159
+ return { id: doc.id };
1160
+ }
1161
+ else {
1162
+ return { id: doc.id, urn: doc.urn };
1163
+ }
1099
1164
  }
1100
1165
  });
1101
1166
  }
@@ -1123,15 +1188,6 @@ class DbRepo {
1123
1188
  // endregion dim
1124
1189
  // region pair
1125
1190
  /** @inheritDoc */
1126
- $toPair(doc) {
1127
- return __awaiter(this, void 0, void 0, function* () {
1128
- if (!doc) {
1129
- return undefined;
1130
- }
1131
- return { id: doc === null || doc === void 0 ? void 0 : doc.id, name: doc === null || doc === void 0 ? void 0 : doc.name };
1132
- });
1133
- }
1134
- /** @inheritDoc */
1135
1191
  getPair(key) {
1136
1192
  return __awaiter(this, void 0, void 0, function* () {
1137
1193
  return this.getDim(key, 'pair');
@@ -1145,44 +1201,30 @@ class DbRepo {
1145
1201
  }
1146
1202
  // endregion pair
1147
1203
  // region view
1148
- $toView(doc) {
1149
- return __awaiter(this, void 0, void 0, function* () {
1150
- return doc;
1151
- });
1152
- }
1153
1204
  /** @inheritDoc */
1154
- getView(identifier) {
1205
+ getView(key) {
1155
1206
  return __awaiter(this, void 0, void 0, function* () {
1156
- return this.getDim(identifier, 'view');
1207
+ return this.getDim(key, 'view');
1157
1208
  });
1158
1209
  }
1159
1210
  /** @inheritDoc */
1160
- listViews(identifiers, ignoreCheck) {
1211
+ listViews(keys, ignoreCheck) {
1161
1212
  return __awaiter(this, void 0, void 0, function* () {
1162
- return this.listDims(identifiers, 'view', ignoreCheck);
1213
+ return this.listDims(keys, 'view', ignoreCheck);
1163
1214
  });
1164
1215
  }
1165
1216
  // endregion view
1166
1217
  // region portion
1167
1218
  /** @inheritDoc */
1168
- $toPortion(doc) {
1169
- return __awaiter(this, void 0, void 0, function* () {
1170
- if (!doc) {
1171
- return undefined;
1172
- }
1173
- return { id: doc === null || doc === void 0 ? void 0 : doc.id };
1174
- });
1175
- }
1176
- /** @inheritDoc */
1177
- getPortion(identifier) {
1219
+ getPortion(key) {
1178
1220
  return __awaiter(this, void 0, void 0, function* () {
1179
- return this.getDim(identifier, 'portion');
1221
+ return this.getDim(key, 'portion');
1180
1222
  });
1181
1223
  }
1182
1224
  /** @inheritDoc */
1183
- listPortions(identifiers, ignoreCheck) {
1225
+ listPortions(keys, ignoreCheck) {
1184
1226
  return __awaiter(this, void 0, void 0, function* () {
1185
- return this.listDims(identifiers, 'portion', ignoreCheck);
1227
+ return this.listDims(keys, 'portion', ignoreCheck);
1186
1228
  });
1187
1229
  }
1188
1230
  }
@@ -1,5 +1,5 @@
1
- import type { DefDims, Entity, IdDocLike, Pair, Portion, UrnDocLike, UrnTuple, View } from "@samet-it/be-base-common";
2
- import { DbConnectionLike, UserFetcherLambda } from "../connection";
1
+ import type { DefDims, Entity, IdDocLike, Pair, Portion, UrnDef, UrnDocLike, UrnTuple, View } from "@samet-it/be-base-common";
2
+ import type { DbConnectionLike, UserFetcherLambda } from "../connection";
3
3
  import type { QueryAny, QueryRegular } from "@leyyo/query";
4
4
  import type { CacheChannelLike, CacheConnectionLike, CacheExecOpt } from "@samet-it/be-cache-common";
5
5
  import type { KeyValue, OmitError, Opt, StrKey } from "@leyyo/common";
@@ -255,12 +255,12 @@ export interface DbRepoLike<CONN extends DbConnectionLike, OPT extends DbExecOpt
255
255
  /**
256
256
  * Get document by key (primary or secondary)
257
257
  *
258
- * @param {KeyValue} key - primary or secondary key
258
+ * @param {Portion|KeyValue} key - primary or secondary key
259
259
  * @param {(opt?: OPT|string)?} opt - option or query name
260
260
  * @return {Promise<Entity?>} - document
261
261
  * @async
262
262
  * */
263
- get(key: KeyValue, opt?: OPT | string): Promise<ENT | undefined>;
263
+ get(key: PORTION | KeyValue, opt?: OPT | string): Promise<ENT | undefined>;
264
264
  /**
265
265
  * Get document by primary key
266
266
  *
@@ -304,12 +304,12 @@ export interface DbRepoLike<CONN extends DbConnectionLike, OPT extends DbExecOpt
304
304
  /**
305
305
  * Document exists by key (primary or secondary)
306
306
  *
307
- * @param {KeyValue} key - primary or secondary key
307
+ * @param {Portion|KeyValue} key - primary or secondary key
308
308
  * @param {(opt?: OPT|string)?} opt - option or query name
309
309
  * @return {Promise<boolean>} - exists or not
310
310
  * @async
311
311
  * */
312
- exists(key: KeyValue, opt?: OPT | string): Promise<boolean>;
312
+ exists(key: PORTION | KeyValue, opt?: OPT | string): Promise<boolean>;
313
313
  /**
314
314
  * Document exists by primary key
315
315
  *
@@ -353,13 +353,13 @@ export interface DbRepoLike<CONN extends DbConnectionLike, OPT extends DbExecOpt
353
353
  /**
354
354
  * List documents by keys (primary or secondary)
355
355
  *
356
- * @param {Array<KeyValue>} keys - primary or secondary keys
356
+ * @param {Array<Portion|KeyValue>} keys - primary or secondary keys
357
357
  * @param {(opt?: OPT|string)?} opt - option or query name
358
358
  * @param {boolean?} ignoreCheck - ignores to check input
359
359
  * @return {Promise<Array<Entity>>} - documents
360
360
  * @async
361
361
  * */
362
- list(keys: Array<KeyValue>, opt?: OPT | string, ignoreCheck?: boolean): Promise<Array<ENT>>;
362
+ list(keys: Array<PORTION | KeyValue>, opt?: OPT | string, ignoreCheck?: boolean): Promise<Array<ENT>>;
363
363
  /**
364
364
  * List documents by primary keys
365
365
  *
@@ -464,14 +464,14 @@ export interface DbRepoLike<CONN extends DbConnectionLike, OPT extends DbExecOpt
464
464
  /**
465
465
  * Update document by key (primary or secondary)
466
466
  *
467
- * @param {KeyValue} key - primary or secondary key
467
+ * @param {Portion|KeyValue} key - primary or secondary key
468
468
  * @param {Entity} doc - document
469
469
  * @param {(opt?: OPT|string)?} opt - option or query name
470
470
  * @param {boolean?} ignoreCheck - ignores to check input
471
471
  * @return {Promise<string>}
472
472
  * @async
473
473
  * */
474
- update(key: KeyValue, doc: Partial<ENT>, opt?: OPT | string, ignoreCheck?: boolean): Promise<string>;
474
+ update(key: PORTION | KeyValue, doc: Partial<ENT>, opt?: OPT | string, ignoreCheck?: boolean): Promise<string>;
475
475
  /**
476
476
  * Update document by primary key
477
477
  *
@@ -519,33 +519,33 @@ export interface DbRepoLike<CONN extends DbConnectionLike, OPT extends DbExecOpt
519
519
  /**
520
520
  * Set document with partial data by key (primary or secondary)
521
521
  *
522
- * @param {KeyValue} key - primary or secondary key
522
+ * @param {Portion|KeyValue} key - primary or secondary key
523
523
  * @param {Partial<Entity>} doc - partial entity will be set
524
524
  * @param {(opt?: OPT|string)?} opt - option or query name
525
525
  * @param {boolean?} ignoreCheck - ignores to check input
526
526
  * @return {Promise<string>}
527
527
  * @async
528
528
  * */
529
- set(key: KeyValue, doc: Partial<ENT>, opt?: OPT | string, ignoreCheck?: boolean): Promise<string>;
529
+ set(key: PORTION | KeyValue, doc: Partial<ENT>, opt?: OPT | string, ignoreCheck?: boolean): Promise<string>;
530
530
  /**
531
531
  * Unset document for given fields
532
532
  *
533
- * @param {KeyValue} key - primary or secondary key
533
+ * @param {Portion|KeyValue} key - primary or secondary key
534
534
  * @param {Array<string>} fields - fields will be unset
535
535
  * @param {(opt?: OPT|string)?} opt - option or query name
536
536
  * @param {boolean?} ignoreCheck - ignores to check input
537
537
  * @return {Promise<string>}
538
538
  * @async
539
539
  * */
540
- unset(key: KeyValue, fields: Array<keyof ENT | KEYS | string>, opt?: OPT | string, ignoreCheck?: boolean): Promise<string>;
540
+ unset(key: PORTION | KeyValue, fields: Array<keyof ENT | KEYS | string>, opt?: OPT | string, ignoreCheck?: boolean): Promise<string>;
541
541
  /**
542
542
  * Remove document (hard delete) by key (primary or secondary)
543
543
  *
544
- * @param {KeyValue} key - primary or secondary key
544
+ * @param {Portion|KeyValue} key - primary or secondary key
545
545
  * @param {(opt?: OPT|string)?} opt - option or query name
546
546
  * @async
547
547
  * */
548
- remove(key: KeyValue, opt?: OPT | string): Promise<string>;
548
+ remove(key: PORTION | KeyValue, opt?: OPT | string): Promise<string>;
549
549
  /**
550
550
  * Remove document by primary key (hard delete)
551
551
  *
@@ -585,11 +585,11 @@ export interface DbRepoLike<CONN extends DbConnectionLike, OPT extends DbExecOpt
585
585
  /**
586
586
  * Trash document (soft delete) by key (primary or secondary)
587
587
  *
588
- * @param {KeyValue} key - primary or secondary key
588
+ * @param {Portion|KeyValue} key - primary or secondary key
589
589
  * @param {(opt?: OPT|string)?} opt - option or query name
590
590
  * @async
591
591
  * */
592
- trash(key: KeyValue, opt?: OPT | string): Promise<string>;
592
+ trash(key: PORTION | KeyValue, opt?: OPT | string): Promise<string>;
593
593
  /**
594
594
  * Trash document by primary key (soft delete)
595
595
  *
@@ -644,64 +644,48 @@ export interface DbRepoLike<CONN extends DbConnectionLike, OPT extends DbExecOpt
644
644
  * Generics
645
645
  * - 0-`R`: dim interface {@link IdDocLike}
646
646
  *
647
- * @param {KeyValue} key - primary or secondary key
647
+ * @param {Portion|KeyValue} key - primary or secondary key
648
648
  * @param {string?} dim - dimension, default: `def`
649
649
  * @return {Promise<IdDocLike>} - dim object
650
650
  * @async
651
651
  * */
652
- getDim<R extends IdDocLike<ID>>(key: KeyValue, dim?: DIMS): Promise<R>;
652
+ getDim<R extends IdDocLike<ID>>(key: PORTION | KeyValue, dim?: DIMS): Promise<R>;
653
653
  /**
654
654
  * List dims by keys (primary or secondary)
655
655
  *
656
656
  * Generics
657
657
  * - 0-`R`: dim interface {@link IdDocLike}
658
658
  *
659
- * @param {Array<string>} keys - primary or secondary keys
659
+ * @param {Array<Portion|KeyValue>} keys - primary or secondary keys
660
660
  * @param {string?} dim - dimension, default: `def`
661
661
  * @param {boolean?} ignoreCheck - ignores to check input
662
662
  * @return {Promise<Array<IdDocLike>>} - dim objects
663
663
  * */
664
- listDims<R extends IdDocLike<ID>>(keys: Array<KeyValue>, dim?: DIMS, ignoreCheck?: boolean): Promise<Array<R>>;
665
- /**
666
- * Set pair by document
667
- *
668
- * @param {Entity} doc - document
669
- * @return {Promise<Pair>} - pair object
670
- * @async
671
- * */
672
- $toPair(doc: ENT): Promise<PAIR>;
664
+ listDims<R extends IdDocLike<ID>>(keys: Array<PORTION | KeyValue>, dim?: DIMS, ignoreCheck?: boolean): Promise<Array<R>>;
673
665
  /**
674
666
  * Get pair by key (primary or secondary)
675
667
  *
676
- * @param {KeyValue} key - primary or secondary key
668
+ * @param {Portion|KeyValue} key - primary or secondary key
677
669
  * @return {Promise<Pair>} - pair object
678
670
  * @async
679
671
  * */
680
- getPair(key: KeyValue): Promise<PAIR>;
672
+ getPair(key: PORTION | KeyValue): Promise<PAIR>;
681
673
  /**
682
674
  * List pairs by keys (primary or secondary)
683
675
  *
684
- * @param {Array<KeyValue>} keys - primary or secondary keys
676
+ * @param {Array<Portion|KeyValue>} keys - primary or secondary keys
685
677
  * @param {boolean?} ignoreCheck - ignores to check input
686
678
  * @return {Promise<Array<Pair>>} - pair objects
687
679
  * */
688
- listPairs(keys: Array<KeyValue>, ignoreCheck?: boolean): Promise<Array<PAIR>>;
689
- /**
690
- * Set view by document
691
- *
692
- * @param {Entity} doc - document
693
- * @return {Promise<View>} - view object
694
- * @async
695
- * */
696
- $toView(doc: ENT): Promise<VIEW>;
680
+ listPairs(keys: Array<PORTION | KeyValue>, ignoreCheck?: boolean): Promise<Array<PAIR>>;
697
681
  /**
698
682
  * Get view by key (primary or secondary)
699
683
  *
700
- * @param {KeyValue} key - primary or secondary key
684
+ * @param {Portion|KeyValue} key - primary or secondary key
701
685
  * @return {Promise<View>} - view object
702
686
  * @async
703
687
  * */
704
- getView(key: KeyValue): Promise<VIEW>;
688
+ getView(key: PORTION | KeyValue): Promise<VIEW>;
705
689
  /**
706
690
  * List views by keys (primary or secondary)
707
691
  *
@@ -710,30 +694,22 @@ export interface DbRepoLike<CONN extends DbConnectionLike, OPT extends DbExecOpt
710
694
  * @return {Promise<Array<View>>} - view objects
711
695
  * */
712
696
  listViews(keys: Array<KeyValue>, ignoreCheck?: boolean): Promise<Array<VIEW>>;
713
- /**
714
- * Set portion by document
715
- *
716
- * @param {Entity} doc - document
717
- * @return {Promise<Portion>} - portion object
718
- * @async
719
- * */
720
- $toPortion(doc: ENT): Promise<PORTION>;
721
697
  /**
722
698
  * Get portion by key (primary or secondary)
723
699
  *
724
- * @param {KeyValue} key - primary or secondary key
700
+ * @param {Portion|KeyValue} key - primary or secondary key
725
701
  * @return {Promise<Portion>} - portion object
726
702
  * @async
727
703
  * */
728
- getPortion(key: KeyValue): Promise<PORTION>;
704
+ getPortion(key: PORTION | KeyValue): Promise<PORTION>;
729
705
  /**
730
706
  * List portions by key (primary or secondary)
731
707
  *
732
- * @param {Array<KeyValue>} keys - primary or secondary keys
708
+ * @param {Array<Portion|KeyValue>} keys - primary or secondary keys
733
709
  * @param {boolean?} ignoreCheck - ignores to check input
734
710
  * @return {Promise<Array<Portion>>} - portion objects
735
711
  * */
736
- listPortions(keys: Array<KeyValue>, ignoreCheck?: boolean): Promise<Array<PORTION>>;
712
+ listPortions(keys: Array<PORTION | KeyValue>, ignoreCheck?: boolean): Promise<Array<PORTION>>;
737
713
  }
738
714
  /**
739
715
  * DB repository option
@@ -978,6 +954,10 @@ export interface DbRepoProps<CONN extends DbConnectionLike, ENT extends Entity<I
978
954
  * @type {number}
979
955
  * */
980
956
  urnLength?: number;
957
+ /**
958
+ * Indexed fields
959
+ * */
960
+ indexedFields?: Array<keyof ENT | string>;
981
961
  }
982
962
  /**
983
963
  * DB keys result
@@ -1036,14 +1016,17 @@ export type DbRepoToUrnTuple<URN extends UrnDocLike = UrnDocLike> = (urnRec: URN
1036
1016
  export type DbRepoToDim<R, ENT extends Entity<ID>, ID extends KeyValue, DIMS extends DefDims = DefDims> = (doc: ENT, dim: DIMS | undefined) => Promise<R>;
1037
1017
  /**
1038
1018
  * Default model which contains keys
1019
+ *
1020
+ * Generics:
1021
+ * - 0-`ID`: id type {@link KeyValue}
1039
1022
  * */
1040
- export interface DbIdLike {
1023
+ export interface DbIdLike<ID extends KeyValue = KeyValue> {
1041
1024
  /**
1042
1025
  * Secondary key for an entity
1043
1026
  *
1044
1027
  * @type {KeyValue}
1045
1028
  */
1046
- id?: KeyValue;
1029
+ id?: ID;
1047
1030
  /**
1048
1031
  * Primary key for an entity
1049
1032
  *
@@ -1106,6 +1089,10 @@ export interface DbExecOpt extends Opt {
1106
1089
  }
1107
1090
  /**
1108
1091
  * DB query result as a row
1092
+ *
1093
+ * Generics:
1094
+ * - 0-`E`: result model, it will be row type
1095
+ * - 1-`META`: query result metadata {@link DbMeta}
1109
1096
  * */
1110
1097
  export interface DbQueryResultOne<E = any, META extends DbMeta = DbMeta> extends DbQueryResult<META> {
1111
1098
  /**
@@ -1115,6 +1102,10 @@ export interface DbQueryResultOne<E = any, META extends DbMeta = DbMeta> extends
1115
1102
  }
1116
1103
  /**
1117
1104
  * DB query result as rows
1105
+ *
1106
+ * Generics:
1107
+ * - 0-`E`: result model, it will be rows item type, `rows: E[]`
1108
+ * - 1-`META`: query result metadata {@link DbMeta}
1118
1109
  * */
1119
1110
  export interface DbQueryResultMore<E = any, META extends DbMeta = DbMeta> extends DbQueryResult<META> {
1120
1111
  /**
@@ -1125,7 +1116,8 @@ export interface DbQueryResultMore<E = any, META extends DbMeta = DbMeta> extend
1125
1116
  /**
1126
1117
  * DB query result
1127
1118
  *
1128
- * Generics
1119
+ * Generics:
1120
+ * - 0-`META`: query result metadata {@link DbMeta}
1129
1121
  * */
1130
1122
  export interface DbQueryResult<META> {
1131
1123
  /**
@@ -1141,3 +1133,31 @@ export interface DbQueryResult<META> {
1141
1133
  */
1142
1134
  error?: OmitError;
1143
1135
  }
1136
+ /**
1137
+ * Couchbase direct repository (collection) option
1138
+ *
1139
+ * Generics:
1140
+ * - 0-`ENT`: entity {@link Entity}
1141
+ * - 1-`ID`: id type {@link KeyValue}
1142
+ * - 2-`URN`: urn interface {@link UrnDocLike}
1143
+ * - 3-`DIMS`: dimensions for presentation layer {@link DefDims}
1144
+ * - 4-`R`: ??
1145
+ * */
1146
+ export interface DbRepoDirectOpt<ENT extends Entity<ID>, ID extends KeyValue = KeyValue, URN extends UrnDocLike = UrnDef<ID>, DIMS extends DefDims = DefDims, R = unknown> extends DbRepoOpt<ENT, ID> {
1147
+ /**
1148
+ * Name of logger class
1149
+ * */
1150
+ name?: string;
1151
+ /**
1152
+ * Overridable method to easy use
1153
+ * */
1154
+ $toUrnTuple?: DbRepoToUrnTuple<URN>;
1155
+ /**
1156
+ * Overridable method to easy use
1157
+ * */
1158
+ $toDim?: DbRepoToDim<R, ENT, ID, DIMS>;
1159
+ /**
1160
+ * Indexed fields
1161
+ * */
1162
+ indexedFields?: Array<keyof ENT | string>;
1163
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samet-it/be-db-common",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "description": "Backend DB Common",
5
5
  "repository": {
6
6
  "type": "git",