ottoman
Version:
Ottoman Couchbase ODM
34 lines • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildIndexQuery = void 0;
const ottoman_errors_1 = require("../../../exceptions/ottoman-errors");
/**
* View index function factory.
*/
const buildIndexQuery = (Model, fields, indexFnName, indexOptions = {}) => (values, options = {}) => {
values = Array.isArray(values) ? values : typeof values === 'string' ? [values] : values;
let filter = values;
const n1qlOptions = Object.assign(Object.assign({}, indexOptions), options);
if (Array.isArray(values)) {
if (values.length !== fields.length) {
throw new ottoman_errors_1.BuildIndexQueryError(`Function '${indexFnName}' received wrong number of arguments, '${fields.length}:[${fields}]' argument(s) was expected and '${values.length}:[${values}]' were received`);
}
filter = {};
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
if (field.includes('[*]')) {
const [target, targetField] = field.split('[*].');
filter['$any'] = {
$expr: [{ x: { $in: target } }],
$satisfies: { [`x.${targetField}`]: values[i] },
};
}
else {
filter[field] = values[i];
}
}
}
return Model.find(filter, n1qlOptions);
};
exports.buildIndexQuery = buildIndexQuery;
//# sourceMappingURL=build-index-query.js.map