@samet-it/be-db-common 1.1.8 → 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.
@@ -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
55
87
  * */
56
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
94
+ * */
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>>;
@@ -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) {
@@ -194,6 +237,9 @@ class DbRepo {
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
  }
@@ -954,6 +954,10 @@ export interface DbRepoProps<CONN extends DbConnectionLike, ENT extends Entity<I
954
954
  * @type {number}
955
955
  * */
956
956
  urnLength?: number;
957
+ /**
958
+ * Indexed fields
959
+ * */
960
+ indexedFields?: Array<keyof ENT | string>;
957
961
  }
958
962
  /**
959
963
  * DB keys result
@@ -1155,5 +1159,5 @@ export interface DbRepoDirectOpt<ENT extends Entity<ID>, ID extends KeyValue = K
1155
1159
  /**
1156
1160
  * Indexed fields
1157
1161
  * */
1158
- $indexedFields?: Array<keyof ENT | string>;
1162
+ indexedFields?: Array<keyof ENT | string>;
1159
1163
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samet-it/be-db-common",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "Backend DB Common",
5
5
  "repository": {
6
6
  "type": "git",