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 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 (!collection_exists) {
151
- yield _this3.createCollection(_this3.collection_name, _this3.scope_name);
152
- yield _this3.wait(2000);
153
- yield _this3.createPrimaryIndex(_this3.scope_name, _this3.collection_name);
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
- console.log("insert", _this9.use_bucket_name, _this9.connection._name, _this9.collection._name);
259
- yield (_this9.use_bucket_name ? _this9.connection : _this9.collection).insert(doc_id, document, {
260
- expiry: expiry
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-couchbase",
3
- "version": "2.0.20",
3
+ "version": "2.0.22",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
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 (!collection_exists) {
157
- await this.createCollection(this.collection_name, this.scope_name);
158
- await this.wait(2000);
159
- await this.createPrimaryIndex(this.scope_name, this.collection_name);
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
- console.log(
249
- "insert",
250
- this.use_bucket_name,
251
- this.connection._name,
252
- this.collection._name
253
- );
254
- await (this.use_bucket_name ? this.connection : this.collection).insert(
255
- doc_id,
256
- document,
257
- {
258
- expiry: expiry,
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
- let is_legacy =
411
+ const is_legacy =
392
412
  this.scope_name !== "_default" && this.collection_name !== "_default";
393
- let alias = "_reference";
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
- //let uses_scopes = this.scope this.scope_name !== "_default";
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 == "id") {
425
- fix = ", META(" + bucket + ").id AS id";
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
- var front_query = `SELECT ${columns} ${fix} FROM ${bucket}`;
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
- query = front_query + conditions.join(" AND ");
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
- if (limit > 0) {
456
- query = query + " LIMIT " + limit;
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
- if (offset > 0) {
460
- query = query + " OFFSET " + offset;
461
- }
446
+ query = query + order_by_clause + limit_clause + offset_clause;
462
447
 
463
- let active_query_space =
448
+ const active_query_space =
464
449
  query_scope || !this.use_bucket_name ? "scope" : "cluster";
465
450
 
466
- let queries = [this.getData(query, active_query_space)];
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
- let responses = await Promise.all(queries);
455
+ const responses = await Promise.all(queries);
471
456
 
472
457
  return {
473
458
  data: responses[0],