Crates.io | sprattus-derive |
lib.rs | sprattus-derive |
version | 0.0.1 |
source | src |
created_at | 2019-10-18 15:21:27.918367 |
updated_at | 2019-10-18 15:21:27.918367 |
description | The derive macros for a async orm for Postgres |
homepage | |
repository | https://github.com/dutchmartin/sprattus |
max_upload_size | |
id | 173703 |
size | 18,987 |
sprattus is a crate that let's you easily do async CRUD operations on your Postgres database with Rust structs.
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.