-
Notifications
You must be signed in to change notification settings - Fork 54
Description
(Follow-up from #192)
"model.rs" contains many structs which are used to map to Diesel DB tables, but within this file, there exists some
repetition.
As a prime example, "IdentityMetadata" is flattened and repeated for all tables that contain this similar-looking prefix.
Notably: The representation in the database is unique (there are some identity-related columns that happen to share the same name/type, but are separate in each table) but their in-memory representation is the same.
It would be nice to use a shared structure in Rust to represent these types.
Some of Diesel's derive macros support a field attribute #[diesel(embed)]
which allows nested structures.
These include:
In contrast, the Queryable derive macro does not support this embedding - it requires that the count and order of the Queryable
struct will match the on-disk format, column-for-column.
To make use of embedded structures (like this shared identity prefix), we'd need to do the following:
- For all structs which derive
Queryable
, also deriveSelectable
- Derive
Selectable
on the structure we'd like to embed - Replace the long-form column fields with the
embed
-ed structure - Invoke
.select(MyStruct::as_select())
when doing lookups of the nested object
There are a couple spots where this might be tricky:
- Updating the CTE to make this viable across generic types
- Allowing a "single structure" (like
IdentityMetadata
) to be used across multiple tables. This might require some macro wrapping - theSelectable
derive macro requires a single table selection at declaration time.