@joktec/mysql
Version:
JokTec - MySql Service
128 lines • 5.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MysqlFinder = void 0;
const utils_1 = require("@joktec/utils");
const lodash_1 = require("lodash");
const typeorm_1 = require("typeorm");
const mysql_exception_1 = require("../mysql.exception");
class MysqlFinder {
static parsePagination(query = {}) {
const limit = typeof query.limit === 'number' && query.limit > 0 ? query.limit : undefined;
const page = typeof query.page === 'number' && query.page > 0 ? query.page : undefined;
const offset = typeof query.offset === 'number' && query.offset >= 0 ? query.offset : undefined;
if (limit && page)
return { limit, offset: (page - 1) * limit };
if (limit)
return { limit, offset: offset ?? 0 };
return {};
}
static parseFilter(query) {
const { condition = {}, keyword } = query;
const where = MysqlFinder.parseCondition(condition);
if (keyword && typeof where === 'object') {
where['name'] = (0, typeorm_1.ILike)(`%${keyword}%`);
}
return { where };
}
static parseProjection(select) {
if (typeof select === 'object') {
return Object.entries(select).reduce((acc, [field, direction]) => {
acc[field] = (0, utils_1.toBool)(direction);
return acc;
}, {});
}
return (0, utils_1.toArray)(select, { split: ',' }).reduce((acc, field) => {
acc[field] = true;
return acc;
}, {});
}
static parseCondition(condition) {
const where = {};
for (const [key, value] of Object.entries(condition)) {
if (key === '$and' || key === '$or') {
where[key === '$and' ? 'AND' : 'OR'] = value.map((c) => MysqlFinder.parseCondition(c));
continue;
}
if ((0, lodash_1.isNil)(value)) {
where[key] = (0, typeorm_1.IsNull)();
continue;
}
if (typeof value === 'object') {
const conditions = [];
for (const [op, val] of Object.entries(value)) {
switch (op) {
case '$eq':
conditions.push((0, lodash_1.isNil)(val) ? (0, typeorm_1.IsNull)() : (0, typeorm_1.Equal)(val));
break;
case '$gt':
conditions.push((0, typeorm_1.MoreThan)(val));
break;
case '$gte':
conditions.push((0, typeorm_1.MoreThanOrEqual)(val));
break;
case '$lt':
conditions.push((0, typeorm_1.LessThan)(val));
break;
case '$lte':
conditions.push((0, typeorm_1.LessThanOrEqual)(val));
break;
case '$ne':
conditions.push((0, lodash_1.isNil)(val) ? (0, typeorm_1.Not)((0, typeorm_1.IsNull)()) : (0, typeorm_1.Not)(val));
break;
case '$in':
if ((0, utils_1.toArray)(val).length)
conditions.push((0, typeorm_1.In)((0, utils_1.toArray)(val)));
break;
case '$nin':
if ((0, utils_1.toArray)(val).length)
conditions.push((0, typeorm_1.Not)((0, typeorm_1.In)((0, utils_1.toArray)(val))));
break;
case '$like':
conditions.push((0, typeorm_1.ILike)(`%${val}%`));
break;
case '$begin':
conditions.push((0, typeorm_1.ILike)(`${val}%`));
break;
case '$end':
conditions.push((0, typeorm_1.ILike)(`%${val}`));
break;
case '$not':
conditions.push((0, typeorm_1.Not)(this.parseCondition(val)));
break;
default:
throw new mysql_exception_1.MysqlException(`Operator ${op} not supported`, { op, val });
}
}
if (conditions.length > 1)
where[key] = (0, typeorm_1.And)(...conditions);
else
where[key] = conditions[0];
continue;
}
where[key] = value;
}
return where;
}
static parseOrder(sort) {
const order = {};
for (const [key, value] of Object.entries(sort)) {
order[key] = value === 'asc' ? 'ASC' : 'DESC';
}
return order;
}
static parseRelations(populate) {
const relations = {};
for (const [path, value] of Object.entries(populate)) {
if (value === '*') {
relations[path] = true;
continue;
}
if (typeof value === 'object') {
relations[path] = MysqlFinder.parseRelations(value['populate'] || {});
}
}
return relations;
}
}
exports.MysqlFinder = MysqlFinder;
//# sourceMappingURL=mysql.finder.js.map