spice-couchbase 2.0.13 → 2.0.16
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 +348 -231
- package/package.json +5 -3
- package/src/index.js +374 -199
package/build/index.js
CHANGED
|
@@ -1,72 +1,241 @@
|
|
|
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");
|
|
8
16
|
|
|
9
|
-
var
|
|
17
|
+
var {
|
|
18
|
+
default: axios
|
|
19
|
+
} = require("axios");
|
|
20
|
+
|
|
21
|
+
var qs = require("qs");
|
|
22
|
+
|
|
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
|
-
var
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
103
|
+
var update_user_response = yield _this2.userManager.upsertUser(_this2.db_config.username, {
|
|
104
|
+
roles: [{
|
|
105
|
+
name: "scope_admin",
|
|
106
|
+
bucket: _this2.db_config.bucket
|
|
107
|
+
}, {
|
|
108
|
+
name: "data_reader",
|
|
109
|
+
bucket: _this2.db_config.bucket
|
|
110
|
+
}]
|
|
38
111
|
});
|
|
39
112
|
} catch (e) {
|
|
40
|
-
|
|
41
|
-
reject(e);
|
|
113
|
+
console.log(e);
|
|
42
114
|
}
|
|
43
|
-
});
|
|
115
|
+
})();
|
|
44
116
|
}
|
|
45
117
|
|
|
46
|
-
|
|
47
|
-
var
|
|
48
|
-
return
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
118
|
+
getCouchbaseHost() {
|
|
119
|
+
var default_port = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 8091;
|
|
120
|
+
return (this.db_config.api_base || "http://".concat(_.last(_.first(this.db_config.nodes.split(",")).split("://")))) + ":" + default_port;
|
|
121
|
+
} //create wait function
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
wait(ms) {
|
|
125
|
+
return _asyncToGenerator(function* () {
|
|
126
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
127
|
+
})();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
upsertCollection() {
|
|
131
|
+
var _this3 = this;
|
|
54
132
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
_.defaults(return_value, res.value);
|
|
133
|
+
return _asyncToGenerator(function* () {
|
|
134
|
+
try {
|
|
135
|
+
var collection_exists = yield _this3.collectionExists(_this3.collection_name, _this3.scope_name);
|
|
59
136
|
|
|
60
|
-
|
|
61
|
-
|
|
137
|
+
if (!collection_exists) {
|
|
138
|
+
yield _this3.createCollection(_this3.collection_name, _this3.scope_name);
|
|
139
|
+
yield _this3.wait(2000);
|
|
140
|
+
yield _this3.createPrimaryIndex(_this3.scope_name, _this3.collection_name);
|
|
141
|
+
}
|
|
142
|
+
} catch (e) {
|
|
143
|
+
console.log(e);
|
|
144
|
+
}
|
|
145
|
+
})();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
collectionExists(collection_name, scope_name) {
|
|
149
|
+
var _this4 = this;
|
|
150
|
+
|
|
151
|
+
return _asyncToGenerator(function* () {
|
|
152
|
+
try {
|
|
153
|
+
var _$find;
|
|
154
|
+
|
|
155
|
+
var all_scopes = yield axios.get("".concat(_this4.getCouchbaseHost(), "/pools/default/buckets/").concat(_this4.db_config.bucket, "/scopes"), {
|
|
156
|
+
auth: {
|
|
157
|
+
username: _this4.db_config.username,
|
|
158
|
+
password: _this4.db_config.password
|
|
62
159
|
}
|
|
63
160
|
});
|
|
64
|
-
|
|
161
|
+
var collection_e = _.find(((_$find = _.find(all_scopes.data.scopes, {
|
|
162
|
+
name: scope_name
|
|
163
|
+
})) === null || _$find === void 0 ? void 0 : _$find.collections) || [], {
|
|
164
|
+
name: collection_name
|
|
165
|
+
}) != undefined;
|
|
166
|
+
return collection_e;
|
|
167
|
+
} catch (e) {
|
|
168
|
+
console.log("Exists error happened", e);
|
|
169
|
+
}
|
|
170
|
+
})();
|
|
171
|
+
}
|
|
65
172
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
173
|
+
createCollection(connection_name, scope_name) {
|
|
174
|
+
var _this5 = this;
|
|
175
|
+
|
|
176
|
+
return _asyncToGenerator(function* () {
|
|
177
|
+
try {
|
|
178
|
+
var token = "".concat(_this5.db_config.username, ":").concat(_this5.db_config.password);
|
|
179
|
+
var encodedToken = Buffer.from(token).toString("base64");
|
|
180
|
+
var headers = {
|
|
181
|
+
Authorization: "Basic " + encodedToken,
|
|
182
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
183
|
+
};
|
|
184
|
+
var response = yield axios({
|
|
185
|
+
method: "post",
|
|
186
|
+
url: "".concat(_this5.getCouchbaseHost(), "/pools/default/buckets/").concat(_this5.db_config.bucket, "/scopes/").concat(scope_name, "/collections"),
|
|
187
|
+
headers,
|
|
188
|
+
data: qs.stringify({
|
|
189
|
+
name: connection_name
|
|
190
|
+
})
|
|
191
|
+
}).data;
|
|
192
|
+
return response;
|
|
193
|
+
} catch (e) {}
|
|
194
|
+
})();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
createPrimaryIndex(scope_name, col_name) {
|
|
198
|
+
var _this6 = this;
|
|
199
|
+
|
|
200
|
+
return _asyncToGenerator(function* () {
|
|
201
|
+
try {
|
|
202
|
+
var response = yield _this6.query("CREATE PRIMARY INDEX ON default:`".concat(_this6.db_config.bucket, "`.`").concat(scope_name, "`.`").concat(col_name, "`"));
|
|
203
|
+
return response;
|
|
204
|
+
} catch (e) {
|
|
205
|
+
console.log("CREATE Index :::", e);
|
|
206
|
+
}
|
|
207
|
+
})();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
createScope(scope_name) {
|
|
211
|
+
var _this7 = this;
|
|
212
|
+
|
|
213
|
+
return _asyncToGenerator(function* () {
|
|
214
|
+
try {
|
|
215
|
+
yield _this7.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
|
+
|
|
226
|
+
get(doc_id) {
|
|
227
|
+
var _this8 = this;
|
|
228
|
+
|
|
229
|
+
return _asyncToGenerator(function* () {
|
|
230
|
+
try {
|
|
231
|
+
var response = yield _this8.connection.get(doc_id);
|
|
232
|
+
return _objectSpread({
|
|
233
|
+
id: doc_id
|
|
234
|
+
}, response.content);
|
|
235
|
+
} catch (e) {
|
|
236
|
+
console.error(e);
|
|
237
|
+
}
|
|
238
|
+
})();
|
|
70
239
|
}
|
|
71
240
|
|
|
72
241
|
insert(doc_id, document, expiry) {
|
|
@@ -74,13 +243,13 @@ module.exports = class Database {
|
|
|
74
243
|
return new Promise(
|
|
75
244
|
/*#__PURE__*/
|
|
76
245
|
function () {
|
|
77
|
-
var
|
|
246
|
+
var _ref = _asyncToGenerator(function* (resolve, reject) {
|
|
78
247
|
that.connection.insert(doc_id, document, {
|
|
79
248
|
expiry: expiry
|
|
80
249
|
},
|
|
81
250
|
/*#__PURE__*/
|
|
82
251
|
function () {
|
|
83
|
-
var
|
|
252
|
+
var _ref2 = _asyncToGenerator(function* (err, res) {
|
|
84
253
|
if (err) {
|
|
85
254
|
reject(err);
|
|
86
255
|
} else {
|
|
@@ -93,120 +262,59 @@ module.exports = class Database {
|
|
|
93
262
|
}
|
|
94
263
|
});
|
|
95
264
|
|
|
96
|
-
return function (
|
|
97
|
-
return
|
|
265
|
+
return function (_x3, _x4) {
|
|
266
|
+
return _ref2.apply(this, arguments);
|
|
98
267
|
};
|
|
99
268
|
}());
|
|
100
269
|
});
|
|
101
270
|
|
|
102
|
-
return function (
|
|
103
|
-
return
|
|
271
|
+
return function (_x, _x2) {
|
|
272
|
+
return _ref.apply(this, arguments);
|
|
104
273
|
};
|
|
105
274
|
}());
|
|
106
275
|
}
|
|
107
276
|
|
|
108
277
|
touch(doc_id) {
|
|
109
|
-
var
|
|
110
|
-
var that = this;
|
|
111
|
-
return new Promise(
|
|
112
|
-
/*#__PURE__*/
|
|
113
|
-
function () {
|
|
114
|
-
var _ref4 = _asyncToGenerator(function* (resolve, reject) {
|
|
115
|
-
that.connection.touch(doc_id, expiry,
|
|
116
|
-
/*#__PURE__*/
|
|
117
|
-
function () {
|
|
118
|
-
var _ref5 = _asyncToGenerator(function* (err, res) {
|
|
119
|
-
if (err) {
|
|
120
|
-
reject(err);
|
|
121
|
-
} else {
|
|
122
|
-
try {
|
|
123
|
-
var created = yield that.get(doc_id);
|
|
124
|
-
resolve(created);
|
|
125
|
-
} catch (e) {
|
|
126
|
-
reject(e);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
return function (_x9, _x10) {
|
|
132
|
-
return _ref5.apply(this, arguments);
|
|
133
|
-
};
|
|
134
|
-
}());
|
|
135
|
-
});
|
|
278
|
+
var _this9 = this;
|
|
136
279
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
280
|
+
var expiry = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
281
|
+
return _asyncToGenerator(function* () {
|
|
282
|
+
yield _this9.connection.touch(doc_id, expiry);
|
|
283
|
+
return yield that.get(doc_id);
|
|
284
|
+
})();
|
|
141
285
|
}
|
|
142
286
|
|
|
143
287
|
count(doc_id, adjustment, initial_value) {
|
|
144
|
-
var
|
|
145
|
-
return new Promise(
|
|
146
|
-
/*#__PURE__*/
|
|
147
|
-
function () {
|
|
148
|
-
var _ref6 = _asyncToGenerator(function* (resolve, reject) {
|
|
149
|
-
that.connection.counter(doc_id, adjustment, {
|
|
150
|
-
initial: initial_value
|
|
151
|
-
}, function (err, res) {
|
|
152
|
-
if (err) {
|
|
153
|
-
reject(err);
|
|
154
|
-
} else {
|
|
155
|
-
resolve(res.value);
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
});
|
|
288
|
+
var _this10 = this;
|
|
159
289
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
290
|
+
return _asyncToGenerator(function* () {
|
|
291
|
+
var res = yield _this10.connection.counter(doc_id, adjustment, {
|
|
292
|
+
initial: initial_value
|
|
293
|
+
});
|
|
294
|
+
return res.value;
|
|
295
|
+
})();
|
|
164
296
|
}
|
|
165
297
|
|
|
166
298
|
update(doc_id, document) {
|
|
167
|
-
var
|
|
168
|
-
return new Promise(
|
|
169
|
-
/*#__PURE__*/
|
|
170
|
-
function () {
|
|
171
|
-
var _ref7 = _asyncToGenerator(function* (resolve, reject) {
|
|
172
|
-
that.connection.replace(doc_id, document, function (err, res) {
|
|
173
|
-
if (err) {
|
|
174
|
-
reject(err);
|
|
175
|
-
} else {
|
|
176
|
-
resolve(res.value);
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
});
|
|
299
|
+
var _this11 = this;
|
|
180
300
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}()
|
|
301
|
+
return _asyncToGenerator(function* () {
|
|
302
|
+
var res = yield _this11.connection.replace(doc_id, document);
|
|
303
|
+
return res.value;
|
|
304
|
+
})();
|
|
185
305
|
}
|
|
186
306
|
|
|
187
307
|
delete(doc_id) {
|
|
188
|
-
var
|
|
189
|
-
return new Promise(
|
|
190
|
-
/*#__PURE__*/
|
|
191
|
-
function () {
|
|
192
|
-
var _ref8 = _asyncToGenerator(function* (resolve, reject) {
|
|
193
|
-
that.connection.remove(doc_id, function (err, res) {
|
|
194
|
-
if (err) {
|
|
195
|
-
reject(err);
|
|
196
|
-
} else {
|
|
197
|
-
resolve(res.value);
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
});
|
|
308
|
+
var _this12 = this;
|
|
201
309
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}()
|
|
310
|
+
return _asyncToGenerator(function* () {
|
|
311
|
+
var res = yield _this12.connection.remove(doc_id);
|
|
312
|
+
return res.value;
|
|
313
|
+
})();
|
|
206
314
|
}
|
|
207
315
|
|
|
208
316
|
multi_get(keys, keep_type) {
|
|
209
|
-
var
|
|
317
|
+
var _this13 = this;
|
|
210
318
|
|
|
211
319
|
return _asyncToGenerator(function* () {
|
|
212
320
|
/* var that = this; */
|
|
@@ -218,9 +326,14 @@ module.exports = class Database {
|
|
|
218
326
|
|
|
219
327
|
|
|
220
328
|
if (_.isArray(keys) && keys.length > 0) {
|
|
221
|
-
var functions = _.map(keys, key =>
|
|
329
|
+
var functions = _.map(keys, key => _this13.connection.get(key));
|
|
330
|
+
|
|
331
|
+
var returned_all = yield Promise.allSettled(functions);
|
|
332
|
+
|
|
333
|
+
var responses = _.flatten(_.compact(_.map(returned_all, returned_obj => {
|
|
334
|
+
if (returned_obj.status == "fulfilled") return returned_obj.value;
|
|
335
|
+
})));
|
|
222
336
|
|
|
223
|
-
var responses = yield Promise.all(functions);
|
|
224
337
|
return _.map(responses, (res, index) => {
|
|
225
338
|
if (!keep_type) {
|
|
226
339
|
delete res.value.type;
|
|
@@ -229,23 +342,6 @@ module.exports = class Database {
|
|
|
229
342
|
res.value.id = keys[index];
|
|
230
343
|
return res.value;
|
|
231
344
|
});
|
|
232
|
-
/* that.connection.getMulti(keys, function (err, res) {
|
|
233
|
-
if (res) {
|
|
234
|
-
let return_value = [];
|
|
235
|
-
for (var i in res) {
|
|
236
|
-
if (res[i].value) {
|
|
237
|
-
if (!keep_type) {
|
|
238
|
-
delete res[i].value['type']
|
|
239
|
-
}
|
|
240
|
-
res[i].value.id = i;
|
|
241
|
-
return_value.push(res[i].value);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
resolve(return_value);
|
|
245
|
-
} else if (err) {
|
|
246
|
-
reject(err);
|
|
247
|
-
}
|
|
248
|
-
}); */
|
|
249
345
|
} else {
|
|
250
346
|
return [];
|
|
251
347
|
}
|
|
@@ -257,7 +353,7 @@ module.exports = class Database {
|
|
|
257
353
|
return new Promise(
|
|
258
354
|
/*#__PURE__*/
|
|
259
355
|
function () {
|
|
260
|
-
var
|
|
356
|
+
var _ref3 = _asyncToGenerator(function* (resolve, reject) {
|
|
261
357
|
var ViewQuery = couchbase.ViewQuery;
|
|
262
358
|
var query = ViewQuery.from(design_doc, view);
|
|
263
359
|
|
|
@@ -284,7 +380,7 @@ module.exports = class Database {
|
|
|
284
380
|
that.connection.query(query,
|
|
285
381
|
/*#__PURE__*/
|
|
286
382
|
function () {
|
|
287
|
-
var
|
|
383
|
+
var _ref4 = _asyncToGenerator(function* (err, results, matrix) {
|
|
288
384
|
var return_value = [];
|
|
289
385
|
|
|
290
386
|
if (err) {
|
|
@@ -307,100 +403,112 @@ module.exports = class Database {
|
|
|
307
403
|
}
|
|
308
404
|
});
|
|
309
405
|
|
|
310
|
-
return function (
|
|
311
|
-
return
|
|
406
|
+
return function (_x7, _x8, _x9) {
|
|
407
|
+
return _ref4.apply(this, arguments);
|
|
312
408
|
};
|
|
313
409
|
}());
|
|
314
410
|
});
|
|
315
411
|
|
|
316
|
-
return function (
|
|
317
|
-
return
|
|
412
|
+
return function (_x5, _x6) {
|
|
413
|
+
return _ref3.apply(this, arguments);
|
|
318
414
|
};
|
|
319
415
|
}());
|
|
320
416
|
}
|
|
321
417
|
|
|
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
|
-
}
|
|
418
|
+
search(type, columns, query, limit, offset, order_by, do_count, query_scope) {
|
|
419
|
+
var _this14 = this;
|
|
332
420
|
|
|
333
|
-
|
|
334
|
-
|
|
421
|
+
return _asyncToGenerator(function* () {
|
|
422
|
+
var use_bucket_name = !(spice.config.database.connections[_this14.connection_name].scope && spice.config.database.connections[_this14.connection_name].collection);
|
|
423
|
+
var is_legacy = _this14.scope_name !== "_default" && _this14.collection_name !== "_default";
|
|
424
|
+
var alias = "_reference";
|
|
425
|
+
var bucket = "`".concat(use_bucket_name ? _this14.bucket : _this14.collection_name, "`");
|
|
335
426
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
427
|
+
try {
|
|
428
|
+
//let uses_scopes = this.scope this.scope_name !== "_default";
|
|
429
|
+
if (do_count == undefined) {
|
|
430
|
+
do_count = "false";
|
|
431
|
+
}
|
|
339
432
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
}
|
|
433
|
+
var fix = "";
|
|
434
|
+
var count_query = "";
|
|
343
435
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
}
|
|
436
|
+
if (!limit) {
|
|
437
|
+
limit = 0;
|
|
438
|
+
}
|
|
348
439
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}
|
|
440
|
+
if (!offset) {
|
|
441
|
+
offset = 0;
|
|
442
|
+
}
|
|
353
443
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
444
|
+
if (columns == "*") {
|
|
445
|
+
columns = "(" + bucket + ").*";
|
|
446
|
+
fix = ", META(" + bucket + ").id AS id";
|
|
447
|
+
}
|
|
358
448
|
|
|
359
|
-
|
|
360
|
-
|
|
449
|
+
if (!columns) {
|
|
450
|
+
columns = "(" + bucket + ").*";
|
|
451
|
+
fix = ", META(" + bucket + ").id AS id";
|
|
452
|
+
}
|
|
361
453
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
query = front_query;
|
|
367
|
-
}
|
|
454
|
+
if (columns == "id") {
|
|
455
|
+
fix = ", META(" + bucket + ").id AS id";
|
|
456
|
+
columns = "";
|
|
457
|
+
}
|
|
368
458
|
|
|
369
|
-
|
|
370
|
-
query = "".concat(query, " ORDER BY ").concat(order_by);
|
|
371
|
-
}
|
|
459
|
+
var front_query = "SELECT ".concat(columns, " ").concat(fix, " FROM ").concat(bucket);
|
|
372
460
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
461
|
+
if (is_legacy || query) {
|
|
462
|
+
front_query += " WHERE ";
|
|
463
|
+
}
|
|
376
464
|
|
|
377
|
-
|
|
378
|
-
query = query + " OFFSET " + offset;
|
|
379
|
-
}
|
|
465
|
+
var conditions = [];
|
|
380
466
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
467
|
+
if (use_bucket_name) {
|
|
468
|
+
conditions.push("type = '".concat(type, "'"));
|
|
469
|
+
}
|
|
384
470
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
471
|
+
if (query) {
|
|
472
|
+
conditions.push(query);
|
|
473
|
+
}
|
|
388
474
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
} catch (error) {
|
|
395
|
-
console.log(error.stack);
|
|
396
|
-
reject(error);
|
|
475
|
+
query = front_query + conditions.join(" AND ");
|
|
476
|
+
count_query = "SELECT count(meta().id) as count FROM ".concat(bucket);
|
|
477
|
+
|
|
478
|
+
if (use_bucket_name || query) {
|
|
479
|
+
count_query += " WHERE ";
|
|
397
480
|
}
|
|
398
|
-
});
|
|
399
481
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
482
|
+
query = front_query + conditions.join(" AND ");
|
|
483
|
+
|
|
484
|
+
if (order_by) {
|
|
485
|
+
query = "".concat(query, " ORDER BY ").concat(order_by);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (limit > 0) {
|
|
489
|
+
query = query + " LIMIT " + limit;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (offset > 0) {
|
|
493
|
+
query = query + " OFFSET " + offset;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
var active_query_space = query_scope || !use_bucket_name ? "scope" : "cluster";
|
|
497
|
+
var queries = [_this14.getData(query, active_query_space)];
|
|
498
|
+
|
|
499
|
+
if (do_count === "true") {
|
|
500
|
+
queries.push(_this14.getCount(count_query, active_query_space));
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
var responses = yield Promise.all(queries);
|
|
504
|
+
return {
|
|
505
|
+
data: responses[0],
|
|
506
|
+
total: responses.length > 1 ? responses[1] : null
|
|
507
|
+
};
|
|
508
|
+
} catch (error) {
|
|
509
|
+
throw error;
|
|
510
|
+
}
|
|
511
|
+
})();
|
|
404
512
|
}
|
|
405
513
|
|
|
406
514
|
full_text_search(type, search_query, limit) {
|
|
@@ -410,7 +518,7 @@ module.exports = class Database {
|
|
|
410
518
|
return new Promise(
|
|
411
519
|
/*#__PURE__*/
|
|
412
520
|
function () {
|
|
413
|
-
var
|
|
521
|
+
var _ref5 = _asyncToGenerator(function* (resolve, reject) {
|
|
414
522
|
try {
|
|
415
523
|
var full_query = search_query + " AND type:" + type;
|
|
416
524
|
var query = that.SearchQuery.new(that.full_text_index || full_text_index, that.SearchQuery.queryString(full_query));
|
|
@@ -431,7 +539,7 @@ module.exports = class Database {
|
|
|
431
539
|
that.cluster.query(query,
|
|
432
540
|
/*#__PURE__*/
|
|
433
541
|
function () {
|
|
434
|
-
var
|
|
542
|
+
var _ref6 = _asyncToGenerator(function* (err, results, matrix) {
|
|
435
543
|
var return_value = [];
|
|
436
544
|
|
|
437
545
|
if (err) {
|
|
@@ -457,8 +565,8 @@ module.exports = class Database {
|
|
|
457
565
|
}
|
|
458
566
|
});
|
|
459
567
|
|
|
460
|
-
return function (
|
|
461
|
-
return
|
|
568
|
+
return function (_x12, _x13, _x14) {
|
|
569
|
+
return _ref6.apply(this, arguments);
|
|
462
570
|
};
|
|
463
571
|
}());
|
|
464
572
|
} catch (e) {
|
|
@@ -466,35 +574,44 @@ module.exports = class Database {
|
|
|
466
574
|
}
|
|
467
575
|
});
|
|
468
576
|
|
|
469
|
-
return function (
|
|
470
|
-
return
|
|
577
|
+
return function (_x10, _x11) {
|
|
578
|
+
return _ref5.apply(this, arguments);
|
|
471
579
|
};
|
|
472
580
|
}());
|
|
473
581
|
}
|
|
474
582
|
|
|
475
583
|
getCount(count_query_element) {
|
|
476
|
-
var
|
|
584
|
+
var _this15 = this;
|
|
477
585
|
|
|
586
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
478
587
|
return _asyncToGenerator(function* () {
|
|
479
|
-
var response = yield
|
|
588
|
+
var response = yield _this15.query(count_query_element, query_scope);
|
|
480
589
|
return response.rows[0].count;
|
|
481
590
|
})();
|
|
482
591
|
}
|
|
483
592
|
|
|
484
593
|
getData(query_element) {
|
|
485
|
-
var
|
|
594
|
+
var _this16 = this;
|
|
486
595
|
|
|
596
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
487
597
|
return _asyncToGenerator(function* () {
|
|
488
|
-
var response = yield
|
|
598
|
+
var response = yield _this16.query(query_element, query_scope);
|
|
489
599
|
return response.rows;
|
|
490
600
|
})();
|
|
491
601
|
}
|
|
492
602
|
|
|
493
603
|
query(query) {
|
|
494
|
-
var
|
|
604
|
+
var _this17 = this;
|
|
495
605
|
|
|
606
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
496
607
|
return _asyncToGenerator(function* () {
|
|
497
|
-
|
|
608
|
+
var scope = query_scope || _this17.query_scope;
|
|
609
|
+
|
|
610
|
+
if (scope == "cluster") {
|
|
611
|
+
return yield _this17.cluster.query(query);
|
|
612
|
+
} else {
|
|
613
|
+
return yield _this17.scope.query(query);
|
|
614
|
+
}
|
|
498
615
|
})();
|
|
499
616
|
}
|
|
500
617
|
|