spice-couchbase 2.0.46 → 2.0.47

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
@@ -824,9 +824,7 @@ module.exports = class Database {
824
824
  }); // 4. Execute the index creation if we got a valid statement
825
825
 
826
826
  if (indexStatement) {
827
- console.log("Executing index creation:", indexStatement);
828
827
  var response = yield exec_scope.query(indexStatement);
829
- console.log("Index creation response:", response);
830
828
  }
831
829
  } catch (e) {// Silently handle errors (index may already exist, etc.)
832
830
  }
@@ -859,10 +857,10 @@ module.exports = class Database {
859
857
  var endTime = process.hrtime.bigint(); // Calculate duration in microseconds
860
858
 
861
859
  var durationInMilliseconds = (endTime - startTime) / BigInt(1000000); // optimize if response time is greater than 1 second
862
- //if (durationInMilliseconds > 2000) {
863
-
864
- _this19.optimize(query, exec_scope); //}
865
860
 
861
+ if (durationInMilliseconds > 2000) {
862
+ _this19.optimize(query, exec_scope);
863
+ }
866
864
 
867
865
  return response;
868
866
  })();
package/build/utility.js CHANGED
@@ -323,22 +323,17 @@ function buildCoveringIndex(query, options) {
323
323
  if (allFields.length === 0) {
324
324
  // No fields to index besides deleted
325
325
  return null;
326
- } // Generate unique index name based on query hash
326
+ } // Build the field list with deleted INCLUDE MISSING first
327
327
 
328
328
 
329
- var hash = generateHash(query);
330
- var indexName = "adv_covered_".concat(hash); // Build the field list with deleted INCLUDE MISSING first
329
+ var fieldList = ["`deleted` INCLUDE MISSING", ...allFields.map(f => "`".concat(f, "`"))].join(", "); // Generate unique index name based on bucket, scope, collection, and fields
330
+ // This ensures multiple queries covered by the same index get the same hash
331
331
 
332
- var fieldList = ["`deleted` INCLUDE MISSING", ...allFields.map(f => "`".concat(f, "`"))].join(", "); // Build the CREATE INDEX statement
332
+ var indexKey = "".concat(bucket, "|").concat(scope, "|").concat(collection, "|").concat(allFields.sort().join(","));
333
+ var hash = generateHash(indexKey);
334
+ var indexName = "adv_covered_".concat(hash); // Build the CREATE INDEX statement
333
335
 
334
- var indexStatement = "CREATE INDEX `".concat(indexName, "` ON `default`:`").concat(bucket, "`.`").concat(scope, "`.`").concat(collection, "`(").concat(fieldList, ")"); // Debug logs
335
-
336
- console.log("[DEBUG buildCoveringIndex] Original query:", query);
337
- console.log("[DEBUG buildCoveringIndex] WHERE fields:", filteredWhereFields);
338
- console.log("[DEBUG buildCoveringIndex] GROUP BY fields:", filteredGroupByFields);
339
- console.log("[DEBUG buildCoveringIndex] ORDER BY fields:", filteredOrderByFields);
340
- console.log("[DEBUG buildCoveringIndex] SELECT fields:", selectFieldsFiltered);
341
- console.log("[DEBUG buildCoveringIndex] Index statement:", indexStatement);
336
+ var indexStatement = "CREATE INDEX `".concat(indexName, "` ON `default`:`").concat(bucket, "`.`").concat(scope, "`.`").concat(collection, "`(").concat(fieldList, ")");
342
337
  return indexStatement;
343
338
  }
344
339
 
@@ -586,6 +586,29 @@ test("consistent index name for same query", () => {
586
586
  var result2 = buildCoveringIndex(query, defaultOptions);
587
587
  assert.strictEqual(result1, result2);
588
588
  });
