spice-couchbase 2.0.54 → 2.0.55
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 +67 -11
- package/build/index.query-consistency.test.js +229 -0
- package/package.json +3 -2
- package/src/index.js +86 -12
- package/src/index.query-consistency.test.js +154 -0
package/build/index.js
CHANGED
|
@@ -34,6 +34,56 @@ if (!Promise.allSettled) {
|
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
var LOOKUP_IN_MAX_SPECS = 16;
|
|
37
|
+
|
|
38
|
+
function normalizeScanConsistency(value) {
|
|
39
|
+
if (value === true || value === QueryScanConsistency.RequestPlus || typeof value === "string" && ["true", "request_plus"].includes(value.toLowerCase())) {
|
|
40
|
+
return QueryScanConsistency.RequestPlus;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (value === undefined || value === null || value === "" || value === false || value === QueryScanConsistency.NotBounded || typeof value === "string" && ["false", "not_bounded"].includes(value.toLowerCase())) {
|
|
44
|
+
return QueryScanConsistency.NotBounded;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
throw new Error("Unsupported query scan consistency: ".concat(value));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function buildQueryOptions() {
|
|
51
|
+
var _queryOptions$consist, _queryOptions$scanWai;
|
|
52
|
+
|
|
53
|
+
var queryOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
54
|
+
|
|
55
|
+
if (typeof queryOptions !== "object" || Array.isArray(queryOptions)) {
|
|
56
|
+
queryOptions = {
|
|
57
|
+
scan_consistency: queryOptions
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
var options = {};
|
|
62
|
+
var consistentWith = (_queryOptions$consist = queryOptions.consistentWith) !== null && _queryOptions$consist !== void 0 ? _queryOptions$consist : queryOptions.consistent_with;
|
|
63
|
+
|
|
64
|
+
if (consistentWith !== undefined && consistentWith !== null) {
|
|
65
|
+
options.consistentWith = consistentWith;
|
|
66
|
+
} else {
|
|
67
|
+
var _queryOptions$scanCon;
|
|
68
|
+
|
|
69
|
+
options.scanConsistency = normalizeScanConsistency((_queryOptions$scanCon = queryOptions.scanConsistency) !== null && _queryOptions$scanCon !== void 0 ? _queryOptions$scanCon : queryOptions.scan_consistency);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
var scanWait = (_queryOptions$scanWai = queryOptions.scanWait) !== null && _queryOptions$scanWai !== void 0 ? _queryOptions$scanWai : queryOptions.scan_wait;
|
|
73
|
+
|
|
74
|
+
if (scanWait !== undefined && scanWait !== null && scanWait !== "") {
|
|
75
|
+
var numericScanWait = Number(scanWait);
|
|
76
|
+
|
|
77
|
+
if (!Number.isFinite(numericScanWait) || numericScanWait < 0) {
|
|
78
|
+
throw new Error("Invalid query scan wait: ".concat(scanWait));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
options.scanWait = numericScanWait;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return options;
|
|
85
|
+
}
|
|
86
|
+
|
|
37
87
|
module.exports = class Database {
|
|
38
88
|
constructor(connection) {
|
|
39
89
|
var {
|
|
@@ -600,6 +650,7 @@ module.exports = class Database {
|
|
|
600
650
|
var do_count = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : "false";
|
|
601
651
|
var query_scope = arguments.length > 7 ? arguments[7] : undefined;
|
|
602
652
|
var join = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : "";
|
|
653
|
+
var query_options = arguments.length > 9 && arguments[9] !== undefined ? arguments[9] : {};
|
|
603
654
|
return _asyncToGenerator(function* () {
|
|
604
655
|
var is_legacy = _this16.scope_name !== "_default" && _this16.collection_name !== "_default";
|
|
605
656
|
var bucket = "`".concat(_this16.use_bucket_name ? _this16.bucket : _this16.collection_name, "`");
|
|
@@ -663,11 +714,11 @@ module.exports = class Database {
|
|
|
663
714
|
var limit_clause = limit > 0 ? " LIMIT ".concat(limit) : "";
|
|
664
715
|
var offset_clause = offset > 0 ? " OFFSET ".concat(offset) : "";
|
|
665
716
|
query = query + order_by_clause + limit_clause + offset_clause;
|
|
666
|
-
var active_query_space = query_scope || !_this16.use_bucket_name ? "scope" : "cluster";
|
|
667
|
-
var queries = [_this16.getData(query, active_query_space)];
|
|
717
|
+
var active_query_space = query_scope || (!_this16.use_bucket_name ? "scope" : "cluster");
|
|
718
|
+
var queries = [_this16.getData(query, active_query_space, query_options)];
|
|
668
719
|
|
|
669
720
|
if (do_count != "false") {
|
|
670
|
-
queries.push(_this16.getCount(count_query, active_query_space));
|
|
721
|
+
queries.push(_this16.getCount(count_query, active_query_space, query_options));
|
|
671
722
|
}
|
|
672
723
|
|
|
673
724
|
var responses = yield Promise.all(queries);
|
|
@@ -754,8 +805,9 @@ module.exports = class Database {
|
|
|
754
805
|
var _this17 = this;
|
|
755
806
|
|
|
756
807
|
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
808
|
+
var query_options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
757
809
|
return _asyncToGenerator(function* () {
|
|
758
|
-
var response = yield _this17.query(count_query_element, query_scope);
|
|
810
|
+
var response = yield _this17.query(count_query_element, query_scope, query_options);
|
|
759
811
|
return response.rows[0].count;
|
|
760
812
|
})();
|
|
761
813
|
}
|
|
@@ -764,8 +816,9 @@ module.exports = class Database {
|
|
|
764
816
|
var _this18 = this;
|
|
765
817
|
|
|
766
818
|
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
819
|
+
var query_options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
767
820
|
return _asyncToGenerator(function* () {
|
|
768
|
-
var response = yield _this18.query(query_element, query_scope);
|
|
821
|
+
var response = yield _this18.query(query_element, query_scope, query_options);
|
|
769
822
|
return response.rows;
|
|
770
823
|
})();
|
|
771
824
|
}
|
|
@@ -843,12 +896,17 @@ module.exports = class Database {
|
|
|
843
896
|
})();
|
|
844
897
|
}
|
|
845
898
|
|
|
846
|
-
query(query) {
|
|
899
|
+
query(query, query_scope) {
|
|
847
900
|
var _this20 = this;
|
|
848
901
|
|
|
849
|
-
var
|
|
902
|
+
var query_options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
850
903
|
return _asyncToGenerator(function* () {
|
|
851
|
-
|
|
904
|
+
if (query_scope && typeof query_scope === "object" && !Array.isArray(query_scope)) {
|
|
905
|
+
query_options = query_scope;
|
|
906
|
+
query_scope = undefined;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
var scope = query_scope || _this20.query_scope || "cluster";
|
|
852
910
|
var sanitizedQuery = query; //sanitization step to be implemented
|
|
853
911
|
|
|
854
912
|
var exec_scope = null;
|
|
@@ -861,9 +919,7 @@ module.exports = class Database {
|
|
|
861
919
|
|
|
862
920
|
|
|
863
921
|
var startTime = process.hrtime.bigint();
|
|
864
|
-
var options =
|
|
865
|
-
scanConsistency: QueryScanConsistency.RequestPlus
|
|
866
|
-
};
|
|
922
|
+
var options = buildQueryOptions(query_options);
|
|
867
923
|
var response = yield exec_scope.query(sanitizedQuery, options); // End time
|
|
868
924
|
|
|
869
925
|
var endTime = process.hrtime.bigint(); // Calculate duration in microseconds
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
+
|
|
5
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
6
|
+
|
|
7
|
+
var assert = require("assert");
|
|
8
|
+
|
|
9
|
+
var {
|
|
10
|
+
QueryScanConsistency
|
|
11
|
+
} = require("couchbase");
|
|
12
|
+
|
|
13
|
+
var Database = require("./index");
|
|
14
|
+
|
|
15
|
+
var passed = 0;
|
|
16
|
+
var failed = 0;
|
|
17
|
+
|
|
18
|
+
function createQueryDb() {
|
|
19
|
+
var calls = [];
|
|
20
|
+
var db = Object.create(Database.prototype);
|
|
21
|
+
db.query_scope = "cluster";
|
|
22
|
+
db.cluster = {
|
|
23
|
+
query: function () {
|
|
24
|
+
var _query = _asyncToGenerator(function* (statement, options) {
|
|
25
|
+
calls.push({
|
|
26
|
+
target: "cluster",
|
|
27
|
+
statement,
|
|
28
|
+
options
|
|
29
|
+
});
|
|
30
|
+
return {
|
|
31
|
+
rows: []
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
function query(_x, _x2) {
|
|
36
|
+
return _query.apply(this, arguments);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return query;
|
|
40
|
+
}()
|
|
41
|
+
};
|
|
42
|
+
db.scope = {
|
|
43
|
+
query: function () {
|
|
44
|
+
var _query2 = _asyncToGenerator(function* (statement, options) {
|
|
45
|
+
calls.push({
|
|
46
|
+
target: "scope",
|
|
47
|
+
statement,
|
|
48
|
+
options
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
rows: []
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
function query(_x3, _x4) {
|
|
56
|
+
return _query2.apply(this, arguments);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return query;
|
|
60
|
+
}()
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
db.optimize = () => {};
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
db,
|
|
67
|
+
calls
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function test(_x5, _x6) {
|
|
72
|
+
return _test.apply(this, arguments);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function _test() {
|
|
76
|
+
_test = _asyncToGenerator(function* (name, fn) {
|
|
77
|
+
try {
|
|
78
|
+
yield fn();
|
|
79
|
+
console.log("\u2713 ".concat(name));
|
|
80
|
+
passed += 1;
|
|
81
|
+
} catch (error) {
|
|
82
|
+
console.log("\u2717 ".concat(name));
|
|
83
|
+
console.log(" Error: ".concat(error.stack || error.message));
|
|
84
|
+
failed += 1;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return _test.apply(this, arguments);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function run() {
|
|
91
|
+
return _run.apply(this, arguments);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function _run() {
|
|
95
|
+
_run = _asyncToGenerator(function* () {
|
|
96
|
+
console.log("\n--- query consistency tests ---");
|
|
97
|
+
yield test("ordinary SQL++ queries default to not_bounded",
|
|
98
|
+
/*#__PURE__*/
|
|
99
|
+
_asyncToGenerator(function* () {
|
|
100
|
+
var {
|
|
101
|
+
db,
|
|
102
|
+
calls
|
|
103
|
+
} = createQueryDb();
|
|
104
|
+
yield db.query("SELECT 1");
|
|
105
|
+
assert.strictEqual(calls[0].target, "cluster");
|
|
106
|
+
assert.strictEqual(calls[0].options.scanConsistency, QueryScanConsistency.NotBounded);
|
|
107
|
+
}));
|
|
108
|
+
yield test("request_plus can be selected explicitly",
|
|
109
|
+
/*#__PURE__*/
|
|
110
|
+
_asyncToGenerator(function* () {
|
|
111
|
+
var {
|
|
112
|
+
db,
|
|
113
|
+
calls
|
|
114
|
+
} = createQueryDb();
|
|
115
|
+
yield db.query("SELECT 1", "scope", {
|
|
116
|
+
scan_consistency: "request_plus",
|
|
117
|
+
scan_wait: 2500
|
|
118
|
+
});
|
|
119
|
+
assert.strictEqual(calls[0].target, "scope");
|
|
120
|
+
assert.strictEqual(calls[0].options.scanConsistency, QueryScanConsistency.RequestPlus);
|
|
121
|
+
assert.strictEqual(calls[0].options.scanWait, 2500);
|
|
122
|
+
}));
|
|
123
|
+
yield test("legacy boolean consistency values remain supported",
|
|
124
|
+
/*#__PURE__*/
|
|
125
|
+
_asyncToGenerator(function* () {
|
|
126
|
+
var {
|
|
127
|
+
db,
|
|
128
|
+
calls
|
|
129
|
+
} = createQueryDb();
|
|
130
|
+
yield db.query("SELECT 1", "cluster", {
|
|
131
|
+
scan_consistency: true
|
|
132
|
+
});
|
|
133
|
+
assert.strictEqual(calls[0].options.scanConsistency, QueryScanConsistency.RequestPlus);
|
|
134
|
+
}));
|
|
135
|
+
yield test("mutation state is forwarded without a conflicting scan mode",
|
|
136
|
+
/*#__PURE__*/
|
|
137
|
+
_asyncToGenerator(function* () {
|
|
138
|
+
var {
|
|
139
|
+
db,
|
|
140
|
+
calls
|
|
141
|
+
} = createQueryDb();
|
|
142
|
+
var mutationState = {
|
|
143
|
+
token: "test-token"
|
|
144
|
+
};
|
|
145
|
+
yield db.query("SELECT 1", {
|
|
146
|
+
consistent_with: mutationState,
|
|
147
|
+
scan_wait: 1000
|
|
148
|
+
});
|
|
149
|
+
assert.strictEqual(calls[0].options.consistentWith, mutationState);
|
|
150
|
+
assert.strictEqual(calls[0].options.scanConsistency, undefined);
|
|
151
|
+
assert.strictEqual(calls[0].options.scanWait, 1000);
|
|
152
|
+
}));
|
|
153
|
+
yield test("search keeps query scope separate from consistency",
|
|
154
|
+
/*#__PURE__*/
|
|
155
|
+
_asyncToGenerator(function* () {
|
|
156
|
+
var db = Object.create(Database.prototype);
|
|
157
|
+
var calls = [];
|
|
158
|
+
db.use_bucket_name = true;
|
|
159
|
+
db.bucket = "documents";
|
|
160
|
+
db.scope_name = "_default";
|
|
161
|
+
db.collection_name = "_default";
|
|
162
|
+
|
|
163
|
+
db.getData =
|
|
164
|
+
/*#__PURE__*/
|
|
165
|
+
function () {
|
|
166
|
+
var _ref6 = _asyncToGenerator(function* (statement, scope, options) {
|
|
167
|
+
calls.push({
|
|
168
|
+
kind: "data",
|
|
169
|
+
statement,
|
|
170
|
+
scope,
|
|
171
|
+
options
|
|
172
|
+
});
|
|
173
|
+
return [];
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
return function (_x7, _x8, _x9) {
|
|
177
|
+
return _ref6.apply(this, arguments);
|
|
178
|
+
};
|
|
179
|
+
}();
|
|
180
|
+
|
|
181
|
+
db.getCount =
|
|
182
|
+
/*#__PURE__*/
|
|
183
|
+
function () {
|
|
184
|
+
var _ref7 = _asyncToGenerator(function* (statement, scope, options) {
|
|
185
|
+
calls.push({
|
|
186
|
+
kind: "count",
|
|
187
|
+
statement,
|
|
188
|
+
scope,
|
|
189
|
+
options
|
|
190
|
+
});
|
|
191
|
+
return 0;
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
return function (_x10, _x11, _x12) {
|
|
195
|
+
return _ref7.apply(this, arguments);
|
|
196
|
+
};
|
|
197
|
+
}();
|
|
198
|
+
|
|
199
|
+
var options = {
|
|
200
|
+
scan_consistency: "request_plus"
|
|
201
|
+
};
|
|
202
|
+
yield db.search("application", "", "", 10, 0, "created_at DESC", "true", "cluster", "", options);
|
|
203
|
+
assert.deepStrictEqual(calls.map(call => call.scope), ["cluster", "cluster"]);
|
|
204
|
+
assert.strictEqual(calls[0].options, options);
|
|
205
|
+
assert.strictEqual(calls[1].options, options);
|
|
206
|
+
}));
|
|
207
|
+
yield test("invalid consistency values fail before issuing a query",
|
|
208
|
+
/*#__PURE__*/
|
|
209
|
+
_asyncToGenerator(function* () {
|
|
210
|
+
var {
|
|
211
|
+
db,
|
|
212
|
+
calls
|
|
213
|
+
} = createQueryDb();
|
|
214
|
+
yield assert.rejects(() => db.query("SELECT 1", "cluster", {
|
|
215
|
+
scan_consistency: "eventual"
|
|
216
|
+
}), /Unsupported query scan consistency/);
|
|
217
|
+
assert.strictEqual(calls.length, 0);
|
|
218
|
+
}));
|
|
219
|
+
console.log("\nPassed: ".concat(passed));
|
|
220
|
+
console.log("Failed: ".concat(failed));
|
|
221
|
+
if (failed > 0) process.exitCode = 1;
|
|
222
|
+
});
|
|
223
|
+
return _run.apply(this, arguments);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
run().catch(error => {
|
|
227
|
+
console.error(error);
|
|
228
|
+
process.exit(1);
|
|
229
|
+
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "spice-couchbase",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.55",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "node src/utility.test.js"
|
|
7
|
+
"test": "node src/utility.test.js",
|
|
8
|
+
"test:query-consistency": "node src/index.query-consistency.test.js"
|
|
8
9
|
},
|
|
9
10
|
"author": "Chad Fraser <chad@sonover.com> (http://sonover.com)",
|
|
10
11
|
"license": "ISC",
|
package/src/index.js
CHANGED
|
@@ -25,6 +25,60 @@ if (!Promise.allSettled) {
|
|
|
25
25
|
// Couchbase lookupIn has a limit of 16 operations per call
|
|
26
26
|
const LOOKUP_IN_MAX_SPECS = 16;
|
|
27
27
|
|
|
28
|
+
function normalizeScanConsistency(value) {
|
|
29
|
+
if (
|
|
30
|
+
value === true ||
|
|
31
|
+
value === QueryScanConsistency.RequestPlus ||
|
|
32
|
+
(typeof value === "string" &&
|
|
33
|
+
["true", "request_plus"].includes(value.toLowerCase()))
|
|
34
|
+
) {
|
|
35
|
+
return QueryScanConsistency.RequestPlus;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (
|
|
39
|
+
value === undefined ||
|
|
40
|
+
value === null ||
|
|
41
|
+
value === "" ||
|
|
42
|
+
value === false ||
|
|
43
|
+
value === QueryScanConsistency.NotBounded ||
|
|
44
|
+
(typeof value === "string" &&
|
|
45
|
+
["false", "not_bounded"].includes(value.toLowerCase()))
|
|
46
|
+
) {
|
|
47
|
+
return QueryScanConsistency.NotBounded;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
throw new Error(`Unsupported query scan consistency: ${value}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function buildQueryOptions(queryOptions = {}) {
|
|
54
|
+
if (typeof queryOptions !== "object" || Array.isArray(queryOptions)) {
|
|
55
|
+
queryOptions = { scan_consistency: queryOptions };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const options = {};
|
|
59
|
+
const consistentWith =
|
|
60
|
+
queryOptions.consistentWith ?? queryOptions.consistent_with;
|
|
61
|
+
|
|
62
|
+
if (consistentWith !== undefined && consistentWith !== null) {
|
|
63
|
+
options.consistentWith = consistentWith;
|
|
64
|
+
} else {
|
|
65
|
+
options.scanConsistency = normalizeScanConsistency(
|
|
66
|
+
queryOptions.scanConsistency ?? queryOptions.scan_consistency,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const scanWait = queryOptions.scanWait ?? queryOptions.scan_wait;
|
|
71
|
+
if (scanWait !== undefined && scanWait !== null && scanWait !== "") {
|
|
72
|
+
const numericScanWait = Number(scanWait);
|
|
73
|
+
if (!Number.isFinite(numericScanWait) || numericScanWait < 0) {
|
|
74
|
+
throw new Error(`Invalid query scan wait: ${scanWait}`);
|
|
75
|
+
}
|
|
76
|
+
options.scanWait = numericScanWait;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return options;
|
|
80
|
+
}
|
|
81
|
+
|
|
28
82
|
module.exports = class Database {
|
|
29
83
|
constructor(
|
|
30
84
|
connection,
|
|
@@ -557,6 +611,7 @@ module.exports = class Database {
|
|
|
557
611
|
do_count = "false",
|
|
558
612
|
query_scope,
|
|
559
613
|
join = "",
|
|
614
|
+
query_options = {},
|
|
560
615
|
) {
|
|
561
616
|
const is_legacy =
|
|
562
617
|
this.scope_name !== "_default" && this.collection_name !== "_default";
|
|
@@ -628,10 +683,14 @@ module.exports = class Database {
|
|
|
628
683
|
query = query + order_by_clause + limit_clause + offset_clause;
|
|
629
684
|
|
|
630
685
|
const active_query_space =
|
|
631
|
-
query_scope || !this.use_bucket_name ? "scope" : "cluster";
|
|
632
|
-
const queries = [
|
|
686
|
+
query_scope || (!this.use_bucket_name ? "scope" : "cluster");
|
|
687
|
+
const queries = [
|
|
688
|
+
this.getData(query, active_query_space, query_options),
|
|
689
|
+
];
|
|
633
690
|
if (do_count != "false") {
|
|
634
|
-
queries.push(
|
|
691
|
+
queries.push(
|
|
692
|
+
this.getCount(count_query, active_query_space, query_options),
|
|
693
|
+
);
|
|
635
694
|
}
|
|
636
695
|
const responses = await Promise.all(queries);
|
|
637
696
|
|
|
@@ -698,12 +757,20 @@ module.exports = class Database {
|
|
|
698
757
|
});
|
|
699
758
|
}
|
|
700
759
|
|
|
701
|
-
async getCount(
|
|
702
|
-
|
|
760
|
+
async getCount(
|
|
761
|
+
count_query_element,
|
|
762
|
+
query_scope = "cluster",
|
|
763
|
+
query_options = {},
|
|
764
|
+
) {
|
|
765
|
+
let response = await this.query(
|
|
766
|
+
count_query_element,
|
|
767
|
+
query_scope,
|
|
768
|
+
query_options,
|
|
769
|
+
);
|
|
703
770
|
return response.rows[0].count;
|
|
704
771
|
}
|
|
705
|
-
async getData(query_element, query_scope = "cluster") {
|
|
706
|
-
let response = await this.query(query_element, query_scope);
|
|
772
|
+
async getData(query_element, query_scope = "cluster", query_options = {}) {
|
|
773
|
+
let response = await this.query(query_element, query_scope, query_options);
|
|
707
774
|
return response.rows;
|
|
708
775
|
}
|
|
709
776
|
|
|
@@ -772,8 +839,17 @@ module.exports = class Database {
|
|
|
772
839
|
}
|
|
773
840
|
}
|
|
774
841
|
|
|
775
|
-
async query(query, query_scope =
|
|
776
|
-
|
|
842
|
+
async query(query, query_scope, query_options = {}) {
|
|
843
|
+
if (
|
|
844
|
+
query_scope &&
|
|
845
|
+
typeof query_scope === "object" &&
|
|
846
|
+
!Array.isArray(query_scope)
|
|
847
|
+
) {
|
|
848
|
+
query_options = query_scope;
|
|
849
|
+
query_scope = undefined;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
let scope = query_scope || this.query_scope || "cluster";
|
|
777
853
|
const sanitizedQuery = query; //sanitization step to be implemented
|
|
778
854
|
let exec_scope = null;
|
|
779
855
|
|
|
@@ -784,9 +860,7 @@ module.exports = class Database {
|
|
|
784
860
|
}
|
|
785
861
|
// Start time
|
|
786
862
|
let startTime = process.hrtime.bigint();
|
|
787
|
-
const options =
|
|
788
|
-
scanConsistency: QueryScanConsistency.RequestPlus,
|
|
789
|
-
};
|
|
863
|
+
const options = buildQueryOptions(query_options);
|
|
790
864
|
let response = await exec_scope.query(sanitizedQuery, options);
|
|
791
865
|
// End time
|
|
792
866
|
let endTime = process.hrtime.bigint();
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const assert = require("assert");
|
|
4
|
+
const { QueryScanConsistency } = require("couchbase");
|
|
5
|
+
const Database = require("./index");
|
|
6
|
+
|
|
7
|
+
let passed = 0;
|
|
8
|
+
let failed = 0;
|
|
9
|
+
|
|
10
|
+
function createQueryDb() {
|
|
11
|
+
const calls = [];
|
|
12
|
+
const db = Object.create(Database.prototype);
|
|
13
|
+
db.query_scope = "cluster";
|
|
14
|
+
db.cluster = {
|
|
15
|
+
query: async (statement, options) => {
|
|
16
|
+
calls.push({ target: "cluster", statement, options });
|
|
17
|
+
return { rows: [] };
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
db.scope = {
|
|
21
|
+
query: async (statement, options) => {
|
|
22
|
+
calls.push({ target: "scope", statement, options });
|
|
23
|
+
return { rows: [] };
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
db.optimize = () => {};
|
|
27
|
+
return { db, calls };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function test(name, fn) {
|
|
31
|
+
try {
|
|
32
|
+
await fn();
|
|
33
|
+
console.log(`✓ ${name}`);
|
|
34
|
+
passed += 1;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
console.log(`✗ ${name}`);
|
|
37
|
+
console.log(` Error: ${error.stack || error.message}`);
|
|
38
|
+
failed += 1;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function run() {
|
|
43
|
+
console.log("\n--- query consistency tests ---");
|
|
44
|
+
|
|
45
|
+
await test("ordinary SQL++ queries default to not_bounded", async () => {
|
|
46
|
+
const { db, calls } = createQueryDb();
|
|
47
|
+
|
|
48
|
+
await db.query("SELECT 1");
|
|
49
|
+
|
|
50
|
+
assert.strictEqual(calls[0].target, "cluster");
|
|
51
|
+
assert.strictEqual(
|
|
52
|
+
calls[0].options.scanConsistency,
|
|
53
|
+
QueryScanConsistency.NotBounded,
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
await test("request_plus can be selected explicitly", async () => {
|
|
58
|
+
const { db, calls } = createQueryDb();
|
|
59
|
+
|
|
60
|
+
await db.query("SELECT 1", "scope", {
|
|
61
|
+
scan_consistency: "request_plus",
|
|
62
|
+
scan_wait: 2500,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
assert.strictEqual(calls[0].target, "scope");
|
|
66
|
+
assert.strictEqual(
|
|
67
|
+
calls[0].options.scanConsistency,
|
|
68
|
+
QueryScanConsistency.RequestPlus,
|
|
69
|
+
);
|
|
70
|
+
assert.strictEqual(calls[0].options.scanWait, 2500);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
await test("legacy boolean consistency values remain supported", async () => {
|
|
74
|
+
const { db, calls } = createQueryDb();
|
|
75
|
+
|
|
76
|
+
await db.query("SELECT 1", "cluster", { scan_consistency: true });
|
|
77
|
+
|
|
78
|
+
assert.strictEqual(
|
|
79
|
+
calls[0].options.scanConsistency,
|
|
80
|
+
QueryScanConsistency.RequestPlus,
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
await test("mutation state is forwarded without a conflicting scan mode", async () => {
|
|
85
|
+
const { db, calls } = createQueryDb();
|
|
86
|
+
const mutationState = { token: "test-token" };
|
|
87
|
+
|
|
88
|
+
await db.query("SELECT 1", {
|
|
89
|
+
consistent_with: mutationState,
|
|
90
|
+
scan_wait: 1000,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
assert.strictEqual(calls[0].options.consistentWith, mutationState);
|
|
94
|
+
assert.strictEqual(calls[0].options.scanConsistency, undefined);
|
|
95
|
+
assert.strictEqual(calls[0].options.scanWait, 1000);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
await test("search keeps query scope separate from consistency", async () => {
|
|
99
|
+
const db = Object.create(Database.prototype);
|
|
100
|
+
const calls = [];
|
|
101
|
+
db.use_bucket_name = true;
|
|
102
|
+
db.bucket = "documents";
|
|
103
|
+
db.scope_name = "_default";
|
|
104
|
+
db.collection_name = "_default";
|
|
105
|
+
db.getData = async (statement, scope, options) => {
|
|
106
|
+
calls.push({ kind: "data", statement, scope, options });
|
|
107
|
+
return [];
|
|
108
|
+
};
|
|
109
|
+
db.getCount = async (statement, scope, options) => {
|
|
110
|
+
calls.push({ kind: "count", statement, scope, options });
|
|
111
|
+
return 0;
|
|
112
|
+
};
|
|
113
|
+
const options = { scan_consistency: "request_plus" };
|
|
114
|
+
|
|
115
|
+
await db.search(
|
|
116
|
+
"application",
|
|
117
|
+
"",
|
|
118
|
+
"",
|
|
119
|
+
10,
|
|
120
|
+
0,
|
|
121
|
+
"created_at DESC",
|
|
122
|
+
"true",
|
|
123
|
+
"cluster",
|
|
124
|
+
"",
|
|
125
|
+
options,
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
assert.deepStrictEqual(
|
|
129
|
+
calls.map((call) => call.scope),
|
|
130
|
+
["cluster", "cluster"],
|
|
131
|
+
);
|
|
132
|
+
assert.strictEqual(calls[0].options, options);
|
|
133
|
+
assert.strictEqual(calls[1].options, options);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
await test("invalid consistency values fail before issuing a query", async () => {
|
|
137
|
+
const { db, calls } = createQueryDb();
|
|
138
|
+
|
|
139
|
+
await assert.rejects(
|
|
140
|
+
() => db.query("SELECT 1", "cluster", { scan_consistency: "eventual" }),
|
|
141
|
+
/Unsupported query scan consistency/,
|
|
142
|
+
);
|
|
143
|
+
assert.strictEqual(calls.length, 0);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
console.log(`\nPassed: ${passed}`);
|
|
147
|
+
console.log(`Failed: ${failed}`);
|
|
148
|
+
if (failed > 0) process.exitCode = 1;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
run().catch((error) => {
|
|
152
|
+
console.error(error);
|
|
153
|
+
process.exit(1);
|
|
154
|
+
});
|