slim-ef
Version:
An implementation of basic entity framework functionnalities in typescript
72 lines • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.format = exports.convertToSqlComparisonOperator = exports.ComparisonOperators = exports.SQLJoinFunctions = exports.SQLArrayFunctions = exports.SQLStringFunctions = exports.SQLConstants = exports.isQueryable = exports.isSpecification = void 0;
const slim_exp_1 = require("slim-exp");
const nameof = slim_exp_1.SlimExpression.nameOf;
function isSpecification(obj) {
return (nameof(s => s.getCriterias) in obj &&
nameof(s => s.getIncludes) in obj &&
nameof(s => s.getIsPagingEnabled) in obj &&
nameof(s => s.getOrderBy) in obj &&
nameof(s => s.getOrderByDescending) in obj &&
nameof(s => s.getSelector) in obj &&
nameof(s => s.getSkip) in obj &&
nameof(s => s.getTake) in obj);
}
exports.isSpecification = isSpecification;
function isQueryable(obj) {
const props = [
nameof(s => s.include),
nameof(s => s.orderBy),
nameof(s => s.orderByDescending),
nameof(s => s.select),
nameof(s => s.thenOrderBy),
nameof(s => s.where)
];
const content = obj.toString();
return props.some(p => content.includes(`.${p}(`));
}
exports.isQueryable = isQueryable;
exports.SQLConstants = {
FALSE: 'FALSE',
TRUE: 'TRUE'
};
exports.SQLStringFunctions = {
startsWith: "LOWER({0}) LIKE LOWER('{1}%')",
endsWith: "LOWER({0}) LIKE LOWER('%{1}')",
includes: "LOWER({0}) LIKE LOWER('%{1}%')",
toLowerCase: 'LOWER({0})',
toUpperCase: 'UPPER({0})'
};
exports.SQLArrayFunctions = {
includes: '{0} IN ({1})'
};
exports.SQLJoinFunctions = {
some: 'some'
};
exports.ComparisonOperators = {
ALL: ['==', '===', '>=', '<=', '!=', '!==', '<', '>'],
EQUAL_TO: '==',
STRICTLY_EQUAL_TO: '===',
GREATER_THAN_OR_EQUAL: '>=',
GREATER_THAN: '>',
LESS_THAN_OR_EQUAL: '<=',
LESS_THAN: '<',
NOT_EQUAL_TO: '!=',
STRICTLY_NOT_EQUAL_TO: '!=='
};
function convertToSqlComparisonOperator(op, val) {
let res = '' + op;
if (res === exports.ComparisonOperators.EQUAL_TO ||
res === exports.ComparisonOperators.STRICTLY_EQUAL_TO)
res = val === null ? 'is' : '=';
else if (res === exports.ComparisonOperators.STRICTLY_NOT_EQUAL_TO)
res = val === null ? 'is not' : exports.ComparisonOperators.NOT_EQUAL_TO;
return res;
}
exports.convertToSqlComparisonOperator = convertToSqlComparisonOperator;
function format(toFormat, ...args) {
return toFormat.replace(/{(\d+)}/g, (match, n) => typeof args[n] !== 'undefined' ? args[n] : match);
}
exports.format = format;
//# sourceMappingURL=utils.js.map