Skip to content

[diesel][cleanup] Make use of Diesel "Selectable" to cut down on repetition #209

@smklein

Description

@smklein

(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 derive Selectable
  • 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 - the Selectable derive macro requires a single table selection at declaration time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    databaseRelated to database access

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions