spice-couchbase 2.0.15 → 2.0.18

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.
Files changed (3) hide show
  1. package/build/index.js +61 -142
  2. package/package.json +1 -1
  3. package/src/index.js +62 -117
package/build/index.js CHANGED
@@ -100,7 +100,6 @@ module.exports = class Database {
100
100
  var allUsers = yield _this2.userManager.getAllUsers({
101
101
  timeout: 6000
102
102
  });
103
- console.log("UserName::::", _this2.db_config.username, allUsers);
104
103
  var update_user_response = yield _this2.userManager.upsertUser(_this2.db_config.username, {
105
104
  roles: [{
106
105
  name: "scope_admin",
@@ -110,7 +109,6 @@ module.exports = class Database {
110
109
  bucket: _this2.db_config.bucket
111
110
  }]
112
111
  });
113
- console.log("UPdate Response", update_user_response);
114
112
  } catch (e) {
115
113
  console.log(e);
116
114
  }
@@ -231,7 +229,6 @@ module.exports = class Database {
231
229
  return _asyncToGenerator(function* () {
232
230
  try {
233
231
  var response = yield _this8.connection.get(doc_id);
234
- console.log("Response ", response);
235
232
  return _objectSpread({
236
233
  id: doc_id
237
234
  }, response.content);
@@ -278,107 +275,46 @@ module.exports = class Database {
278
275
  }
279
276
 
280
277
  touch(doc_id) {
281
- var expiry = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
282
- var that = this;
283
- return new Promise(
284
- /*#__PURE__*/
285
- function () {
286
- var _ref3 = _asyncToGenerator(function* (resolve, reject) {
287
- that.connection.touch(doc_id, expiry,
288
- /*#__PURE__*/
289
- function () {
290
- var _ref4 = _asyncToGenerator(function* (err, res) {
291
- if (err) {
292
- reject(err);
293
- } else {
294
- try {
295
- var created = yield that.get(doc_id);
296
- resolve(created);
297
- } catch (e) {
298
- reject(e);
299
- }
300
- }
301
- });
302
-
303
- return function (_x7, _x8) {
304
- return _ref4.apply(this, arguments);
305
- };
306
- }());
307
- });
278
+ var _this9 = this;
308
279
 
309
- return function (_x5, _x6) {
310
- return _ref3.apply(this, arguments);
311
- };
312
- }());
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
+ })();
313
285
  }
314
286
 
315
287
  count(doc_id, adjustment, initial_value) {
316
- var that = this;
317
- return new Promise(
318
- /*#__PURE__*/
319
- function () {
320
- var _ref5 = _asyncToGenerator(function* (resolve, reject) {
321
- that.connection.counter(doc_id, adjustment, {
322
- initial: initial_value
323
- }, function (err, res) {
324
- if (err) {
325
- reject(err);
326
- } else {
327
- resolve(res.value);
328
- }
329
- });
330
- });
288
+ var _this10 = this;
331
289
 
332
- return function (_x9, _x10) {
333
- return _ref5.apply(this, arguments);
334
- };
335
- }());
290
+ return _asyncToGenerator(function* () {
291
+ var res = yield _this10.connection.counter(doc_id, adjustment, {
292
+ initial: initial_value
293
+ });
294
+ return res.value;
295
+ })();
336
296
  }
337
297
 
338
298
  update(doc_id, document) {
339
- var that = this;
340
- return new Promise(
341
- /*#__PURE__*/
342
- function () {
343
- var _ref6 = _asyncToGenerator(function* (resolve, reject) {
344
- that.connection.replace(doc_id, document, function (err, res) {
345
- if (err) {
346
- reject(err);
347
- } else {
348
- resolve(res.value);
349
- }
350
- });
351
- });
299
+ var _this11 = this;
352
300
 
353
- return function (_x11, _x12) {
354
- return _ref6.apply(this, arguments);
355
- };
356
- }());
301
+ return _asyncToGenerator(function* () {
302
+ var res = yield _this11.connection.replace(doc_id, document);
303
+ return res.value;
304
+ })();
357
305
  }
358
306
 
359
307
  delete(doc_id) {
360
- var that = this;
361
- return new Promise(
362
- /*#__PURE__*/
363
- function () {
364
- var _ref7 = _asyncToGenerator(function* (resolve, reject) {
365
- that.connection.remove(doc_id, function (err, res) {
366
- if (err) {
367
- reject(err);
368
- } else {
369
- resolve(res.value);
370
- }
371
- });
372
- });
308
+ var _this12 = this;
373
309
 
374
- return function (_x13, _x14) {
375
- return _ref7.apply(this, arguments);
376
- };
377
- }());
310
+ return _asyncToGenerator(function* () {
311
+ var res = yield _this12.connection.remove(doc_id);
312
+ return res.value;
313
+ })();
378
314
  }
379
315
 
380
316
  multi_get(keys, keep_type) {
381
- var _this9 = this;
317
+ var _this13 = this;
382
318
 
383
319
  return _asyncToGenerator(function* () {
384
320
  /* var that = this; */
@@ -390,7 +326,7 @@ module.exports = class Database {
390
326
 
391
327
 
392
328
  if (_.isArray(keys) && keys.length > 0) {
393
- var functions = _.map(keys, key => _this9.connection.get(key));
329
+ var functions = _.map(keys, key => _this13.connection.get(key));
394
330
 
395
331
  var returned_all = yield Promise.allSettled(functions);
396
332
 
@@ -406,23 +342,6 @@ module.exports = class Database {
406
342
  res.value.id = keys[index];
407
343
  return res.value;
408
344
  });
409
- /* that.connection.getMulti(keys, function (err, res) {
410
- if (res) {
411
- let return_value = [];
412
- for (var i in res) {
413
- if (res[i].value) {
414
- if (!keep_type) {
415
- delete res[i].value['type']
416
- }
417
- res[i].value.id = i;
418
- return_value.push(res[i].value);
419
- }
420
- }
421
- resolve(return_value);
422
- } else if (err) {
423
- reject(err);
424
- }
425
- }); */
426
345
  } else {
427
346
  return [];
428
347
  }
@@ -434,7 +353,7 @@ module.exports = class Database {
434
353
  return new Promise(
435
354
  /*#__PURE__*/
436
355
  function () {
437
- var _ref8 = _asyncToGenerator(function* (resolve, reject) {
356
+ var _ref3 = _asyncToGenerator(function* (resolve, reject) {
438
357
  var ViewQuery = couchbase.ViewQuery;
439
358
  var query = ViewQuery.from(design_doc, view);
440
359
 
@@ -461,7 +380,7 @@ module.exports = class Database {
461
380
  that.connection.query(query,
462
381
  /*#__PURE__*/
463
382
  function () {
464
- var _ref9 = _asyncToGenerator(function* (err, results, matrix) {
383
+ var _ref4 = _asyncToGenerator(function* (err, results, matrix) {
465
384
  var return_value = [];
466
385
 
467
386
  if (err) {
@@ -484,27 +403,29 @@ module.exports = class Database {
484
403
  }
485
404
  });
486
405
 
487
- return function (_x17, _x18, _x19) {
488
- return _ref9.apply(this, arguments);
406
+ return function (_x7, _x8, _x9) {
407
+ return _ref4.apply(this, arguments);
489
408
  };
490
409
  }());
491
410
  });
492
411
 
493
- return function (_x15, _x16) {
494
- return _ref8.apply(this, arguments);
412
+ return function (_x5, _x6) {
413
+ return _ref3.apply(this, arguments);
495
414
  };
496
415
  }());
497
416
  }
498
417
 
499
418
  search(type, columns, query, limit, offset, order_by, do_count, query_scope) {
500
- var _this10 = this;
419
+ var _this14 = this;
501
420
 
502
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";
503
424
  var alias = "_reference";
504
- var bucket = "`".concat(_this10.collection_name, "` AS ").concat(alias);
505
- var include_type = _this10.scope_name && _this10.collection_name;
425
+ var bucket = "`".concat(use_bucket_name ? _this14.bucket : _this14.collection_name, "`");
506
426
 
507
427
  try {
428
+ //let uses_scopes = this.scope this.scope_name !== "_default";
508
429
  if (do_count == undefined) {
509
430
  do_count = "false";
510
431
  }
@@ -521,29 +442,29 @@ module.exports = class Database {
521
442
  }
522
443
 
523
444
  if (columns == "*") {
524
- columns = "(" + alias + ").*";
525
- fix = ", META(" + alias + ").id AS id";
445
+ columns = "(" + bucket + ").*";
446
+ fix = ", META(" + bucket + ").id AS id";
526
447
  }
527
448
 
528
449
  if (!columns) {
529
- columns = "(" + alias + ").*";
530
- fix = ", META(" + alias + ").id AS id";
450
+ columns = "(" + bucket + ").*";
451
+ fix = ", META(" + bucket + ").id AS id";
531
452
  }
532
453
 
533
454
  if (columns == "id") {
534
- fix = ", META(" + alias + ").id AS id";
455
+ fix = ", META(" + bucket + ").id AS id";
535
456
  columns = "";
536
457
  }
537
458
 
538
459
  var front_query = "SELECT ".concat(columns, " ").concat(fix, " FROM ").concat(bucket);
539
460
 
540
- if (include_type || query) {
461
+ if (is_legacy || query) {
541
462
  front_query += " WHERE ";
542
463
  }
543
464
 
544
465
  var conditions = [];
545
466
 
546
- if (include_type) {
467
+ if (use_bucket_name) {
547
468
  conditions.push("type = '".concat(type, "'"));
548
469
  }
549
470
 
@@ -554,7 +475,7 @@ module.exports = class Database {
554
475
  query = front_query + conditions.join(" AND ");
555
476
  count_query = "SELECT count(meta().id) as count FROM ".concat(bucket);
556
477
 
557
- if (include_type || query) {
478
+ if (use_bucket_name || query) {
558
479
  count_query += " WHERE ";
559
480
  }
560
481
 
@@ -572,13 +493,11 @@ module.exports = class Database {
572
493
  query = query + " OFFSET " + offset;
573
494
  }
574
495
 
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)];
496
+ var active_query_space = query_scope || !use_bucket_name ? "scope" : "cluster";
497
+ var queries = [_this14.getData(query, active_query_space)];
579
498
 
580
499
  if (do_count === "true") {
581
- queries.push(_this10.getCount(count_query, active_query_space));
500
+ queries.push(_this14.getCount(count_query, active_query_space));
582
501
  }
583
502
 
584
503
  var responses = yield Promise.all(queries);
@@ -599,7 +518,7 @@ module.exports = class Database {
599
518
  return new Promise(
600
519
  /*#__PURE__*/
601
520
  function () {
602
- var _ref10 = _asyncToGenerator(function* (resolve, reject) {
521
+ var _ref5 = _asyncToGenerator(function* (resolve, reject) {
603
522
  try {
604
523
  var full_query = search_query + " AND type:" + type;
605
524
  var query = that.SearchQuery.new(that.full_text_index || full_text_index, that.SearchQuery.queryString(full_query));
@@ -620,7 +539,7 @@ module.exports = class Database {
620
539
  that.cluster.query(query,
621
540
  /*#__PURE__*/
622
541
  function () {
623
- var _ref11 = _asyncToGenerator(function* (err, results, matrix) {
542
+ var _ref6 = _asyncToGenerator(function* (err, results, matrix) {
624
543
  var return_value = [];
625
544
 
626
545
  if (err) {
@@ -646,8 +565,8 @@ module.exports = class Database {
646
565
  }
647
566
  });
648
567
 
649
- return function (_x22, _x23, _x24) {
650
- return _ref11.apply(this, arguments);
568
+ return function (_x12, _x13, _x14) {
569
+ return _ref6.apply(this, arguments);
651
570
  };
652
571
  }());
653
572
  } catch (e) {
@@ -655,43 +574,43 @@ module.exports = class Database {
655
574
  }
656
575
  });
657
576
 
658
- return function (_x20, _x21) {
659
- return _ref10.apply(this, arguments);
577
+ return function (_x10, _x11) {
578
+ return _ref5.apply(this, arguments);
660
579
  };
661
580
  }());
662
581
  }
663
582
 
664
583
  getCount(count_query_element) {
665
- var _this11 = this;
584
+ var _this15 = this;
666
585
 
667
586
  var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
668
587
  return _asyncToGenerator(function* () {
669
- var response = yield _this11.query(count_query_element, query_scope);
588
+ var response = yield _this15.query(count_query_element, query_scope);
670
589
  return response.rows[0].count;
671
590
  })();
672
591
  }
673
592
 
674
593
  getData(query_element) {
675
- var _this12 = this;
594
+ var _this16 = this;
676
595
 
677
596
  var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
678
597
  return _asyncToGenerator(function* () {
679
- var response = yield _this12.query(query_element, query_scope);
598
+ var response = yield _this16.query(query_element, query_scope);
680
599
  return response.rows;
681
600
  })();
682
601
  }
683
602
 
684
603
  query(query) {
685
- var _this13 = this;
604
+ var _this17 = this;
686
605
 
687
606
  var query_scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cluster";
688
607
  return _asyncToGenerator(function* () {
689
- var scope = query_scope || _this13.query_scope;
608
+ var scope = query_scope || _this17.query_scope;
690
609
 
691
610
  if (scope == "cluster") {
692
- return yield _this13.cluster.query(query);
611
+ return yield _this17.cluster.query(query);
693
612
  } else {
694
- return yield _this13.scope.query(query);
613
+ return yield _this17.scope.query(query);
695
614
  }
696
615
  })();
697
616
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-couchbase",
3
- "version": "2.0.15",
3
+ "version": "2.0.18",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -70,6 +70,9 @@ module.exports = class Database {
70
70
  if (this.auto_create_collection) {
71
71
  this.upsertCollection();
72
72
  }
73
+
74
+ this.use_bucket_name =
75
+ !spice.config.database.connections[this.connection_name].scope;
73
76
  }
74
77
  }
75
78
 
@@ -105,7 +108,6 @@ module.exports = class Database {
105
108
  async assignUserPermission() {
106
109
  try {
107
110
  let allUsers = await this.userManager.getAllUsers({ timeout: 6000 });
108
- console.log("UserName::::", this.db_config.username, allUsers);
109
111
  let update_user_response = await this.userManager.upsertUser(
110
112
  this.db_config.username,
111
113
  {
@@ -115,8 +117,6 @@ module.exports = class Database {
115
117
  ],
116
118
  }
117
119
  );
118
-
119
- console.log("UPdate Response", update_user_response);
120
120
  } catch (e) {
121
121
  console.log(e);
122
122
  }
@@ -224,101 +224,60 @@ module.exports = class Database {
224
224
 
225
225
  async get(doc_id) {
226
226
  try {
227
- let response = await this.connection.get(doc_id);
228
- console.log("Response ", response);
227
+ let response = await (this.use_bucket_name
228
+ ? this.connection
229
+ : this.collection
230
+ ).get(doc_id);
231
+
229
232
  return { id: doc_id, ...response.content };
230
233
  } catch (e) {
231
234
  console.error(e);
232
235
  }
233
236
  }
234
237
 
235
- insert(doc_id, document, expiry) {
236
- var that = this;
237
- return new Promise(async function (resolve, reject) {
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
- }
254
- }
255
- }
256
- );
257
- });
238
+ async insert(doc_id, document, expiry) {
239
+ await (this.use_bucket_name ? this.connection : this.collection).insert(
240
+ doc_id,
241
+ document,
242
+ {
243
+ expiry: expiry,
244
+ }
245
+ );
246
+ return await this.get(doc_id);
258
247
  }
259
248
 
260
- touch(doc_id, expiry = 0) {
261
- var that = this;
262
- return new Promise(async function (resolve, reject) {
263
- that.connection.touch(doc_id, expiry, async function (err, res) {
264
- if (err) {
265
- reject(err);
266
- } else {
267
- try {
268
- let created = await that.get(doc_id);
269
- resolve(created);
270
- } catch (e) {
271
- reject(e);
272
- }
273
- }
274
- });
275
- });
249
+ async touch(doc_id, expiry = 0) {
250
+ await (this.use_bucket_name ? this.connection : this.collection).touch(
251
+ doc_id,
252
+ expiry
253
+ );
254
+ return await this.get(doc_id);
276
255
  }
277
256
 
278
- count(doc_id, adjustment, initial_value) {
279
- var that = this;
280
- return new Promise(async function (resolve, reject) {
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
- }
293
- }
294
- );
257
+ async count(doc_id, adjustment, initial_value) {
258
+ let res = await (this.use_bucket_name
259
+ ? this.connection
260
+ : this.collection
261
+ ).counter(doc_id, adjustment, {
262
+ initial: initial_value,
295
263
  });
264
+ return res.value;
296
265
  }
297
266
 
298
- update(doc_id, document) {
299
- var that = this;
300
- return new Promise(async function (resolve, reject) {
301
- that.connection.replace(doc_id, document, function (err, res) {
302
- if (err) {
303
- reject(err);
304
- } else {
305
- resolve(res.value);
306
- }
307
- });
308
- });
267
+ async update(doc_id, document) {
268
+ let res = await (this.use_bucket_name
269
+ ? this.connection
270
+ : this.collection
271
+ ).replace(doc_id, document);
272
+ return res.value;
309
273
  }
310
274
 
311
- delete(doc_id) {
312
- var that = this;
313
- return new Promise(async function (resolve, reject) {
314
- that.connection.remove(doc_id, function (err, res) {
315
- if (err) {
316
- reject(err);
317
- } else {
318
- resolve(res.value);
319
- }
320
- });
321
- });
275
+ async delete(doc_id) {
276
+ let res = await (this.use_bucket_name
277
+ ? this.connection
278
+ : this.collection
279
+ ).remove(doc_id);
280
+ return res.value;
322
281
  }
323
282
 
324
283
  async multi_get(keys, keep_type) {
@@ -329,7 +288,9 @@ module.exports = class Database {
329
288
  }
330
289
  // console.log("on Multiget", keys);
331
290
  if (_.isArray(keys) && keys.length > 0) {
332
- let functions = _.map(keys, (key) => this.connection.get(key));
291
+ let functions = _.map(keys, (key) =>
292
+ (this.use_bucket_name ? this.connection : this.collection).get(key)
293
+ );
333
294
  let returned_all = await Promise.allSettled(functions);
334
295
 
335
296
  let responses = _.flatten(
@@ -347,24 +308,6 @@ module.exports = class Database {
347
308
  res.value.id = keys[index];
348
309
  return res.value;
349
310
  });
350
-
351
- /* that.connection.getMulti(keys, function (err, res) {
352
- if (res) {
353
- let return_value = [];
354
- for (var i in res) {
355
- if (res[i].value) {
356
- if (!keep_type) {
357
- delete res[i].value['type']
358
- }
359
- res[i].value.id = i;
360
- return_value.push(res[i].value);
361
- }
362
- }
363
- resolve(return_value);
364
- } else if (err) {
365
- reject(err);
366
- }
367
- }); */
368
311
  } else {
369
312
  return [];
370
313
  }
@@ -430,12 +373,16 @@ module.exports = class Database {
430
373
  do_count,
431
374
  query_scope
432
375
  ) {
376
+ let is_legacy =
377
+ this.scope_name !== "_default" && this.collection_name !== "_default";
433
378
  let alias = "_reference";
434
- let bucket = `\`${this.collection_name}\` AS ${alias}`;
435
-
436
- let include_type = this.scope_name && this.collection_name;
379
+ let bucket = `\`${
380
+ this.use_bucket_name ? this.bucket : this.collection_name
381
+ }\``;
437
382
 
438
383
  try {
384
+ //let uses_scopes = this.scope this.scope_name !== "_default";
385
+
439
386
  if (do_count == undefined) {
440
387
  do_count = "false";
441
388
  }
@@ -450,28 +397,28 @@ module.exports = class Database {
450
397
  }
451
398
 
452
399
  if (columns == "*") {
453
- columns = "(" + alias + ").*";
454
- fix = ", META(" + alias + ").id AS id";
400
+ columns = "(" + bucket + ").*";
401
+ fix = ", META(" + bucket + ").id AS id";
455
402
  }
456
403
 
457
404
  if (!columns) {
458
- columns = "(" + alias + ").*";
459
- fix = ", META(" + alias + ").id AS id";
405
+ columns = "(" + bucket + ").*";
406
+ fix = ", META(" + bucket + ").id AS id";
460
407
  }
461
408
 
462
409
  if (columns == "id") {
463
- fix = ", META(" + alias + ").id AS id";
410
+ fix = ", META(" + bucket + ").id AS id";
464
411
  columns = "";
465
412
  }
466
413
 
467
414
  var front_query = `SELECT ${columns} ${fix} FROM ${bucket}`;
468
415
 
469
- if (include_type || query) {
416
+ if (is_legacy || query) {
470
417
  front_query += " WHERE ";
471
418
  }
472
419
 
473
420
  let conditions = [];
474
- if (include_type) {
421
+ if (this.use_bucket_name) {
475
422
  conditions.push(`type = '${type}'`);
476
423
  }
477
424
  if (query) {
@@ -481,7 +428,7 @@ module.exports = class Database {
481
428
  query = front_query + conditions.join(" AND ");
482
429
 
483
430
  count_query = `SELECT count(meta().id) as count FROM ${bucket}`;
484
- if (include_type || query) {
431
+ if (this.use_bucket_name || query) {
485
432
  count_query += " WHERE ";
486
433
  }
487
434
  query = front_query + conditions.join(" AND ");
@@ -498,10 +445,8 @@ module.exports = class Database {
498
445
  query = query + " OFFSET " + offset;
499
446
  }
500
447
 
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);
448
+ let active_query_space =
449
+ query_scope || !this.use_bucket_name ? "scope" : "cluster";
505
450
 
506
451
  let queries = [this.getData(query, active_query_space)];
507
452
  if (do_count === "true") {