mission.core
Version:
mission core
210 lines (209 loc) • 7.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vendor_1 = require("../vendor");
const operator_alias_1 = require("./operator-alias");
class QueryOptionBuilder {
constructor(req) {
this.whereParam = {};
this.order = [];
this.attributes = [];
this.pg = null;
this.includeOptions = [];
req = req || {};
this.attributes = req.attributes || [];
this.pg = new vendor_1.Paginator(req.pageContext);
this.transaction = req.transaction;
vendor_1._.forEach(req.sorters, (s) => {
this.order.push([s.key, vendor_1.OrderBy[s.orderBy]]);
});
}
sortBy(key, orderby) {
this.order.push([key, orderby]);
return this;
}
where(params, resolver) {
params = params || [];
vendor_1._.forEach(params, (x) => {
const v = this.addAdvWhareRec(x, resolver);
const key = resolver && typeof x.key === 'number' ? resolver(x.key) : x.key;
const keySym = operator_alias_1.Opa.getOperator(key);
if (this.whereParam[keySym]) {
let array = Object.getOwnPropertyNames(v);
array = array.concat(Object.getOwnPropertySymbols(v));
array.forEach((y) => this.whereParam[keySym][y] = v[y]);
}
else {
this.whereParam[keySym] = v;
}
});
return this;
}
include(includes, resolver) {
includes = includes || [];
vendor_1._.forEach(includes, (inc) => {
let option = resolver(inc);
if (!option) {
throw new Error(`Include ${inc.key} is not registered in AppIncludes`);
}
option = this.fillIncludes(option, inc, resolver);
this.includeOptions.push(option);
});
return this;
}
// tslint:disable-next-line:max-line-length
includeModel(model, where, attributes, as, required, association, through, include) {
const inc = { model, required: !!required };
if (attributes && attributes.length > 0) {
inc.attributes = attributes;
}
if (as) {
inc.as = as;
}
if (where) {
inc.where = where;
}
if (association) {
inc.association = association;
}
if (through) {
inc.through = through;
}
if (include) {
inc.include = include;
}
this.includeOptions.push(inc);
return this;
}
addAttribute(attribute) {
this.attributes instanceof Array ? this.attributes.push(attribute) : this.attributes.include.push(attribute);
return this;
}
addAttributes(attributes) {
this.attributes instanceof Array
? this.attributes.concat(attributes)
: this.attributes.include.concat(attributes);
return this;
}
excludeAttribute(attribute) {
this.attributes = this.attributeArrayToObject(this.attributes);
this.attributes.exclude.push(attribute);
return this;
}
excludeAttributes(attrb) {
this.attributes = this.attributeArrayToObject(this.attributes);
this.attributes.exclude.concat(attrb);
return this;
}
attributeArrayToObject(attrb) {
if (!attrb) {
attrb = { include: [], exclude: [] };
}
if (attrb instanceof Array) {
attrb = { include: attrb, exclude: [] };
}
if (!attrb.exclude) {
attrb.exclude = [];
}
return attrb;
}
addSqlFunc(funcName, columnName, aliasName) {
aliasName = aliasName || columnName + funcName;
columnName = columnName || '1';
const atribute = [vendor_1.fn(funcName, vendor_1.col(columnName)), aliasName];
this.addAttribute(atribute);
return this;
}
addWhereClauseFunc() {
throw new Error('Not Implemented');
// return this;
}
get findOptions() {
let attributes;
if (this.attributes) {
if (this.attributes instanceof Array) {
attributes = this.attributes.length > 0 ? this.attributes : undefined;
}
else {
attributes = (this.attributes.include || []).length > 0 ? this.attributes : undefined;
}
}
return {
attributes,
include: this.includeOptions,
limit: this.pg.limit,
offset: this.pg.offset,
order: this.order,
transaction: this.transaction,
where: this.whereParam,
};
}
// TODO: Need to rewrite this method
addAdvWhareRec(param, resolver) {
let key = param.key;
let value = {};
if (resolver && typeof key === 'number') {
key = resolver(key);
}
if (param.value && typeof param.value !== 'string' && param.value.length > 0) {
param.value.forEach((x) => {
const v = this.addAdvWhareRec(x, resolver);
const k = operator_alias_1.Opa.getOperator(x.key);
if (value[k]) {
let array = Object.getOwnPropertyNames(v);
array = array.concat(Object.getOwnPropertySymbols(v));
array.forEach((p) => {
value[k][p] = v[p];
});
}
else {
value[k] = v;
}
});
}
else if (param.value && typeof param.value === 'object'
&& Object.getOwnPropertySymbols(param.value).length === 0) {
const temp = {};
Object.getOwnPropertyNames(param.value)
.forEach((p) => {
const k = operator_alias_1.Opa.getOperator(p);
temp[k] = param.value[p];
});
value = temp;
}
else {
value = param.value;
}
if (param.searchType && typeof value === 'string') {
value = vendor_1.SearchType[param.searchType] === vendor_1.SearchType[vendor_1.SearchType.StartsWith]
? { [vendor_1.Op.like]: value + '%' }
: { [vendor_1.Op.like]: '%' + value + '%' };
}
return value;
}
fillIncludes(option, inc, resolver) {
const currentInc = { required: !!inc.required };
if (inc.attributes && inc.attributes.length > 0) {
currentInc.attributes = inc.attributes;
}
if (inc.where) {
currentInc.where = inc.where;
}
if (inc.as) {
currentInc.as = inc.as;
}
if (inc.include && inc.include.length > 0) {
const inclOptions = [];
vendor_1._.forEach(inc.include, (inc2) => {
let option2 = resolver(inc2);
if (!option2) {
throw new Error(`Include ${inc.key} is not registered in AppIncludes`);
}
option2 = this.fillIncludes(resolver(inc2), inc2, resolver);
inclOptions.push(option2);
});
currentInc.include = inclOptions;
}
return vendor_1._.assign(option, currentInc);
}
}
exports.QueryOptionBuilder = QueryOptionBuilder;