Crudly
Derivable crud helpers for sqlx. Write your SELECT, INSERT, UPDATE, and DELETE queries once and reuse them for all your entities.
Provides the #[derive(IntoRow)] and #[derive(Crudly)] macros.
Features
derive: (Default) Enables the#[derive(Crudly)]and#[derive(IntoRow)]macrospostgres: Enables default crud implementations for databasemysql: Enables default crud implementations for MySQLsqlite: Enables default crud implementations for sqliteall-databases: Enables default crud implementations all DBs abovejson: Enables support for the#[sqlx(json)]/#[crudly(json)]attribute using serde.
Example
const CREATE_USERS_TABLE_SQL: &str =
"CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT NOT NULL);";
async
The IntoRow trait
IntoRow tries to be the counterpart to sqlx::FromRow. It exposes the following two functions:
columns: Returns the column names for the struct (respecting all sqlx attributes)bind_arguments: Binds the values of the struct to an SQLx [sqlx::Arguments] buffer Deriving IntoRow IntoRow understands the same attributes assqlx::FromRowsupports as of sqlx 0.8.6. So The code below will just work:
assert_eq!;
In case you only want to derive IntoRow and not FromRow all the attributes also have a #[crudly(rename = "name")] format.
Crudly understands the #[sqlx(try_from = ...))] as #[crudly(try_into = ...)] for binding to arguments it will then use
the TryInto trait before decoding.
Note: the field with the #[crudly(id)] attribute will never be included in the arguments generated by IntoRow,
because depending on context the id might or might not be desired.
The Crudly trait
Deriving Crudly enables the following methods: select_all, select_by_id, update_by_id,
id_exists, delete_by_id and either insert_returning_id or insert_with_id (see next section).
IDs
Currently crudly requires that every entity has a single id column that is expected to be unique (probably a PK).
A future version might introduce support for multiple id columns if theres a need for it.
The id must be marked with the #[crudly(id)] attribute.
Further crudly distinguishes between two types of ids:
DBAssignedId: The id is assigned by the database using something like an AUTOINCREMENT or SERIAL column.ExternallyAssignedId: The id is assigned by the application using something like a uuid. By default crudly will assume that the id is assigned by the database. If you want to assign the id inside of rust instead, use#[crudly(external_ids)]on the struct. See the external_ids example.
Executors
Crudly provides default implementations for performing the crud operations for sqlite, mysql and postgres.
However you might want to customize their behavior. Add some logging, return a custom error type or even add another Database.
For this purpose crudly provides the CRUDExecutor trait. By default crudly will use the DefaultCRUDExecutor struct for all crud operations.
you can overwrite this by using the #[crudly(executor = ...)] attribute. See the executor example for more details.
// <-- Use your custom executor here
MSRV
The minimum supported Rust version is 1.85.0 (the version that released edition 2024).
Contributing
Contributions, bug reports and feature requests are always welcome! Please open an issue or a pull request on GitHub.