Skip to content

Commit

Permalink
feat(code-gen): allow CRUD generation for entities with file relations
Browse files Browse the repository at this point in the history
With fields options, pre-modifiers and custom readable types, the user can handle files however they want to. At some point we will support inline file updates in the CRUD generators.
  • Loading branch information
dirkdev98 committed May 6, 2023
1 parent 247aa0a commit 727b504
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/code-gen/src/crud/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ function crudHandlersGetModifiers(crud) {
modifierDestructure.push(`${crudName}DeletePreModifier,`);
}

// Include all custom modifiers for the nested relations
for (const nestedCrud of crud.nestedRelations) {
// @ts-expect-error
const result = crudHandlersGetModifiers(nestedCrud);

modifierDocs.push(...result.modifierDocs);
modifierDestructure.push(...result.modifierDestructure);
}
Expand Down
18 changes: 7 additions & 11 deletions packages/code-gen/src/processors/crud-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,11 @@ function crudValidateType(generateContext, crud) {
});
}

if (
(model.group === "store" && model.name === "file") ||
model.relations.find(
(it) =>
it.reference.reference.group === "store" &&
it.reference.reference.name === "file",
)
) {
if (model.group === "store" && model.name === "file") {
throw AppError.serverError({
message: `CRUD generation does not support generating routes that include files. This is used either directly via 'T.crud()' or via a relation of the defined '.entity()' in the '${crud.group}' group.`,
message: `CRUD generation does not support generating routes for ${stringFormatNameForError(
model,
)}.`,
});
}

Expand All @@ -98,8 +93,8 @@ function crudValidateType(generateContext, crud) {

if (["oneToOne", "manyToOne"].includes(relation.subType)) {
// Don't allow list, create and delete routes on the owning side of a relation.
// These always reference to a single entity and when removed or recreated don't link
// to this entity anymore.
// These always reference to a single entity and when removed or recreated don't
// link to this entity anymore.
crud.routeOptions ??= {};

Object.assign(crud.routeOptions, {
Expand Down Expand Up @@ -162,6 +157,7 @@ function crudValidateType(generateContext, crud) {

crudInformationSetHasCustomReadableType(crud, hasCustomReadableType);

// Recurse in to the relations
for (const relation of crud.inlineRelations) {
// @ts-expect-error
crudValidateType(generateContext, relation);
Expand Down
2 changes: 1 addition & 1 deletion packages/store/src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export async function fileSyncDeletedWithObjectStorage(sql, s3Client, options) {
export function fileFormatMetadata(file, options) {
if (!options.url) {
throw AppError.serverError({
message: `'fileFormatResponse' requires that the url is provided.`,
message: `'fileFormatMetadata' requires that the url is provided.`,
});
}

Expand Down

0 comments on commit 727b504

Please sign in to comment.