spice-couchbase 2.0.50 → 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 +52 -50
- package/build/utility.js +1 -1
- package/build/utility.test.js +1 -1
- package/package.json +1 -1
- package/src/index.js +13 -8
package/build/index.js
CHANGED
|
@@ -280,15 +280,11 @@ module.exports = class Database {
|
|
|
280
280
|
|
|
281
281
|
return _asyncToGenerator(function* () {
|
|
282
282
|
try {
|
|
283
|
-
var response = yield _this6.cluster.query("SELECT RAW name\n FROM system:indexes\n WHERE bucket_id =
|
|
284
|
-
|
|
285
|
-
bucket: _this6.db_config.bucket,
|
|
286
|
-
scope: scope_name,
|
|
287
|
-
collection: col_name
|
|
288
|
-
},
|
|
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],
|
|
289
285
|
scanConsistency: QueryScanConsistency.RequestPlus
|
|
290
286
|
});
|
|
291
|
-
return Array.isArray(response
|
|
287
|
+
return Array.isArray(response === null || response === void 0 ? void 0 : response.rows) && response.rows.length > 0;
|
|
292
288
|
} catch (e) {
|
|
293
289
|
console.log("Primary index lookup failed:", e);
|
|
294
290
|
return false;
|
|
@@ -297,28 +293,34 @@ module.exports = class Database {
|
|
|
297
293
|
}
|
|
298
294
|
|
|
299
295
|
createPrimaryIndex(scope_name, col_name) {
|
|
300
|
-
var
|
|
296
|
+
var _this7 = this;
|
|
301
297
|
|
|
302
298
|
return _asyncToGenerator(function* () {
|
|
303
299
|
try {
|
|
304
|
-
if (yield
|
|
300
|
+
if (yield _this7.primaryIndexExists(scope_name, col_name)) {
|
|
305
301
|
return;
|
|
306
302
|
}
|
|
307
303
|
|
|
308
|
-
var response = yield
|
|
304
|
+
var response = yield _this7.query("CREATE PRIMARY INDEX ON default:`".concat(_this7.db_config.bucket, "`.`").concat(scope_name, "`.`").concat(col_name, "`"));
|
|
309
305
|
return response;
|
|
310
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
|
+
|
|
311
313
|
console.log("CREATE Index :::", e);
|
|
312
314
|
}
|
|
313
315
|
})();
|
|
314
316
|
}
|
|
315
317
|
|
|
316
318
|
createScope(scope_name) {
|
|
317
|
-
var
|
|
319
|
+
var _this8 = this;
|
|
318
320
|
|
|
319
321
|
return _asyncToGenerator(function* () {
|
|
320
322
|
try {
|
|
321
|
-
yield
|
|
323
|
+
yield _this8.collection_manager.createScope(scope_name);
|
|
322
324
|
} catch (e) {
|
|
323
325
|
if (e instanceof couchbase.ScopeExistsError) {
|
|
324
326
|
console.log("The scope already exists");
|
|
@@ -330,7 +332,7 @@ module.exports = class Database {
|
|
|
330
332
|
}
|
|
331
333
|
|
|
332
334
|
get(doc_id) {
|
|
333
|
-
var
|
|
335
|
+
var _this9 = this;
|
|
334
336
|
|
|
335
337
|
var {
|
|
336
338
|
columns = []
|
|
@@ -338,7 +340,7 @@ module.exports = class Database {
|
|
|
338
340
|
return _asyncToGenerator(function* () {
|
|
339
341
|
try {
|
|
340
342
|
var useLookupIn = columns.length > 0;
|
|
341
|
-
var collection =
|
|
343
|
+
var collection = _this9.use_bucket_name ? _this9.connection : _this9.collection;
|
|
342
344
|
|
|
343
345
|
if (!useLookupIn) {
|
|
344
346
|
var response = yield collection.get(doc_id);
|
|
@@ -379,36 +381,36 @@ module.exports = class Database {
|
|
|
379
381
|
}
|
|
380
382
|
|
|
381
383
|
insert(doc_id, document, expiry) {
|
|
382
|
-
var
|
|
384
|
+
var _this10 = this;
|
|
383
385
|
|
|
384
386
|
return _asyncToGenerator(function* () {
|
|
385
387
|
try {
|
|
386
|
-
yield (
|
|
388
|
+
yield (_this10.use_bucket_name ? _this10.connection : _this10.collection).insert(doc_id, document, {
|
|
387
389
|
expiry: expiry
|
|
388
390
|
});
|
|
389
391
|
} catch (e) {
|
|
390
392
|
console.log(e.message, e, doc_id);
|
|
391
393
|
}
|
|
392
394
|
|
|
393
|
-
return yield
|
|
395
|
+
return yield _this10.get(doc_id);
|
|
394
396
|
})();
|
|
395
397
|
}
|
|
396
398
|
|
|
397
399
|
touch(doc_id) {
|
|
398
|
-
var
|
|
400
|
+
var _this11 = this;
|
|
399
401
|
|
|
400
402
|
var expiry = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
401
403
|
return _asyncToGenerator(function* () {
|
|
402
|
-
yield (
|
|
403
|
-
return yield
|
|
404
|
+
yield (_this11.use_bucket_name ? _this11.connection : _this11.collection).touch(doc_id, expiry);
|
|
405
|
+
return yield _this11.get(doc_id);
|
|
404
406
|
})();
|
|
405
407
|
}
|
|
406
408
|
|
|
407
409
|
count(doc_id, adjustment, initial_value) {
|
|
408
|
-
var
|
|
410
|
+
var _this12 = this;
|
|
409
411
|
|
|
410
412
|
return _asyncToGenerator(function* () {
|
|
411
|
-
var res = yield (
|
|
413
|
+
var res = yield (_this12.use_bucket_name ? _this12.connection : _this12.collection).counter(doc_id, adjustment, {
|
|
412
414
|
initial: initial_value
|
|
413
415
|
});
|
|
414
416
|
return res.value;
|
|
@@ -416,10 +418,10 @@ module.exports = class Database {
|
|
|
416
418
|
}
|
|
417
419
|
|
|
418
420
|
update(doc_id, document, expiry) {
|
|
419
|
-
var
|
|
421
|
+
var _this13 = this;
|
|
420
422
|
|
|
421
423
|
return _asyncToGenerator(function* () {
|
|
422
|
-
var res = yield (
|
|
424
|
+
var res = yield (_this13.use_bucket_name ? _this13.connection : _this13.collection).replace(doc_id, document, {
|
|
423
425
|
expiry: expiry
|
|
424
426
|
});
|
|
425
427
|
return res.value;
|
|
@@ -427,16 +429,16 @@ module.exports = class Database {
|
|
|
427
429
|
}
|
|
428
430
|
|
|
429
431
|
delete(doc_id) {
|
|
430
|
-
var
|
|
432
|
+
var _this14 = this;
|
|
431
433
|
|
|
432
434
|
return _asyncToGenerator(function* () {
|
|
433
|
-
var res = yield (
|
|
435
|
+
var res = yield (_this14.use_bucket_name ? _this14.connection : _this14.collection).remove(doc_id);
|
|
434
436
|
return res.value;
|
|
435
437
|
})();
|
|
436
438
|
}
|
|
437
439
|
|
|
438
440
|
multi_get(keys) {
|
|
439
|
-
var
|
|
441
|
+
var _this15 = this;
|
|
440
442
|
|
|
441
443
|
var {
|
|
442
444
|
keep_type = true,
|
|
@@ -449,7 +451,7 @@ module.exports = class Database {
|
|
|
449
451
|
|
|
450
452
|
if (_.isArray(keys) && keys.length > 0) {
|
|
451
453
|
var useLookupIn = columns.length > 0;
|
|
452
|
-
var collection =
|
|
454
|
+
var collection = _this15.use_bucket_name ? _this15.connection : _this15.collection;
|
|
453
455
|
|
|
454
456
|
if (!useLookupIn) {
|
|
455
457
|
// Regular get - no batching needed
|
|
@@ -609,7 +611,7 @@ module.exports = class Database {
|
|
|
609
611
|
}
|
|
610
612
|
|
|
611
613
|
search(type) {
|
|
612
|
-
var
|
|
614
|
+
var _this16 = this;
|
|
613
615
|
|
|
614
616
|
var columns = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
|
|
615
617
|
var query = arguments.length > 2 ? arguments[2] : undefined;
|
|
@@ -620,8 +622,8 @@ module.exports = class Database {
|
|
|
620
622
|
var query_scope = arguments.length > 7 ? arguments[7] : undefined;
|
|
621
623
|
var join = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : "";
|
|
622
624
|
return _asyncToGenerator(function* () {
|
|
623
|
-
var is_legacy =
|
|
624
|
-
var bucket = "`".concat(
|
|
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, "`");
|
|
625
627
|
|
|
626
628
|
function processColumns(columnsStr) {
|
|
627
629
|
// If columns is empty or *, return default
|
|
@@ -666,7 +668,7 @@ module.exports = class Database {
|
|
|
666
668
|
var front_query = "SELECT ".concat(columns, " FROM ").concat(bucket, " ").concat(join);
|
|
667
669
|
var conditions = [];
|
|
668
670
|
|
|
669
|
-
if (
|
|
671
|
+
if (_this16.use_bucket_name) {
|
|
670
672
|
conditions.push("type = '".concat(type, "'"));
|
|
671
673
|
}
|
|
672
674
|
|
|
@@ -682,11 +684,11 @@ module.exports = class Database {
|
|
|
682
684
|
var limit_clause = limit > 0 ? " LIMIT ".concat(limit) : "";
|
|
683
685
|
var offset_clause = offset > 0 ? " OFFSET ".concat(offset) : "";
|
|
684
686
|
query = query + order_by_clause + limit_clause + offset_clause;
|
|
685
|
-
var active_query_space = query_scope || !
|
|
686
|
-
var queries = [
|
|
687
|
+
var active_query_space = query_scope || !_this16.use_bucket_name ? "scope" : "cluster";
|
|
688
|
+
var queries = [_this16.getData(query, active_query_space)];
|
|
687
689
|
|
|
688
690
|
if (do_count != "false") {
|
|
689
|
-
queries.push(
|
|
691
|
+
queries.push(_this16.getCount(count_query, active_query_space));
|
|
690
692
|
}
|
|
691
693
|
|
|
692
694
|
var responses = yield Promise.all(queries);
|
|
@@ -770,21 +772,21 @@ module.exports = class Database {
|
|
|
770
772
|
}
|
|
771
773
|
|
|
772
774
|
getCount(count_query_element) {
|
|
773
|
-
var
|
|
775
|
+
var _this17 = this;
|
|
774
776
|
|
|
775
777
|
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
776
778
|
return _asyncToGenerator(function* () {
|
|
777
|
-
var response = yield
|
|
779
|
+
var response = yield _this17.query(count_query_element, query_scope);
|
|
778
780
|
return response.rows[0].count;
|
|
779
781
|
})();
|
|
780
782
|
}
|
|
781
783
|
|
|
782
784
|
getData(query_element) {
|
|
783
|
-
var
|
|
785
|
+
var _this18 = this;
|
|
784
786
|
|
|
785
787
|
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
786
788
|
return _asyncToGenerator(function* () {
|
|
787
|
-
var response = yield
|
|
789
|
+
var response = yield _this18.query(query_element, query_scope);
|
|
788
790
|
return response.rows;
|
|
789
791
|
})();
|
|
790
792
|
}
|
|
@@ -826,7 +828,7 @@ module.exports = class Database {
|
|
|
826
828
|
}
|
|
827
829
|
|
|
828
830
|
optimize(query, exec_scope) {
|
|
829
|
-
var
|
|
831
|
+
var _this19 = this;
|
|
830
832
|
|
|
831
833
|
return _asyncToGenerator(function* () {
|
|
832
834
|
try {
|
|
@@ -842,16 +844,16 @@ module.exports = class Database {
|
|
|
842
844
|
|
|
843
845
|
var plan = explainResponse === null || explainResponse === void 0 ? void 0 : (_explainResponse$rows = explainResponse.rows) === null || _explainResponse$rows === void 0 ? void 0 : _explainResponse$rows[0];
|
|
844
846
|
|
|
845
|
-
if (!
|
|
847
|
+
if (!_this19.hasFetchOperator(plan)) {
|
|
846
848
|
// Query is already covered, no need to create index
|
|
847
849
|
return;
|
|
848
850
|
} // 3. Build covering index from the query
|
|
849
851
|
|
|
850
852
|
|
|
851
853
|
var indexStatement = buildCoveringIndex(query, {
|
|
852
|
-
bucket:
|
|
853
|
-
scope:
|
|
854
|
-
collection:
|
|
854
|
+
bucket: _this19.bucket,
|
|
855
|
+
scope: _this19.scope_name,
|
|
856
|
+
collection: _this19.collection_name
|
|
855
857
|
}); // 4. Execute the index creation if we got a valid statement
|
|
856
858
|
|
|
857
859
|
if (indexStatement) {
|
|
@@ -863,19 +865,19 @@ module.exports = class Database {
|
|
|
863
865
|
}
|
|
864
866
|
|
|
865
867
|
query(query) {
|
|
866
|
-
var
|
|
868
|
+
var _this20 = this;
|
|
867
869
|
|
|
868
870
|
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
869
871
|
return _asyncToGenerator(function* () {
|
|
870
|
-
var scope = query_scope ||
|
|
872
|
+
var scope = query_scope || _this20.query_scope;
|
|
871
873
|
var sanitizedQuery = query; //sanitization step to be implemented
|
|
872
874
|
|
|
873
875
|
var exec_scope = null;
|
|
874
876
|
|
|
875
877
|
if (scope === "cluster") {
|
|
876
|
-
exec_scope =
|
|
878
|
+
exec_scope = _this20.cluster;
|
|
877
879
|
} else {
|
|
878
|
-
exec_scope =
|
|
880
|
+
exec_scope = _this20.scope;
|
|
879
881
|
} // Start time
|
|
880
882
|
|
|
881
883
|
|
|
@@ -890,11 +892,11 @@ module.exports = class Database {
|
|
|
890
892
|
var durationInMilliseconds = (endTime - startTime) / BigInt(1000000); // optimize if response time is greater than 1 second
|
|
891
893
|
|
|
892
894
|
if (durationInMilliseconds > 2000) {
|
|
893
|
-
|
|
895
|
+
_this20.optimize(query, exec_scope);
|
|
894
896
|
}
|
|
895
897
|
|
|
896
898
|
return response;
|
|
897
899
|
})();
|
|
898
900
|
}
|
|
899
901
|
|
|
900
|
-
};
|
|
902
|
+
};
|
package/build/utility.js
CHANGED
package/build/utility.test.js
CHANGED
|
@@ -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
package/src/index.js
CHANGED
|
@@ -255,17 +255,13 @@ module.exports = class Database {
|
|
|
255
255
|
let response = await this.cluster.query(
|
|
256
256
|
`SELECT RAW name
|
|
257
257
|
FROM system:indexes
|
|
258
|
-
WHERE bucket_id =
|
|
259
|
-
AND scope_id =
|
|
260
|
-
AND keyspace_id =
|
|
258
|
+
WHERE bucket_id = ?
|
|
259
|
+
AND scope_id = ?
|
|
260
|
+
AND keyspace_id = ?
|
|
261
261
|
AND is_primary = true
|
|
262
262
|
LIMIT 1`,
|
|
263
263
|
{
|
|
264
|
-
|
|
265
|
-
bucket: this.db_config.bucket,
|
|
266
|
-
scope: scope_name,
|
|
267
|
-
collection: col_name,
|
|
268
|
-
},
|
|
264
|
+
parameters: [this.db_config.bucket, scope_name, col_name],
|
|
269
265
|
scanConsistency: QueryScanConsistency.RequestPlus,
|
|
270
266
|
},
|
|
271
267
|
);
|
|
@@ -286,6 +282,15 @@ module.exports = class Database {
|
|
|
286
282
|
);
|
|
287
283
|
return response;
|
|
288
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
|
+
}
|
|
289
294
|
console.log("CREATE Index :::", e);
|
|
290
295
|
}
|
|
291
296
|
}
|