mongoose-restapi-ui
Version:
Rest api library for your mongoose apps, with react ui: mongoose-restapi-ui-component react library
292 lines • 12.9 kB
JavaScript
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var mongoose_1 = require("mongoose");
var utils = require("../utils");
var Query = /** @class */ (function () {
function Query(ctx, query, models, model, mongo4) {
this.models = models;
this.model = model;
this._query = query;
this.modelDefinition = ctx;
this.isMongo4 = mongo4;
}
Object.defineProperty(Query.prototype, "hasAny", {
get: function () {
return Object.keys(this._query).indexOf('$any') > -1;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Query.prototype, "hasRefs", {
get: function () {
var _this = this;
if (this.modelDefinition.refFullPaths.length > 0) {
return this.hasAny || Object.keys(this._query).some(function (field) { return _this.modelDefinition.refFullPaths.indexOf(field) > -1; });
}
return false;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Query.prototype, "refPaths", {
get: function () {
var _this = this;
if (this.hasAny)
return this.modelDefinition.refFullPaths;
return Object.keys(this._query).filter(function (field) { return _this.modelDefinition.refFullPaths.indexOf(field) > -1; });
},
enumerable: true,
configurable: true
});
Object.defineProperty(Query.prototype, "numberPaths", {
get: function () {
return this.modelDefinition.numberFullPaths;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Query.prototype, "booleanPaths", {
get: function () {
return this.modelDefinition.booleanFullPaths;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Query.prototype, "datePaths", {
get: function () {
return this.modelDefinition.dateFullPaths;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Query.prototype, "objectIdPaths", {
get: function () {
return this.modelDefinition.objectIdFullPaths;
},
enumerable: true,
configurable: true
});
Query.prototype.parseQueryValue = function (val, fn) {
if (fn === void 0) { fn = function (el) { return el; }; }
if (Array.isArray(val))
return { $in: val.map(fn) };
return fn(val);
};
Query.prototype.getValuesContains = function (fields, val) {
if (Array.isArray(val)) {
var res_1 = [];
val.forEach(function (subVal) {
res_1 = res_1.concat(fields.map(function (field) {
var _a;
return _a = {}, _a[field] = utils.containsStringFx(subVal), _a;
}));
});
return res_1;
}
return fields.map(function (field) {
var _a;
return _a = {}, _a[field] = utils.containsStringFx(val), _a;
});
};
Query.prototype._anyOperatorQuery = function (val) {
var _this = this;
var query = this.getValuesContains(this.modelDefinition.stringFullPaths.concat(this.modelDefinition.refFullPaths), val);
if (this.isMongo4) {
query = query.concat(this.getValuesContains(this.numberPaths
.concat(this.booleanPaths)
.concat(this.datePaths)
.concat(this.objectIdPaths), val));
}
if (Array.isArray(val)) {
val.forEach(function (subVal) {
query = query.concat(_this.modelDefinition.booleanFullPaths.filter(function (path) { return path === subVal; }).map(function (path) {
var _a;
return (_a = {},
_a[path] = true,
_a);
}));
});
}
else {
query = query.concat(this.modelDefinition.booleanFullPaths.filter(function (path) { return path === val; }).map(function (path) {
var _a;
return (_a = {},
_a[path] = true,
_a);
}));
}
if (!this.isMongo4) {
if (Array.isArray(val)) {
var numbers_1 = val.filter(utils.isNumber);
var boolean_1 = val.filter(function (v) { return v === 'true' || v === 'false'; });
if (numbers_1.length > 0) {
query = query.concat(this.modelDefinition.numberFullPaths.map(function (numberField) {
var _a;
return _a = {}, _a[numberField] = _this.parseQueryValue(numbers_1, utils.parseNumberFx), _a;
}));
}
if (boolean_1.length > 0) {
query = query.concat(this.modelDefinition.booleanFullPaths.map(function (booleanField) {
var _a;
return _a = {},
_a[booleanField] = _this.parseQueryValue(boolean_1, function (el) { return JSON.parse(el); }),
_a;
}));
}
if (val.some(function (subval) { return subval.length === 24; })) {
query = query.concat(this.modelDefinition.objectIdFullPaths.map(function (objectIdField) {
var _a;
return _a = {},
_a[objectIdField] = _this.parseQueryValue(val.filter(function (subval) { return subval.length === 24; }), function (el) { return mongoose_1.Types.ObjectId(el); }),
_a;
}));
}
}
else {
if (utils.isNumber(val)) {
query = query.concat(this.modelDefinition.numberFullPaths.map(function (numberField) {
var _a;
return _a = {}, _a[numberField] = _this.parseQueryValue(val, utils.parseNumberFx), _a;
}));
}
if (val === 'true' || val === 'false') {
query = query.concat(this.modelDefinition.booleanFullPaths.map(function (booleanField) {
var _a;
return _a = {}, _a[booleanField] = _this.parseQueryValue(val), _a;
}));
}
if (val.length === 24) {
query = query.concat(this.modelDefinition.objectIdFullPaths.map(function (objectIdField) {
var _a;
return _a = {},
_a[objectIdField] = _this.parseQueryValue(val, function (el) { return mongoose_1.Types.ObjectId(el); }),
_a;
}));
}
}
}
if (query.length > 0)
return { $or: query };
return {};
};
Object.defineProperty(Query.prototype, "_basicQuery", {
get: function () {
var _this = this;
var _a = this._query, $any = _a.$any, others = __rest(_a, ["$any"]);
var query = Object.keys(others).map(function (field) {
var value = others[field];
if (_this.modelDefinition.fullPathTypes[field].type === 'Number') {
return {
field: field,
value: _this.parseQueryValue(value, utils.parseNumberFx)
};
}
if (_this.modelDefinition.fullPathTypes[field].type === 'ObjectId') {
return { field: field, value: _this.parseQueryValue(value, utils.parseObjectId) };
}
return {
field: field, value: _this.parseQueryValue(value)
};
}).reduce(function (obj, next) {
obj[next.field] = next.value;
return obj;
}, {});
if (this.hasAny && Object.keys(query).length > 0)
return { $and: [query, this._anyOperatorQuery($any)] };
if (this.hasAny)
return this._anyOperatorQuery($any);
return query;
},
enumerable: true,
configurable: true
});
Query.prototype._convertStep = function () {
var aggregation = [];
if (this.isMongo4 && this.hasAny) {
if ((this.numberPaths.length + this.booleanPaths.length + this.datePaths.length + this.objectIdPaths.length) > 0) {
aggregation = [{
$addFields: this.numberPaths.concat(this.booleanPaths).concat(this.datePaths).concat(this.objectIdPaths).reduce(function (el, path) {
el[path] = { $convert: { input: "$" + path, to: 'string', onError: '', onNull: '' } };
return el;
}, {})
}];
}
}
return aggregation;
};
Query.prototype.query = function (prevFilter, callback) {
var _this = this;
var convertStep = this._convertStep();
if (this.hasRefs) {
var aggregation = this.refPaths.map(function (el) {
var targetModel = _this.models[_this.modelDefinition.fullPathTypes[el].to];
return {
$lookup: {
from: targetModel.model.collection.name,
localField: el,
foreignField: '_id',
as: el
}
};
});
aggregation = aggregation.concat([
{
$addFields: this.refPaths.reduce(function (el, path) {
var targetModel = _this.models[_this.modelDefinition.fullPathTypes[path].to];
el[path] = "$" + path + "." + targetModel.label;
return el;
}, {})
}
]);
aggregation = aggregation.concat(convertStep, [{
$match: this._basicQuery
}, {
$project: {
_id: '$_id'
}
}]);
return this.model.aggregate(aggregation, function (err, res) {
if (err)
return callback(err);
callback(null, _this.model.find({ $and: [prevFilter, { _id: { $in: res.map(function (e) { return e._id; }) } }] }));
});
}
if (convertStep.length > 0) {
return this.model.aggregate(convertStep.concat([{
$match: this._basicQuery
}, {
$project: {
_id: '$_id'
}
}]), function (err, res) {
if (err)
return callback(err);
callback(null, _this.model.find({ $and: [prevFilter, { _id: { $in: res.map(function (e) { return e._id; }) } }] }));
});
}
return callback(null, this.model.find({ $and: [prevFilter, this._basicQuery] }));
};
return Query;
}());
exports.Query = Query;
exports.default = (function (mongo4, models, model, ctx, query, prevFilter, callback) {
if (Object.keys(query).filter(function (key) { return key !== '$any'; }).some(function (key) { return !ctx.fullPathTypes.hasOwnProperty(key); }))
return callback(new Error(Object.keys(query).filter(function (key) { return key !== '$any'; }).find(function (key) { return !ctx.fullPathTypes.hasOwnProperty(key); }) + ' not in schema.'));
var q = new Query(ctx, Object.keys(query)
.filter(function (key) { return query[key].length > 0; })
.reduce(function (obj, key) {
obj[key] = query[key];
return obj;
}, {}), models, model, mongo4);
return q.query(prevFilter ? prevFilter : {}, callback);
});
//# sourceMappingURL=query.js.map