#sqlx #orm #crud


Derive macro for SQLx to implement Create, Read, Update, and Delete (CRUD) methods for you

5 unstable releases

0.4.0 Apr 18, 2023
0.3.2 Sep 7, 2022
0.3.1 Mar 29, 2022
0.3.0 Mar 26, 2022
0.2.0 Mar 16, 2022

#729 in Database interfaces

Download history 28/week @ 2023-06-04 11/week @ 2023-06-11 40/week @ 2023-06-18 18/week @ 2023-06-25 27/week @ 2023-07-02 45/week @ 2023-07-09 31/week @ 2023-07-16 26/week @ 2023-07-23 19/week @ 2023-07-30 12/week @ 2023-08-06 22/week @ 2023-08-13 22/week @ 2023-08-20 18/week @ 2023-08-27 22/week @ 2023-09-03 28/week @ 2023-09-10 59/week @ 2023-09-17

131 downloads per month
Used in 2 crates (via mostro-core)

MIT license

153 lines


sqlx-crud is an extension to SQLx to derive Create, Read, Update, and Delete (CRUD) methods for a struct representing a table in a sqlx database.

use sqlx::FromRow;
use sqlx_crud::SqlxCrud;

#[derive(Debug, FromRow, SqlxCrud)]
struct User {
    user_id: i32,
    name: String,

if let Some(user) = User::by_id(&pool, 42) {
    println!("Found user user_id=42: {:?}", user);

Notable Features

  • Single Derive Macro for Structs

  • Methods to Create, Read, Update, and Delete Records

  • Primary Key and Table Name Inference

  • Table Metadata for Reuse

  • Internal and External Database ID Assignment

This removes much of the common, repetitive code needed when dealing with the typical CRUD operations.

sqlx-crud strives to do a few, narrowly defined things well in an effort to reduce 80% of the redundant code you might write for a database application when operating on a single table at a time. It provides mechanisms: Schema and Crud, to access and reuse the generated id, column, and query metadata. This can help with writing more complex queries outside of the single table CRUD paradigm, but its primary use case is for CRUD.


Installing sqlx-crud is similar to installing SQLx.

# Cargo.toml
sqlx-crud = { version = "0", features = ["runtime-tokio-rustls"] }

See the documentation for full usage instructions.


sqlx-crud uses the same features as SQLx runtime-* flags and are required because of the dependency on SQLx.


You can find real-world examples under the examples directory.

Source code

Source code for sqlx-crud is available at https://www.github.com/treydempsey/sqlx-crud.


The doctests depend on SQLx and it's runtime-tokio-rustls and sqlite features.

The tests can be run with:

$ cargo test


Documentation is hosted at sqlx-crud docs.


sqlx-crud does most of what I need it to do, however while packaging it for release to others I realized there are several improvements that could be made. Planned updates and major achievements are listed in MILESTONES.


sqlx-crud is licensed under the MIT license (see: LICENSE).


~428K SLoC