Diesel Rust ORM
Written
— Updated
- Website: https://diesel.rs/
- Useful other crates
- `diesel-derive-enum` helps with translating between Rust enums and database enum types.
- Although Diesel doesn't support async natively, there are many packages that enable that support with minimal fuss. I've been using `deadpool-diesel`.
- Selecting into other structures
- When creating a reusable structure to select into that isn't the normal one, you can derive
Selectable
and then usetype::as_select()
to return the list of columns for a select column. Note that this structure must also implementQueryable
. - This is useful, for example, for structures that include the columns of a database table that the user should be able to see, while omitting internal data.
use crate the_table;
- When creating a reusable structure to select into that isn't the normal one, you can derive
- JSON Deserializing
- One of diesel's design decisions is that it will never include any translation code that could fail to serialize or deserialize data from the database. As such, they do not natively support translating from JSON types into Rust structs using the
Deserialize
attribute. However, this can be implemented generically with a macro, such as this one which converts between the Rust struct and a PostgreSQL JSONB column.
- One of diesel's design decisions is that it will never include any translation code that could fail to serialize or deserialize data from the database. As such, they do not natively support translating from JSON types into Rust structs using the