UNPKG

@mee4dy/crud

Version:

Create a backend and frontend in 5 minutes! With our powerful full stack crud system, customize it to suit you.

148 lines (147 loc) 4.67 kB
import { Op as g } from "sequelize"; import { FilterType as m } from "../common/enums/filter-type.enum.js"; import { OrderDirection as k } from "../common/enums/order-direction.enum.js"; import { merge as F } from "../common/helpers/merge.helper.js"; import { PK as o } from "../common/constatns/constatns.js"; class K { constructor(e) { this.pk = "id", this.allowFilters = [{ key: o }], this.allowGroups = [], this.allowOrders = [o], this.defaultGroups = [o], this.defaultOrders = [[o, k.desc]], e && Object.assign(this, { ...e }); } getPK() { return this.pk; } getRepository() { return this.repository; } getFields(e = []) { const t = this.fields || [], s = this.fieldsExclude || [], r = []; for (const [n, l] of t) this.allowGroups.includes(l) ? e.length && e.includes(l) ? r.push([n, l]) : s.push(l) : r.push([n, l]); return { include: r, exclude: s }; } getQueryFilters(e) { const t = structuredClone(e); return t != null && t[o] && (t[this.pk] = t[o], delete t[o]), t; } getAllowFilters() { return structuredClone(this.allowFilters).map((t) => ({ ...t, key: t.key === o ? this.pk : t.key })); } getAllowOrders() { return this.allowOrders.map((t) => t === o ? this.pk : t); } getFilters(e) { const t = [], s = this.getQueryFilters(e.filters), r = this.getAllowFilters(); if (s) for (const { key: n, field: l, type: i } of r) { const u = n, c = l || u, h = s == null ? void 0 : s[u], a = s == null ? void 0 : s[`${c}_from`], d = s == null ? void 0 : s[`${c}_to`], f = (this.fields || []).find(([P, O]) => O === c); let w = f ? f[0] : this.repository.sequelize.col(`${this.repository.name}.${c}`); const p = {}; switch (i) { case m.text: h && (p[g.like] = `%${h}%`); break; case m.period: case m.range: a && (p[g.gte] = a), d && (p[g.lte] = d); break; case m.number: default: h && (p[g.eq] = h); break; } w && Reflect.ownKeys(p).length && t.push(this.repository.sequelize.where(w, p)); } return t; } getGroups(e) { const t = [], s = (e.groups || this.defaultGroups).map((r) => r === o ? this.pk : r); if (s) for (let r of this.allowGroups) { const n = r === o ? this.pk : r; s.includes(n) && t.push(n); } return t; } getOrders(e) { const t = [], s = this.getAllowOrders(), r = e.orders ? Object.entries(e.orders).map(([u, c]) => [u, c]) : this.defaultOrders, n = this.getGroups(e), i = this.getFields(n).include.map((u) => u[1]); if (r) for (const u of s) { const c = u, h = !i.length || i.includes(c), a = r.map(([d, f]) => (d === o && (d = this.pk), [d, f])).find(([d, f]) => d === c); a && h && t.push(a); } return t; } getLimit(e) { return e != null && e.limit ? +e.limit : this.limit; } getOffset(e) { return +(e == null ? void 0 : e.offset) || 0; } getIncludes(e) { return e.includes || []; } findAll({ scope: e, ...t }) { return this.repository.scope(e).findAll(t); } findOne({ scope: e, ...t }) { return this.repository.scope(e).findOne(t); } getFindParams({ params: e, query: t }) { var d, f; const s = this.getFilters(t), r = this.getIncludes(t), n = this.getOrders(t), l = this.getGroups(t), i = this.getFields(l), u = this.getLimit(t), c = this.getOffset(t), h = e || {}, a = { attributes: { include: (d = i == null ? void 0 : i.include) != null && d.length ? i == null ? void 0 : i.include : [], exclude: (f = i == null ? void 0 : i.exclude) != null && f.length ? i == null ? void 0 : i.exclude : [] }, include: r, scope: ["defaultScope"], limit: u, offset: c, where: s.length ? s : void 0, order: n, group: l }; return F(a, h); } getItems({ params: e, query: t }) { const s = this.getFindParams({ params: e, query: t }); return this.findAll({ ...s }); } getItem({ params: e, query: t }) { const s = this.getFindParams({ params: e, query: t }); return this.findOne({ ...s }); } create(e) { return this.repository.create(e); } update(e, t, s = !0) { return this.repository.update(t, { where: { [this.pk]: e } }).then((r) => s ? this.findOne({ where: { [this.pk]: e } }) : r); } delete(e) { return this.repository.destroy({ where: e }); } } export { K as CrudService }; //# sourceMappingURL=crud.service.js.map