Comparing version 0.0.15 to 0.0.16
@@ -88,2 +88,7 @@ import { FastifyBaseLogger } from 'fastify'; | ||
relationBridgeFieldId?: string; | ||
value?: any; | ||
columns: Array<{ | ||
enabled: boolean; | ||
fieldId: string; | ||
}>; | ||
}; | ||
@@ -90,0 +95,0 @@ |
@@ -58,2 +58,5 @@ import { schema, sc } from "compact-json-schema"; | ||
let value = body[item.fieldId]; | ||
if (item.format === "const") { | ||
value = item.value; | ||
} | ||
if (item.modifiers && item.modifiers.length > 0) { | ||
@@ -68,67 +71,9 @@ req.currentField = value; | ||
if (item.format === "multiselect" && item.relationType && Array.isArray(value)) { | ||
if (!req.postCallbacks) req.postCallbacks = []; | ||
const idField = getIdField({ systemTable: item.relationType }); | ||
const sourceTable = Prisma.dmmf.datamodel.models.find((table) => table.name === form.systemTable); | ||
if (item.relationBridgeType) { | ||
req.postCallbacks.push(async (obj) => { | ||
const relationTable = Prisma.dmmf.datamodel.models.find((table) => table.name === item.relationBridgeType); | ||
const sourceRelationField = sourceTable.fields.find((field) => field.name === item.fieldId).relationName; | ||
const relationField = relationTable.fields.find((field) => field.relationName === sourceRelationField); | ||
const oppositeField = relationTable.fields.find((field) => field.name === item.relationBridgeFieldId); | ||
const data2 = Object.fromEntries(relationField.relationFromFields.map((item2, index) => [item2, obj[relationField.relationToFields[index]]])); | ||
const existingItems = await fastify.prisma[item.relationBridgeType].findMany({ | ||
where: data2 | ||
}); | ||
const set = new Set(existingItems.map((item2) => item2[oppositeField.relationFromFields[0]])); | ||
const newItemIds = []; | ||
for (let item2 of value) { | ||
if (set.has(item2[idField.name])) { | ||
set.delete(item2[idField.name]); | ||
} else { | ||
newItemIds.push(item2[idField.name]); | ||
} | ||
} | ||
if (set.size > 0) { | ||
await fastify.prisma[item.relationBridgeType].deleteMany({ | ||
where: { | ||
...data2, | ||
[oppositeField.relationFromFields[0]]: { in: Array.from(set.values()) } | ||
} | ||
}); | ||
} | ||
if (newItemIds.length > 0) { | ||
await fastify.prisma[item.relationBridgeType].createMany({ | ||
data: newItemIds.map((itemId) => ({ | ||
...data2, | ||
[oppositeField.relationFromFields[0]]: itemId | ||
})) | ||
}); | ||
} | ||
}); | ||
continue; | ||
} | ||
req.postCallbacks.push(async (obj) => { | ||
const relationTable = Prisma.dmmf.datamodel.models.find((table) => table.name === item.relationType); | ||
const sourceRelationField = sourceTable.fields.find((field) => field.name === item.fieldId).relationName; | ||
const relationField = relationTable.fields.find((item2) => item2.relationName === sourceRelationField); | ||
const data2 = Object.fromEntries(relationField.relationFromFields.map((item2, index) => [item2, obj[relationField.relationToFields[index]]])); | ||
const nullData = Object.fromEntries(relationField.relationFromFields.map((item2, index) => [item2, null])); | ||
if (value.length > 0) { | ||
await fastify.prisma[item.relationType].updateMany({ | ||
data: data2, | ||
where: { [idField.name]: { in: value.map((item2) => item2[idField.name]) } } | ||
}); | ||
} | ||
if (!relationField.isRequired) { | ||
await fastify.prisma[item.relationType].updateMany({ | ||
data: nullData, | ||
where: { | ||
[idField.name]: { notIn: value.map((item2) => item2[idField.name]) }, | ||
...data2 | ||
} | ||
}); | ||
} | ||
}); | ||
insertMultiselectValues(fastify, req, item, value, form.systemTable); | ||
continue; | ||
} | ||
if (item.format === "subitems" && item.relationType) { | ||
insertSubitemsValues(fastify, req, item, value, form.systemTable); | ||
continue; | ||
} | ||
if (item.jsonField) { | ||
@@ -182,2 +127,107 @@ if (!_body[item.jsonField]) { | ||
}; | ||
const insertSubitemsValues = (fastify, req, item, value, systemTable) => { | ||
if (!req.postCallbacks) req.postCallbacks = []; | ||
if (!item.relationType) return; | ||
const idField = getIdField({ systemTable: item.relationType }); | ||
const sourceTable = Prisma.dmmf.datamodel.models.find((table) => table.name === systemTable); | ||
req.postCallbacks.push(async (obj) => { | ||
const relationTable = Prisma.dmmf.datamodel.models.find((table) => table.name === item.relationType); | ||
const sourceRelationField = sourceTable.fields.find((field) => field.name === item.fieldId).relationName; | ||
const relationField = relationTable.fields.find((item2) => item2.relationName === sourceRelationField); | ||
const data2 = Object.fromEntries(relationField.relationFromFields.map((item2, index) => [item2, obj[relationField.relationToFields[index]]])); | ||
const existingItems = await fastify.prisma[item.relationType].findMany({ | ||
where: data2 | ||
}); | ||
const toDelete = new Set(existingItems.map((item2) => item2[idField.name])); | ||
for (let newItem of value) { | ||
if (!newItem[idField.name]) { | ||
await fastify.prisma[item.relationType].create({ | ||
data: { ...newItem, ...data2 } | ||
}); | ||
continue; | ||
} | ||
const newItemId = parseIdField(idField, newItem[idField.name]); | ||
toDelete.delete(newItemId); | ||
const existingItem = existingItems.find((existingItem2) => existingItem2[idField.name] === newItemId); | ||
if (!existingItem) continue; | ||
await fastify.prisma[item.relationType].update({ | ||
where: { [idField.name]: existingItem[idField.name], ...data2 }, | ||
data: { ...existingItem[idField.name], ...newItem } | ||
}); | ||
} | ||
if (toDelete.size > 0) { | ||
await fastify.prisma[item.relationType].deleteMany({ | ||
where: { | ||
...data2, | ||
[idField.name]: { in: Array.from(toDelete.values()) } | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
const insertMultiselectValues = (fastify, req, item, value, systemTable) => { | ||
if (!req.postCallbacks) req.postCallbacks = []; | ||
const idField = getIdField({ systemTable: item.relationType }); | ||
const sourceTable = Prisma.dmmf.datamodel.models.find((table) => table.name === systemTable); | ||
if (item.relationBridgeType) { | ||
req.postCallbacks.push(async (obj) => { | ||
const relationTable = Prisma.dmmf.datamodel.models.find((table) => table.name === item.relationBridgeType); | ||
const sourceRelationField = sourceTable.fields.find((field) => field.name === item.fieldId).relationName; | ||
const relationField = relationTable.fields.find((field) => field.relationName === sourceRelationField); | ||
const oppositeField = relationTable.fields.find((field) => field.name === item.relationBridgeFieldId); | ||
const data2 = Object.fromEntries(relationField.relationFromFields.map((item2, index) => [item2, obj[relationField.relationToFields[index]]])); | ||
const existingItems = await fastify.prisma[item.relationBridgeType].findMany({ | ||
where: data2 | ||
}); | ||
const set = new Set(existingItems.map((item2) => item2[oppositeField.relationFromFields[0]])); | ||
const newItemIds = []; | ||
for (let item2 of value) { | ||
if (set.has(item2[idField.name])) { | ||
set.delete(item2[idField.name]); | ||
} else { | ||
newItemIds.push(item2[idField.name]); | ||
} | ||
} | ||
if (set.size > 0) { | ||
await fastify.prisma[item.relationBridgeType].deleteMany({ | ||
where: { | ||
...data2, | ||
[oppositeField.relationFromFields[0]]: { in: Array.from(set.values()) } | ||
} | ||
}); | ||
} | ||
if (newItemIds.length > 0) { | ||
await fastify.prisma[item.relationBridgeType].createMany({ | ||
data: newItemIds.map((itemId) => ({ | ||
...data2, | ||
[oppositeField.relationFromFields[0]]: itemId | ||
})) | ||
}); | ||
} | ||
}); | ||
return; | ||
} | ||
req.postCallbacks.push(async (obj) => { | ||
const relationTable = Prisma.dmmf.datamodel.models.find((table) => table.name === item.relationType); | ||
const sourceRelationField = sourceTable.fields.find((field) => field.name === item.fieldId).relationName; | ||
const relationField = relationTable.fields.find((item2) => item2.relationName === sourceRelationField); | ||
const data2 = Object.fromEntries(relationField.relationFromFields.map((item2, index) => [item2, obj[relationField.relationToFields[index]]])); | ||
const nullData = Object.fromEntries(relationField.relationFromFields.map((item2, index) => [item2, null])); | ||
if (value.length > 0) { | ||
await fastify.prisma[item.relationType].updateMany({ | ||
data: data2, | ||
where: { [idField.name]: { in: value.map((item2) => item2[idField.name]) } } | ||
}); | ||
} | ||
if (!relationField.isRequired) { | ||
await fastify.prisma[item.relationType].updateMany({ | ||
data: nullData, | ||
where: { | ||
[idField.name]: { notIn: value.map((item2) => item2[idField.name]) }, | ||
...data2 | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
const PAGE_SIZE = 20; | ||
@@ -214,2 +264,6 @@ const data = async (fastify, { onRequest }) => { | ||
select[field.jsonField] = true; | ||
} else if (field.fieldId && field.format === "subitems") { | ||
select[field.fieldId] = { | ||
select: Object.fromEntries(field.columns.map((item) => [item.fieldId, true])) | ||
}; | ||
} else if (field.fieldId && field.relationBridgeFieldId) { | ||
@@ -216,0 +270,0 @@ const table = Prisma.dmmf.datamodel.models.find((item) => item.name === field.relationType); |
{ | ||
"name": "marceline", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"main": "dist/plugin/index.js", | ||
@@ -29,2 +29,3 @@ "license": "MIT", | ||
"dotenv": "^16.4.5", | ||
"fastify": "^4.28.1", | ||
"jwt-runner": "^0.0.2", | ||
@@ -48,6 +49,8 @@ "pinia": "^2.2.1", | ||
"dayjs": "^1.11.12", | ||
"fastify": "^4.28.1", | ||
"fastify-plugin": "^4.5.1", | ||
"uid": "^2.0.2" | ||
}, | ||
"peerDependencies": { | ||
"fastify": "^4" | ||
}, | ||
"prisma": { | ||
@@ -54,0 +57,0 @@ "schema": "src/app/prisma/schema.prisma" |
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
497257
2955
21
- Removedfastify@^4.28.1