#sqlx #postgres #orm #activerecord


ActiveRecord based on SQLx for Postgres

5 releases

0.1.6 Jul 17, 2022
0.1.5 Jul 16, 2022
0.1.3 May 28, 2022

#1039 in Database interfaces

Download history 1/week @ 2023-06-03 22/week @ 2023-06-17 14/week @ 2023-06-24 72/week @ 2023-07-01 27/week @ 2023-07-08 4/week @ 2023-07-15 10/week @ 2023-07-22 19/week @ 2023-07-29 17/week @ 2023-08-05 11/week @ 2023-08-12 19/week @ 2023-08-19 17/week @ 2023-08-26 19/week @ 2023-09-02 20/week @ 2023-09-09 21/week @ 2023-09-16

81 downloads per month


227 lines

Sqlx Models ORM

ActiveRecord pattern for Rust based on SQLx. Write idiomatic DB code (Postgres only).

sqlx-models-orm = "0.1"


Read the in-depth tutorial that doubles as a "kitchen sink" test in the examples

These are just some of the time-saving, boilerplate-killing features:


    state: App,
    table: humans,
    struct Human {
      #[sqlx_model_hints(int4, default)]
      id: i32,
      name: String,
      age: Option<i32>,
      #[sqlx_model_hints(boolean, default)]
      is_allowed_unlimited_cats: bool,
      likes_dogs_too: bool,
    has_many {


  let alice = app.human()
      name: "Alice".to_string(),
      age: Some(19),
      likes_dogs_too: true,

  assert_eq!(alice.attrs, HumanAttrs{
    id: 1,
    name: "Alice".to_string(),
    age: Some(19),
    is_allowed_unlimited_cats: false,
    likes_dogs_too: true,


  let some_humans = app.human()

  assert_eq!(some_humans, vec![alice]);


  let updated_alice = alice.update().use_struct(UpdateHuman{
    name: Some("Alice Alison".to_string()),
    age: Some(None),
    is_allowed_unlimited_cats: Some(true),

  assert_eq!(updated_alice.attrs, HumanAttrs{
    id: 1,
    name: "Alice Alison".to_string(),
    age: None,
    is_allowed_unlimited_cats: true,
    likes_dogs_too: true,



Design principles:

  • Stateful: You're not supposed to be passing a connection pool around explicitly.
  • Your structs, your abstractions: This crate has a proc macro that creates a number of structs for different operations on a single database table. You can add any methods you want to any of these structs. Structs for the same operation in different tables implement a common trait to allow some degree of generalization across operations in these tables.
  • Idiomatic rather than performant: This should be easy to learn and use, even if there were performance tradeoffs.
  • Fallaback to SQLx: Always make it possible to fall back for custom queries and performance enhancements.
  • One table per query. Reinventing SQL for joining tables in the ORM is hard to debug and understand. Favour multiple single-table queries over a single multi-table one. (see previous item).
  • Only compile time checked queries. No chance of sql injection, no need for silly tests, at the cost of longer queries.
  • Only Postgres for now. Sorry about that :(


~1M SLoC