Comparing version 0.0.21 to 0.0.22
@@ -79,3 +79,3 @@ import { FastifyBaseLogger } from 'fastify'; | ||
placeholder?: string; | ||
fileField?: string; | ||
fileIdField?: string; | ||
jsonField?: string; | ||
@@ -82,0 +82,0 @@ isCustom?: boolean; |
@@ -33,2 +33,33 @@ import { schema, sc } from "compact-json-schema"; | ||
}; | ||
const attachFiles = async (prisma, filesTable, items, fileFields) => { | ||
const fileIds = []; | ||
for (let item of items) { | ||
for (let field of fileFields) { | ||
if (!item[field.fileIdField]) continue; | ||
if (Array.isArray(item[field.fileIdField])) { | ||
fileIds.push(...item[field.fileIdField]); | ||
item[field.fieldId] = []; | ||
} else { | ||
fileIds.push(item[field.fileIdField]); | ||
item[field.fieldId] = null; | ||
} | ||
} | ||
} | ||
if (fileIds.length > 0) { | ||
const files2 = await prisma[filesTable].findMany({ | ||
where: { id: { in: fileIds } } | ||
}); | ||
const filesMap = new Map(files2.map((item) => [item.id, item])); | ||
for (let item of items) { | ||
for (let field of fileFields) { | ||
if (!item[field.fileIdField]) continue; | ||
if (Array.isArray(item[field.fileIdField])) { | ||
item[field.fieldId] = item[field.fileIdField].map((item2) => filesMap.get(item2)); | ||
} else { | ||
item[field.fieldId] = filesMap.get(item[field.fileIdField]) ?? null; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
const getIdField = (form) => { | ||
@@ -88,12 +119,13 @@ const systemTable = Prisma.dmmf.datamodel.models.find((item) => item.name === form.systemTable); | ||
} | ||
if (item.fieldId in body) { | ||
_body[item.aliasFieldId ?? item.fieldId] = value; | ||
} | ||
if (item.fileField && item.fileField in body) { | ||
if (item.fileIdField && item.fieldId in body) { | ||
if (item.format === "files-group") { | ||
_body[item.fieldId] = body[item.fileField].map((item2) => item2.id); | ||
_body[item.fileIdField] = body[item.fieldId].map((item2) => item2.id); | ||
} else { | ||
_body[item.fieldId] = body[item.fileField]?.id ?? null; | ||
_body[item.fileIdField] = body[item.fieldId]?.id ?? null; | ||
} | ||
continue; | ||
} | ||
if (item.fieldId in body) { | ||
_body[item.aliasFieldId ?? item.fieldId] = value; | ||
} | ||
} | ||
@@ -234,3 +266,3 @@ req.modifiedBody = _body; | ||
const PAGE_SIZE = 20; | ||
const data = async (fastify, { onRequest }) => { | ||
const data = async (fastify, { onRequest, files: files2 }) => { | ||
if (onRequest) { | ||
@@ -248,2 +280,11 @@ fastify.addHook("onRequest", onRequest); | ||
}); | ||
const filesReg = /file/i; | ||
const systemTable = files2?.systemTable?.toLowerCase(); | ||
const filesTable = Prisma.dmmf.datamodel.models.find((item) => { | ||
if (systemTable) { | ||
return item.name.toLowerCase() === systemTable; | ||
} else { | ||
return filesReg.test(item.name); | ||
} | ||
}); | ||
const getDataQuery = schema({ page: "number?" }); | ||
@@ -262,2 +303,3 @@ fastify.get("/data/:viewId/items", sc(params, getDataQuery, "query"), async (_req, reply) => { | ||
const select = generateSelect(req.view.columns, postCallbacks); | ||
const fileFields = []; | ||
if (editForm) { | ||
@@ -272,4 +314,4 @@ traverseFormFields(editForm.fields, (field) => { | ||
} else if (field.fieldId && field.relationBridgeFieldId) { | ||
const table = Prisma.dmmf.datamodel.models.find((item) => item.name === field.relationType); | ||
const keys = ["name", "surname", "email", "id", "uuid"].filter((fieldId) => table.fields.find((item) => item.name === fieldId)); | ||
const relationTable = Prisma.dmmf.datamodel.models.find((item) => item.name === field.relationType); | ||
const keys = ["name", "surname", "email", "id", "uuid"].filter((fieldId) => relationTable.fields.find((item) => item.name === fieldId)); | ||
select[field.fieldId] = { | ||
@@ -288,2 +330,5 @@ select: { | ||
select[field.aliasFieldId] = true; | ||
} else if (field.fileIdField) { | ||
select[field.fileIdField] = true; | ||
fileFields.push(field); | ||
} else if (field.fieldId && field.format !== "password") { | ||
@@ -304,2 +349,5 @@ select[field.fieldId] = true; | ||
}); | ||
if (fileFields.length > 0 && filesTable) { | ||
await attachFiles(fastify.prisma, filesTable.name, resp, fileFields); | ||
} | ||
const totalItems = await fastify.prisma[req.view.systemTable].count({}); | ||
@@ -306,0 +354,0 @@ if (resp.length > 0 && postCallbacks.length > 0) { |
{ | ||
"name": "marceline", | ||
"version": "0.0.21", | ||
"version": "0.0.22", | ||
"main": "dist/plugin/index.js", | ||
@@ -22,3 +22,4 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@fastify/multipart": "^8.3.0", | ||
"@fastify/multipart": "^9.0.1", | ||
"@fastify/static": "^8.0.3", | ||
"@prisma/client": "^5.18.0", | ||
@@ -32,3 +33,3 @@ "@types/leaflet": "^1.9.14", | ||
"dotenv": "^16.4.5", | ||
"fastify": "^4.28.1", | ||
"fastify": "^5.1.0", | ||
"fastify-plugin": "^4.5.1", | ||
@@ -35,0 +36,0 @@ "jwt-runner": "^0.0.2", |
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
713221
3920
27