spice-couchbase 2.0.30 → 2.0.31

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
@@ -586,18 +586,54 @@ module.exports = class Database {
586
586
  })();
587
587
  }
588
588
 
589
+ optimize(query, exec_scope) {
590
+ return _asyncToGenerator(function* () {
591
+ try {
592
+ var _$last, _$first, _$first$advice, _$first$advice$advise, _$first$advice$advise2;
593
+
594
+ var options = {
595
+ adhoc: true
596
+ };
597
+ var advise_query = "ADVISE ".concat(query);
598
+ var response = yield exec_scope.query(advise_query, options);
599
+ 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;
600
+
601
+ if (best_index) {
602
+ yield exec_scope.query(best_index);
603
+ }
604
+ } catch (e) {}
605
+ })();
606
+ }
607
+
589
608
  query(query) {
590
609
  var _this18 = this;
591
610
 
592
611
  var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
593
612
  return _asyncToGenerator(function* () {
594
613
  var scope = query_scope || _this18.query_scope;
614
+ var sanitizedQuery = query; //sanitization step to be implemented
615
+
616
+ var exec_scope = null;
595
617
 
596
- if (scope == "cluster") {
597
- return yield _this18.cluster.query(query);
618
+ if (scope === "cluster") {
619
+ exec_scope = _this18.cluster;
598
620
  } else {
599
- return yield _this18.scope.query(query);
621
+ exec_scope = _this18.scope;
622
+ } // Start time
623
+
624
+
625
+ var startTime = process.hrtime.bigint();
626
+ var response = yield exec_scope.query(sanitizedQuery); // End time
627
+
628
+ var endTime = process.hrtime.bigint(); // Calculate duration in microseconds
629
+
630
+ var durationInMilliseconds = (endTime - startTime) / BigInt(1000000); // optimize if response time is greater than 1 second
631
+
632
+ if (durationInMilliseconds > 1000) {
633
+ yield _this18.optimize(query, exec_scope);
600
634
  }
635
+
636
+ return response;
601
637
  })();
602
638
  }
603
639
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-couchbase",
3
- "version": "2.0.30",
3
+ "version": "2.0.31",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -531,25 +531,45 @@ module.exports = class Database {
531
531
  return response.rows;
532
532
  }
533
533
 
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
- } */
534
+ async optimize(query, exec_scope) {
535
+ try {
536
+ const options = {
537
+ adhoc: true,
538
+ };
539
+ let advise_query = `ADVISE ${query}`;
540
+ let response = await exec_scope.query(advise_query, options);
541
+
542
+ let best_index = _.last(
543
+ _.first(response?.rows || [])?.advice?.adviseinfo?.recommended_indexes
544
+ ?.indexes || []
545
+ )?.index_statement;
546
+ if (best_index) {
547
+ await exec_scope.query(best_index);
548
+ }
549
+ } catch (e) {}
550
+ }
542
551
 
543
552
  async query(query, query_scope = "cluster") {
544
553
  let scope = query_scope || this.query_scope;
545
-
546
- // Escape the query string
547
- const sanitizedQuery = couchbase.N1qlQuery.fromString(query).toString();
554
+ const sanitizedQuery = query; //sanitization step to be implemented
555
+ let exec_scope = null;
548
556
 
549
557
  if (scope === "cluster") {
550
- return await this.cluster.query(sanitizedQuery);
558
+ exec_scope = this.cluster;
551
559
  } else {
552
- return await this.scope.query(sanitizedQuery);
560
+ exec_scope = this.scope;
561
+ }
562
+ // Start time
563
+ let startTime = process.hrtime.bigint();
564
+ let response = await exec_scope.query(sanitizedQuery);
565
+ // End time
566
+ let endTime = process.hrtime.bigint();
567
+ // Calculate duration in microseconds
568
+ let durationInMilliseconds = (endTime - startTime) / BigInt(1000000);
569
+ // optimize if response time is greater than 1 second
570
+ if (durationInMilliseconds > 1000) {
571
+ await this.optimize(query, exec_scope);
553
572
  }
573
+ return response;
554
574
  }
555
575
  };