UNPKG

ottoman

Version:
37 lines 2.13 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildViewIndexQuery = void 0; const ottoman_errors_1 = require("../../../exceptions/ottoman-errors"); /** * Index function factory. */ const buildViewIndexQuery = (ottoman, designDocName, indexName, fields, Model) => (values, options = {}) => __awaiter(void 0, void 0, void 0, function* () { if (!values) { throw new ottoman_errors_1.BuildIndexQueryError(`Function '${indexName}' received wrong argument value, '${values}' wasn't expected`); } const arrayValues = Array.isArray(values) ? values : [values]; const fieldsLength = fields.length; const valuesLength = arrayValues.length; if (valuesLength !== fieldsLength) { throw new ottoman_errors_1.BuildIndexQueryError(`Function '${indexName}' received wrong number of arguments, '${fieldsLength}:[${fields}]' argument(s) was expected and '${valuesLength}:[${arrayValues}]' were received`); } options.keys = arrayValues; const result = yield ottoman.bucket.viewQuery(designDocName, indexName, options); const populatedResults = []; for (const row of result.rows) { const populatedDocument = yield Model.findById(row.id, options); populatedResults.push(populatedDocument); } return { rows: populatedResults, meta: result.meta }; }); exports.buildViewIndexQuery = buildViewIndexQuery; //# sourceMappingURL=build-view-index-query.js.map