adonis-forge
Version:
Bundle utils for AdonisJS
139 lines (137 loc) • 4.11 kB
JavaScript
import {
QueryHandler
} from "./chunk-5AVUSAWG.js";
import {
paginate
} from "./chunk-GA46K6AU.js";
import {
exclude
} from "./chunk-UGENPQQY.js";
// src/crud/decorators/crud.ts
import vine from "@vinejs/vine";
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) {
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
}
);
return res;
};
}
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 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(vine.compile(validater));
}
exclude(payload, ["id", "created_at", "updated_at"]);
const res = await options.model.create(payload);
options.events?.stored?.(res);
return 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(vine.compile(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 res = await options.model.query().where("id", ctx.params.id).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-YZWDAUXU.js.map