@touchway/touchway-rest-provider
Version:
196 lines (195 loc) • 7.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var TWRestQuery = /** @class */ (function () {
function TWRestQuery() {
this._where = [];
this._columns = [];
this._limit = 1000;
this._offset = 0;
this._language = "";
this._resolveForeignKeys = false;
this._tableName = "";
}
Object.defineProperty(TWRestQuery.prototype, "resolveForeignKeys", {
get: function () {
return this._resolveForeignKeys;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TWRestQuery.prototype, "action", {
get: function () {
return this._action;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TWRestQuery.prototype, "where", {
get: function () {
return this._where;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TWRestQuery.prototype, "limit", {
get: function () {
return this._limit;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TWRestQuery.prototype, "offset", {
get: function () {
return this._offset;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TWRestQuery.prototype, "columns", {
get: function () {
return this._columns;
},
enumerable: true,
configurable: true
});
/*get prototype() {
return this._prototype;
}*/
TWRestQuery.get = function (columns) {
if (columns === void 0) { columns = []; }
var tmpQuery = new TWRestQuery();
tmpQuery._columns = columns;
tmpQuery._action = "select";
return tmpQuery;
};
TWRestQuery.put = function () {
var tmpQuery = new TWRestQuery();
tmpQuery._action = "save";
return tmpQuery;
};
TWRestQuery.resolveRegex = function (pattern, str) {
var m;
var tmpResult = [];
while ((m = pattern.exec(str)) !== null) {
if (m.index === pattern.lastIndex) {
pattern.lastIndex++;
}
tmpResult.push(m);
}
return tmpResult;
};
TWRestQuery.prototype.from = function (tableName) {
this._tableName = tableName;
return this;
};
TWRestQuery.prototype.toString = function () {
return this.encode();
};
TWRestQuery.prototype.encode = function () {
var key = this._tableName;
var tmpUrlParts = [this._limit + "-" + this._offset];
// Add columns
if (this._columns && this._columns.length) {
tmpUrlParts.push(this._columns.join('&'));
}
else {
tmpUrlParts.push('*');
}
// Add where part
if (this._where && this._where.length) {
var tmpWhereArr = [];
for (var x = 0; x < this._where.length; x++) {
var tmpWhere = this._where[x];
tmpWhereArr.push(tmpWhere.column + "=" + tmpWhere.operator + ":{{" + tmpWhere.value + "}}");
}
tmpUrlParts.push(tmpWhereArr.join('&'));
}
var tmpUrl = tmpUrlParts.join('/');
return [key, btoa(tmpUrl)].join('/');
};
TWRestQuery.decode = function (url) {
var tmpSplit = url.split('/');
var tmpTable = tmpSplit[0];
if (tmpSplit.length > 1) {
var tmpParams = tmpSplit[1];
var tmpDecodedParams = atob(tmpParams);
var tmpSplitParams = tmpDecodedParams.split('/');
var tmpLimitOffset = String(tmpSplitParams[0]).split('-');
var tmpLimit = parseInt(tmpLimitOffset[0]);
var tmpOffset = parseInt(tmpLimitOffset[1]);
var tmpColumns = tmpSplitParams[1];
var tmpWhere = (tmpSplitParams[2]) ? TWRestQuery.decodeWhere(tmpSplitParams[2]) : [];
var tmpQuery = new TWRestQuery();
tmpQuery.setTableName(tmpTable);
for (var x = 0; x < tmpWhere.length; x++) {
var tmpWherePart = tmpWhere[x];
tmpQuery.addWhere(tmpWherePart.column, tmpWherePart.operator, tmpWherePart.value);
}
tmpQuery.setLimit(tmpLimit, tmpOffset);
console.log("\n Limit offset: " + tmpLimitOffset + "\n Columns: " + tmpColumns + "\n Where: " + tmpWhere + "\n ToString: " + tmpQuery.toString() + "\n ============================================= \n ");
}
return url;
};
TWRestQuery.prototype.addWhere = function (column, operator, value) {
this.where.push({
column: column,
operator: operator,
value: value
});
return this;
};
TWRestQuery.decodeWhere = function (whereString) {
if (!whereString)
return [];
var tmpReturnResult = [];
var tmpSplit = whereString.split('&');
if (tmpSplit.length) {
for (var x = 0; x < tmpSplit.length; x++) {
var tmpRegexResult = TWRestQuery.resolveRegex(TWRestQuery.regex.where, tmpSplit[x]);
if (tmpRegexResult.length) {
var tmpColumn = tmpRegexResult[0][1] || null;
var tmpOperator = tmpRegexResult[0][2] || null;
var tmpValue = tmpRegexResult[0][3] || null;
if (tmpColumn && tmpOperator && tmpValue) {
tmpReturnResult.push({
column: tmpColumn,
operator: tmpOperator,
value: tmpValue
});
}
}
}
}
return tmpReturnResult;
};
TWRestQuery.prototype.setWhere = function (filter) {
this._where = filter;
return this;
};
TWRestQuery.prototype.setTableName = function (tableName) {
this._tableName = tableName;
};
TWRestQuery.prototype.filterByLanguage = function (language) {
this._language = language;
return this;
};
TWRestQuery.prototype.setResolveForeignKeys = function (val) {
if (val === void 0) { val = true; }
this._resolveForeignKeys = val;
return this;
};
TWRestQuery.prototype.setLimit = function (limit, offset) {
this._limit = limit;
};
/**
* Regex patterns used inside this class
*
* @static
* @memberof IntelligentQuery
*/
TWRestQuery.regex = {
where: /^([^=]*)=([\w]{2,3}):{{([^}]*)}}/gm
};
return TWRestQuery;
}());
exports.TWRestQuery = TWRestQuery;