Replies: 2 comments
-
|
A similar idea (with reverse logic) can be used when the file is actually stored in the database: a handler is needed when serializing to replace the value with a string (or whatever the replacement type is), and to handle the result back by saving the new value if a file is coming. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
And those handlers could optionally do more, by offering a way to generate download links and handling GET requests for those links. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Introduce an
@Assetannotation for JPA entity fields in Hilla that transparently supports file upload and storage integration. The goal is to enhanceCrudRepositoryServiceand entity-based endpoints to supportFile | string | undefinedinput on the client side, while decoupling file persistence from business logic. (Let's assume strings, although that can also be a number, a UUID or something else.)Motivation
File handling in Hilla currently requires manual endpoint design, DTO mapping, and storage logic. This breaks the simplicity of CRUD-style editing via
CrudRepositoryService.By leveraging:
ContextualDeserializerto inject field-specific deserialization logicthe
@Assetpattern can allow seamless full-stack handling of file fields, preserving the developer experience and performance benefits of the Hilla model.Proposal
Java Usage
TypeScript Generation
Runtime Behavior
MultipartFileis submitted as the value ofinvoiceFile, it is passed to a customAssetHandlerimplementation.AssetHandleris user-defined and injected via Spring:handle(...)is used to populate the field (e.g., storing a path, ID, or URI).null, the asset is deleted (field nullability should dictate if the file can be deleted).Implementation Details
Jackson Integration
Implement
MultipartFileDeserializeras aContextualDeserializerUse
DeserializationContext.createContextual(...)to access:Look up the appropriate
AssetHandlerusing Spring byClass(from annotation)Annotation Definition
File Upload Semantics
stringin the field: keep existing file (identified by ID)Filein the field: upload new file and replaceundefined: delete (if allowed)Advantages
AssetHandlerNotes
Example of enhanced deserializer (improving what's already in place):
As a reminder, Hilla is already capable to receive multiple files in a request and put them back in place at the good location in the parameters. So, we can perfectly receive 10 orders, for 3 of which there's a new invoice attached. The functionality has a guard that disallows using
MultipartFileas entity property, but we can allow if@Assetis present.Beta Was this translation helpful? Give feedback.
All reactions