spice-couchbase 2.0.30 → 2.0.32

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/build/index.js CHANGED
@@ -45,6 +45,8 @@ module.exports = class Database {
45
45
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
46
46
 
47
47
  if (connection) {
48
+ var _spice, _spice$db;
49
+
48
50
  this.connection_name = connection;
49
51
  this.connection = spice.database.connections[connection];
50
52
  this.query_scope = query_scope;
@@ -69,6 +71,11 @@ module.exports = class Database {
69
71
  this.collection = this.scope.collection(this.collection_name);
70
72
  this.userManager = this.getUserManager(connection);
71
73
 
74
+ if (!((_spice = spice) === null || _spice === void 0 ? void 0 : (_spice$db = _spice.db) === null || _spice$db === void 0 ? void 0 : _spice$db.collections)) {
75
+ spice.db = spice.db || {};
76
+ spice.db.collections = new Set();
77
+ }
78
+
72
79
  if (this.auto_create_collection) {
73
80
  this.upsertCollection();
74
81
  }
@@ -170,9 +177,9 @@ module.exports = class Database {
170
177
 
171
178
  if (!_this3.checkGlobelConnectionObject(_this3.collection_name)) {
172
179
  if (!collection_exists) {
173
- yield _this3.createCollection(_this3.collection_name, _this3.scope_name);
174
- yield _this3.wait(2000);
175
- yield _this3.createPrimaryIndex(_this3.scope_name, _this3.collection_name);
180
+ /* await this.createCollection(this.collection_name, this.scope_name);
181
+ await this.wait(2000);
182
+ await this.createPrimaryIndex(this.scope_name, this.collection_name); */
176
183
  }
177
184
  }
178
185
  } catch (e) {
@@ -186,20 +193,56 @@ module.exports = class Database {
186
193
 
187
194
  return _asyncToGenerator(function* () {
188
195
  try {
189
- var _$find;
196
+ var _spice2, _spice2$db, _spice2$db$collection;
190
197
 
191
- var all_scopes = yield axios.get("".concat(_this4.getCouchbaseHost(), "/pools/default/buckets/").concat(_this4.db_config.bucket, "/scopes"), {
192
- auth: {
193
- username: _this4.db_config.username,
194
- password: _this4.db_config.password
195
- }
196
- });
197
- var collection_e = _.find(((_$find = _.find(all_scopes.data.scopes, {
198
- name: scope_name
199
- })) === null || _$find === void 0 ? void 0 : _$find.collections) || [], {
200
- name: collection_name
201
- }) != undefined;
202
- return collection_e;
198
+ var that = _this4;
199
+
200
+ function loadAllCollections() {
201
+ return _loadAllCollections.apply(this, arguments);
202
+ }
203
+ /* console.log("================================");
204
+ console.log(
205
+ "Collection Set",
206
+ spice?.db?.collections?.has(collection_name),
207
+ collection_name
208
+ ); */
209
+
210
+
211
+ function _loadAllCollections() {
212
+ _loadAllCollections = _asyncToGenerator(function* () {
213
+ var all_scopes = yield axios.get("".concat(that.getCouchbaseHost(), "/pools/default/buckets/").concat(that.db_config.bucket, "/scopes"), {
214
+ auth: {
215
+ username: that.db_config.username,
216
+ password: that.db_config.password
217
+ }
218
+ });
219
+
220
+ var scope = _.find(all_scopes.data.scopes, {
221
+ name: scope_name
222
+ });
223
+
224
+ var collectionSet = new Set();
225
+
226
+ _.each(scope.collections, collection => {
227
+ collectionSet.add(collection.name);
228
+ });
229
+
230
+ return collectionSet;
231
+ });
232
+ return _loadAllCollections.apply(this, arguments);
233
+ }
234
+
235
+ if ((_spice2 = spice) === null || _spice2 === void 0 ? void 0 : (_spice2$db = _spice2.db) === null || _spice2$db === void 0 ? void 0 : (_spice2$db$collection = _spice2$db.collections) === null || _spice2$db$collection === void 0 ? void 0 : _spice2$db$collection.has(collection_name)) {
236
+ //console.log("Using Internal Collection Set", true);
237
+ return true;
238
+ } else {
239
+ var _spice3, _spice3$db, _spice3$db$collection;
240
+
241
+ //console.log("Using Internal Collection Set", false);
242
+ spice.db.collections = yield loadAllCollections(); //console.log("Collection Set", spice.db.collections.entries());
243
+
244
+ return (_spice3 = spice) === null || _spice3 === void 0 ? void 0 : (_spice3$db = _spice3.db) === null || _spice3$db === void 0 ? void 0 : (_spice3$db$collection = _spice3$db.collections) === null || _spice3$db$collection === void 0 ? void 0 : _spice3$db$collection.has(collection_name);
245
+ }
203
246
  } catch (e) {
204
247
  console.log("Exists error happened", e);
205
248
  }
@@ -586,18 +629,54 @@ module.exports = class Database {
586
629
  })();
587
630
  }
588
631
 
632
+ optimize(query, exec_scope) {
633
+ return _asyncToGenerator(function* () {
634
+ try {
635
+ var _$last, _$first, _$first$advice, _$first$advice$advise, _$first$advice$advise2;
636
+
637
+ var options = {
638
+ adhoc: true
639
+ };
640
+ var advise_query = "ADVISE ".concat(query);
641
+ var response = yield exec_scope.query(advise_query, options);
642
+ var best_index = (_$last = _.last(((_$first = _.first((response === null || response === void 0 ? void 0 : response.rows) || [])) === null || _$first === void 0 ? void 0 : (_$first$advice = _$first.advice) === null || _$first$advice === void 0 ? void 0 : (_$first$advice$advise = _$first$advice.adviseinfo) === null || _$first$advice$advise === void 0 ? void 0 : (_$first$advice$advise2 = _$first$advice$advise.recommended_indexes) === null || _$first$advice$advise2 === void 0 ? void 0 : _$first$advice$advise2.indexes) || [])) === null || _$last === void 0 ? void 0 : _$last.index_statement;
643
+
644
+ if (best_index) {
645
+ yield exec_scope.query(best_index);
646
+ }
647
+ } catch (e) {}
648
+ })();
649
+ }
650
+
589
651
  query(query) {
590
652
  var _this18 = this;
591
653
 
592
654
  var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
593
655
  return _asyncToGenerator(function* () {
594
656
  var scope = query_scope || _this18.query_scope;
657
+ var sanitizedQuery = query; //sanitization step to be implemented
658
+
659
+ var exec_scope = null;
595
660
 
596
- if (scope == "cluster") {
597
- return yield _this18.cluster.query(query);
661
+ if (scope === "cluster") {
662
+ exec_scope = _this18.cluster;
598
663
  } else {
599
- return yield _this18.scope.query(query);
664
+ exec_scope = _this18.scope;
665
+ } // Start time
666
+
667
+
668
+ var startTime = process.hrtime.bigint();
669
+ var response = yield exec_scope.query(sanitizedQuery); // End time
670
+
671
+ var endTime = process.hrtime.bigint(); // Calculate duration in microseconds
672
+
673
+ var durationInMilliseconds = (endTime - startTime) / BigInt(1000000); // optimize if response time is greater than 1 second
674
+
675
+ if (durationInMilliseconds > 1000) {
676
+ yield _this18.optimize(query, exec_scope);
600
677
  }
678
+
679
+ return response;
601
680
  })();
602
681
  }
603
682
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-couchbase",
3
- "version": "2.0.30",
3
+ "version": "2.0.32",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -10,7 +10,7 @@
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
12
  "axios": "^0.26.1",
13
- "couchbase": "4.2.4",
13
+ "couchbase": "^4.2.7",
14
14
  "lodash": "^4.17.15",
15
15
  "qs": "^6.10.3"
16
16
  },
package/src/index.js CHANGED
@@ -76,6 +76,10 @@ module.exports = class Database {
76
76
  this.SearchQuery = couchbase.SearchQuery;
77
77
  this.collection = this.scope.collection(this.collection_name);
78
78
  this.userManager = this.getUserManager(connection);
79
+ if (!spice?.db?.collections) {
80
+ spice.db = spice.db || {};
81
+ spice.db.collections = new Set();
82
+ }
79
83
  if (this.auto_create_collection) {
80
84
  this.upsertCollection();
81
85
  }
@@ -174,9 +178,9 @@ module.exports = class Database {
174
178
  );
175
179
  if (!this.checkGlobelConnectionObject(this.collection_name)) {
176
180
  if (!collection_exists) {
177
- await this.createCollection(this.collection_name, this.scope_name);
181
+ /* await this.createCollection(this.collection_name, this.scope_name);
178
182
  await this.wait(2000);
179
- await this.createPrimaryIndex(this.scope_name, this.collection_name);
183
+ await this.createPrimaryIndex(this.scope_name, this.collection_name); */
180
184
  }
181
185
  }
182
186
  } catch (e) {
@@ -186,24 +190,42 @@ module.exports = class Database {
186
190
 
187
191
  async collectionExists(collection_name, scope_name) {
188
192
  try {
189
- let all_scopes = await axios.get(
190
- `${this.getCouchbaseHost()}/pools/default/buckets/${
191
- this.db_config.bucket
192
- }/scopes`,
193
- {
194
- auth: {
195
- username: this.db_config.username,
196
- password: this.db_config.password,
197
- },
198
- }
199
- );
200
- let collection_e =
201
- _.find(
202
- _.find(all_scopes.data.scopes, { name: scope_name })?.collections ||
203
- [],
204
- { name: collection_name }
205
- ) != undefined;
206
- return collection_e;
193
+ let that = this;
194
+ async function loadAllCollections() {
195
+ let all_scopes = await axios.get(
196
+ `${that.getCouchbaseHost()}/pools/default/buckets/${
197
+ that.db_config.bucket
198
+ }/scopes`,
199
+ {
200
+ auth: {
201
+ username: that.db_config.username,
202
+ password: that.db_config.password,
203
+ },
204
+ }
205
+ );
206
+ let scope = _.find(all_scopes.data.scopes, { name: scope_name });
207
+ let collectionSet = new Set();
208
+ _.each(scope.collections, (collection) => {
209
+ collectionSet.add(collection.name);
210
+ });
211
+ return collectionSet;
212
+ }
213
+ /* console.log("================================");
214
+ console.log(
215
+ "Collection Set",
216
+ spice?.db?.collections?.has(collection_name),
217
+ collection_name
218
+ ); */
219
+ if (spice?.db?.collections?.has(collection_name)) {
220
+ //console.log("Using Internal Collection Set", true);
221
+ return true;
222
+ } else {
223
+ //console.log("Using Internal Collection Set", false);
224
+
225
+ spice.db.collections = await loadAllCollections();
226
+ //console.log("Collection Set", spice.db.collections.entries());
227
+ return spice?.db?.collections?.has(collection_name);
228
+ }
207
229
  } catch (e) {
208
230
  console.log("Exists error happened", e);
209
231
  }
@@ -531,25 +553,45 @@ module.exports = class Database {
531
553
  return response.rows;
532
554
  }
533
555
 
534
- /* async query(query, query_scope = "cluster") {
535
- let scope = query_scope || this.query_scope;
536
- if (scope == "cluster") {
537
- return await this.cluster.query(query);
538
- } else {
539
- return await this.scope.query(query);
540
- }
541
- } */
556
+ async optimize(query, exec_scope) {
557
+ try {
558
+ const options = {
559
+ adhoc: true,
560
+ };
561
+ let advise_query = `ADVISE ${query}`;
562
+ let response = await exec_scope.query(advise_query, options);
563
+
564
+ let best_index = _.last(
565
+ _.first(response?.rows || [])?.advice?.adviseinfo?.recommended_indexes
566
+ ?.indexes || []
567
+ )?.index_statement;
568
+ if (best_index) {
569
+ await exec_scope.query(best_index);
570
+ }
571
+ } catch (e) {}
572
+ }
542
573
 
543
574
  async query(query, query_scope = "cluster") {
544
575
  let scope = query_scope || this.query_scope;
545
-
546
- // Escape the query string
547
- const sanitizedQuery = couchbase.N1qlQuery.fromString(query).toString();
576
+ const sanitizedQuery = query; //sanitization step to be implemented
577
+ let exec_scope = null;
548
578
 
549
579
  if (scope === "cluster") {
550
- return await this.cluster.query(sanitizedQuery);
580
+ exec_scope = this.cluster;
551
581
  } else {
552
- return await this.scope.query(sanitizedQuery);
582
+ exec_scope = this.scope;
583
+ }
584
+ // Start time
585
+ let startTime = process.hrtime.bigint();
586
+ let response = await exec_scope.query(sanitizedQuery);
587
+ // End time
588
+ let endTime = process.hrtime.bigint();
589
+ // Calculate duration in microseconds
590
+ let durationInMilliseconds = (endTime - startTime) / BigInt(1000000);
591
+ // optimize if response time is greater than 1 second
592
+ if (durationInMilliseconds > 1000) {
593
+ await this.optimize(query, exec_scope);
553
594
  }
595
+ return response;
554
596
  }
555
597
  };