UNPKG

slim-ef

Version:

An implementation of basic entity framework functionnalities in typescript

177 lines 5.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseSpecification = void 0; const slim_exp_1 = require("slim-exp"); class BaseSpecification { constructor() { this._includes = []; this._distinct = false; this._chainedIncludes = []; this._criterias = []; this._thenOrderBy = []; this._thenGroupBy = []; this._take = 0; this._skip = 0; } getIncludePaths() { const paths = this._includes.map(i => slim_exp_1.SlimExpression.nameOf(i)); const chainedPaths = this._chainedIncludes.map(ci => { const nameOfInititial = slim_exp_1.SlimExpression.nameOf(ci.initial); return ci.chain.reduce((p, c) => (p.push(`${p[p.length - 1]}.${slim_exp_1.SlimExpression.nameOf(c)}`), p), [nameOfInititial]); }); paths.push(...chainedPaths.reduce((p, c) => (p.push(...c), p), [])); return paths; } getIncludes() { return this._includes; } getDistinct() { return this._distinct; } getFunction() { return this._func; } getChainedIncludes() { return this._chainedIncludes; } getCriterias() { return this._criterias; } getOrderBy() { return this._orderBy; } getGroupBy() { return this._groupBy; } getOrderByDescending() { return this._orderByDescending; } getThenOrderBy() { return this._thenOrderBy; } getThenGroupBy() { return this._thenGroupBy; } getTake() { return this._take; } getSkip() { return this._skip; } getIsPagingEnabled() { return this._isPagingEnabled; } getSelector() { return this._selector; } applyPaging(skip, take) { this._skip = skip; this._take = take; this._isPagingEnabled = true; } addInclude(include) { if (include) { this._includes.push(include); this._initializeThenInclude = true; } } applyDistinct(distinct = true) { this._distinct = distinct; } addChainedInclude(include, include2, include3, include4, include5) { const chain = []; if (include2) chain.push(include2); if (include3) chain.push(include3); if (include4) chain.push(include4); if (include5) chain.push(include5); if (chain.length === 0) { this._includes.push(include); return; } this._includes = this._includes.filter(v => v.toString() !== include.toString()); const lastIndex = this._chainedIncludes .map(val => val.initial.toString().trim()) .lastIndexOf(include.toString().trim()); const i = this._chainedIncludes[lastIndex]; if (i && !this._initializeThenInclude) { chain.forEach(c => { if (!i.chain.find(v => v.toString() === c.toString())) { i.chain.push(c); } }); } else { this._chainedIncludes.push({ initial: include, chain }); this._initializeThenInclude = false; } } addCriteria(func, context) { if (func) this._criterias.push({ func, context }); } applySelector(selector) { if (selector) this._selector = selector; } applyOrderBy(orderBy) { this._orderBy = orderBy; this._orderByDescending = null; } applyGroupBy(groupBy) { this._groupBy = groupBy; } applyFunction(type, func) { if (type) this._func = { type, func }; } applyThenOrderBy(thenOrderBy) { if (thenOrderBy) { this._thenOrderBy.push(thenOrderBy); } } applyThenGroupBy(thenBy) { if (thenBy) { this._thenGroupBy.push(thenBy); } } applyOrderByDescending(orderBy) { this._orderByDescending = orderBy; this._orderBy = null; } extend(spec) { this._includes.concat(spec.getIncludes()); this._distinct = spec.getDistinct(); this._chainedIncludes.concat(spec.getChainedIncludes()); this._criterias.concat(spec.getCriterias()); this._skip = spec.getSkip(); this._take = spec.getTake(); this._isPagingEnabled = spec.getIsPagingEnabled(); } clearSpecs() { this._includes = []; this._distinct = false; this._chainedIncludes = []; this._criterias = []; this._func = null; this._isPagingEnabled = false; this._orderBy = null; this._orderByDescending = null; this._selector = null; this._skip = 0; this._take = 0; this._thenOrderBy = []; this._thenGroupBy = []; } } exports.BaseSpecification = BaseSpecification; //# sourceMappingURL=base.specification.js.map