spice-couchbase 2.0.20 → 2.0.22
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 +36 -8
- package/package.json +1 -1
- package/src/index.js +60 -75
package/build/index.js
CHANGED
|
@@ -77,6 +77,27 @@ module.exports = class Database {
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
setGlobalConnectionObject(connection) {
|
|
81
|
+
if (spice.checked_collections == undefined) {
|
|
82
|
+
spice.checked_collections = {};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
_.set(spice.checked_collections, connection, true);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
checkGlobelConnectionObject(connection) {
|
|
89
|
+
if (spice.checked_collections == undefined) {
|
|
90
|
+
spice.checked_collections = {};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (_.get(spice.checked_collections, connection)) {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
this.setGlobalConnectionObject(connection);
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
80
101
|
connect(connection_name, config) {
|
|
81
102
|
var _this = this;
|
|
82
103
|
|
|
@@ -147,10 +168,12 @@ module.exports = class Database {
|
|
|
147
168
|
try {
|
|
148
169
|
var collection_exists = yield _this3.collectionExists(_this3.collection_name, _this3.scope_name);
|
|
149
170
|
|
|
150
|
-
if (!
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
171
|
+
if (!_this3.checkGlobelConnectionObject(_this3.collection_name)) {
|
|
172
|
+
if (!collection_exists) {
|
|
173
|
+
yield _this3.createCollection(_this3.collection_name, _this3.scope_name);
|
|
174
|
+
yield _this3.wait(2000);
|
|
175
|
+
yield _this3.createPrimaryIndex(_this3.scope_name, _this3.collection_name);
|
|
176
|
+
}
|
|
154
177
|
}
|
|
155
178
|
} catch (e) {
|
|
156
179
|
console.log(e);
|
|
@@ -255,10 +278,15 @@ module.exports = class Database {
|
|
|
255
278
|
var _this9 = this;
|
|
256
279
|
|
|
257
280
|
return _asyncToGenerator(function* () {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
281
|
+
try {
|
|
282
|
+
yield (_this9.use_bucket_name ? _this9.connection : _this9.collection).insert(doc_id, document, {
|
|
283
|
+
expiry: expiry
|
|
284
|
+
});
|
|
285
|
+
} catch (e) {
|
|
286
|
+
console.log(e.message, e, doc_id);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
console.log("Return from Insert", doc_id);
|
|
262
290
|
return yield _this9.get(doc_id);
|
|
263
291
|
})();
|
|
264
292
|
}
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -22,6 +22,7 @@ if (!Promise.allSettled) {
|
|
|
22
22
|
)
|
|
23
23
|
);
|
|
24
24
|
}
|
|
25
|
+
|
|
25
26
|
module.exports = class Database {
|
|
26
27
|
constructor(
|
|
27
28
|
connection,
|
|
@@ -85,6 +86,24 @@ module.exports = class Database {
|
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
|
|
89
|
+
setGlobalConnectionObject(connection) {
|
|
90
|
+
if (spice.checked_collections == undefined) {
|
|
91
|
+
spice.checked_collections = {};
|
|
92
|
+
}
|
|
93
|
+
_.set(spice.checked_collections, connection, true);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
checkGlobelConnectionObject(connection) {
|
|
97
|
+
if (spice.checked_collections == undefined) {
|
|
98
|
+
spice.checked_collections = {};
|
|
99
|
+
}
|
|
100
|
+
if (_.get(spice.checked_collections, connection)) {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
this.setGlobalConnectionObject(connection);
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
|
|
88
107
|
async connect(connection_name, config) {
|
|
89
108
|
let cluster = await couchbase.connect(config.nodes, {
|
|
90
109
|
username: config.username,
|
|
@@ -153,10 +172,12 @@ module.exports = class Database {
|
|
|
153
172
|
this.collection_name,
|
|
154
173
|
this.scope_name
|
|
155
174
|
);
|
|
156
|
-
if (!
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
175
|
+
if (!this.checkGlobelConnectionObject(this.collection_name)) {
|
|
176
|
+
if (!collection_exists) {
|
|
177
|
+
await this.createCollection(this.collection_name, this.scope_name);
|
|
178
|
+
await this.wait(2000);
|
|
179
|
+
await this.createPrimaryIndex(this.scope_name, this.collection_name);
|
|
180
|
+
}
|
|
160
181
|
}
|
|
161
182
|
} catch (e) {
|
|
162
183
|
console.log(e);
|
|
@@ -245,19 +266,18 @@ module.exports = class Database {
|
|
|
245
266
|
}
|
|
246
267
|
|
|
247
268
|
async insert(doc_id, document, expiry) {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
);
|
|
269
|
+
try {
|
|
270
|
+
await (this.use_bucket_name ? this.connection : this.collection).insert(
|
|
271
|
+
doc_id,
|
|
272
|
+
document,
|
|
273
|
+
{
|
|
274
|
+
expiry: expiry,
|
|
275
|
+
}
|
|
276
|
+
);
|
|
277
|
+
} catch (e) {
|
|
278
|
+
console.log(e.message, e, doc_id);
|
|
279
|
+
}
|
|
280
|
+
console.log("Return from Insert", doc_id);
|
|
261
281
|
return await this.get(doc_id);
|
|
262
282
|
}
|
|
263
283
|
|
|
@@ -382,57 +402,32 @@ module.exports = class Database {
|
|
|
382
402
|
type,
|
|
383
403
|
columns,
|
|
384
404
|
query,
|
|
385
|
-
limit,
|
|
386
|
-
offset,
|
|
405
|
+
limit = 0,
|
|
406
|
+
offset = 0,
|
|
387
407
|
order_by,
|
|
388
|
-
do_count,
|
|
408
|
+
do_count = "false",
|
|
389
409
|
query_scope
|
|
390
410
|
) {
|
|
391
|
-
|
|
411
|
+
const is_legacy =
|
|
392
412
|
this.scope_name !== "_default" && this.collection_name !== "_default";
|
|
393
|
-
|
|
394
|
-
let bucket = `\`${
|
|
413
|
+
const bucket = `\`${
|
|
395
414
|
this.use_bucket_name ? this.bucket : this.collection_name
|
|
396
415
|
}\``;
|
|
397
416
|
|
|
398
417
|
try {
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
if (do_count == undefined) {
|
|
402
|
-
do_count = "false";
|
|
403
|
-
}
|
|
404
|
-
var fix = "";
|
|
405
|
-
var count_query = "";
|
|
406
|
-
if (!limit) {
|
|
407
|
-
limit = 0;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
if (!offset) {
|
|
411
|
-
offset = 0;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
if (columns == "*") {
|
|
415
|
-
columns = "(" + bucket + ").*";
|
|
416
|
-
fix = ", META(" + bucket + ").id AS id";
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
if (!columns) {
|
|
420
|
-
columns = "(" + bucket + ").*";
|
|
421
|
-
fix = ", META(" + bucket + ").id AS id";
|
|
422
|
-
}
|
|
418
|
+
const fix = `, META(${bucket}).id AS id`;
|
|
423
419
|
|
|
424
|
-
if (columns
|
|
425
|
-
|
|
420
|
+
if (columns === "*" || !columns) {
|
|
421
|
+
columns = `(${bucket}).*`;
|
|
422
|
+
} else if (columns !== "id") {
|
|
423
|
+
columns = `${columns}${fix}`;
|
|
424
|
+
} else {
|
|
426
425
|
columns = "";
|
|
427
426
|
}
|
|
428
427
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
if (is_legacy || query) {
|
|
432
|
-
front_query += " WHERE ";
|
|
433
|
-
}
|
|
428
|
+
const front_query = `SELECT ${columns} ${fix} FROM ${bucket}`;
|
|
429
|
+
const conditions = [];
|
|
434
430
|
|
|
435
|
-
let conditions = [];
|
|
436
431
|
if (this.use_bucket_name) {
|
|
437
432
|
conditions.push(`type = '${type}'`);
|
|
438
433
|
}
|
|
@@ -440,34 +435,24 @@ module.exports = class Database {
|
|
|
440
435
|
conditions.push(query);
|
|
441
436
|
}
|
|
442
437
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
count_query = `SELECT count(meta().id) as count FROM ${bucket}`;
|
|
446
|
-
if (this.use_bucket_name || query) {
|
|
447
|
-
count_query += " WHERE ";
|
|
448
|
-
}
|
|
449
|
-
query = front_query + conditions.join(" AND ");
|
|
450
|
-
|
|
451
|
-
if (order_by) {
|
|
452
|
-
query = `${query} ORDER BY ${order_by}`;
|
|
453
|
-
}
|
|
438
|
+
const where_clause = conditions.length ? " WHERE " : "";
|
|
439
|
+
query = front_query + where_clause + conditions.join(" AND ");
|
|
454
440
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
}
|
|
441
|
+
const count_query = `SELECT count(meta().id) as count FROM ${bucket}${where_clause}`;
|
|
442
|
+
const order_by_clause = order_by ? ` ORDER BY ${order_by}` : "";
|
|
443
|
+
const limit_clause = limit > 0 ? ` LIMIT ${limit}` : "";
|
|
444
|
+
const offset_clause = offset > 0 ? ` OFFSET ${offset}` : "";
|
|
458
445
|
|
|
459
|
-
|
|
460
|
-
query = query + " OFFSET " + offset;
|
|
461
|
-
}
|
|
446
|
+
query = query + order_by_clause + limit_clause + offset_clause;
|
|
462
447
|
|
|
463
|
-
|
|
448
|
+
const active_query_space =
|
|
464
449
|
query_scope || !this.use_bucket_name ? "scope" : "cluster";
|
|
465
450
|
|
|
466
|
-
|
|
451
|
+
const queries = [this.getData(query, active_query_space)];
|
|
467
452
|
if (do_count === "true") {
|
|
468
453
|
queries.push(this.getCount(count_query, active_query_space));
|
|
469
454
|
}
|
|
470
|
-
|
|
455
|
+
const responses = await Promise.all(queries);
|
|
471
456
|
|
|
472
457
|
return {
|
|
473
458
|
data: responses[0],
|