spice-couchbase 2.0.14 → 2.0.17
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 +332 -230
- package/package.json +5 -3
- package/src/index.js +348 -198
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");
|
|
14
|
+
|
|
15
|
+
var couchbase = require("couchbase");
|
|
8
16
|
|
|
9
|
-
var
|
|
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,211 @@ 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
|
-
var
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
+
}]
|
|
48
111
|
});
|
|
49
112
|
} catch (e) {
|
|
50
|
-
|
|
51
|
-
reject(e);
|
|
113
|
+
console.log(e);
|
|
52
114
|
}
|
|
53
|
-
});
|
|
115
|
+
})();
|
|
54
116
|
}
|
|
55
117
|
|
|
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 = {};
|
|
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
|
|
64
122
|
|
|
65
|
-
if (err) {
|
|
66
|
-
reject(err);
|
|
67
|
-
} else {
|
|
68
|
-
_.defaults(return_value, res.value);
|
|
69
123
|
|
|
70
|
-
|
|
71
|
-
|
|
124
|
+
wait(ms) {
|
|
125
|
+
return _asyncToGenerator(function* () {
|
|
126
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
127
|
+
})();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
upsertCollection() {
|
|
131
|
+
var _this3 = this;
|
|
132
|
+
|
|
133
|
+
return _asyncToGenerator(function* () {
|
|
134
|
+
try {
|
|
135
|
+
var collection_exists = yield _this3.collectionExists(_this3.collection_name, _this3.scope_name);
|
|
136
|
+
|
|
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
|
|
72
159
|
}
|
|
73
160
|
});
|
|
74
|
-
|
|
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
|
+
}
|
|
75
172
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
+
})();
|
|
80
239
|
}
|
|
81
240
|
|
|
82
241
|
insert(doc_id, document, expiry) {
|
|
@@ -84,13 +243,13 @@ module.exports = class Database {
|
|
|
84
243
|
return new Promise(
|
|
85
244
|
/*#__PURE__*/
|
|
86
245
|
function () {
|
|
87
|
-
var
|
|
246
|
+
var _ref = _asyncToGenerator(function* (resolve, reject) {
|
|
88
247
|
that.connection.insert(doc_id, document, {
|
|
89
248
|
expiry: expiry
|
|
90
249
|
},
|
|
91
250
|
/*#__PURE__*/
|
|
92
251
|
function () {
|
|
93
|
-
var
|
|
252
|
+
var _ref2 = _asyncToGenerator(function* (err, res) {
|
|
94
253
|
if (err) {
|
|
95
254
|
reject(err);
|
|
96
255
|
} else {
|
|
@@ -103,120 +262,59 @@ module.exports = class Database {
|
|
|
103
262
|
}
|
|
104
263
|
});
|
|
105
264
|
|
|
106
|
-
return function (
|
|
107
|
-
return
|
|
265
|
+
return function (_x3, _x4) {
|
|
266
|
+
return _ref2.apply(this, arguments);
|
|
108
267
|
};
|
|
109
268
|
}());
|
|
110
269
|
});
|
|
111
270
|
|
|
112
|
-
return function (
|
|
113
|
-
return
|
|
271
|
+
return function (_x, _x2) {
|
|
272
|
+
return _ref.apply(this, arguments);
|
|
114
273
|
};
|
|
115
274
|
}());
|
|
116
275
|
}
|
|
117
276
|
|
|
118
277
|
touch(doc_id) {
|
|
119
|
-
var
|
|
120
|
-
var that = this;
|
|
121
|
-
return new Promise(
|
|
122
|
-
/*#__PURE__*/
|
|
123
|
-
function () {
|
|
124
|
-
var _ref4 = _asyncToGenerator(function* (resolve, reject) {
|
|
125
|
-
that.connection.touch(doc_id, expiry,
|
|
126
|
-
/*#__PURE__*/
|
|
127
|
-
function () {
|
|
128
|
-
var _ref5 = _asyncToGenerator(function* (err, res) {
|
|
129
|
-
if (err) {
|
|
130
|
-
reject(err);
|
|
131
|
-
} else {
|
|
132
|
-
try {
|
|
133
|
-
var created = yield that.get(doc_id);
|
|
134
|
-
resolve(created);
|
|
135
|
-
} catch (e) {
|
|
136
|
-
reject(e);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
});
|
|
278
|
+
var _this9 = this;
|
|
140
279
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return function (_x7, _x8) {
|
|
148
|
-
return _ref4.apply(this, arguments);
|
|
149
|
-
};
|
|
150
|
-
}());
|
|
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
|
+
})();
|
|
151
285
|
}
|
|
152
286
|
|
|
153
287
|
count(doc_id, adjustment, initial_value) {
|
|
154
|
-
var
|
|
155
|
-
return new Promise(
|
|
156
|
-
/*#__PURE__*/
|
|
157
|
-
function () {
|
|
158
|
-
var _ref6 = _asyncToGenerator(function* (resolve, reject) {
|
|
159
|
-
that.connection.counter(doc_id, adjustment, {
|
|
160
|
-
initial: initial_value
|
|
161
|
-
}, function (err, res) {
|
|
162
|
-
if (err) {
|
|
163
|
-
reject(err);
|
|
164
|
-
} else {
|
|
165
|
-
resolve(res.value);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
});
|
|
288
|
+
var _this10 = this;
|
|
169
289
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
290
|
+
return _asyncToGenerator(function* () {
|
|
291
|
+
var res = yield _this10.connection.counter(doc_id, adjustment, {
|
|
292
|
+
initial: initial_value
|
|
293
|
+
});
|
|
294
|
+
return res.value;
|
|
295
|
+
})();
|
|
174
296
|
}
|
|
175
297
|
|
|
176
298
|
update(doc_id, document) {
|
|
177
|
-
var
|
|
178
|
-
return new Promise(
|
|
179
|
-
/*#__PURE__*/
|
|
180
|
-
function () {
|
|
181
|
-
var _ref7 = _asyncToGenerator(function* (resolve, reject) {
|
|
182
|
-
that.connection.replace(doc_id, document, function (err, res) {
|
|
183
|
-
if (err) {
|
|
184
|
-
reject(err);
|
|
185
|
-
} else {
|
|
186
|
-
resolve(res.value);
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
});
|
|
299
|
+
var _this11 = this;
|
|
190
300
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}()
|
|
301
|
+
return _asyncToGenerator(function* () {
|
|
302
|
+
var res = yield _this11.connection.replace(doc_id, document);
|
|
303
|
+
return res.value;
|
|
304
|
+
})();
|
|
195
305
|
}
|
|
196
306
|
|
|
197
307
|
delete(doc_id) {
|
|
198
|
-
var
|
|
199
|
-
return new Promise(
|
|
200
|
-
/*#__PURE__*/
|
|
201
|
-
function () {
|
|
202
|
-
var _ref8 = _asyncToGenerator(function* (resolve, reject) {
|
|
203
|
-
that.connection.remove(doc_id, function (err, res) {
|
|
204
|
-
if (err) {
|
|
205
|
-
reject(err);
|
|
206
|
-
} else {
|
|
207
|
-
resolve(res.value);
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
});
|
|
308
|
+
var _this12 = this;
|
|
211
309
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}()
|
|
310
|
+
return _asyncToGenerator(function* () {
|
|
311
|
+
var res = yield _this12.connection.remove(doc_id);
|
|
312
|
+
return res.value;
|
|
313
|
+
})();
|
|
216
314
|
}
|
|
217
315
|
|
|
218
316
|
multi_get(keys, keep_type) {
|
|
219
|
-
var
|
|
317
|
+
var _this13 = this;
|
|
220
318
|
|
|
221
319
|
return _asyncToGenerator(function* () {
|
|
222
320
|
/* var that = this; */
|
|
@@ -228,7 +326,7 @@ module.exports = class Database {
|
|
|
228
326
|
|
|
229
327
|
|
|
230
328
|
if (_.isArray(keys) && keys.length > 0) {
|
|
231
|
-
var functions = _.map(keys, key =>
|
|
329
|
+
var functions = _.map(keys, key => _this13.connection.get(key));
|
|
232
330
|
|
|
233
331
|
var returned_all = yield Promise.allSettled(functions);
|
|
234
332
|
|
|
@@ -244,23 +342,6 @@ module.exports = class Database {
|
|
|
244
342
|
res.value.id = keys[index];
|
|
245
343
|
return res.value;
|
|
246
344
|
});
|
|
247
|
-
/* that.connection.getMulti(keys, function (err, res) {
|
|
248
|
-
if (res) {
|
|
249
|
-
let return_value = [];
|
|
250
|
-
for (var i in res) {
|
|
251
|
-
if (res[i].value) {
|
|
252
|
-
if (!keep_type) {
|
|
253
|
-
delete res[i].value['type']
|
|
254
|
-
}
|
|
255
|
-
res[i].value.id = i;
|
|
256
|
-
return_value.push(res[i].value);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
resolve(return_value);
|
|
260
|
-
} else if (err) {
|
|
261
|
-
reject(err);
|
|
262
|
-
}
|
|
263
|
-
}); */
|
|
264
345
|
} else {
|
|
265
346
|
return [];
|
|
266
347
|
}
|
|
@@ -272,7 +353,7 @@ module.exports = class Database {
|
|
|
272
353
|
return new Promise(
|
|
273
354
|
/*#__PURE__*/
|
|
274
355
|
function () {
|
|
275
|
-
var
|
|
356
|
+
var _ref3 = _asyncToGenerator(function* (resolve, reject) {
|
|
276
357
|
var ViewQuery = couchbase.ViewQuery;
|
|
277
358
|
var query = ViewQuery.from(design_doc, view);
|
|
278
359
|
|
|
@@ -299,7 +380,7 @@ module.exports = class Database {
|
|
|
299
380
|
that.connection.query(query,
|
|
300
381
|
/*#__PURE__*/
|
|
301
382
|
function () {
|
|
302
|
-
var
|
|
383
|
+
var _ref4 = _asyncToGenerator(function* (err, results, matrix) {
|
|
303
384
|
var return_value = [];
|
|
304
385
|
|
|
305
386
|
if (err) {
|
|
@@ -322,100 +403,112 @@ module.exports = class Database {
|
|
|
322
403
|
}
|
|
323
404
|
});
|
|
324
405
|
|
|
325
|
-
return function (
|
|
326
|
-
return
|
|
406
|
+
return function (_x7, _x8, _x9) {
|
|
407
|
+
return _ref4.apply(this, arguments);
|
|
327
408
|
};
|
|
328
409
|
}());
|
|
329
410
|
});
|
|
330
411
|
|
|
331
|
-
return function (
|
|
332
|
-
return
|
|
412
|
+
return function (_x5, _x6) {
|
|
413
|
+
return _ref3.apply(this, arguments);
|
|
333
414
|
};
|
|
334
415
|
}());
|
|
335
416
|
}
|
|
336
417
|
|
|
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
|
-
}
|
|
418
|
+
search(type, columns, query, limit, offset, order_by, do_count, query_scope) {
|
|
419
|
+
var _this14 = this;
|
|
347
420
|
|
|
348
|
-
|
|
349
|
-
|
|
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, "`");
|
|
350
426
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
427
|
+
try {
|
|
428
|
+
//let uses_scopes = this.scope this.scope_name !== "_default";
|
|
429
|
+
if (do_count == undefined) {
|
|
430
|
+
do_count = "false";
|
|
431
|
+
}
|
|
354
432
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
}
|
|
433
|
+
var fix = "";
|
|
434
|
+
var count_query = "";
|
|
358
435
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}
|
|
436
|
+
if (!limit) {
|
|
437
|
+
limit = 0;
|
|
438
|
+
}
|
|
363
439
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
}
|
|
440
|
+
if (!offset) {
|
|
441
|
+
offset = 0;
|
|
442
|
+
}
|
|
368
443
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
444
|
+
if (columns == "*") {
|
|
445
|
+
columns = "(" + bucket + ").*";
|
|
446
|
+
fix = ", META(" + bucket + ").id AS id";
|
|
447
|
+
}
|
|
373
448
|
|
|
374
|
-
|
|
375
|
-
|
|
449
|
+
if (!columns) {
|
|
450
|
+
columns = "(" + bucket + ").*";
|
|
451
|
+
fix = ", META(" + bucket + ").id AS id";
|
|
452
|
+
}
|
|
376
453
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
query = front_query;
|
|
382
|
-
}
|
|
454
|
+
if (columns == "id") {
|
|
455
|
+
fix = ", META(" + bucket + ").id AS id";
|
|
456
|
+
columns = "";
|
|
457
|
+
}
|
|
383
458
|
|
|
384
|
-
|
|
385
|
-
query = "".concat(query, " ORDER BY ").concat(order_by);
|
|
386
|
-
}
|
|
459
|
+
var front_query = "SELECT ".concat(columns, " ").concat(fix, " FROM ").concat(bucket);
|
|
387
460
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
461
|
+
if (is_legacy || query) {
|
|
462
|
+
front_query += " WHERE ";
|
|
463
|
+
}
|
|
391
464
|
|
|
392
|
-
|
|
393
|
-
query = query + " OFFSET " + offset;
|
|
394
|
-
}
|
|
465
|
+
var conditions = [];
|
|
395
466
|
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
467
|
+
if (use_bucket_name) {
|
|
468
|
+
conditions.push("type = '".concat(type, "'"));
|
|
469
|
+
}
|
|
399
470
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
471
|
+
if (query) {
|
|
472
|
+
conditions.push(query);
|
|
473
|
+
}
|
|
403
474
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
} catch (error) {
|
|
410
|
-
console.log(error.stack);
|
|
411
|
-
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 ";
|
|
412
480
|
}
|
|
413
|
-
});
|
|
414
481
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
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
|
+
})();
|
|
419
512
|
}
|
|
420
513
|
|
|
421
514
|
full_text_search(type, search_query, limit) {
|
|
@@ -425,7 +518,7 @@ module.exports = class Database {
|
|
|
425
518
|
return new Promise(
|
|
426
519
|
/*#__PURE__*/
|
|
427
520
|
function () {
|
|
428
|
-
var
|
|
521
|
+
var _ref5 = _asyncToGenerator(function* (resolve, reject) {
|
|
429
522
|
try {
|
|
430
523
|
var full_query = search_query + " AND type:" + type;
|
|
431
524
|
var query = that.SearchQuery.new(that.full_text_index || full_text_index, that.SearchQuery.queryString(full_query));
|
|
@@ -446,7 +539,7 @@ module.exports = class Database {
|
|
|
446
539
|
that.cluster.query(query,
|
|
447
540
|
/*#__PURE__*/
|
|
448
541
|
function () {
|
|
449
|
-
var
|
|
542
|
+
var _ref6 = _asyncToGenerator(function* (err, results, matrix) {
|
|
450
543
|
var return_value = [];
|
|
451
544
|
|
|
452
545
|
if (err) {
|
|
@@ -472,8 +565,8 @@ module.exports = class Database {
|
|
|
472
565
|
}
|
|
473
566
|
});
|
|
474
567
|
|
|
475
|
-
return function (
|
|
476
|
-
return
|
|
568
|
+
return function (_x12, _x13, _x14) {
|
|
569
|
+
return _ref6.apply(this, arguments);
|
|
477
570
|
};
|
|
478
571
|
}());
|
|
479
572
|
} catch (e) {
|
|
@@ -481,35 +574,44 @@ module.exports = class Database {
|
|
|
481
574
|
}
|
|
482
575
|
});
|
|
483
576
|
|
|
484
|
-
return function (
|
|
485
|
-
return
|
|
577
|
+
return function (_x10, _x11) {
|
|
578
|
+
return _ref5.apply(this, arguments);
|
|
486
579
|
};
|
|
487
580
|
}());
|
|
488
581
|
}
|
|
489
582
|
|
|
490
583
|
getCount(count_query_element) {
|
|
491
|
-
var
|
|
584
|
+
var _this15 = this;
|
|
492
585
|
|
|
586
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
493
587
|
return _asyncToGenerator(function* () {
|
|
494
|
-
var response = yield
|
|
588
|
+
var response = yield _this15.query(count_query_element, query_scope);
|
|
495
589
|
return response.rows[0].count;
|
|
496
590
|
})();
|
|
497
591
|
}
|
|
498
592
|
|
|
499
593
|
getData(query_element) {
|
|
500
|
-
var
|
|
594
|
+
var _this16 = this;
|
|
501
595
|
|
|
596
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
502
597
|
return _asyncToGenerator(function* () {
|
|
503
|
-
var response = yield
|
|
598
|
+
var response = yield _this16.query(query_element, query_scope);
|
|
504
599
|
return response.rows;
|
|
505
600
|
})();
|
|
506
601
|
}
|
|
507
602
|
|
|
508
603
|
query(query) {
|
|
509
|
-
var
|
|
604
|
+
var _this17 = this;
|
|
510
605
|
|
|
606
|
+
var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
|
|
511
607
|
return _asyncToGenerator(function* () {
|
|
512
|
-
|
|
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
|
+
}
|
|
513
615
|
})();
|
|
514
616
|
}
|
|
515
617
|
|