spice-couchbase 2.0.14 → 2.0.15
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 +327 -144
- package/package.json +5 -3
- package/src/index.js +342 -139
package/build/index.js
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
4
|
+
|
|
5
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
6
|
+
|
|
7
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
|
|
3
9
|
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
10
|
|
|
5
11
|
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
12
|
|
|
7
|
-
var _ = require(
|
|
13
|
+
var _ = require("lodash");
|
|
8
14
|
|
|
9
|
-
var couchbase = require(
|
|
15
|
+
var couchbase = require("couchbase");
|
|
16
|
+
|
|
17
|
+
var {
|
|
18
|
+
default: axios
|
|
19
|
+
} = require("axios");
|
|
20
|
+
|
|
21
|
+
var qs = require("qs");
|
|
10
22
|
|
|
11
23
|
if (!Promise.allSettled) {
|
|
12
24
|
Promise.allSettled = promises => Promise.all(promises.map((promise, i) => promise.then(value => ({
|
|
@@ -19,64 +31,214 @@ if (!Promise.allSettled) {
|
|
|
19
31
|
}
|
|
20
32
|
|
|
21
33
|
module.exports = class Database {
|
|
22
|
-
constructor(connection
|
|
34
|
+
constructor(connection) {
|
|
35
|
+
var {
|
|
36
|
+
full_text_index,
|
|
37
|
+
collection,
|
|
38
|
+
scope,
|
|
39
|
+
query_scope = "cluster",
|
|
40
|
+
auto_create_collection
|
|
41
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
42
|
+
|
|
23
43
|
if (connection) {
|
|
24
44
|
this.connection_name = connection;
|
|
25
45
|
this.connection = spice.database.connections[connection];
|
|
46
|
+
this.query_scope = query_scope;
|
|
26
47
|
this.cluster = spice.database.clusters[connection];
|
|
27
48
|
this.bucket = this.connection._scope._bucket._name;
|
|
49
|
+
this.db_config = spice.config.database.connections[connection];
|
|
50
|
+
this.collection_manager = spice.database.buckets[connection].collections();
|
|
51
|
+
this.auto_create_collection = auto_create_collection || (collection || spice.config.database.connections[connection].collection) != undefined && (collection || spice.config.database.connections[connection].collection) != "default" && (scope || spice.config.database.connections[connection].scope) != "_default";
|
|
52
|
+
this.collection_name = collection || spice.config.database.connections[connection].collection || "_default";
|
|
53
|
+
this.scope_name = scope || spice.config.database.connections[connection].scope || "_default";
|
|
54
|
+
this.scope = spice.database.scopes[connection];
|
|
28
55
|
this.full_text_index = spice.config.database.connections[connection].full_text_index || full_text_index;
|
|
29
56
|
this.N1qlQuery = couchbase.N1qlQuery;
|
|
30
57
|
this.SearchQuery = couchbase.SearchQuery;
|
|
58
|
+
this.connection = this.scope.collection(this.collection_name);
|
|
59
|
+
this.userManager = this.getUserManager(connection);
|
|
60
|
+
|
|
61
|
+
if (this.auto_create_collection) {
|
|
62
|
+
this.upsertCollection();
|
|
63
|
+
}
|
|
31
64
|
}
|
|
32
65
|
}
|
|
33
66
|
|
|
34
|
-
connect(
|
|
35
|
-
|
|
67
|
+
connect(connection_name, config) {
|
|
68
|
+
var _this = this;
|
|
69
|
+
|
|
70
|
+
return _asyncToGenerator(function* () {
|
|
71
|
+
var cluster = yield couchbase.connect(config.nodes, {
|
|
72
|
+
username: config.username,
|
|
73
|
+
password: config.password
|
|
74
|
+
});
|
|
75
|
+
var bucket = cluster.bucket(config.bucket);
|
|
76
|
+
var collection;
|
|
77
|
+
var scope_name = _this.scope_name || config.scope || "_default";
|
|
78
|
+
var collection_name = _this.collection_name || config.collection || "_default";
|
|
79
|
+
var scope = bucket.scope(scope_name);
|
|
80
|
+
collection = bucket.scope(scope_name).collection(collection_name || "_default");
|
|
81
|
+
return {
|
|
82
|
+
connection: connection_name,
|
|
83
|
+
bucket,
|
|
84
|
+
cluster,
|
|
85
|
+
scope,
|
|
86
|
+
collection
|
|
87
|
+
};
|
|
88
|
+
})();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
getUserManager(connection) {
|
|
92
|
+
return spice.database.clusters[connection].users();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
assignUserPermission() {
|
|
96
|
+
var _this2 = this;
|
|
97
|
+
|
|
98
|
+
return _asyncToGenerator(function* () {
|
|
36
99
|
try {
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
-
password: value.password
|
|
100
|
+
var allUsers = yield _this2.userManager.getAllUsers({
|
|
101
|
+
timeout: 6000
|
|
40
102
|
});
|
|
41
|
-
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
103
|
+
console.log("UserName::::", _this2.db_config.username, allUsers);
|
|
104
|
+
var update_user_response = yield _this2.userManager.upsertUser(_this2.db_config.username, {
|
|
105
|
+
roles: [{
|
|
106
|
+
name: "scope_admin",
|
|
107
|
+
bucket: _this2.db_config.bucket
|
|
108
|
+
}, {
|
|
109
|
+
name: "data_reader",
|
|
110
|
+
bucket: _this2.db_config.bucket
|
|
111
|
+
}]
|
|
48
112
|
});
|
|
113
|
+
console.log("UPdate Response", update_user_response);
|
|
49
114
|
} catch (e) {
|
|
50
|
-
|
|
51
|
-
reject(e);
|
|
115
|
+
console.log(e);
|
|
52
116
|
}
|
|
53
|
-
});
|
|
117
|
+
})();
|
|
54
118
|
}
|
|
55
119
|
|
|
56
|
-
|
|
57
|
-
var
|
|
58
|
-
return
|
|
59
|
-
|
|
60
|
-
function () {
|
|
61
|
-
var _ref = _asyncToGenerator(function* (resolve, reject) {
|
|
62
|
-
that.connection.get(doc_id, function (err, res) {
|
|
63
|
-
var return_value = {};
|
|
120
|
+
getCouchbaseHost() {
|
|
121
|
+
var default_port = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 8091;
|
|
122
|
+
return (this.db_config.api_base || "http://".concat(_.last(_.first(this.db_config.nodes.split(",")).split("://")))) + ":" + default_port;
|
|
123
|
+
} //create wait function
|
|
64
124
|
|
|
65
|
-
if (err) {
|
|
66
|
-
reject(err);
|
|
67
|
-
} else {
|
|
68
|
-
_.defaults(return_value, res.value);
|
|
69
125
|
|
|
70
|
-
|
|
71
|
-
|
|
126
|
+
wait(ms) {
|
|
127
|
+
return _asyncToGenerator(function* () {
|
|
128
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
129
|
+
})();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
upsertCollection() {
|
|
133
|
+
var _this3 = this;
|
|
134
|
+
|
|
135
|
+
return _asyncToGenerator(function* () {
|
|
136
|
+
try {
|
|
137
|
+
var collection_exists = yield _this3.collectionExists(_this3.collection_name, _this3.scope_name);
|
|
138
|
+
|
|
139
|
+
if (!collection_exists) {
|
|
140
|
+
yield _this3.createCollection(_this3.collection_name, _this3.scope_name);
|
|
141
|
+
yield _this3.wait(2000);
|
|
142
|
+
yield _this3.createPrimaryIndex(_this3.scope_name, _this3.collection_name);
|
|
143
|
+
}
|
|
144
|
+
} catch (e) {
|
|
145
|
+
console.log(e);
|
|
146
|
+
}
|
|
147
|
+
})();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
collectionExists(collection_name, scope_name) {
|
|
151
|
+
var _this4 = this;
|
|
152
|
+
|
|
153
|
+
return _asyncToGenerator(function* () {
|
|
154
|
+
try {
|
|
155
|
+
var _$find;
|
|
156
|
+
|
|
157
|
+
var all_scopes = yield axios.get("".concat(_this4.getCouchbaseHost(), "/pools/default/buckets/").concat(_this4.db_config.bucket, "/scopes"), {
|
|
158
|
+
auth: {
|
|
159
|
+
username: _this4.db_config.username,
|
|
160
|
+
password: _this4.db_config.password
|
|
72
161
|
}
|
|
73
162
|
});
|
|
74
|
-
|
|
163
|
+
var collection_e = _.find(((_$find = _.find(all_scopes.data.scopes, {
|
|
164
|
+
name: scope_name
|
|
165
|
+
})) === null || _$find === void 0 ? void 0 : _$find.collections) || [], {
|
|
166
|
+
name: collection_name
|
|
167
|
+
}) != undefined;
|
|
168
|
+
return collection_e;
|
|
169
|
+
} catch (e) {
|
|
170
|
+
console.log("Exists error happened", e);
|
|
171
|
+
}
|
|
172
|
+
})();
|
|
173
|
+
}
|
|
75
174
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
175
|
+
createCollection(connection_name, scope_name) {
|
|
176
|
+
var _this5 = this;
|
|
177
|
+
|
|
178
|
+
return _asyncToGenerator(function* () {
|
|
179
|
+
try {
|
|
180
|
+
var token = "".concat(_this5.db_config.username, ":").concat(_this5.db_config.password);
|
|
181
|
+
var encodedToken = Buffer.from(token).toString("base64");
|
|
182
|
+
var headers = {
|
|
183
|
+
Authorization: "Basic " + encodedToken,
|
|
184
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
185
|
+
};
|
|
186
|
+
var response = yield axios({
|
|
187
|
+
method: "post",
|
|
188
|
+
url: "".concat(_this5.getCouchbaseHost(), "/pools/default/buckets/").concat(_this5.db_config.bucket, "/scopes/").concat(scope_name, "/collections"),
|
|
189
|
+
headers,
|
|
190
|
+
data: qs.stringify({
|
|
191
|
+
name: connection_name
|
|
192
|
+
})
|
|
193
|
+
}).data;
|
|
194
|
+
return response;
|
|
195
|
+
} catch (e) {}
|
|
196
|
+
})();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
createPrimaryIndex(scope_name, col_name) {
|
|
200
|
+
var _this6 = this;
|
|
201
|
+
|
|
202
|
+
return _asyncToGenerator(function* () {
|
|
203
|
+
try {
|
|
204
|
+
var response = yield _this6.query("CREATE PRIMARY INDEX ON default:`".concat(_this6.db_config.bucket, "`.`").concat(scope_name, "`.`").concat(col_name, "`"));
|
|
205
|
+
return response;
|
|
206
|
+
} catch (e) {
|
|
207
|
+
console.log("CREATE Index :::", e);
|
|
208
|
+
}
|
|
209
|
+
})();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
createScope(scope_name) {
|
|
213
|
+
var _this7 = this;
|
|
214
|
+
|
|
215
|
+
return _asyncToGenerator(function* () {
|
|
216
|
+
try {
|
|
217
|
+
yield _this7.collection_manager.createScope(scope_name);
|
|
218
|
+
} catch (e) {
|
|
219
|
+
if (e instanceof couchbase.ScopeExistsError) {
|
|
220
|
+
console.log("The scope already exists");
|
|
221
|
+
} else {
|
|
222
|
+
throw e;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
})();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
get(doc_id) {
|
|
229
|
+
var _this8 = this;
|
|
230
|
+
|
|
231
|
+
return _asyncToGenerator(function* () {
|
|
232
|
+
try {
|
|
233
|
+
var response = yield _this8.connection.get(doc_id);
|
|
234
|
+
console.log("Response ", response);
|
|
235
|
+
return _objectSpread({
|
|
236
|
+
id: doc_id
|
|
237
|
+
}, response.content);
|
|
238
|
+
} catch (e) {
|
|
239
|
+
console.error(e);
|
|
240
|
+
}
|
|
241
|
+
})();
|
|
80
242
|
}
|
|
81
243
|
|
|
82
244
|
insert(doc_id, document, expiry) {
|
|
@@ -84,13 +246,13 @@ module.exports = class Database {
|
|
|
84
246
|
return new Promise(
|
|
85
247
|
/*#__PURE__*/
|
|
86
248
|
function () {
|
|
87
|
-
var
|
|
249
|
+
var _ref = _asyncToGenerator(function* (resolve, reject) {
|
|
88
250
|
that.connection.insert(doc_id, document, {
|
|
89
251
|
expiry: expiry
|
|
90
252
|
},
|
|
91
253
|
/*#__PURE__*/
|
|
92
254
|
function () {
|
|
93
|
-
var
|
|
255
|
+
var _ref2 = _asyncToGenerator(function* (err, res) {
|
|
94
256
|
if (err) {
|
|
95
257
|
reject(err);
|
|
96
258
|
} else {
|
|
@@ -103,14 +265,14 @@ module.exports = class Database {
|
|
|
103
265
|
}
|
|
104
266
|
});
|
|
105
267
|
|
|
106
|
-
return function (
|
|
107
|
-
return
|
|
268
|
+
return function (_x3, _x4) {
|
|
269
|
+
return _ref2.apply(this, arguments);
|
|
108
270
|
};
|
|
109
271
|
}());
|
|
110
272
|
});
|
|
111
273
|
|
|
112
|
-
return function (
|
|
113
|
-
return
|
|
274
|
+
return function (_x, _x2) {
|
|
275
|
+
return _ref.apply(this, arguments);
|
|
114
276
|
};
|
|
115
277
|
}());
|
|
116
278
|
}
|
|
@@ -121,11 +283,11 @@ module.exports = class Database {
|
|
|
121
283
|
return new Promise(
|
|
122
284
|
/*#__PURE__*/
|
|
123
285
|
function () {
|
|
124
|
-
var
|
|
286
|
+
var _ref3 = _asyncToGenerator(function* (resolve, reject) {
|
|
125
287
|
that.connection.touch(doc_id, expiry,
|
|
126
288
|
/*#__PURE__*/
|
|
127
289
|
function () {
|
|
128
|
-
var
|
|
290
|
+
var _ref4 = _asyncToGenerator(function* (err, res) {
|
|
129
291
|
if (err) {
|
|
130
292
|
reject(err);
|
|
131
293
|
} else {
|
|
@@ -138,14 +300,14 @@ module.exports = class Database {
|
|
|
138
300
|
}
|
|
139
301
|
});
|
|
140
302
|
|
|
141
|
-
return function (
|
|
142
|
-
return
|
|
303
|
+
return function (_x7, _x8) {
|
|
304
|
+
return _ref4.apply(this, arguments);
|
|
143
305
|
};
|
|
144
306
|
}());
|
|
145
307
|
});
|
|
146
308
|
|
|
147
|
-
return function (
|
|
148
|
-
return
|
|
309
|
+
return function (_x5, _x6) {
|
|
310
|
+
return _ref3.apply(this, arguments);
|
|
149
311
|
};
|
|
150
312
|
}());
|
|
151
313
|
}
|
|
@@ -155,7 +317,7 @@ module.exports = class Database {
|
|
|
155
317
|
return new Promise(
|
|
156
318
|
/*#__PURE__*/
|
|
157
319
|
function () {
|
|
158
|
-
var
|
|
320
|
+
var _ref5 = _asyncToGenerator(function* (resolve, reject) {
|
|
159
321
|
that.connection.counter(doc_id, adjustment, {
|
|
160
322
|
initial: initial_value
|
|
161
323
|
}, function (err, res) {
|
|
@@ -167,8 +329,8 @@ module.exports = class Database {
|
|
|
167
329
|
});
|
|
168
330
|
});
|
|
169
331
|
|
|
170
|
-
return function (
|
|
171
|
-
return
|
|
332
|
+
return function (_x9, _x10) {
|
|
333
|
+
return _ref5.apply(this, arguments);
|
|
172
334
|
};
|
|
173
335
|
}());
|
|
174
336
|
}
|
|
@@ -178,7 +340,7 @@ module.exports = class Database {
|
|
|
178
340
|
return new Promise(
|
|
179
341
|
/*#__PURE__*/
|
|
180
342
|
function () {
|
|
181
|
-
var
|
|
343
|
+
var _ref6 = _asyncToGenerator(function* (resolve, reject) {
|
|
182
344
|
that.connection.replace(doc_id, document, function (err, res) {
|
|
183
345
|
if (err) {
|
|
184
346
|
reject(err);
|
|
@@ -188,8 +350,8 @@ module.exports = class Database {
|
|
|
188
350
|
});
|
|
189
351
|
});
|
|
190
352
|
|
|
191
|
-
return function (
|
|
192
|
-
return
|
|
353
|
+
return function (_x11, _x12) {
|
|
354
|
+
return _ref6.apply(this, arguments);
|
|
193
355
|
};
|
|
194
356
|
}());
|
|
195
357
|
}
|
|
@@ -199,7 +361,7 @@ module.exports = class Database {
|
|
|
199
361
|
return new Promise(
|
|
200
362
|
/*#__PURE__*/
|
|
201
363
|
function () {
|
|
202
|
-
var
|
|
364
|
+
var _ref7 = _asyncToGenerator(function* (resolve, reject) {
|
|
203
365
|
that.connection.remove(doc_id, function (err, res) {
|
|
204
366
|
if (err) {
|
|
205
367
|
reject(err);
|
|
@@ -209,14 +371,14 @@ module.exports = class Database {
|
|
|
209
371
|
});
|
|
210
372
|
});
|
|
211
373
|
|
|
212
|
-
return function (
|
|
213
|
-
return
|
|
374
|
+
return function (_x13, _x14) {
|
|
375
|
+
return _ref7.apply(this, arguments);
|
|
214
376
|
};
|
|
215
377
|
}());
|
|
216
378
|
}
|
|
217
379
|
|
|
218
380
|
multi_get(keys, keep_type) {
|
|
219
|
-
var
|
|
381
|
+
var _this9 = this;
|
|
220
382
|
|
|
221
383
|
return _asyncToGenerator(function* () {
|
|
222
384
|
/* var that = this; */
|
|
@@ -228,7 +390,7 @@ module.exports = class Database {
|
|
|
228
390
|
|
|
229
391
|
|
|
230
392
|
if (_.isArray(keys) && keys.length > 0) {
|
|
231
|
-
var functions = _.map(keys, key =>
|
|
393
|
+
var functions = _.map(keys, key => _this9.connection.get(key));
|
|
232
394
|
|
|
233
395
|
var returned_all = yield Promise.allSettled(functions);
|
|
234
396
|
|
|
@@ -272,7 +434,7 @@ module.exports = class Database {
|
|
|
272
434
|
return new Promise(
|
|
273
435
|
/*#__PURE__*/
|
|
274
436
|
function () {
|
|
275
|
-
var
|
|
437
|
+
var _ref8 = _asyncToGenerator(function* (resolve, reject) {
|
|
276
438
|
var ViewQuery = couchbase.ViewQuery;
|
|
277
439
|
var query = ViewQuery.from(design_doc, view);
|
|
278
440
|
|
|
@@ -299,7 +461,7 @@ module.exports = class Database {
|
|
|
299
461
|
that.connection.query(query,
|
|
300
462
|
/*#__PURE__*/
|
|
301
463
|
function () {
|
|
302
|
-
var
|
|
464
|
+
var _ref9 = _asyncToGenerator(function* (err, results, matrix) {
|
|
303
465
|
var return_value = [];
|
|
304
466
|
|
|
305
467
|
if (err) {
|
|
@@ -322,100 +484,112 @@ module.exports = class Database {
|
|
|
322
484
|
}
|
|
323
485
|
});
|
|
324
486
|
|
|
325
|
-
return function (
|
|
326
|
-
return
|
|
487
|
+
return function (_x17, _x18, _x19) {
|
|
488
|
+
return _ref9.apply(this, arguments);
|
|
327
489
|
};
|
|
328
490
|
}());
|
|
329
491
|
});
|
|
330
492
|
|
|
331
|
-
return function (
|
|
332
|
-
return
|
|
493
|
+
return function (_x15, _x16) {
|
|
494
|
+
return _ref8.apply(this, arguments);
|
|
333
495
|
};
|
|
334
496
|
}());
|
|
335
497
|
}
|
|
336
498
|
|
|
337
|
-
search(type, columns, query, limit, offset, order_by, do_count,
|
|
338
|
-
var
|
|
339
|
-
return new Promise(
|
|
340
|
-
/*#__PURE__*/
|
|
341
|
-
function () {
|
|
342
|
-
var _ref11 = _asyncToGenerator(function* (resolve, reject) {
|
|
343
|
-
try {
|
|
344
|
-
if (do_count == undefined) {
|
|
345
|
-
do_count = "false";
|
|
346
|
-
}
|
|
499
|
+
search(type, columns, query, limit, offset, order_by, do_count, query_scope) {
|
|
500
|
+
var _this10 = this;
|
|
347
501
|
|
|
348
|
-
|
|
349
|
-
|
|
502
|
+
return _asyncToGenerator(function* () {
|
|
503
|
+
var alias = "_reference";
|
|
504
|
+
var bucket = "`".concat(_this10.collection_name, "` AS ").concat(alias);
|
|
505
|
+
var include_type = _this10.scope_name && _this10.collection_name;
|
|
350
506
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
507
|
+
try {
|
|
508
|
+
if (do_count == undefined) {
|
|
509
|
+
do_count = "false";
|
|
510
|
+
}
|
|
354
511
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
}
|
|
512
|
+
var fix = "";
|
|
513
|
+
var count_query = "";
|
|
358
514
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}
|
|
515
|
+
if (!limit) {
|
|
516
|
+
limit = 0;
|
|
517
|
+
}
|
|
363
518
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
}
|
|
519
|
+
if (!offset) {
|
|
520
|
+
offset = 0;
|
|
521
|
+
}
|
|
368
522
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
523
|
+
if (columns == "*") {
|
|
524
|
+
columns = "(" + alias + ").*";
|
|
525
|
+
fix = ", META(" + alias + ").id AS id";
|
|
526
|
+
}
|
|
373
527
|
|
|
374
|
-
|
|
375
|
-
|
|
528
|
+
if (!columns) {
|
|
529
|
+
columns = "(" + alias + ").*";
|
|
530
|
+
fix = ", META(" + alias + ").id AS id";
|
|
531
|
+
}
|
|
376
532
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
query = front_query;
|
|
382
|
-
}
|
|
533
|
+
if (columns == "id") {
|
|
534
|
+
fix = ", META(" + alias + ").id AS id";
|
|
535
|
+
columns = "";
|
|
536
|
+
}
|
|
383
537
|
|
|
384
|
-
|
|
385
|
-
query = "".concat(query, " ORDER BY ").concat(order_by);
|
|
386
|
-
}
|
|
538
|
+
var front_query = "SELECT ".concat(columns, " ").concat(fix, " FROM ").concat(bucket);
|
|
387
539
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
540
|
+
if (include_type || query) {
|
|
541
|
+
front_query += " WHERE ";
|
|
542
|
+
}
|
|
391
543
|
|
|
392
|
-
|
|
393
|
-
query = query + " OFFSET " + offset;
|
|
394
|
-
}
|
|
544
|
+
var conditions = [];
|
|
395
545
|
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
546
|
+
if (include_type) {
|
|
547
|
+
conditions.push("type = '".concat(type, "'"));
|
|
548
|
+
}
|
|
399
549
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
550
|
+
if (query) {
|
|
551
|
+
conditions.push(query);
|
|
552
|
+
}
|
|
403
553
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
} catch (error) {
|
|
410
|
-
console.log(error.stack);
|
|
411
|
-
reject(error);
|
|
554
|
+
query = front_query + conditions.join(" AND ");
|
|
555
|
+
count_query = "SELECT count(meta().id) as count FROM ".concat(bucket);
|
|
556
|
+
|
|
557
|
+
if (include_type || query) {
|
|
558
|
+
count_query += " WHERE ";
|
|
412
559
|
}
|
|
413
|
-
});
|
|
414
560
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
561
|
+
query = front_query + conditions.join(" AND ");
|
|
562
|
+
|
|
563
|
+
if (order_by) {
|
|
564
|
+
query = "".concat(query, " ORDER BY ").concat(order_by);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (limit > 0) {
|
|
568
|
+
query = query + " LIMIT " + limit;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
if (offset > 0) {
|
|
572
|
+
query = query + " OFFSET " + offset;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
var uses_scopes = _this10.scope_name !== "_default";
|
|
576
|
+
var active_query_space = query_scope || uses_scopes ? "scope" : "cluster";
|
|
577
|
+
console.log(active_query_space, _this10.scope_name, uses_scopes);
|
|
578
|
+
var queries = [_this10.getData(query, active_query_space)];
|
|
579
|
+
|
|
580
|
+
if (do_count === "true") {
|
|
581
|
+
queries.push(_this10.getCount(count_query, active_query_space));
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
var responses = yield Promise.all(queries);
|
|
585
|
+
return {
|
|
586
|
+
data: responses[0],
|
|
587
|
+
total: responses.length > 1 ? responses[1] : null
|
|
588
|
+
};
|
|
589
|
+
} catch (error) {
|
|
590
|
+
throw error;
|
|
591
|
+
}
|
|
592
|
+
})();
|
|
419
593
|
}
|
|
420
594
|
|
|
421
595
|
full_text_search(type, search_query, limit) {
|
|
@@ -425,7 +599,7 @@ module.exports = class Database {
|
|
|
425
599
|
return new Promise(
|
|
426
600
|
/*#__PURE__*/
|
|
427
601
|
function () {
|
|
428
|
-
var
|
|
602
|
+
var _ref10 = _asyncToGenerator(function* (resolve, reject) {
|
|
429
603
|
try {
|
|
430
604
|
var full_query = search_query + " AND type:" + type;
|
|
431
605
|
var query = that.SearchQuery.new(that.full_text_index || full_text_index, that.SearchQuery.queryString(full_query));
|
|
@@ -446,7 +620,7 @@ module.exports = class Database {
|
|
|
446
620
|
that.cluster.query(query,
|
|
447
621
|
/*#__PURE__*/
|
|
448
622
|
function () {
|
|
449
|
-
var
|
|
623
|
+
var _ref11 = _asyncToGenerator(function* (err, results, matrix) {
|
|
450
624
|
var return_value = [];
|
|
451
625
|
|
|
452
626
|
if (err) {
|
|
@@ -472,8 +646,8 @@ module.exports = class Database {
|
|
|
472
646
|
}
|
|
473
647
|
});
|
|
474
648
|
|
|
475
|
-
return function (
|
|
476
|
-
return
|
|
649
|
+
return function (_x22, _x23, _x24) {
|
|
650
|
+
return _ref11.apply(this, arguments);
|
|
477
651
|
};
|
|
478
652
|
}());
|
|
479
653
|
} catch (e) {
|
|
@@ -481,35 +655,44 @@ module.exports = class Database {
|
|
|
481
655
|
}
|
|
482
656
|
});
|
|
483
657
|
|
|
484
|
-
return function (
|
|
485
|
-
return
|
|
658
|
+
return function (_x20, _x21) {
|
|
659
|
+
return _ref10.apply(this, arguments);
|
|
486
660
|
};
|
|
487
661
|
}());
|
|
488
662
|
}
|
|
489
663
|
|
|
490
664
|
getCount(count_query_element) {
|
|
491
|
-
var
|
|
665
|
+
var _this11 = this;
|
|
492
666
|
|
|
667
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
493
668
|
return _asyncToGenerator(function* () {
|
|
494
|
-
var response = yield
|
|
669
|
+
var response = yield _this11.query(count_query_element, query_scope);
|
|
495
670
|
return response.rows[0].count;
|
|
496
671
|
})();
|
|
497
672
|
}
|
|
498
673
|
|
|
499
674
|
getData(query_element) {
|
|
500
|
-
var
|
|
675
|
+
var _this12 = this;
|
|
501
676
|
|
|
677
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
502
678
|
return _asyncToGenerator(function* () {
|
|
503
|
-
var response = yield
|
|
679
|
+
var response = yield _this12.query(query_element, query_scope);
|
|
504
680
|
return response.rows;
|
|
505
681
|
})();
|
|
506
682
|
}
|
|
507
683
|
|
|
508
684
|
query(query) {
|
|
509
|
-
var
|
|
685
|
+
var _this13 = this;
|
|
510
686
|
|
|
687
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
511
688
|
return _asyncToGenerator(function* () {
|
|
512
|
-
|
|
689
|
+
var scope = query_scope || _this13.query_scope;
|
|
690
|
+
|
|
691
|
+
if (scope == "cluster") {
|
|
692
|
+
return yield _this13.cluster.query(query);
|
|
693
|
+
} else {
|
|
694
|
+
return yield _this13.scope.query(query);
|
|
695
|
+
}
|
|
513
696
|
})();
|
|
514
697
|
}
|
|
515
698
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "spice-couchbase",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.15",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -9,8 +9,10 @@
|
|
|
9
9
|
"author": "Chad Fraser <chad@sonover.com> (http://sonover.com)",
|
|
10
10
|
"license": "ISC",
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
12
|
+
"axios": "^0.26.1",
|
|
13
|
+
"couchbase": "^4.0.0",
|
|
14
|
+
"lodash": "^4.17.15",
|
|
15
|
+
"qs": "^6.10.3"
|
|
14
16
|
},
|
|
15
17
|
"devDependencies": {
|
|
16
18
|
"@babel/cli": "^7.7.7",
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
let _ = require(
|
|
4
|
-
let couchbase = require(
|
|
3
|
+
let _ = require("lodash");
|
|
4
|
+
let couchbase = require("couchbase");
|
|
5
|
+
const { default: axios } = require("axios");
|
|
6
|
+
var qs = require("qs");
|
|
5
7
|
|
|
6
8
|
if (!Promise.allSettled) {
|
|
7
9
|
Promise.allSettled = (promises) =>
|
|
@@ -20,72 +22,238 @@ if (!Promise.allSettled) {
|
|
|
20
22
|
);
|
|
21
23
|
}
|
|
22
24
|
module.exports = class Database {
|
|
23
|
-
constructor(
|
|
25
|
+
constructor(
|
|
26
|
+
connection,
|
|
27
|
+
{
|
|
28
|
+
full_text_index,
|
|
29
|
+
collection,
|
|
30
|
+
scope,
|
|
31
|
+
query_scope = "cluster",
|
|
32
|
+
auto_create_collection,
|
|
33
|
+
} = {}
|
|
34
|
+
) {
|
|
24
35
|
if (connection) {
|
|
25
36
|
this.connection_name = connection;
|
|
26
37
|
this.connection = spice.database.connections[connection];
|
|
27
|
-
this.
|
|
38
|
+
this.query_scope = query_scope;
|
|
39
|
+
this.cluster = spice.database.clusters[connection];
|
|
28
40
|
this.bucket = this.connection._scope._bucket._name;
|
|
29
|
-
this.
|
|
30
|
-
this.
|
|
41
|
+
this.db_config = spice.config.database.connections[connection];
|
|
42
|
+
this.collection_manager =
|
|
43
|
+
spice.database.buckets[connection].collections();
|
|
44
|
+
this.auto_create_collection =
|
|
45
|
+
auto_create_collection ||
|
|
46
|
+
((collection ||
|
|
47
|
+
spice.config.database.connections[connection].collection) !=
|
|
48
|
+
undefined &&
|
|
49
|
+
(collection ||
|
|
50
|
+
spice.config.database.connections[connection].collection) !=
|
|
51
|
+
"default" &&
|
|
52
|
+
(scope || spice.config.database.connections[connection].scope) !=
|
|
53
|
+
"_default");
|
|
54
|
+
this.collection_name =
|
|
55
|
+
collection ||
|
|
56
|
+
spice.config.database.connections[connection].collection ||
|
|
57
|
+
"_default";
|
|
58
|
+
this.scope_name =
|
|
59
|
+
scope ||
|
|
60
|
+
spice.config.database.connections[connection].scope ||
|
|
61
|
+
"_default";
|
|
62
|
+
this.scope = spice.database.scopes[connection];
|
|
63
|
+
this.full_text_index =
|
|
64
|
+
spice.config.database.connections[connection].full_text_index ||
|
|
65
|
+
full_text_index;
|
|
66
|
+
this.N1qlQuery = couchbase.N1qlQuery;
|
|
31
67
|
this.SearchQuery = couchbase.SearchQuery;
|
|
68
|
+
this.connection = this.scope.collection(this.collection_name);
|
|
69
|
+
this.userManager = this.getUserManager(connection);
|
|
70
|
+
if (this.auto_create_collection) {
|
|
71
|
+
this.upsertCollection();
|
|
72
|
+
}
|
|
32
73
|
}
|
|
33
74
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
75
|
+
|
|
76
|
+
async connect(connection_name, config) {
|
|
77
|
+
let cluster = await couchbase.connect(config.nodes, {
|
|
78
|
+
username: config.username,
|
|
79
|
+
password: config.password,
|
|
80
|
+
});
|
|
81
|
+
let bucket = cluster.bucket(config.bucket);
|
|
82
|
+
let collection;
|
|
83
|
+
let scope_name = this.scope_name || config.scope || "_default";
|
|
84
|
+
let collection_name =
|
|
85
|
+
this.collection_name || config.collection || "_default";
|
|
86
|
+
let scope = bucket.scope(scope_name);
|
|
87
|
+
|
|
88
|
+
collection = bucket
|
|
89
|
+
.scope(scope_name)
|
|
90
|
+
.collection(collection_name || "_default");
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
connection: connection_name,
|
|
94
|
+
bucket,
|
|
95
|
+
cluster,
|
|
96
|
+
scope,
|
|
97
|
+
collection,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
getUserManager(connection) {
|
|
102
|
+
return spice.database.clusters[connection].users();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async assignUserPermission() {
|
|
106
|
+
try {
|
|
107
|
+
let allUsers = await this.userManager.getAllUsers({ timeout: 6000 });
|
|
108
|
+
console.log("UserName::::", this.db_config.username, allUsers);
|
|
109
|
+
let update_user_response = await this.userManager.upsertUser(
|
|
110
|
+
this.db_config.username,
|
|
111
|
+
{
|
|
112
|
+
roles: [
|
|
113
|
+
{ name: "scope_admin", bucket: this.db_config.bucket },
|
|
114
|
+
{ name: "data_reader", bucket: this.db_config.bucket },
|
|
115
|
+
],
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
console.log("UPdate Response", update_user_response);
|
|
120
|
+
} catch (e) {
|
|
121
|
+
console.log(e);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
getCouchbaseHost(default_port = 8091) {
|
|
126
|
+
return (
|
|
127
|
+
(this.db_config.api_base ||
|
|
128
|
+
`http://${_.last(
|
|
129
|
+
_.first(this.db_config.nodes.split(",")).split("://")
|
|
130
|
+
)}`) +
|
|
131
|
+
":" +
|
|
132
|
+
default_port
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
//create wait function
|
|
137
|
+
async wait(ms) {
|
|
138
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async upsertCollection() {
|
|
142
|
+
try {
|
|
143
|
+
let collection_exists = await this.collectionExists(
|
|
144
|
+
this.collection_name,
|
|
145
|
+
this.scope_name
|
|
146
|
+
);
|
|
147
|
+
if (!collection_exists) {
|
|
148
|
+
await this.createCollection(this.collection_name, this.scope_name);
|
|
149
|
+
await this.wait(2000);
|
|
150
|
+
await this.createPrimaryIndex(this.scope_name, this.collection_name);
|
|
52
151
|
}
|
|
53
|
-
})
|
|
152
|
+
} catch (e) {
|
|
153
|
+
console.log(e);
|
|
154
|
+
}
|
|
54
155
|
}
|
|
55
156
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
157
|
+
async collectionExists(collection_name, scope_name) {
|
|
158
|
+
try {
|
|
159
|
+
let all_scopes = await axios.get(
|
|
160
|
+
`${this.getCouchbaseHost()}/pools/default/buckets/${
|
|
161
|
+
this.db_config.bucket
|
|
162
|
+
}/scopes`,
|
|
163
|
+
{
|
|
164
|
+
auth: {
|
|
165
|
+
username: this.db_config.username,
|
|
166
|
+
password: this.db_config.password,
|
|
167
|
+
},
|
|
67
168
|
}
|
|
68
|
-
|
|
69
|
-
|
|
169
|
+
);
|
|
170
|
+
let collection_e =
|
|
171
|
+
_.find(
|
|
172
|
+
_.find(all_scopes.data.scopes, { name: scope_name })?.collections ||
|
|
173
|
+
[],
|
|
174
|
+
{ name: collection_name }
|
|
175
|
+
) != undefined;
|
|
176
|
+
return collection_e;
|
|
177
|
+
} catch (e) {
|
|
178
|
+
console.log("Exists error happened", e);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async createCollection(connection_name, scope_name) {
|
|
183
|
+
try {
|
|
184
|
+
const token = `${this.db_config.username}:${this.db_config.password}`;
|
|
185
|
+
const encodedToken = Buffer.from(token).toString("base64");
|
|
186
|
+
const headers = {
|
|
187
|
+
Authorization: "Basic " + encodedToken,
|
|
188
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
189
|
+
};
|
|
190
|
+
let response = await axios({
|
|
191
|
+
method: "post",
|
|
192
|
+
url: `${this.getCouchbaseHost()}/pools/default/buckets/${
|
|
193
|
+
this.db_config.bucket
|
|
194
|
+
}/scopes/${scope_name}/collections`,
|
|
195
|
+
headers,
|
|
196
|
+
data: qs.stringify({ name: connection_name }),
|
|
197
|
+
}).data;
|
|
198
|
+
return response;
|
|
199
|
+
} catch (e) {}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async createPrimaryIndex(scope_name, col_name) {
|
|
203
|
+
try {
|
|
204
|
+
let response = await this.query(
|
|
205
|
+
`CREATE PRIMARY INDEX ON default:\`${this.db_config.bucket}\`.\`${scope_name}\`.\`${col_name}\``
|
|
206
|
+
);
|
|
207
|
+
return response;
|
|
208
|
+
} catch (e) {
|
|
209
|
+
console.log("CREATE Index :::", e);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async createScope(scope_name) {
|
|
214
|
+
try {
|
|
215
|
+
await this.collection_manager.createScope(scope_name);
|
|
216
|
+
} catch (e) {
|
|
217
|
+
if (e instanceof couchbase.ScopeExistsError) {
|
|
218
|
+
console.log("The scope already exists");
|
|
219
|
+
} else {
|
|
220
|
+
throw e;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async get(doc_id) {
|
|
226
|
+
try {
|
|
227
|
+
let response = await this.connection.get(doc_id);
|
|
228
|
+
console.log("Response ", response);
|
|
229
|
+
return { id: doc_id, ...response.content };
|
|
230
|
+
} catch (e) {
|
|
231
|
+
console.error(e);
|
|
232
|
+
}
|
|
70
233
|
}
|
|
71
234
|
|
|
72
235
|
insert(doc_id, document, expiry) {
|
|
73
236
|
var that = this;
|
|
74
237
|
return new Promise(async function (resolve, reject) {
|
|
75
|
-
that.connection.insert(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
238
|
+
that.connection.insert(
|
|
239
|
+
doc_id,
|
|
240
|
+
document,
|
|
241
|
+
{
|
|
242
|
+
expiry: expiry,
|
|
243
|
+
},
|
|
244
|
+
async function (err, res) {
|
|
245
|
+
if (err) {
|
|
246
|
+
reject(err);
|
|
247
|
+
} else {
|
|
248
|
+
try {
|
|
249
|
+
let created = await that.get(doc_id);
|
|
250
|
+
resolve(created);
|
|
251
|
+
} catch (e) {
|
|
252
|
+
reject(e);
|
|
253
|
+
}
|
|
86
254
|
}
|
|
87
255
|
}
|
|
88
|
-
|
|
256
|
+
);
|
|
89
257
|
});
|
|
90
258
|
}
|
|
91
259
|
|
|
@@ -97,33 +265,36 @@ module.exports = class Database {
|
|
|
97
265
|
reject(err);
|
|
98
266
|
} else {
|
|
99
267
|
try {
|
|
100
|
-
let created = await that.get(doc_id)
|
|
101
|
-
resolve(created)
|
|
268
|
+
let created = await that.get(doc_id);
|
|
269
|
+
resolve(created);
|
|
102
270
|
} catch (e) {
|
|
103
|
-
reject(e)
|
|
271
|
+
reject(e);
|
|
104
272
|
}
|
|
105
273
|
}
|
|
106
274
|
});
|
|
107
275
|
});
|
|
108
276
|
}
|
|
109
277
|
|
|
110
|
-
|
|
111
278
|
count(doc_id, adjustment, initial_value) {
|
|
112
279
|
var that = this;
|
|
113
280
|
return new Promise(async function (resolve, reject) {
|
|
114
|
-
that.connection.counter(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
281
|
+
that.connection.counter(
|
|
282
|
+
doc_id,
|
|
283
|
+
adjustment,
|
|
284
|
+
{
|
|
285
|
+
initial: initial_value,
|
|
286
|
+
},
|
|
287
|
+
function (err, res) {
|
|
288
|
+
if (err) {
|
|
289
|
+
reject(err);
|
|
290
|
+
} else {
|
|
291
|
+
resolve(res.value);
|
|
292
|
+
}
|
|
121
293
|
}
|
|
122
|
-
|
|
294
|
+
);
|
|
123
295
|
});
|
|
124
296
|
}
|
|
125
297
|
|
|
126
|
-
|
|
127
298
|
update(doc_id, document) {
|
|
128
299
|
var that = this;
|
|
129
300
|
return new Promise(async function (resolve, reject) {
|
|
@@ -131,7 +302,7 @@ module.exports = class Database {
|
|
|
131
302
|
if (err) {
|
|
132
303
|
reject(err);
|
|
133
304
|
} else {
|
|
134
|
-
resolve(res.value)
|
|
305
|
+
resolve(res.value);
|
|
135
306
|
}
|
|
136
307
|
});
|
|
137
308
|
});
|
|
@@ -144,7 +315,7 @@ module.exports = class Database {
|
|
|
144
315
|
if (err) {
|
|
145
316
|
reject(err);
|
|
146
317
|
} else {
|
|
147
|
-
resolve(res.value)
|
|
318
|
+
resolve(res.value);
|
|
148
319
|
}
|
|
149
320
|
});
|
|
150
321
|
});
|
|
@@ -158,7 +329,7 @@ module.exports = class Database {
|
|
|
158
329
|
}
|
|
159
330
|
// console.log("on Multiget", keys);
|
|
160
331
|
if (_.isArray(keys) && keys.length > 0) {
|
|
161
|
-
let functions = _.map(keys, key => this.connection.get(key))
|
|
332
|
+
let functions = _.map(keys, (key) => this.connection.get(key));
|
|
162
333
|
let returned_all = await Promise.allSettled(functions);
|
|
163
334
|
|
|
164
335
|
let responses = _.flatten(
|
|
@@ -228,7 +399,7 @@ module.exports = class Database {
|
|
|
228
399
|
that.connection.query(query, async function (err, results, matrix) {
|
|
229
400
|
var return_value = [];
|
|
230
401
|
if (err) {
|
|
231
|
-
reject(err)
|
|
402
|
+
reject(err);
|
|
232
403
|
} else {
|
|
233
404
|
for (var i = 0; i < results.length; i++) {
|
|
234
405
|
if (results[i].value) {
|
|
@@ -239,88 +410,112 @@ module.exports = class Database {
|
|
|
239
410
|
}
|
|
240
411
|
|
|
241
412
|
try {
|
|
242
|
-
let created = await that.multi_get(return_value)
|
|
243
|
-
resolve(created)
|
|
413
|
+
let created = await that.multi_get(return_value);
|
|
414
|
+
resolve(created);
|
|
244
415
|
} catch (e) {
|
|
245
|
-
reject(e)
|
|
416
|
+
reject(e);
|
|
246
417
|
}
|
|
247
418
|
}
|
|
248
419
|
});
|
|
249
420
|
});
|
|
250
421
|
}
|
|
251
422
|
|
|
423
|
+
async search(
|
|
424
|
+
type,
|
|
425
|
+
columns,
|
|
426
|
+
query,
|
|
427
|
+
limit,
|
|
428
|
+
offset,
|
|
429
|
+
order_by,
|
|
430
|
+
do_count,
|
|
431
|
+
query_scope
|
|
432
|
+
) {
|
|
433
|
+
let alias = "_reference";
|
|
434
|
+
let bucket = `\`${this.collection_name}\` AS ${alias}`;
|
|
435
|
+
|
|
436
|
+
let include_type = this.scope_name && this.collection_name;
|
|
437
|
+
|
|
438
|
+
try {
|
|
439
|
+
if (do_count == undefined) {
|
|
440
|
+
do_count = "false";
|
|
441
|
+
}
|
|
442
|
+
var fix = "";
|
|
443
|
+
var count_query = "";
|
|
444
|
+
if (!limit) {
|
|
445
|
+
limit = 0;
|
|
446
|
+
}
|
|
252
447
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
try {
|
|
257
|
-
if (do_count == undefined) {
|
|
258
|
-
do_count = "false";
|
|
259
|
-
}
|
|
260
|
-
var fix = "";
|
|
261
|
-
var count_query = "";
|
|
262
|
-
if (!limit) {
|
|
263
|
-
limit = 0;
|
|
264
|
-
}
|
|
448
|
+
if (!offset) {
|
|
449
|
+
offset = 0;
|
|
450
|
+
}
|
|
265
451
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
452
|
+
if (columns == "*") {
|
|
453
|
+
columns = "(" + alias + ").*";
|
|
454
|
+
fix = ", META(" + alias + ").id AS id";
|
|
455
|
+
}
|
|
269
456
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
457
|
+
if (!columns) {
|
|
458
|
+
columns = "(" + alias + ").*";
|
|
459
|
+
fix = ", META(" + alias + ").id AS id";
|
|
460
|
+
}
|
|
274
461
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
462
|
+
if (columns == "id") {
|
|
463
|
+
fix = ", META(" + alias + ").id AS id";
|
|
464
|
+
columns = "";
|
|
465
|
+
}
|
|
279
466
|
|
|
280
|
-
|
|
281
|
-
fix = ", META(" + that.bucket + ").id AS id";
|
|
282
|
-
columns = "";
|
|
283
|
-
}
|
|
467
|
+
var front_query = `SELECT ${columns} ${fix} FROM ${bucket}`;
|
|
284
468
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
count_query = count_query + " AND " + query;
|
|
289
|
-
query = front_query + " AND " + query;
|
|
290
|
-
} else {
|
|
291
|
-
query = front_query;
|
|
292
|
-
}
|
|
469
|
+
if (include_type || query) {
|
|
470
|
+
front_query += " WHERE ";
|
|
471
|
+
}
|
|
293
472
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
}
|
|
473
|
+
let conditions = [];
|
|
474
|
+
if (include_type) {
|
|
475
|
+
conditions.push(`type = '${type}'`);
|
|
476
|
+
}
|
|
477
|
+
if (query) {
|
|
478
|
+
conditions.push(query);
|
|
479
|
+
}
|
|
297
480
|
|
|
298
|
-
|
|
299
|
-
query = query + " LIMIT " + limit;
|
|
300
|
-
}
|
|
481
|
+
query = front_query + conditions.join(" AND ");
|
|
301
482
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
483
|
+
count_query = `SELECT count(meta().id) as count FROM ${bucket}`;
|
|
484
|
+
if (include_type || query) {
|
|
485
|
+
count_query += " WHERE ";
|
|
486
|
+
}
|
|
487
|
+
query = front_query + conditions.join(" AND ");
|
|
305
488
|
|
|
306
|
-
|
|
307
|
-
|
|
489
|
+
if (order_by) {
|
|
490
|
+
query = `${query} ORDER BY ${order_by}`;
|
|
491
|
+
}
|
|
308
492
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
let responses = await Promise.all(queries);
|
|
493
|
+
if (limit > 0) {
|
|
494
|
+
query = query + " LIMIT " + limit;
|
|
495
|
+
}
|
|
314
496
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
total: responses.length > 1 ? responses[1] : null,
|
|
318
|
-
});
|
|
319
|
-
} catch (error) {
|
|
320
|
-
console.log(error.stack);
|
|
321
|
-
reject(error);
|
|
497
|
+
if (offset > 0) {
|
|
498
|
+
query = query + " OFFSET " + offset;
|
|
322
499
|
}
|
|
323
|
-
|
|
500
|
+
|
|
501
|
+
let uses_scopes = this.scope_name !== "_default";
|
|
502
|
+
|
|
503
|
+
let active_query_space = query_scope || uses_scopes ? "scope" : "cluster";
|
|
504
|
+
console.log(active_query_space, this.scope_name, uses_scopes);
|
|
505
|
+
|
|
506
|
+
let queries = [this.getData(query, active_query_space)];
|
|
507
|
+
if (do_count === "true") {
|
|
508
|
+
queries.push(this.getCount(count_query, active_query_space));
|
|
509
|
+
}
|
|
510
|
+
let responses = await Promise.all(queries);
|
|
511
|
+
|
|
512
|
+
return {
|
|
513
|
+
data: responses[0],
|
|
514
|
+
total: responses.length > 1 ? responses[1] : null,
|
|
515
|
+
};
|
|
516
|
+
} catch (error) {
|
|
517
|
+
throw error;
|
|
518
|
+
}
|
|
324
519
|
}
|
|
325
520
|
|
|
326
521
|
full_text_search(type, search_query, limit, offset = 0, full_text_index) {
|
|
@@ -328,7 +523,10 @@ module.exports = class Database {
|
|
|
328
523
|
return new Promise(async function (resolve, reject) {
|
|
329
524
|
try {
|
|
330
525
|
let full_query = search_query + " AND type:" + type;
|
|
331
|
-
var query = that.SearchQuery.new(
|
|
526
|
+
var query = that.SearchQuery.new(
|
|
527
|
+
that.full_text_index || full_text_index,
|
|
528
|
+
that.SearchQuery.queryString(full_query)
|
|
529
|
+
);
|
|
332
530
|
|
|
333
531
|
if (!limit) {
|
|
334
532
|
limit = 0;
|
|
@@ -347,7 +545,7 @@ module.exports = class Database {
|
|
|
347
545
|
that.cluster.query(query, async function (err, results, matrix) {
|
|
348
546
|
var return_value = [];
|
|
349
547
|
if (err) {
|
|
350
|
-
reject(err)
|
|
548
|
+
reject(err);
|
|
351
549
|
} else {
|
|
352
550
|
for (var i = 0; i < results.length; i++) {
|
|
353
551
|
if (results[i].value) {
|
|
@@ -358,7 +556,7 @@ module.exports = class Database {
|
|
|
358
556
|
}
|
|
359
557
|
|
|
360
558
|
try {
|
|
361
|
-
let getted = await that.multi_get(return_value)
|
|
559
|
+
let getted = await that.multi_get(return_value);
|
|
362
560
|
resolve({
|
|
363
561
|
data: getted,
|
|
364
562
|
total: null,
|
|
@@ -374,16 +572,21 @@ module.exports = class Database {
|
|
|
374
572
|
});
|
|
375
573
|
}
|
|
376
574
|
|
|
377
|
-
async getCount(count_query_element) {
|
|
378
|
-
let response = await this.
|
|
575
|
+
async getCount(count_query_element, query_scope = "cluster") {
|
|
576
|
+
let response = await this.query(count_query_element, query_scope);
|
|
379
577
|
return response.rows[0].count;
|
|
380
578
|
}
|
|
381
|
-
async getData(query_element) {
|
|
382
|
-
let response = await this.
|
|
579
|
+
async getData(query_element, query_scope = "cluster") {
|
|
580
|
+
let response = await this.query(query_element, query_scope);
|
|
383
581
|
return response.rows;
|
|
384
582
|
}
|
|
385
583
|
|
|
386
|
-
async query(query) {
|
|
387
|
-
|
|
584
|
+
async query(query, query_scope = "cluster") {
|
|
585
|
+
let scope = query_scope || this.query_scope;
|
|
586
|
+
if (scope == "cluster") {
|
|
587
|
+
return await this.cluster.query(query);
|
|
588
|
+
} else {
|
|
589
|
+
return await this.scope.query(query);
|
|
590
|
+
}
|
|
388
591
|
}
|
|
389
|
-
}
|
|
592
|
+
};
|