spice-couchbase 2.0.42 → 2.0.44

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
@@ -299,12 +299,30 @@ module.exports = class Database {
299
299
  get(doc_id) {
300
300
  var _this8 = this;
301
301
 
302
+ var {
303
+ columns = []
304
+ } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
302
305
  return _asyncToGenerator(function* () {
303
306
  try {
304
- var response = yield (_this8.use_bucket_name ? _this8.connection : _this8.collection).get(doc_id);
305
- return _objectSpread({
306
- id: doc_id
307
- }, response.content);
307
+ var useLookupIn = columns.length > 0;
308
+ var response = yield useLookupIn ? (_this8.use_bucket_name ? _this8.connection : _this8.collection).lookupIn(doc_id, columns.map(col => couchbase.LookupInSpec.get(col))) : (_this8.use_bucket_name ? _this8.connection : _this8.collection).get(doc_id);
309
+
310
+ if (useLookupIn) {
311
+ // lookupIn returns content array with each spec result
312
+ var value = {
313
+ id: doc_id
314
+ };
315
+ columns.forEach((col, colIndex) => {
316
+ if (response.content && response.content[colIndex]) {
317
+ value[col] = response.content[colIndex].value;
318
+ }
319
+ });
320
+ return value;
321
+ } else {
322
+ return _objectSpread({
323
+ id: doc_id
324
+ }, response.content);
325
+ }
308
326
  } catch (e) {
309
327
  console.error(e);
310
328
  }
@@ -368,16 +386,22 @@ module.exports = class Database {
368
386
  })();
369
387
  }
370
388
 
371
- multi_get(keys, keep_type) {
389
+ multi_get(keys) {
372
390
  var _this14 = this;
373
391
 
392
+ var {
393
+ keep_type = true,
394
+ columns = []
395
+ } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
374
396
  return _asyncToGenerator(function* () {
375
397
  if (keys.length == 0) {
376
398
  return [];
377
399
  }
378
400
 
379
401
  if (_.isArray(keys) && keys.length > 0) {
380
- var functions = _.map(keys, key => (_this14.use_bucket_name ? _this14.connection : _this14.collection).get(key));
402
+ var useLookupIn = columns.length > 0;
403
+
404
+ var functions = _.map(keys, key => useLookupIn ? (_this14.use_bucket_name ? _this14.connection : _this14.collection).lookupIn(key, columns.map(col => couchbase.LookupInSpec.get(col))) : (_this14.use_bucket_name ? _this14.connection : _this14.collection).get(key));
381
405
 
382
406
  var returned_all = yield Promise.allSettled(functions);
383
407
 
@@ -385,16 +409,36 @@ module.exports = class Database {
385
409
  return returned_obj === null || returned_obj === void 0 ? void 0 : returned_obj.value;
386
410
  }));
387
411
 
388
- return _.compact(_.map(responses, (res, index) => {
412
+ var final_responses = _.compact(_.map(responses, (res, index) => {
389
413
  if (res) {
390
- if (!keep_type) {
391
- delete res.value.type;
414
+ var value;
415
+
416
+ if (useLookupIn) {
417
+ // lookupIn returns content array with each spec result
418
+ value = {};
419
+ columns.forEach((col, colIndex) => {
420
+ if (res.content && res.content[colIndex]) {
421
+ value[col] = res.content[colIndex].value;
422
+ }
423
+ });
424
+ } else {
425
+ // regular get returns the full document in value
426
+ value = res.value;
427
+ }
428
+
429
+ if (!keep_type && value) {
430
+ delete value.type;
392
431
  }
393
432
 
394
- res.value.id = keys[index];
395
- return res.value;
433
+ if (value) {
434
+ value.id = keys[index];
435
+ }
436
+
437
+ return value;
396
438
  }
397
439
  }));
440
+
441
+ return final_responses;
398
442
  } else {
399
443
  return [];
400
444
  }
@@ -483,19 +527,46 @@ module.exports = class Database {
483
527
  var is_legacy = _this15.scope_name !== "_default" && _this15.collection_name !== "_default";
484
528
  var bucket = "`".concat(_this15.use_bucket_name ? _this15.bucket : _this15.collection_name, "`");
485
529
 
486
- function containsIdWithSpace(str) {
487
- return /(?<=\W|^)(?!`)(?:(?:\w+`?\.)*)(?:\w+`?\.)?id(?<!`)(?!\w)/i.test(str);
488
- }
530
+ function processColumns(columnsStr) {
531
+ // If columns is empty or *, return default
532
+ if (columnsStr === "*" || !columnsStr) {
533
+ return "(".concat(bucket, ").*, META(").concat(bucket, ").id AS id");
534
+ } // Split by comma and process each column
489
535
 
490
- try {
491
- var fix = ", META(".concat(bucket, ").id AS id");
492
536
 
493
- if (columns === "*" || !columns) {
494
- columns = "(".concat(bucket, ").* ").concat(fix);
495
- } else if (!containsIdWithSpace(columns)) {
496
- columns = "".concat(columns).concat(fix);
537
+ var columnList = columnsStr.split(",").map(col => col.trim());
538
+ var hasId = false;
539
+ var processedColumns = columnList.map(col => {
540
+ // Check if this column is an id field in various formats
541
+ // Patterns: `collection`.`id`, collection.id, `id`, id
542
+ var idPattern = /^(?:`?(\w+)`?\.)?`?id`?$/i;
543
+ var match = col.match(idPattern);
544
+
545
+ if (match) {
546
+ hasId = true; // match[1] contains the collection name if present
547
+
548
+ if (match[1]) {
549
+ // Has collection prefix: `collection`.`id` or collection.id
550
+ return "META(`".concat(match[1], "`).id AS id");
551
+ } else {
552
+ // Just id without collection prefix
553
+ return "META(".concat(bucket, ").id AS id");
554
+ }
555
+ } // Return the column as-is
556
+
557
+
558
+ return col;
559
+ }); // If no id column was provided, add it automatically
560
+
561
+ if (!hasId) {
562
+ processedColumns.push("META(".concat(bucket, ").id AS id"));
497
563
  }
498
564
 
565
+ return processedColumns.join(", ");
566
+ }
567
+
568
+ try {
569
+ columns = processColumns(columns);
499
570
  var front_query = "SELECT ".concat(columns, " FROM ").concat(bucket, " ").concat(join);
500
571
  var conditions = [];
501
572
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-couchbase",
3
- "version": "2.0.42",
3
+ "version": "2.0.44",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -10,7 +10,7 @@
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
12
  "axios": "^0.26.1",
13
- "couchbase": "^4.2.7",
13
+ "couchbase": "^4.6.0",
14
14
  "lodash": "^4.17.15",
15
15
  "qs": "^6.10.3"
16
16
  },
package/src/index.js CHANGED
@@ -265,14 +265,30 @@ module.exports = class Database {
265
265
  }
266
266
  }
267
267
 
268
- async get(doc_id) {
268
+ async get(doc_id, { columns = [] } = {}) {
269
269
  try {
270
+ const useLookupIn = columns.length > 0;
270
271
  let response = await (
271
- this.use_bucket_name ?
272
- this.connection
273
- : this.collection).get(doc_id);
272
+ useLookupIn ?
273
+ (this.use_bucket_name ? this.connection : this.collection).lookupIn(
274
+ doc_id,
275
+ columns.map((col) => couchbase.LookupInSpec.get(col))
276
+ )
277
+ : (this.use_bucket_name ? this.connection : this.collection).get(doc_id)
278
+ );
274
279
 
275
- return { id: doc_id, ...response.content };
280
+ if (useLookupIn) {
281
+ // lookupIn returns content array with each spec result
282
+ let value = { id: doc_id };
283
+ columns.forEach((col, colIndex) => {
284
+ if (response.content && response.content[colIndex]) {
285
+ value[col] = response.content[colIndex].value;
286
+ }
287
+ });
288
+ return value;
289
+ } else {
290
+ return { id: doc_id, ...response.content };
291
+ }
276
292
  } catch (e) {
277
293
  console.error(e);
278
294
  }
@@ -329,33 +345,60 @@ module.exports = class Database {
329
345
  return res.value;
330
346
  }
331
347
 
332
- async multi_get(keys, keep_type) {
348
+ async multi_get(keys, { keep_type = true, columns = [] } = {}) {
333
349
  if (keys.length == 0) {
334
350
  return [];
335
351
  }
336
352
  if (_.isArray(keys) && keys.length > 0) {
353
+ const useLookupIn = columns.length > 0;
337
354
  let functions = _.map(keys, (key) =>
338
- (this.use_bucket_name ? this.connection : this.collection).get(key)
355
+ useLookupIn ?
356
+ (this.use_bucket_name ? this.connection : this.collection).lookupIn(
357
+ key,
358
+ columns.map((col) => couchbase.LookupInSpec.get(col))
359
+ )
360
+ : (this.use_bucket_name ? this.connection : this.collection).get(key)
339
361
  );
340
362
  let returned_all = await Promise.allSettled(functions);
341
363
 
364
+
365
+
342
366
  let responses = _.flatten(
343
367
  _.map(returned_all, (returned_obj) => {
344
368
  return returned_obj?.value;
345
369
  })
346
370
  );
347
371
 
348
- return _.compact(
372
+
373
+ let final_responses = _.compact(
349
374
  _.map(responses, (res, index) => {
350
375
  if (res) {
351
- if (!keep_type) {
352
- delete res.value.type;
376
+ let value;
377
+ if (useLookupIn) {
378
+ // lookupIn returns content array with each spec result
379
+ value = {};
380
+ columns.forEach((col, colIndex) => {
381
+ if (res.content && res.content[colIndex]) {
382
+ value[col] = res.content[colIndex].value;
383
+ }
384
+ });
385
+ } else {
386
+ // regular get returns the full document in value
387
+ value = res.value;
388
+ }
389
+
390
+ if (!keep_type && value) {
391
+ delete value.type;
392
+ }
393
+ if (value) {
394
+ value.id = keys[index];
353
395
  }
354
- res.value.id = keys[index];
355
- return res.value;
396
+ return value;
356
397
  }
357
398
  })
358
399
  );
400
+
401
+ return final_responses;
359
402
  } else {
360
403
  return [];
361
404
  }
@@ -428,20 +471,48 @@ module.exports = class Database {
428
471
  this.use_bucket_name ? this.bucket : this.collection_name
429
472
  }\``;
430
473
 
431
- function containsIdWithSpace(str) {
432
- return /(?<=\W|^)(?!`)(?:(?:\w+`?\.)*)(?:\w+`?\.)?id(?<!`)(?!\w)/i.test(
433
- str
434
- );
435
- }
474
+ function processColumns(columnsStr) {
475
+ // If columns is empty or *, return default
476
+ if (columnsStr === "*" || !columnsStr) {
477
+ return `(${bucket}).*, META(${bucket}).id AS id`;
478
+ }
436
479
 
437
- try {
438
- const fix = `, META(${bucket}).id AS id`;
480
+ // Split by comma and process each column
481
+ const columnList = columnsStr.split(",").map((col) => col.trim());
482
+ let hasId = false;
483
+
484
+ const processedColumns = columnList.map((col) => {
485
+ // Check if this column is an id field in various formats
486
+ // Patterns: `collection`.`id`, collection.id, `id`, id
487
+ const idPattern = /^(?:`?(\w+)`?\.)?`?id`?$/i;
488
+ const match = col.match(idPattern);
489
+
490
+ if (match) {
491
+ hasId = true;
492
+ // match[1] contains the collection name if present
493
+ if (match[1]) {
494
+ // Has collection prefix: `collection`.`id` or collection.id
495
+ return `META(\`${match[1]}\`).id AS id`;
496
+ } else {
497
+ // Just id without collection prefix
498
+ return `META(${bucket}).id AS id`;
499
+ }
500
+ }
439
501
 
440
- if (columns === "*" || !columns) {
441
- columns = `(${bucket}).* ${fix}`;
442
- } else if (!containsIdWithSpace(columns)) {
443
- columns = `${columns}${fix}`;
502
+ // Return the column as-is
503
+ return col;
504
+ });
505
+
506
+ // If no id column was provided, add it automatically
507
+ if (!hasId) {
508
+ processedColumns.push(`META(${bucket}).id AS id`);
444
509
  }
510
+
511
+ return processedColumns.join(", ");
512
+ }
513
+
514
+ try {
515
+ columns = processColumns(columns);
445
516
  const front_query = `SELECT ${columns} FROM ${bucket} ${join}`;
446
517
  const conditions = [];
447
518