postro-macros 0.1.0

Postro Proc Macro
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented0 out of 1 items with examples
  • Size
  • Source code size: 6.7 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 28.86 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 8s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • ariaandika/postro
    0 0 2
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ariaandika

Postro Postgres Driver

Asynchronous Postgres Driver and Utility.

Originally, this is an attempt to take out the Postgres part of sqlx crate, but the underlying structure is very different, although the public interface api is pretty simmilar.

Installation

To install postro, run:

cargo add postro

or add this line to your Cargo.toml in [dependencies] section:

postro = "0.1.0"

Usage

The general usage is to use database pooling via the Pool API:

use postro::{FromRow, Pool, Result, execute, query};

// automatically extract query result
#[derive(Debug, FromRow)]
struct Post {
    #[allow(unused)]
    id: i32,
    name: String,
}

#[tokio::main]
async fn main() -> Result<()> {
    // will read the `DATABASE_URL` environment variable
    let mut pool = Pool::connect_env().await?;
    let mut handles = vec![];

    // execute a statement
    execute("CREATE TABLE post(id serial, name text)", &mut pool)
        .execute()
        .await?;

    for i in 0..24 {
        // cloning pool is cheap and share the same connection pool
        let mut pool = pool.clone();

        handles.push(tokio::spawn(async move {
            execute("INSERT INTO post(name) VALUES($1)", &mut pool)
                .bind(&format!("thread{i}"))
                .execute()
                .await
        }));
    }

    for h in handles {
        h.await.unwrap()?;
    }

    // extract query result
    let posts = query::<_, _, Post>("SELECT * FROM post", &mut pool)
        .fetch_all()
        .await?;

    assert!(posts.iter().any(|e| e.name.as_str() == "thread23"));
    assert_eq!(posts.len(), 24);

    Ok(())
}

see the documentation for more details

License

This project is licensed under the MIT License - see the LICENSE file for details.