Repository navigation
From DeriveIntoActiveModel to a Rust MapStruct: should SeaORM add DeriveIntoVo or incubate a dedicated mapper?
#3230
Closed
littlehBinBin
started this conversation in
Ideas
Replies: 2 comments 4 replies
|
https://github.com/SeaQL/.github/blob/master/AI_POLICY.md |
1 reply
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.
From
DeriveIntoActiveModelto a Rust MapStruct: should SeaORM addDeriveIntoVoor incubate a dedicated mapper?Summary
SeaORM already provides:
DeriveIntoActiveModelfor mapping input DTOs toActiveModelIntoActiveModelfor mapping an existingEntity::ModeltoActiveModelFromQueryResultfor mapping query results directly into custom structsHowever, there is no built-in way to map an existing
Entity::Modelto an application-level View DTO or VO.A common pattern is:
This is repetitive when a project has many models and many view DTOs.
Narrow Feature Request
Add a narrow, optional derive macro such as
DeriveIntoVoorDeriveFromModel:It could generate:
Desired Scope
This is not a request to turn SeaORM into MapStruct.
A useful first version could stay intentionally small:
For example:
One important safety property would be that only fields declared on the target VO are mapped. This avoids accidentally exposing fields such as
password,tenant_id, oris_super_admin.Existing Alternatives
Today users can:
impl From<Entity::Model>manually.o2o.FromQueryResultand query directly into a VO when the data comes from the database.But
FromQueryResultdoes not help when the application already has anEntity::Modelin memory.Broader Idea: A Full Rust MapStruct-like Crate
While the immediate request is for a narrow
Model -> View DTOderive, I also want to raise a broader question.Would the SeaORM team consider a more complete, Rust-native mapping ecosystem, similar in spirit to Java MapStruct?
I understand that a full mapper should probably not become part of SeaORM core. SeaORM should remain focused on entities, queries, relations, and persistence. A separate crate or repository may be a better fit.
However, I think there is real development value in a well-maintained mapping crate that supports:
From,Into,TryFrom, andTryIntoOptionhandlingVec,HashMap, and other collectionsModel,ActiveModel, DTOs, commands, and VOsThe most important point is trust and long-term maintenance.
There are already some community crates such as
o2o,structural-convert,model-mapper, andmagic_map, but the ecosystem is fragmented and many users are not sure which one will be maintained long term.If a trusted team maintained a dedicated mapper crate, I believe it could remove a large amount of repetitive code across Rust backend projects. It would not need to become part of SeaORM core, but a SeaQL-adjacent or SeaQL-endorsed crate could be very valuable.
Possible paths:
DeriveIntoVoif it is considered in scope.sea-orm-mapperor a more generalrust-mapper, maintained as a SeaQL-adjacent project.I think the narrow derivation and the full mapping crate are separate decisions.
The narrow feature could solve the common SeaORM pain point. A separate mapper crate could solve the broader ecosystem problem without increasing SeaORM's maintenance burden.
Questions
Model -> View DTOconsidered in scope for SeaORM?I searched existing issues and discussions and could not find an existing proposal specifically for
Model -> View DTO. I also saw thatDeriveIntoActiveModelhas already been extended in related issue #2960, so there may be some precedent for improving derive-based mapping.I would be happy to help clarify the design or contribute a narrow implementation if the team considers this in scope.
All reactions