Comparing version 0.0.10 to 0.0.11
@@ -14,2 +14,3 @@ import { FastifyBaseLogger } from 'fastify'; | ||
table?: TableName | TableName[]; | ||
allow?: "list" | "object" | "all"; | ||
}; | ||
@@ -36,2 +37,3 @@ | ||
filters?: { | ||
id: string; | ||
param?: string; | ||
@@ -89,3 +91,3 @@ field?: string; | ||
declare type HookType = "onRequest" | "bodyModifier" | "postEffect" | "fieldModifier"; | ||
declare type HookType = "onRequest" | "bodyModifier" | "postEffect" | "fieldModifier" | "filter" | "responseModifier"; | ||
@@ -165,3 +167,6 @@ export declare const marceline: (fastify: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>, options: Options) => Promise<void>; | ||
endpointAction: "create" | "get" | "edit" | "delete" | "list"; | ||
fieldId: string; | ||
where: Record<string, any>; | ||
endpointResponse: any; | ||
} | ||
} |
@@ -9,3 +9,3 @@ import { schema, sc } from "compact-json-schema"; | ||
const generateSelect = (columns) => { | ||
if (!columns || columns.length === 0) return void 0; | ||
if (!columns || columns.length === 0) return {}; | ||
const targetObject = {}; | ||
@@ -82,2 +82,11 @@ for (let item of columns) { | ||
}; | ||
const traverseFormFields = (form, callback) => { | ||
for (let item of form) { | ||
if (item.children) { | ||
traverseFormFields(item.children, callback); | ||
} else { | ||
callback(item); | ||
} | ||
} | ||
}; | ||
const getIdField = (form) => { | ||
@@ -121,3 +130,21 @@ const systemTable = Prisma.dmmf.datamodel.models.find((item) => item.name === form.systemTable); | ||
const { page } = req.query; | ||
let createForm = null, editForm = null; | ||
if (req.view.data.create.enabled && req.view.data.create.form) { | ||
createForm = fastify.marceline.forms.getItem(req.view.data.create.form) ?? null; | ||
} | ||
if (req.view.data.edit.enabled && req.view.data.edit.form) { | ||
editForm = fastify.marceline.forms.getItem(req.view.data.edit.form) ?? null; | ||
} | ||
const select = generateSelect(req.view.columns); | ||
if (editForm) { | ||
traverseFormFields(editForm.fields, (field) => { | ||
if (field.jsonField) { | ||
select[field.jsonField] = true; | ||
} else if (field.aliasFieldId) { | ||
select[field.aliasFieldId] = true; | ||
} else if (field.fieldId && field.format !== "password") { | ||
select[field.fieldId] = true; | ||
} | ||
}); | ||
} | ||
const idField = getIdField(req.view); | ||
@@ -141,9 +168,2 @@ if (select) { | ||
} | ||
let createForm = null, editForm = null; | ||
if (req.view.data.create.enabled && req.view.data.create.form) { | ||
createForm = fastify.marceline.forms.getItem(req.view.data.create.form) ?? null; | ||
} | ||
if (req.view.data.edit.enabled && req.view.data.edit.form) { | ||
editForm = fastify.marceline.forms.getItem(req.view.data.edit.form) ?? null; | ||
} | ||
const totalItems = await fastify.prisma[req.view.systemTable].count({}); | ||
@@ -485,16 +505,25 @@ return { | ||
}; | ||
const getWhere = (req, systemTable, filters) => { | ||
const getWhere = async (fasitfy, req, reply, systemTable, filters) => { | ||
if (!filters) return void 0; | ||
const where = {}; | ||
req.where = {}; | ||
const table = Prisma.dmmf.datamodel.models.find((item) => item.name === systemTable); | ||
if (!table) throw new Error(`Table ${table} not found`); | ||
for (let filter of filters) { | ||
if (filter.param && filter.field) { | ||
if (filter.id === "param" && filter.param && filter.field) { | ||
const value = req.params[filter.param]; | ||
const field = table.fields.find((item) => item.name === filter.field); | ||
if (!field) throw new Error(`Field ${field} not found in table ${systemTable}`); | ||
where[filter.field] = parseIdField(field, value); | ||
req.where[filter.field] = parseIdField(field, value); | ||
} else { | ||
const hook = fasitfy.marceline.hooks.filter.get(filter.id); | ||
if (!hook) { | ||
console.warn(`Hook ${hook} not found`); | ||
continue; | ||
} | ||
if (!filter.field) continue; | ||
req.fieldId = filter.field; | ||
const resp = await hook.hook(req, reply); | ||
if (typeof resp === "object" && resp === reply) return resp; | ||
} | ||
} | ||
return where; | ||
}; | ||
@@ -507,4 +536,2 @@ const endpointRoutes = async (fastify, options) => { | ||
fastify.get(item.path, async (req, reply) => { | ||
const select = getSelect(entry.fields); | ||
const where = getWhere(req, item.systemTable, entry.filters); | ||
req.endpointAction = "list"; | ||
@@ -515,6 +542,15 @@ if (entry.hooks.onRequest && entry.hooks.onRequest.length > 0) { | ||
} | ||
const select = getSelect(entry.fields); | ||
const _resp = await getWhere(fastify, req, reply, item.systemTable, entry.filters) ?? {}; | ||
if (typeof _resp === "object" && _resp === reply) return _resp; | ||
const resp = await fastify.prisma[item.systemTable].findMany({ | ||
select, | ||
where | ||
where: req.where | ||
}); | ||
if (entry.hooks.responseModifier && entry.hooks.responseModifier.length > 0) { | ||
req.endpointResponse = resp; | ||
const _resp2 = await fastify.marceline.applyHooks("responseModifier", entry.hooks.responseModifier, req, reply); | ||
if (typeof _resp2 === "object" && _resp2 === reply) return _resp2; | ||
return req.endpointResponse; | ||
} | ||
return resp; | ||
@@ -524,4 +560,2 @@ }); | ||
fastify.get(item.path + "/:itemId", async (req, reply) => { | ||
const select = getSelect(entry.fields); | ||
const where = getWhere(req, item.systemTable, entry.filters); | ||
req.endpointAction = "get"; | ||
@@ -532,7 +566,16 @@ if (entry.hooks.onRequest && entry.hooks.onRequest.length > 0) { | ||
} | ||
const select = getSelect(entry.fields); | ||
const _resp = await getWhere(fastify, req, reply, item.systemTable, entry.filters) ?? {}; | ||
if (typeof _resp === "object" && _resp === reply) return _resp; | ||
const idField = getIdField(item); | ||
const resp = await fastify.prisma[item.systemTable].findUnique({ | ||
select, | ||
where: { id: parseIdField(idField, req.params.itemId), ...where } | ||
where: { id: parseIdField(idField, req.params.itemId), ...req.where } | ||
}); | ||
if (entry.hooks.responseModifier && entry.hooks.responseModifier.length > 0) { | ||
req.endpointResponse = resp; | ||
const _resp2 = await fastify.marceline.applyHooks("responseModifier", entry.hooks.responseModifier, req, reply); | ||
if (typeof _resp2 === "object" && _resp2 === reply) return _resp2; | ||
return req.endpointResponse; | ||
} | ||
return resp; | ||
@@ -615,3 +658,10 @@ }); | ||
const marcelinePlugin = async (fastify, options) => { | ||
const hooks2 = { onRequest: /* @__PURE__ */ new Map(), bodyModifier: /* @__PURE__ */ new Map(), postEffect: /* @__PURE__ */ new Map(), fieldModifier: /* @__PURE__ */ new Map() }; | ||
const hooks2 = { | ||
onRequest: /* @__PURE__ */ new Map(), | ||
bodyModifier: /* @__PURE__ */ new Map(), | ||
postEffect: /* @__PURE__ */ new Map(), | ||
fieldModifier: /* @__PURE__ */ new Map(), | ||
filter: /* @__PURE__ */ new Map(), | ||
responseModifier: /* @__PURE__ */ new Map() | ||
}; | ||
const views2 = new FlatDB({ path: process.cwd() + "/marceline/views" }); | ||
@@ -675,2 +725,5 @@ await views2.init(); | ||
fastify.decorateRequest("endpointAction", null); | ||
fastify.decorateRequest("fieldId"); | ||
fastify.decorateRequest("where"); | ||
fastify.decorateRequest("endpointResponse", null); | ||
}); | ||
@@ -677,0 +730,0 @@ export { |
{ | ||
"name": "marceline", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"main": "dist/plugin/index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
429886
2534