spice-couchbase 2.0.12 → 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 +343 -145
- package/package.json +5 -3
- package/src/index.js +368 -140
package/build/index.js
CHANGED
|
@@ -1,72 +1,244 @@
|
|
|
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");
|
|
14
|
+
|
|
15
|
+
var couchbase = require("couchbase");
|
|
16
|
+
|
|
17
|
+
var {
|
|
18
|
+
default: axios
|
|
19
|
+
} = require("axios");
|
|
20
|
+
|
|
21
|
+
var qs = require("qs");
|
|
8
22
|
|
|
9
|
-
|
|
23
|
+
if (!Promise.allSettled) {
|
|
24
|
+
Promise.allSettled = promises => Promise.all(promises.map((promise, i) => promise.then(value => ({
|
|
25
|
+
status: "fulfilled",
|
|
26
|
+
value
|
|
27
|
+
})).catch(reason => ({
|
|
28
|
+
status: "rejected",
|
|
29
|
+
reason
|
|
30
|
+
}))));
|
|
31
|
+
}
|
|
10
32
|
|
|
11
33
|
module.exports = class Database {
|
|
12
|
-
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
|
+
|
|
13
43
|
if (connection) {
|
|
14
44
|
this.connection_name = connection;
|
|
15
45
|
this.connection = spice.database.connections[connection];
|
|
46
|
+
this.query_scope = query_scope;
|
|
16
47
|
this.cluster = spice.database.clusters[connection];
|
|
17
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];
|
|
18
55
|
this.full_text_index = spice.config.database.connections[connection].full_text_index || full_text_index;
|
|
19
56
|
this.N1qlQuery = couchbase.N1qlQuery;
|
|
20
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
|
+
}
|
|
21
64
|
}
|
|
22
65
|
}
|
|
23
66
|
|
|
24
|
-
connect(
|
|
25
|
-
|
|
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* () {
|
|
26
99
|
try {
|
|
27
|
-
var
|
|
28
|
-
|
|
29
|
-
password: value.password
|
|
100
|
+
var allUsers = yield _this2.userManager.getAllUsers({
|
|
101
|
+
timeout: 6000
|
|
30
102
|
});
|
|
31
|
-
|
|
32
|
-
var
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
+
}]
|
|
38
112
|
});
|
|
113
|
+
console.log("UPdate Response", update_user_response);
|
|
39
114
|
} catch (e) {
|
|
40
|
-
|
|
41
|
-
reject(e);
|
|
115
|
+
console.log(e);
|
|
42
116
|
}
|
|
43
|
-
});
|
|
117
|
+
})();
|
|
44
118
|
}
|
|
45
119
|
|
|
46
|
-
|
|
47
|
-
var
|
|
48
|
-
return
|
|
49
|
-
|
|
50
|
-
function () {
|
|
51
|
-
var _ref = _asyncToGenerator(function* (resolve, reject) {
|
|
52
|
-
that.connection.get(doc_id, function (err, res) {
|
|
53
|
-
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
|
|
54
124
|
|
|
55
|
-
if (err) {
|
|
56
|
-
reject(err);
|
|
57
|
-
} else {
|
|
58
|
-
_.defaults(return_value, res.value);
|
|
59
125
|
|
|
60
|
-
|
|
61
|
-
|
|
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
|
|
62
161
|
}
|
|
63
162
|
});
|
|
64
|
-
|
|
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
|
+
}
|
|
65
174
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
+
})();
|
|
70
242
|
}
|
|
71
243
|
|
|
72
244
|
insert(doc_id, document, expiry) {
|
|
@@ -74,13 +246,13 @@ module.exports = class Database {
|
|
|
74
246
|
return new Promise(
|
|
75
247
|
/*#__PURE__*/
|
|
76
248
|
function () {
|
|
77
|
-
var
|
|
249
|
+
var _ref = _asyncToGenerator(function* (resolve, reject) {
|
|
78
250
|
that.connection.insert(doc_id, document, {
|
|
79
251
|
expiry: expiry
|
|
80
252
|
},
|
|
81
253
|
/*#__PURE__*/
|
|
82
254
|
function () {
|
|
83
|
-
var
|
|
255
|
+
var _ref2 = _asyncToGenerator(function* (err, res) {
|
|
84
256
|
if (err) {
|
|
85
257
|
reject(err);
|
|
86
258
|
} else {
|
|
@@ -93,14 +265,14 @@ module.exports = class Database {
|
|
|
93
265
|
}
|
|
94
266
|
});
|
|
95
267
|
|
|
96
|
-
return function (
|
|
97
|
-
return
|
|
268
|
+
return function (_x3, _x4) {
|
|
269
|
+
return _ref2.apply(this, arguments);
|
|
98
270
|
};
|
|
99
271
|
}());
|
|
100
272
|
});
|
|
101
273
|
|
|
102
|
-
return function (
|
|
103
|
-
return
|
|
274
|
+
return function (_x, _x2) {
|
|
275
|
+
return _ref.apply(this, arguments);
|
|
104
276
|
};
|
|
105
277
|
}());
|
|
106
278
|
}
|
|
@@ -111,11 +283,11 @@ module.exports = class Database {
|
|
|
111
283
|
return new Promise(
|
|
112
284
|
/*#__PURE__*/
|
|
113
285
|
function () {
|
|
114
|
-
var
|
|
286
|
+
var _ref3 = _asyncToGenerator(function* (resolve, reject) {
|
|
115
287
|
that.connection.touch(doc_id, expiry,
|
|
116
288
|
/*#__PURE__*/
|
|
117
289
|
function () {
|
|
118
|
-
var
|
|
290
|
+
var _ref4 = _asyncToGenerator(function* (err, res) {
|
|
119
291
|
if (err) {
|
|
120
292
|
reject(err);
|
|
121
293
|
} else {
|
|
@@ -128,14 +300,14 @@ module.exports = class Database {
|
|
|
128
300
|
}
|
|
129
301
|
});
|
|
130
302
|
|
|
131
|
-
return function (
|
|
132
|
-
return
|
|
303
|
+
return function (_x7, _x8) {
|
|
304
|
+
return _ref4.apply(this, arguments);
|
|
133
305
|
};
|
|
134
306
|
}());
|
|
135
307
|
});
|
|
136
308
|
|
|
137
|
-
return function (
|
|
138
|
-
return
|
|
309
|
+
return function (_x5, _x6) {
|
|
310
|
+
return _ref3.apply(this, arguments);
|
|
139
311
|
};
|
|
140
312
|
}());
|
|
141
313
|
}
|
|
@@ -145,7 +317,7 @@ module.exports = class Database {
|
|
|
145
317
|
return new Promise(
|
|
146
318
|
/*#__PURE__*/
|
|
147
319
|
function () {
|
|
148
|
-
var
|
|
320
|
+
var _ref5 = _asyncToGenerator(function* (resolve, reject) {
|
|
149
321
|
that.connection.counter(doc_id, adjustment, {
|
|
150
322
|
initial: initial_value
|
|
151
323
|
}, function (err, res) {
|
|
@@ -157,8 +329,8 @@ module.exports = class Database {
|
|
|
157
329
|
});
|
|
158
330
|
});
|
|
159
331
|
|
|
160
|
-
return function (
|
|
161
|
-
return
|
|
332
|
+
return function (_x9, _x10) {
|
|
333
|
+
return _ref5.apply(this, arguments);
|
|
162
334
|
};
|
|
163
335
|
}());
|
|
164
336
|
}
|
|
@@ -168,7 +340,7 @@ module.exports = class Database {
|
|
|
168
340
|
return new Promise(
|
|
169
341
|
/*#__PURE__*/
|
|
170
342
|
function () {
|
|
171
|
-
var
|
|
343
|
+
var _ref6 = _asyncToGenerator(function* (resolve, reject) {
|
|
172
344
|
that.connection.replace(doc_id, document, function (err, res) {
|
|
173
345
|
if (err) {
|
|
174
346
|
reject(err);
|
|
@@ -178,8 +350,8 @@ module.exports = class Database {
|
|
|
178
350
|
});
|
|
179
351
|
});
|
|
180
352
|
|
|
181
|
-
return function (
|
|
182
|
-
return
|
|
353
|
+
return function (_x11, _x12) {
|
|
354
|
+
return _ref6.apply(this, arguments);
|
|
183
355
|
};
|
|
184
356
|
}());
|
|
185
357
|
}
|
|
@@ -189,7 +361,7 @@ module.exports = class Database {
|
|
|
189
361
|
return new Promise(
|
|
190
362
|
/*#__PURE__*/
|
|
191
363
|
function () {
|
|
192
|
-
var
|
|
364
|
+
var _ref7 = _asyncToGenerator(function* (resolve, reject) {
|
|
193
365
|
that.connection.remove(doc_id, function (err, res) {
|
|
194
366
|
if (err) {
|
|
195
367
|
reject(err);
|
|
@@ -199,14 +371,14 @@ module.exports = class Database {
|
|
|
199
371
|
});
|
|
200
372
|
});
|
|
201
373
|
|
|
202
|
-
return function (
|
|
203
|
-
return
|
|
374
|
+
return function (_x13, _x14) {
|
|
375
|
+
return _ref7.apply(this, arguments);
|
|
204
376
|
};
|
|
205
377
|
}());
|
|
206
378
|
}
|
|
207
379
|
|
|
208
380
|
multi_get(keys, keep_type) {
|
|
209
|
-
var
|
|
381
|
+
var _this9 = this;
|
|
210
382
|
|
|
211
383
|
return _asyncToGenerator(function* () {
|
|
212
384
|
/* var that = this; */
|
|
@@ -218,9 +390,14 @@ module.exports = class Database {
|
|
|
218
390
|
|
|
219
391
|
|
|
220
392
|
if (_.isArray(keys) && keys.length > 0) {
|
|
221
|
-
var functions = _.map(keys, key =>
|
|
393
|
+
var functions = _.map(keys, key => _this9.connection.get(key));
|
|
394
|
+
|
|
395
|
+
var returned_all = yield Promise.allSettled(functions);
|
|
396
|
+
|
|
397
|
+
var responses = _.flatten(_.compact(_.map(returned_all, returned_obj => {
|
|
398
|
+
if (returned_obj.status == "fulfilled") return returned_obj.value;
|
|
399
|
+
})));
|
|
222
400
|
|
|
223
|
-
var responses = yield Promise.all(functions);
|
|
224
401
|
return _.map(responses, (res, index) => {
|
|
225
402
|
if (!keep_type) {
|
|
226
403
|
delete res.value.type;
|
|
@@ -257,7 +434,7 @@ module.exports = class Database {
|
|
|
257
434
|
return new Promise(
|
|
258
435
|
/*#__PURE__*/
|
|
259
436
|
function () {
|
|
260
|
-
var
|
|
437
|
+
var _ref8 = _asyncToGenerator(function* (resolve, reject) {
|
|
261
438
|
var ViewQuery = couchbase.ViewQuery;
|
|
262
439
|
var query = ViewQuery.from(design_doc, view);
|
|
263
440
|
|
|
@@ -284,7 +461,7 @@ module.exports = class Database {
|
|
|
284
461
|
that.connection.query(query,
|
|
285
462
|
/*#__PURE__*/
|
|
286
463
|
function () {
|
|
287
|
-
var
|
|
464
|
+
var _ref9 = _asyncToGenerator(function* (err, results, matrix) {
|
|
288
465
|
var return_value = [];
|
|
289
466
|
|
|
290
467
|
if (err) {
|
|
@@ -307,100 +484,112 @@ module.exports = class Database {
|
|
|
307
484
|
}
|
|
308
485
|
});
|
|
309
486
|
|
|
310
|
-
return function (
|
|
311
|
-
return
|
|
487
|
+
return function (_x17, _x18, _x19) {
|
|
488
|
+
return _ref9.apply(this, arguments);
|
|
312
489
|
};
|
|
313
490
|
}());
|
|
314
491
|
});
|
|
315
492
|
|
|
316
|
-
return function (
|
|
317
|
-
return
|
|
493
|
+
return function (_x15, _x16) {
|
|
494
|
+
return _ref8.apply(this, arguments);
|
|
318
495
|
};
|
|
319
496
|
}());
|
|
320
497
|
}
|
|
321
498
|
|
|
322
|
-
search(type, columns, query, limit, offset, order_by, do_count,
|
|
323
|
-
var
|
|
324
|
-
return new Promise(
|
|
325
|
-
/*#__PURE__*/
|
|
326
|
-
function () {
|
|
327
|
-
var _ref11 = _asyncToGenerator(function* (resolve, reject) {
|
|
328
|
-
try {
|
|
329
|
-
if (do_count == undefined) {
|
|
330
|
-
do_count = "false";
|
|
331
|
-
}
|
|
499
|
+
search(type, columns, query, limit, offset, order_by, do_count, query_scope) {
|
|
500
|
+
var _this10 = this;
|
|
332
501
|
|
|
333
|
-
|
|
334
|
-
|
|
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;
|
|
335
506
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
507
|
+
try {
|
|
508
|
+
if (do_count == undefined) {
|
|
509
|
+
do_count = "false";
|
|
510
|
+
}
|
|
339
511
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
}
|
|
512
|
+
var fix = "";
|
|
513
|
+
var count_query = "";
|
|
343
514
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
}
|
|
515
|
+
if (!limit) {
|
|
516
|
+
limit = 0;
|
|
517
|
+
}
|
|
348
518
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}
|
|
519
|
+
if (!offset) {
|
|
520
|
+
offset = 0;
|
|
521
|
+
}
|
|
353
522
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
523
|
+
if (columns == "*") {
|
|
524
|
+
columns = "(" + alias + ").*";
|
|
525
|
+
fix = ", META(" + alias + ").id AS id";
|
|
526
|
+
}
|
|
358
527
|
|
|
359
|
-
|
|
360
|
-
|
|
528
|
+
if (!columns) {
|
|
529
|
+
columns = "(" + alias + ").*";
|
|
530
|
+
fix = ", META(" + alias + ").id AS id";
|
|
531
|
+
}
|
|
361
532
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
query = front_query;
|
|
367
|
-
}
|
|
533
|
+
if (columns == "id") {
|
|
534
|
+
fix = ", META(" + alias + ").id AS id";
|
|
535
|
+
columns = "";
|
|
536
|
+
}
|
|
368
537
|
|
|
369
|
-
|
|
370
|
-
query = "".concat(query, " ORDER BY ").concat(order_by);
|
|
371
|
-
}
|
|
538
|
+
var front_query = "SELECT ".concat(columns, " ").concat(fix, " FROM ").concat(bucket);
|
|
372
539
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
540
|
+
if (include_type || query) {
|
|
541
|
+
front_query += " WHERE ";
|
|
542
|
+
}
|
|
376
543
|
|
|
377
|
-
|
|
378
|
-
query = query + " OFFSET " + offset;
|
|
379
|
-
}
|
|
544
|
+
var conditions = [];
|
|
380
545
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
546
|
+
if (include_type) {
|
|
547
|
+
conditions.push("type = '".concat(type, "'"));
|
|
548
|
+
}
|
|
384
549
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
550
|
+
if (query) {
|
|
551
|
+
conditions.push(query);
|
|
552
|
+
}
|
|
388
553
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
} catch (error) {
|
|
395
|
-
console.log(error.stack);
|
|
396
|
-
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 ";
|
|
397
559
|
}
|
|
398
|
-
});
|
|
399
560
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
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
|
+
})();
|
|
404
593
|
}
|
|
405
594
|
|
|
406
595
|
full_text_search(type, search_query, limit) {
|
|
@@ -410,7 +599,7 @@ module.exports = class Database {
|
|
|
410
599
|
return new Promise(
|
|
411
600
|
/*#__PURE__*/
|
|
412
601
|
function () {
|
|
413
|
-
var
|
|
602
|
+
var _ref10 = _asyncToGenerator(function* (resolve, reject) {
|
|
414
603
|
try {
|
|
415
604
|
var full_query = search_query + " AND type:" + type;
|
|
416
605
|
var query = that.SearchQuery.new(that.full_text_index || full_text_index, that.SearchQuery.queryString(full_query));
|
|
@@ -431,7 +620,7 @@ module.exports = class Database {
|
|
|
431
620
|
that.cluster.query(query,
|
|
432
621
|
/*#__PURE__*/
|
|
433
622
|
function () {
|
|
434
|
-
var
|
|
623
|
+
var _ref11 = _asyncToGenerator(function* (err, results, matrix) {
|
|
435
624
|
var return_value = [];
|
|
436
625
|
|
|
437
626
|
if (err) {
|
|
@@ -457,8 +646,8 @@ module.exports = class Database {
|
|
|
457
646
|
}
|
|
458
647
|
});
|
|
459
648
|
|
|
460
|
-
return function (
|
|
461
|
-
return
|
|
649
|
+
return function (_x22, _x23, _x24) {
|
|
650
|
+
return _ref11.apply(this, arguments);
|
|
462
651
|
};
|
|
463
652
|
}());
|
|
464
653
|
} catch (e) {
|
|
@@ -466,35 +655,44 @@ module.exports = class Database {
|
|
|
466
655
|
}
|
|
467
656
|
});
|
|
468
657
|
|
|
469
|
-
return function (
|
|
470
|
-
return
|
|
658
|
+
return function (_x20, _x21) {
|
|
659
|
+
return _ref10.apply(this, arguments);
|
|
471
660
|
};
|
|
472
661
|
}());
|
|
473
662
|
}
|
|
474
663
|
|
|
475
664
|
getCount(count_query_element) {
|
|
476
|
-
var
|
|
665
|
+
var _this11 = this;
|
|
477
666
|
|
|
667
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
478
668
|
return _asyncToGenerator(function* () {
|
|
479
|
-
var response = yield
|
|
669
|
+
var response = yield _this11.query(count_query_element, query_scope);
|
|
480
670
|
return response.rows[0].count;
|
|
481
671
|
})();
|
|
482
672
|
}
|
|
483
673
|
|
|
484
674
|
getData(query_element) {
|
|
485
|
-
var
|
|
675
|
+
var _this12 = this;
|
|
486
676
|
|
|
677
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
487
678
|
return _asyncToGenerator(function* () {
|
|
488
|
-
var response = yield
|
|
679
|
+
var response = yield _this12.query(query_element, query_scope);
|
|
489
680
|
return response.rows;
|
|
490
681
|
})();
|
|
491
682
|
}
|
|
492
683
|
|
|
493
684
|
query(query) {
|
|
494
|
-
var
|
|
685
|
+
var _this13 = this;
|
|
495
686
|
|
|
687
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
496
688
|
return _asyncToGenerator(function* () {
|
|
497
|
-
|
|
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
|
+
}
|
|
498
696
|
})();
|
|
499
697
|
}
|
|
500
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,74 +1,259 @@
|
|
|
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");
|
|
7
|
+
|
|
8
|
+
if (!Promise.allSettled) {
|
|
9
|
+
Promise.allSettled = (promises) =>
|
|
10
|
+
Promise.all(
|
|
11
|
+
promises.map((promise, i) =>
|
|
12
|
+
promise
|
|
13
|
+
.then((value) => ({
|
|
14
|
+
status: "fulfilled",
|
|
15
|
+
value,
|
|
16
|
+
}))
|
|
17
|
+
.catch((reason) => ({
|
|
18
|
+
status: "rejected",
|
|
19
|
+
reason,
|
|
20
|
+
}))
|
|
21
|
+
)
|
|
22
|
+
);
|
|
23
|
+
}
|
|
5
24
|
module.exports = class Database {
|
|
6
|
-
constructor(
|
|
25
|
+
constructor(
|
|
26
|
+
connection,
|
|
27
|
+
{
|
|
28
|
+
full_text_index,
|
|
29
|
+
collection,
|
|
30
|
+
scope,
|
|
31
|
+
query_scope = "cluster",
|
|
32
|
+
auto_create_collection,
|
|
33
|
+
} = {}
|
|
34
|
+
) {
|
|
7
35
|
if (connection) {
|
|
8
36
|
this.connection_name = connection;
|
|
9
37
|
this.connection = spice.database.connections[connection];
|
|
10
|
-
this.
|
|
38
|
+
this.query_scope = query_scope;
|
|
39
|
+
this.cluster = spice.database.clusters[connection];
|
|
11
40
|
this.bucket = this.connection._scope._bucket._name;
|
|
12
|
-
this.
|
|
13
|
-
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;
|
|
14
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
|
+
}
|
|
15
73
|
}
|
|
16
74
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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);
|
|
35
151
|
}
|
|
36
|
-
})
|
|
152
|
+
} catch (e) {
|
|
153
|
+
console.log(e);
|
|
154
|
+
}
|
|
37
155
|
}
|
|
38
156
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
+
},
|
|
50
168
|
}
|
|
51
|
-
|
|
52
|
-
|
|
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
|
+
}
|
|
53
233
|
}
|
|
54
234
|
|
|
55
235
|
insert(doc_id, document, expiry) {
|
|
56
236
|
var that = this;
|
|
57
237
|
return new Promise(async function (resolve, reject) {
|
|
58
|
-
that.connection.insert(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
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
|
+
}
|
|
69
254
|
}
|
|
70
255
|
}
|
|
71
|
-
|
|
256
|
+
);
|
|
72
257
|
});
|
|
73
258
|
}
|
|
74
259
|
|
|
@@ -80,33 +265,36 @@ module.exports = class Database {
|
|
|
80
265
|
reject(err);
|
|
81
266
|
} else {
|
|
82
267
|
try {
|
|
83
|
-
let created = await that.get(doc_id)
|
|
84
|
-
resolve(created)
|
|
268
|
+
let created = await that.get(doc_id);
|
|
269
|
+
resolve(created);
|
|
85
270
|
} catch (e) {
|
|
86
|
-
reject(e)
|
|
271
|
+
reject(e);
|
|
87
272
|
}
|
|
88
273
|
}
|
|
89
274
|
});
|
|
90
275
|
});
|
|
91
276
|
}
|
|
92
277
|
|
|
93
|
-
|
|
94
278
|
count(doc_id, adjustment, initial_value) {
|
|
95
279
|
var that = this;
|
|
96
280
|
return new Promise(async function (resolve, reject) {
|
|
97
|
-
that.connection.counter(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
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
|
+
}
|
|
104
293
|
}
|
|
105
|
-
|
|
294
|
+
);
|
|
106
295
|
});
|
|
107
296
|
}
|
|
108
297
|
|
|
109
|
-
|
|
110
298
|
update(doc_id, document) {
|
|
111
299
|
var that = this;
|
|
112
300
|
return new Promise(async function (resolve, reject) {
|
|
@@ -114,7 +302,7 @@ module.exports = class Database {
|
|
|
114
302
|
if (err) {
|
|
115
303
|
reject(err);
|
|
116
304
|
} else {
|
|
117
|
-
resolve(res.value)
|
|
305
|
+
resolve(res.value);
|
|
118
306
|
}
|
|
119
307
|
});
|
|
120
308
|
});
|
|
@@ -127,7 +315,7 @@ module.exports = class Database {
|
|
|
127
315
|
if (err) {
|
|
128
316
|
reject(err);
|
|
129
317
|
} else {
|
|
130
|
-
resolve(res.value)
|
|
318
|
+
resolve(res.value);
|
|
131
319
|
}
|
|
132
320
|
});
|
|
133
321
|
});
|
|
@@ -141,8 +329,16 @@ module.exports = class Database {
|
|
|
141
329
|
}
|
|
142
330
|
// console.log("on Multiget", keys);
|
|
143
331
|
if (_.isArray(keys) && keys.length > 0) {
|
|
144
|
-
let functions = _.map(keys, key => this.connection.get(key))
|
|
145
|
-
let
|
|
332
|
+
let functions = _.map(keys, (key) => this.connection.get(key));
|
|
333
|
+
let returned_all = await Promise.allSettled(functions);
|
|
334
|
+
|
|
335
|
+
let responses = _.flatten(
|
|
336
|
+
_.compact(
|
|
337
|
+
_.map(returned_all, (returned_obj) => {
|
|
338
|
+
if (returned_obj.status == "fulfilled") return returned_obj.value;
|
|
339
|
+
})
|
|
340
|
+
)
|
|
341
|
+
);
|
|
146
342
|
|
|
147
343
|
return _.map(responses, (res, index) => {
|
|
148
344
|
if (!keep_type) {
|
|
@@ -203,7 +399,7 @@ module.exports = class Database {
|
|
|
203
399
|
that.connection.query(query, async function (err, results, matrix) {
|
|
204
400
|
var return_value = [];
|
|
205
401
|
if (err) {
|
|
206
|
-
reject(err)
|
|
402
|
+
reject(err);
|
|
207
403
|
} else {
|
|
208
404
|
for (var i = 0; i < results.length; i++) {
|
|
209
405
|
if (results[i].value) {
|
|
@@ -214,88 +410,112 @@ module.exports = class Database {
|
|
|
214
410
|
}
|
|
215
411
|
|
|
216
412
|
try {
|
|
217
|
-
let created = await that.multi_get(return_value)
|
|
218
|
-
resolve(created)
|
|
413
|
+
let created = await that.multi_get(return_value);
|
|
414
|
+
resolve(created);
|
|
219
415
|
} catch (e) {
|
|
220
|
-
reject(e)
|
|
416
|
+
reject(e);
|
|
221
417
|
}
|
|
222
418
|
}
|
|
223
419
|
});
|
|
224
420
|
});
|
|
225
421
|
}
|
|
226
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
|
+
}
|
|
227
447
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
try {
|
|
232
|
-
if (do_count == undefined) {
|
|
233
|
-
do_count = "false";
|
|
234
|
-
}
|
|
235
|
-
var fix = "";
|
|
236
|
-
var count_query = "";
|
|
237
|
-
if (!limit) {
|
|
238
|
-
limit = 0;
|
|
239
|
-
}
|
|
448
|
+
if (!offset) {
|
|
449
|
+
offset = 0;
|
|
450
|
+
}
|
|
240
451
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
452
|
+
if (columns == "*") {
|
|
453
|
+
columns = "(" + alias + ").*";
|
|
454
|
+
fix = ", META(" + alias + ").id AS id";
|
|
455
|
+
}
|
|
244
456
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
457
|
+
if (!columns) {
|
|
458
|
+
columns = "(" + alias + ").*";
|
|
459
|
+
fix = ", META(" + alias + ").id AS id";
|
|
460
|
+
}
|
|
249
461
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
462
|
+
if (columns == "id") {
|
|
463
|
+
fix = ", META(" + alias + ").id AS id";
|
|
464
|
+
columns = "";
|
|
465
|
+
}
|
|
254
466
|
|
|
255
|
-
|
|
256
|
-
fix = ", META(" + that.bucket + ").id AS id";
|
|
257
|
-
columns = "";
|
|
258
|
-
}
|
|
467
|
+
var front_query = `SELECT ${columns} ${fix} FROM ${bucket}`;
|
|
259
468
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
count_query = count_query + " AND " + query;
|
|
264
|
-
query = front_query + " AND " + query;
|
|
265
|
-
} else {
|
|
266
|
-
query = front_query;
|
|
267
|
-
}
|
|
469
|
+
if (include_type || query) {
|
|
470
|
+
front_query += " WHERE ";
|
|
471
|
+
}
|
|
268
472
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
473
|
+
let conditions = [];
|
|
474
|
+
if (include_type) {
|
|
475
|
+
conditions.push(`type = '${type}'`);
|
|
476
|
+
}
|
|
477
|
+
if (query) {
|
|
478
|
+
conditions.push(query);
|
|
479
|
+
}
|
|
272
480
|
|
|
273
|
-
|
|
274
|
-
query = query + " LIMIT " + limit;
|
|
275
|
-
}
|
|
481
|
+
query = front_query + conditions.join(" AND ");
|
|
276
482
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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 ");
|
|
488
|
+
|
|
489
|
+
if (order_by) {
|
|
490
|
+
query = `${query} ORDER BY ${order_by}`;
|
|
491
|
+
}
|
|
280
492
|
|
|
281
|
-
|
|
282
|
-
|
|
493
|
+
if (limit > 0) {
|
|
494
|
+
query = query + " LIMIT " + limit;
|
|
495
|
+
}
|
|
283
496
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
288
|
-
let responses = await Promise.all(queries);
|
|
497
|
+
if (offset > 0) {
|
|
498
|
+
query = query + " OFFSET " + offset;
|
|
499
|
+
}
|
|
289
500
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
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));
|
|
297
509
|
}
|
|
298
|
-
|
|
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
|
+
}
|
|
299
519
|
}
|
|
300
520
|
|
|
301
521
|
full_text_search(type, search_query, limit, offset = 0, full_text_index) {
|
|
@@ -303,7 +523,10 @@ module.exports = class Database {
|
|
|
303
523
|
return new Promise(async function (resolve, reject) {
|
|
304
524
|
try {
|
|
305
525
|
let full_query = search_query + " AND type:" + type;
|
|
306
|
-
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
|
+
);
|
|
307
530
|
|
|
308
531
|
if (!limit) {
|
|
309
532
|
limit = 0;
|
|
@@ -322,7 +545,7 @@ module.exports = class Database {
|
|
|
322
545
|
that.cluster.query(query, async function (err, results, matrix) {
|
|
323
546
|
var return_value = [];
|
|
324
547
|
if (err) {
|
|
325
|
-
reject(err)
|
|
548
|
+
reject(err);
|
|
326
549
|
} else {
|
|
327
550
|
for (var i = 0; i < results.length; i++) {
|
|
328
551
|
if (results[i].value) {
|
|
@@ -333,7 +556,7 @@ module.exports = class Database {
|
|
|
333
556
|
}
|
|
334
557
|
|
|
335
558
|
try {
|
|
336
|
-
let getted = await that.multi_get(return_value)
|
|
559
|
+
let getted = await that.multi_get(return_value);
|
|
337
560
|
resolve({
|
|
338
561
|
data: getted,
|
|
339
562
|
total: null,
|
|
@@ -349,16 +572,21 @@ module.exports = class Database {
|
|
|
349
572
|
});
|
|
350
573
|
}
|
|
351
574
|
|
|
352
|
-
async getCount(count_query_element) {
|
|
353
|
-
let response = await this.
|
|
575
|
+
async getCount(count_query_element, query_scope = "cluster") {
|
|
576
|
+
let response = await this.query(count_query_element, query_scope);
|
|
354
577
|
return response.rows[0].count;
|
|
355
578
|
}
|
|
356
|
-
async getData(query_element) {
|
|
357
|
-
let response = await this.
|
|
579
|
+
async getData(query_element, query_scope = "cluster") {
|
|
580
|
+
let response = await this.query(query_element, query_scope);
|
|
358
581
|
return response.rows;
|
|
359
582
|
}
|
|
360
583
|
|
|
361
|
-
async query(query) {
|
|
362
|
-
|
|
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
|
+
}
|
|
363
591
|
}
|
|
364
|
-
}
|
|
592
|
+
};
|