UNPKG

adonis-forge

Version:

Bundle utils for AdonisJS

145 lines (143 loc) 4.44 kB
import { QueryHandler } from "./chunk-5AVUSAWG.js"; import { paginate } from "./chunk-XPNES4SH.js"; import { exclude } from "./chunk-UGENPQQY.js"; // src/crud/decorators/crud.ts var crudActions = [ { label: "Paginate", value: "index" }, { label: "Detail", value: "show" }, { label: "Create", value: "store" }, { label: "Update", value: "update" }, { label: "Delete", value: "destroy" } ]; function Crud(options) { const { transformer } = options; let actions = [...crudActions.map((a) => a.value)]; if (options.only) { actions = options.only; } if (options.exclude) { actions = actions.filter((a) => !options.exclude?.includes(a)); } const functionMap = { async index() { }, async show() { }, async store() { }, async update() { }, async destroy() { } }; return (target) => { const targetPrototype = target.prototype; const crudOptions = { ...options, model: targetPrototype.model }; if (targetPrototype.index) { functionMap.index = targetPrototype.index; } else if (actions.includes("index")) { functionMap.index = async (ctx) => { const queryHandler = new QueryHandler(options.model, ctx.request.qs()); const res = await paginate( options.model, (query) => { queryHandler.handle(query); if (options.queryHandlers?.index) { options.queryHandlers.index(query, ctx.request.qs()); } }, { context: ctx } ); if (!transformer) return res; const transformd = transformer?.paginate(res.all(), res.getMeta()); return { data: transformd.collection?.transformerData?.[0], meta: transformd.metaData }; }; } if (targetPrototype.show) { functionMap.show = targetPrototype.show; } else if (actions.includes("show")) { functionMap.show = async (ctx) => { const queryHandler = new QueryHandler(options.model, ctx.request.qs()); const query = queryHandler.handle(options.model.query().where("id", ctx.params.id)); if (options.queryHandlers?.show) { options.queryHandlers.show(query, ctx.request.qs()); } const res = await query.firstOrFail(); return transformer?.transform(res)?.transformerData?.[0] || res; }; } if (targetPrototype.store) { functionMap.store = targetPrototype.store; } else if (actions.includes("store")) { functionMap.store = async (ctx) => { const validater = options.validators?.store; let payload = ctx.request.body(); if (validater) { payload = await ctx.request.validateUsing(validater); } exclude(payload, ["id", "created_at", "updated_at"]); const res = await options.model.create(payload); options.events?.stored?.(res); return transformer?.transform(res)?.transformerData?.[0] || res; }; } if (targetPrototype.update) { functionMap.update = targetPrototype.update; } else if (actions.includes("update")) { functionMap.update = async (ctx) => { const validater = options.validators?.update; let payload = ctx.request.body(); if (validater) { payload = await ctx.request.validateUsing(validater); } exclude(payload, ["id", "created_at", "updated_at"]); const res = await options.model.query().where("id", ctx.params.id).update(payload); options.events?.updated?.(payload); return res; }; } if (targetPrototype.destroy) { functionMap.destroy = targetPrototype.destroy; } else if (actions.includes("destroy")) { functionMap.destroy = async (ctx) => { const ids = ctx.params.id.split(","); const res = await options.model.query().whereIn("id", ids).delete(); return res; }; } Object.assign(targetPrototype, { ...functionMap, model: targetPrototype.model, crudOptions }); Object.defineProperty(targetPrototype, "model", { value: options.model, writable: false, configurable: false }); Object.defineProperty(targetPrototype, "crudOptions", { value: crudOptions, writable: false, configurable: false }); }; } export { crudActions, Crud }; //# sourceMappingURL=chunk-I5HI4GYC.js.map