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 +91 -20
- package/package.json +2 -2
- package/src/index.js +94 -23
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
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
412
|
+
var final_responses = _.compact(_.map(responses, (res, index) => {
|
|
389
413
|
if (res) {
|
|
390
|
-
|
|
391
|
-
|
|
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
|
-
|
|
395
|
-
|
|
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
|
|
487
|
-
return
|
|
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
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
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.
|
|
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.
|
|
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
|
-
|
|
272
|
-
this.connection
|
|
273
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
372
|
+
|
|
373
|
+
let final_responses = _.compact(
|
|
349
374
|
_.map(responses, (res, index) => {
|
|
350
375
|
if (res) {
|
|
351
|
-
|
|
352
|
-
|
|
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
|
-
|
|
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
|
|
432
|
-
return
|
|
433
|
-
|
|
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
|
-
|
|
438
|
-
const
|
|
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
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
443
|
-
|
|
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
|
|