589
+ test("same index name for different queries with same fields", () => {
590
+ // Two different queries that would be covered by the same index
591
+ var query1 = "SELECT name, email FROM users WHERE status = 'active'";
592
+ var query2 = "SELECT email, name FROM users WHERE status = 'pending'";
593
+ var result1 = buildCoveringIndex(query1, defaultOptions);
594
+ var result2 = buildCoveringIndex(query2, defaultOptions); // Extract index names
595
+
596
+ var name1 = result1.match(/CREATE INDEX `(adv_covered_\w+)`/)[1];
597
+ var name2 = result2.match(/CREATE INDEX `(adv_covered_\w+)`/)[1]; // Should have the same index name since they have the same fields
598
+
599
+ assert.strictEqual(name1, name2);
600
+ });
601
+ test("different index name for queries with different fields", () => {
602
+ var query1 = "SELECT name FROM users WHERE status = 'active'";
603
+ var query2 = "SELECT email FROM users WHERE type = 'admin'";
604
+ var result1 = buildCoveringIndex(query1, defaultOptions);
605
+ var result2 = buildCoveringIndex(query2, defaultOptions); // Extract index names
606
+
607
+ var name1 = result1.match(/CREATE INDEX `(adv_covered_\w+)`/)[1];
608
+ var name2 = result2.match(/CREATE INDEX `(adv_covered_\w+)`/)[1]; // Should have different index names since they have different fields
609
+
610
+ assert.notStrictEqual(name1, name2);
611
+ });
589
612
  test("index includes all fields needed for covering", () => {
590
613
  var query = "SELECT \n first_name, \n middle_name, \n last_name, \n status, \n constituency, \n division, \n description \n FROM subject \n WHERE (deleted = false OR deleted IS MISSING) \n AND created_at > '2024-01-01'";
591
614
  var result = buildCoveringIndex(query, defaultOptions); // Verify structure matches the working example pattern
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-couchbase",
3
- "version": "2.0.46",
3
+ "version": "2.0.47",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
package/src/utility.js CHANGED
@@ -342,16 +342,18 @@ function buildCoveringIndex(query, options) {
342
342
  return null;
343
343
  }
344
344
 
345
- // Generate unique index name based on query hash
346
- const hash = generateHash(query);
347
- const indexName = `adv_covered_${hash}`;
348
-
349
345
  // Build the field list with deleted INCLUDE MISSING first
350
346
  const fieldList = [
351
347
  "`deleted` INCLUDE MISSING",
352
348
  ...allFields.map((f) => `\`${f}\``),
353
349
  ].join(", ");
354
350
 
351
+ // Generate unique index name based on bucket, scope, collection, and fields
352
+ // This ensures multiple queries covered by the same index get the same hash
353
+ const indexKey = `${bucket}|${scope}|${collection}|${allFields.sort().join(",")}`;
354
+ const hash = generateHash(indexKey);
355
+ const indexName = `adv_covered_${hash}`;
356
+
355
357
  // Build the CREATE INDEX statement
356
358
  const indexStatement = `CREATE INDEX \`${indexName}\` ON \`default\`:\`${bucket}\`.\`${scope}\`.\`${collection}\`(${fieldList})`;
357
359
 
@@ -706,6 +706,37 @@ test("consistent index name for same query", () => {
706
706
  assert.strictEqual(result1, result2);
707
707
  });
708
708
 
709
+ test("same index name for different queries with same fields", () => {
710
+ // Two different queries that would be covered by the same index
711
+ const query1 = "SELECT name, email FROM users WHERE status = 'active'";
712
+ const query2 = "SELECT email, name FROM users WHERE status = 'pending'";
713
+
714
+ const result1 = buildCoveringIndex(query1, defaultOptions);
715
+ const result2 = buildCoveringIndex(query2, defaultOptions);
716
+
717
+ // Extract index names
718
+ const name1 = result1.match(/CREATE INDEX `(adv_covered_\w+)`/)[1];
719
+ const name2 = result2.match(/CREATE INDEX `(adv_covered_\w+)`/)[1];
720
+
721
+ // Should have the same index name since they have the same fields
722
+ assert.strictEqual(name1, name2);
723
+ });
724
+
725
+ test("different index name for queries with different fields", () => {
726
+ const query1 = "SELECT name FROM users WHERE status = 'active'";
727
+ const query2 = "SELECT email FROM users WHERE type = 'admin'";
728
+
729
+ const result1 = buildCoveringIndex(query1, defaultOptions);
730
+ const result2 = buildCoveringIndex(query2, defaultOptions);
731
+
732
+ // Extract index names
733
+ const name1 = result1.match(/CREATE INDEX `(adv_covered_\w+)`/)[1];
734
+ const name2 = result2.match(/CREATE INDEX `(adv_covered_\w+)`/)[1];
735
+
736
+ // Should have different index names since they have different fields
737
+ assert.notStrictEqual(name1, name2);
738
+ });
739
+
709
740
  test("index includes all fields needed for covering", () => {
710
741
  const query = `SELECT
711
742
  first_name,