#async-orm #postgresql #macro #struct #sprattus

macro sprattus-derive

The derive macros for a async orm for Postgres

1 unstable release

0.0.1 Oct 18, 2019

#50 in #async-orm


Used in sprattus

Custom license

19KB
475 lines

sprattus, the async Rust ORM for Postgres

The Future .awaits

sprattus is a crate that let's you easily do async CRUD operations on your Postgres database with Rust structs.

Getting started

Add sprattus to your cargo.toml:

sprattus = "0.0.1"

Create a table in Postgres:

CREATE TABLE fruits(
   id SERIAL PRIMARY KEY,
   name VARCHAR NOT NULL
);

Create a struct corresponding to the created table:

struct Fruit {
    id: i32,
    name: String
}

And finally add the sprattus macro's and annotations:

use sprattus::*;

#[derive(ToSql, FromSql, Debug)]
#[sql(table = "fruits")]
struct Fruit {
    #[sql(primary_key)]
    id: i32,
    name: String
}

And now you're ready to use the client in combination with you freshly created struct!

use tokio::prelude::*;
use sprattus::*;

#[derive(ToSql, FromSql)]
#[sql(table = "fruits")]
struct Fruit {
    #[sql(primary_key)]
    id: i32,
    name: String
}

#[tokio::main]
async fn main() -> Result<(), Error>{
    let conn = PGConnection::new("postgresql://localhost/dellstore2?user=tg").await.unwrap();
    let fruit = Fruit{
        id: 0,
        name: String::from("apple")
    };
    let created_fruit = conn.create(fruit).await?;
    dbg!(created_fruit);
    Ok(())
}

Please check out the docs for further reference.

Dependencies

~1.5MB
~37K SLoC