@samet-it/be-couchbase-common 1.0.10 → 1.0.12

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.
@@ -8,6 +8,7 @@ export declare abstract class CbRepo<E extends CbEntity, ID> implements CbRepoLi
8
8
  private _scopeName?;
9
9
  private _bucketName?;
10
10
  private _tryCount;
11
+ private _pkIndexed;
11
12
  protected logger: Logger;
12
13
  /** @inheritDoc */
13
14
  readonly fullPath: string;
@@ -22,6 +23,8 @@ export declare abstract class CbRepo<E extends CbEntity, ID> implements CbRepoLi
22
23
  /** @inheritDoc */
23
24
  readonly collection: Collection;
24
25
  protected constructor(colName: string, cb: CbAdapterService, _scopeName?: string, _bucketName?: string);
26
+ private get _randomIndexName();
27
+ protected _indexName(name: string): string;
25
28
  /**
26
29
  * Fetch urn from doc as doc._urn
27
30
  * */
@@ -45,7 +48,7 @@ export declare abstract class CbRepo<E extends CbEntity, ID> implements CbRepoLi
45
48
  /**
46
49
  * Execute the creation of indexes with keys
47
50
  * */
48
- protected _createMoreIndices(...keys: Array<keyof CbEntity>): Promise<void>;
51
+ protected _createMoreIndices(...keys: Array<keyof E>): Promise<void>;
49
52
  /**
50
53
  * Execute the creation of primary key
51
54
  * */
@@ -35,6 +35,31 @@ class CbRepo {
35
35
  F_TRASH_ID = cb.f('_trashId');
36
36
  }
37
37
  }
38
+ get _randomIndexName() {
39
+ 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)
40
+ .map(s => s.toLowerCase())
41
+ .join('_');
42
+ }
43
+ _indexName(name) {
44
+ if (typeof name !== 'string') {
45
+ return this._randomIndexName;
46
+ }
47
+ name = name.trim();
48
+ if (name === '') {
49
+ return this._randomIndexName;
50
+ }
51
+ name = name.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
52
+ .map(s => s.toLowerCase())
53
+ .join('_');
54
+ name = name.replace(/^_+|_+$/g, '');
55
+ if (name === '') {
56
+ return this._randomIndexName;
57
+ }
58
+ if (!name.startsWith('idx_')) {
59
+ name = 'idx_' + name;
60
+ }
61
+ return name;
62
+ }
38
63
  /**
39
64
  * Fetch urn from doc as doc._urn
40
65
  * */
@@ -107,17 +132,16 @@ class CbRepo {
107
132
  let sql;
108
133
  const fields = [];
109
134
  if (Array.isArray(p1)) {
110
- fields.push(...p1.map(f => this.cb.f(f)));
111
- if (common_1.$is.text(name)) {
112
- name = fields[0];
135
+ const given = p1;
136
+ if (!common_1.$is.text(name)) {
137
+ name = given.map(item => item.split('.').pop()).join('.');
113
138
  }
139
+ fields.push(...given.map(f => this.cb.f(f)));
114
140
  }
115
141
  else {
116
142
  fields.push(p1);
117
143
  }
118
- if (!name.startsWith('idx_')) {
119
- name = 'idx_' + name;
120
- }
144
+ name = this._indexName(name);
121
145
  sql = `CREATE INDEX ${this.cb.f(name)} ON ${this.fullPath} (${fields.join(', ')})`;
122
146
  yield this.cb.one(this.def, sql, { name: `${this.path}.index`, ignoredErrors: [couchbase_1.IndexExistsError] });
123
147
  });
@@ -127,9 +151,22 @@ class CbRepo {
127
151
  * */
128
152
  _createMoreIndices(...keys) {
129
153
  return __awaiter(this, void 0, void 0, function* () {
130
- const promises = keys.map(k => this._createIndex(k));
131
- promises.push(this._createPk());
132
- yield Promise.all(promises);
154
+ const already = [];
155
+ const promises = keys
156
+ .filter(k => {
157
+ if (already.includes(k)) {
158
+ return false;
159
+ }
160
+ already.push(k);
161
+ return true;
162
+ })
163
+ .map(k => this._createIndex(k));
164
+ if (!this._pkIndexed) {
165
+ promises.push(this._createPk());
166
+ }
167
+ if (promises.length > 0) {
168
+ yield Promise.all(promises);
169
+ }
133
170
  });
134
171
  }
135
172
  /**
@@ -137,8 +174,11 @@ class CbRepo {
137
174
  * */
138
175
  _createPk() {
139
176
  return __awaiter(this, void 0, void 0, function* () {
140
- const sql = `CREATE PRIMARY INDEX \`pk\` ON ${this.fullPath}`;
141
- yield this.cb.one(this.def, sql, { name: `${this.path}.pk`, ignoredErrors: [couchbase_1.IndexExistsError] });
177
+ if (!this._pkIndexed) {
178
+ const sql = `CREATE PRIMARY INDEX \`pk\` ON ${this.fullPath}`;
179
+ yield this.cb.one(this.def, sql, { name: `${this.path}.pk`, ignoredErrors: [couchbase_1.IndexExistsError] });
180
+ this._pkIndexed = true;
181
+ }
142
182
  });
143
183
  }
144
184
  /** @inheritDoc */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samet-it/be-couchbase-common",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "Couchbase common component",
5
5
  "keywords": [
6
6
  "Couchbase"