spice-couchbase 2.0.49 → 2.0.51

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
@@ -275,25 +275,52 @@ module.exports = class Database {
275
275
  })();
276
276
  }
277
277
 
278
- createPrimaryIndex(scope_name, col_name) {
278
+ primaryIndexExists(scope_name, col_name) {
279
279
  var _this6 = this;
280
280
 
281
281
  return _asyncToGenerator(function* () {
282
282
  try {
283
- var response = yield _this6.query("CREATE PRIMARY INDEX ON default:`".concat(_this6.db_config.bucket, "`.`").concat(scope_name, "`.`").concat(col_name, "`"));
283
+ var response = yield _this6.cluster.query("SELECT RAW name\n FROM system:indexes\n WHERE bucket_id = ?\n AND scope_id = ?\n AND keyspace_id = ?\n AND is_primary = true\n LIMIT 1", {
284
+ parameters: [_this6.db_config.bucket, scope_name, col_name],
285
+ scanConsistency: QueryScanConsistency.RequestPlus
286
+ });
287
+ return Array.isArray(response === null || response === void 0 ? void 0 : response.rows) && response.rows.length > 0;
288
+ } catch (e) {
289
+ console.log("Primary index lookup failed:", e);
290
+ return false;
291
+ }
292
+ })();
293
+ }
294
+
295
+ createPrimaryIndex(scope_name, col_name) {
296
+ var _this7 = this;
297
+
298
+ return _asyncToGenerator(function* () {
299
+ try {
300
+ if (yield _this7.primaryIndexExists(scope_name, col_name)) {
301
+ return;
302
+ }
303
+
304
+ var response = yield _this7.query("CREATE PRIMARY INDEX ON default:`".concat(_this7.db_config.bucket, "`.`").concat(scope_name, "`.`").concat(col_name, "`"));
284
305
  return response;
285
306
  } catch (e) {
307
+ var _e$cause, _e$cause2;
308
+
309
+ if (e instanceof couchbase.IndexExistsError || (e === null || e === void 0 ? void 0 : (_e$cause = e.cause) === null || _e$cause === void 0 ? void 0 : _e$cause.first_error_code) === 4300 || /index .*already exists|index_exists/i.test((e === null || e === void 0 ? void 0 : e.message) || (e === null || e === void 0 ? void 0 : (_e$cause2 = e.cause) === null || _e$cause2 === void 0 ? void 0 : _e$cause2.first_error_message) || "")) {
310
+ return;
311
+ }
312
+
286
313
  console.log("CREATE Index :::", e);
287
314
  }
288
315
  })();
289
316
  }
290
317
 
291
318
  createScope(scope_name) {
292
- var _this7 = this;
319
+ var _this8 = this;
293
320
 
294
321
  return _asyncToGenerator(function* () {
295
322
  try {
296
- yield _this7.collection_manager.createScope(scope_name);
323
+ yield _this8.collection_manager.createScope(scope_name);
297
324
  } catch (e) {
298
325
  if (e instanceof couchbase.ScopeExistsError) {
299
326
  console.log("The scope already exists");
@@ -305,7 +332,7 @@ module.exports = class Database {
305
332
  }
306
333
 
307
334
  get(doc_id) {
308
- var _this8 = this;
335
+ var _this9 = this;
309
336
 
310
337
  var {
311
338
  columns = []
@@ -313,7 +340,7 @@ module.exports = class Database {
313
340
  return _asyncToGenerator(function* () {
314
341
  try {
315
342
  var useLookupIn = columns.length > 0;
316
- var collection = _this8.use_bucket_name ? _this8.connection : _this8.collection;
343
+ var collection = _this9.use_bucket_name ? _this9.connection : _this9.collection;
317
344
 
318
345
  if (!useLookupIn) {
319
346
  var response = yield collection.get(doc_id);
@@ -354,36 +381,36 @@ module.exports = class Database {
354
381
  }
355
382
 
356
383
  insert(doc_id, document, expiry) {
357
- var _this9 = this;
384
+ var _this10 = this;
358
385
 
359
386
  return _asyncToGenerator(function* () {
360
387
  try {
361
- yield (_this9.use_bucket_name ? _this9.connection : _this9.collection).insert(doc_id, document, {
388
+ yield (_this10.use_bucket_name ? _this10.connection : _this10.collection).insert(doc_id, document, {
362
389
  expiry: expiry
363
390
  });
364
391
  } catch (e) {
365
392
  console.log(e.message, e, doc_id);
366
393
  }
367
394
 
368
- return yield _this9.get(doc_id);
395
+ return yield _this10.get(doc_id);
369
396
  })();
370
397
  }
371
398
 
372
399
  touch(doc_id) {
373
- var _this10 = this;
400
+ var _this11 = this;
374
401
 
375
402
  var expiry = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
376
403
  return _asyncToGenerator(function* () {
377
- yield (_this10.use_bucket_name ? _this10.connection : _this10.collection).touch(doc_id, expiry);
378
- return yield _this10.get(doc_id);
404
+ yield (_this11.use_bucket_name ? _this11.connection : _this11.collection).touch(doc_id, expiry);
405
+ return yield _this11.get(doc_id);
379
406
  })();
380
407
  }
381
408
 
382
409
  count(doc_id, adjustment, initial_value) {
383
- var _this11 = this;
410
+ var _this12 = this;
384
411
 
385
412
  return _asyncToGenerator(function* () {
386
- var res = yield (_this11.use_bucket_name ? _this11.connection : _this11.collection).counter(doc_id, adjustment, {
413
+ var res = yield (_this12.use_bucket_name ? _this12.connection : _this12.collection).counter(doc_id, adjustment, {
387
414
  initial: initial_value
388
415
  });
389
416
  return res.value;
@@ -391,10 +418,10 @@ module.exports = class Database {
391
418
  }
392
419
 
393
420
  update(doc_id, document, expiry) {
394
- var _this12 = this;
421
+ var _this13 = this;
395
422
 
396
423
  return _asyncToGenerator(function* () {
397
- var res = yield (_this12.use_bucket_name ? _this12.connection : _this12.collection).replace(doc_id, document, {
424
+ var res = yield (_this13.use_bucket_name ? _this13.connection : _this13.collection).replace(doc_id, document, {
398
425
  expiry: expiry
399
426
  });
400
427
  return res.value;
@@ -402,16 +429,16 @@ module.exports = class Database {
402
429
  }
403
430
 
404
431
  delete(doc_id) {
405
- var _this13 = this;
432
+ var _this14 = this;
406
433
 
407
434
  return _asyncToGenerator(function* () {
408
- var res = yield (_this13.use_bucket_name ? _this13.connection : _this13.collection).remove(doc_id);
435
+ var res = yield (_this14.use_bucket_name ? _this14.connection : _this14.collection).remove(doc_id);
409
436
  return res.value;
410
437
  })();
411
438
  }
412
439
 
413
440
  multi_get(keys) {
414
- var _this14 = this;
441
+ var _this15 = this;
415
442
 
416
443
  var {
417
444
  keep_type = true,
@@ -424,7 +451,7 @@ module.exports = class Database {
424
451
 
425
452
  if (_.isArray(keys) && keys.length > 0) {
426
453
  var useLookupIn = columns.length > 0;
427
- var collection = _this14.use_bucket_name ? _this14.connection : _this14.collection;
454
+ var collection = _this15.use_bucket_name ? _this15.connection : _this15.collection;
428
455
 
429
456
  if (!useLookupIn) {
430
457
  // Regular get - no batching needed
@@ -584,7 +611,7 @@ module.exports = class Database {
584
611
  }
585
612
 
586
613
  search(type) {
587
- var _this15 = this;
614
+ var _this16 = this;
588
615
 
589
616
  var columns = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
590
617
  var query = arguments.length > 2 ? arguments[2] : undefined;
@@ -595,8 +622,8 @@ module.exports = class Database {
595
622
  var query_scope = arguments.length > 7 ? arguments[7] : undefined;
596
623
  var join = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : "";
597
624
  return _asyncToGenerator(function* () {
598
- var is_legacy = _this15.scope_name !== "_default" && _this15.collection_name !== "_default";
599
- var bucket = "`".concat(_this15.use_bucket_name ? _this15.bucket : _this15.collection_name, "`");
625
+ var is_legacy = _this16.scope_name !== "_default" && _this16.collection_name !== "_default";
626
+ var bucket = "`".concat(_this16.use_bucket_name ? _this16.bucket : _this16.collection_name, "`");
600
627
 
601
628
  function processColumns(columnsStr) {
602
629
  // If columns is empty or *, return default
@@ -641,7 +668,7 @@ module.exports = class Database {
641
668
  var front_query = "SELECT ".concat(columns, " FROM ").concat(bucket, " ").concat(join);
642
669
  var conditions = [];
643
670
 
644
- if (_this15.use_bucket_name) {
671
+ if (_this16.use_bucket_name) {
645
672
  conditions.push("type = '".concat(type, "'"));
646
673
  }
647
674
 
@@ -657,11 +684,11 @@ module.exports = class Database {
657
684
  var limit_clause = limit > 0 ? " LIMIT ".concat(limit) : "";
658
685
  var offset_clause = offset > 0 ? " OFFSET ".concat(offset) : "";
659
686
  query = query + order_by_clause + limit_clause + offset_clause;
660
- var active_query_space = query_scope || !_this15.use_bucket_name ? "scope" : "cluster";
661
- var queries = [_this15.getData(query, active_query_space)];
687
+ var active_query_space = query_scope || !_this16.use_bucket_name ? "scope" : "cluster";
688
+ var queries = [_this16.getData(query, active_query_space)];
662
689
 
663
690
  if (do_count != "false") {
664
- queries.push(_this15.getCount(count_query, active_query_space));
691
+ queries.push(_this16.getCount(count_query, active_query_space));
665
692
  }
666
693
 
667
694
  var responses = yield Promise.all(queries);
@@ -745,21 +772,21 @@ module.exports = class Database {
745
772
  }
746
773
 
747
774
  getCount(count_query_element) {
748
- var _this16 = this;
775
+ var _this17 = this;
749
776
 
750
777
  var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
751
778
  return _asyncToGenerator(function* () {
752
- var response = yield _this16.query(count_query_element, query_scope);
779
+ var response = yield _this17.query(count_query_element, query_scope);
753
780
  return response.rows[0].count;
754
781
  })();
755
782
  }
756
783
 
757
784
  getData(query_element) {
758
- var _this17 = this;
785
+ var _this18 = this;
759
786
 
760
787
  var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
761
788
  return _asyncToGenerator(function* () {
762
- var response = yield _this17.query(query_element, query_scope);
789
+ var response = yield _this18.query(query_element, query_scope);
763
790
  return response.rows;
764
791
  })();
765
792
  }
@@ -801,7 +828,7 @@ module.exports = class Database {
801
828
  }
802
829
 
803
830
  optimize(query, exec_scope) {
804
- var _this18 = this;
831
+ var _this19 = this;
805
832
 
806
833
  return _asyncToGenerator(function* () {
807
834
  try {
@@ -817,16 +844,16 @@ module.exports = class Database {
817
844
 
818
845
  var plan = explainResponse === null || explainResponse === void 0 ? void 0 : (_explainResponse$rows = explainResponse.rows) === null || _explainResponse$rows === void 0 ? void 0 : _explainResponse$rows[0];
819
846
 
820
- if (!_this18.hasFetchOperator(plan)) {
847
+ if (!_this19.hasFetchOperator(plan)) {
821
848
  // Query is already covered, no need to create index
822
849
  return;
823
850
  } // 3. Build covering index from the query
824
851
 
825
852
 
826
853
  var indexStatement = buildCoveringIndex(query, {
827
- bucket: _this18.bucket,
828
- scope: _this18.scope_name,
829
- collection: _this18.collection_name
854
+ bucket: _this19.bucket,
855
+ scope: _this19.scope_name,
856
+ collection: _this19.collection_name
830
857
  }); // 4. Execute the index creation if we got a valid statement
831
858
 
832
859
  if (indexStatement) {
@@ -838,19 +865,19 @@ module.exports = class Database {
838
865
  }
839
866
 
840
867
  query(query) {
841
- var _this19 = this;
868
+ var _this20 = this;
842
869
 
843
870
  var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
844
871
  return _asyncToGenerator(function* () {
845
- var scope = query_scope || _this19.query_scope;
872
+ var scope = query_scope || _this20.query_scope;
846
873
  var sanitizedQuery = query; //sanitization step to be implemented
847
874
 
848
875
  var exec_scope = null;
849
876
 
850
877
  if (scope === "cluster") {
851
- exec_scope = _this19.cluster;
878
+ exec_scope = _this20.cluster;
852
879
  } else {
853
- exec_scope = _this19.scope;
880
+ exec_scope = _this20.scope;
854
881
  } // Start time
855
882
 
856
883
 
@@ -865,7 +892,7 @@ module.exports = class Database {
865
892
  var durationInMilliseconds = (endTime - startTime) / BigInt(1000000); // optimize if response time is greater than 1 second
866
893
 
867
894
  if (durationInMilliseconds > 2000) {
868
- _this19.optimize(query, exec_scope);
895
+ _this20.optimize(query, exec_scope);
869
896
  }
870
897
 
871
898
  return response;
package/build/utility.js CHANGED
@@ -389,4 +389,4 @@ module.exports = {
389
389
  extractOrderByFields,
390
390
  extractOrderByIndexFields,
391
391
  generateHash
392
- };
392
+ };
@@ -831,4 +831,4 @@ test("field order: WHERE -> GROUP BY -> ORDER BY -> SELECT", () => {
831
831
  console.log("\n========================================");
832
832
  console.log("Tests: ".concat(passed + failed, " | Passed: ").concat(passed, " | Failed: ").concat(failed));
833
833
  console.log("========================================\n");
834
- process.exit(failed > 0 ? 1 : 0);
834
+ process.exit(failed > 0 ? 1 : 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-couchbase",
3
- "version": "2.0.49",
3
+ "version": "2.0.51",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -250,13 +250,47 @@ module.exports = class Database {
250
250
  } catch (e) {}
251
251
  }
252
252
 
253
+ async primaryIndexExists(scope_name, col_name) {
254
+ try {
255
+ let response = await this.cluster.query(
256
+ `SELECT RAW name
257
+ FROM system:indexes
258
+ WHERE bucket_id = ?
259
+ AND scope_id = ?
260
+ AND keyspace_id = ?
261
+ AND is_primary = true
262
+ LIMIT 1`,
263
+ {
264
+ parameters: [this.db_config.bucket, scope_name, col_name],
265
+ scanConsistency: QueryScanConsistency.RequestPlus,
266
+ },
267
+ );
268
+ return Array.isArray(response?.rows) && response.rows.length > 0;
269
+ } catch (e) {
270
+ console.log("Primary index lookup failed:", e);
271
+ return false;
272
+ }
273
+ }
274
+
253
275
  async createPrimaryIndex(scope_name, col_name) {
254
276
  try {
277
+ if (await this.primaryIndexExists(scope_name, col_name)) {
278
+ return;
279
+ }
255
280
  let response = await this.query(
256
281
  `CREATE PRIMARY INDEX ON default:\`${this.db_config.bucket}\`.\`${scope_name}\`.\`${col_name}\``,
257
282
  );
258
283
  return response;
259
284
  } catch (e) {
285
+ if (
286
+ e instanceof couchbase.IndexExistsError ||
287
+ e?.cause?.first_error_code === 4300 ||
288
+ /index .*already exists|index_exists/i.test(
289
+ e?.message || e?.cause?.first_error_message || "",
290
+ )
291
+ ) {
292
+ return;
293
+ }
260
294
  console.log("CREATE Index :::", e);
261
295
  }
262
296
  }