cap_mdb_handler
Version:
A SAP CAP handler to connect to any MongoDB instance as database layer.
113 lines (112 loc) • 4.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MDBParser = /** @class */ (function () {
function MDBParser(request) {
this.request = request;
this.query = request.query;
}
MDBParser.prototype.parse = function () {
this.sCollection = this.request.entity;
this.oFind = this.parseFind();
this.oSort = this.parseSort();
this.oLimit = this.parseLimit();
};
MDBParser.prototype.parseFind = function () {
var oFind = {};
if (this.query.SELECT) {
if (this.query.SELECT.where && this.query.SELECT.where.length > 0) {
for (var _i = 0, _a = this.query.SELECT.where; _i < _a.length; _i++) {
var oWhere = _a[_i];
if (oWhere.ref && oWhere.ref[0]) {
oFind[oWhere.ref[0].id] = oWhere.val;
}
}
}
// Keys
// @ts-ignore
if (this.query.SELECT.from.ref && this.query.SELECT.from.ref[0].where && this.query.SELECT.from.ref[0].where.length > 0) {
for (var _b = 0, _c = this.query.SELECT.from.ref; _b < _c.length; _b++) {
var oWhere = _c[_b];
if (oWhere.where) {
oFind[oWhere.where[0].ref[0]] = oWhere.where[2].val;
}
}
}
}
if (this.query.UPDATE) {
// @ts-ignore
if (this.query.UPDATE.entity.ref[0].where && this.query.UPDATE.entity.ref[0].where.length > 0) {
// @ts-ignore
for (var _d = 0, _e = this.query.UPDATE.entity.ref; _d < _e.length; _d++) {
var oWhere = _e[_d];
if (oWhere.where) {
oFind[oWhere.where[0].ref[0]] = oWhere.where[2].val;
}
}
}
}
if (this.query.DELETE) {
// @ts-ignore
if (this.query.DELETE.from.ref[0].where && this.query.DELETE.from.ref[0].where.length > 0) {
// @ts-ignore
for (var _f = 0, _g = this.query.DELETE.from.ref; _f < _g.length; _f++) {
var oWhere = _g[_f];
if (oWhere.where) {
oFind[oWhere.where[0].ref[0]] = oWhere.where[2].val;
}
}
}
}
return oFind;
};
MDBParser.prototype.parseSort = function () {
var oOrder = {};
if (this.query.SELECT) {
if (this.query.SELECT.orderBy && this.query.SELECT.orderBy.length > 0) {
for (var _i = 0, _a = this.query.SELECT.orderBy; _i < _a.length; _i++) {
var oOrderBy = _a[_i];
// @ts-ignore
if (oOrderBy.ref) {
// @ts-ignore
oOrder[oOrderBy.ref[0]] = oOrderBy.sort === "asc" ? 1 : -1;
}
}
}
}
return oOrder;
};
MDBParser.prototype.parseLimit = function () {
var oLimit = { limit: 100, skip: 0 };
if (this.query.SELECT && this.query.SELECT.limit) {
if (this.query.SELECT.limit.rows) {
oLimit.limit = this.query.SELECT.limit.rows.val;
}
if (this.query.SELECT.limit.offset) {
oLimit.skip = this.query.SELECT.limit.offset.val;
}
}
return oLimit;
};
MDBParser.prototype.getCollection = function () {
console.log("Using collection: " + this.sCollection);
return this.sCollection;
};
MDBParser.prototype.getFind = function () {
console.log("Using find: ", this.oFind);
return this.oFind;
};
MDBParser.prototype.getSort = function () {
console.log("Using order: ", this.oSort);
return this.oSort;
};
MDBParser.prototype.getLimit = function () {
console.log("Using limit: ", this.oLimit);
return this.oLimit;
};
MDBParser.prototype.getData = function () {
console.log("Transporting data:", this.request.data);
return this.request.data;
};
return MDBParser;
}());
exports.default = MDBParser;