Crate parsql_tokio_postgres

Source
Expand description

§parsql-tokio-postgres

Asynchronous PostgreSQL integration for parsql using tokio-postgres. This crate provides async/await APIs for working with PostgreSQL databases.

§Features

  • Asynchronous PostgreSQL operations
  • Automatic SQL query generation
  • Secure parameter management
  • Generic CRUD operations
  • Deadpool connection pool support
  • SQL Injection protection
  • Detailed error reporting

§Usage

use tokio_postgres::{NoTls, Error};
use parsql::tokio_postgres::{CrudOps};
use parsql::macros::{Insertable, Queryable, SqlParams, FromRow};
 
#[derive(Insertable, SqlParams)]
#[table("users")]
pub struct InsertUser {
    pub name: String,
    pub email: String,
}
 
#[derive(Queryable, SqlParams, FromRow)]
#[table("users")]
#[where_clause("id = $")]
pub struct GetUser {
    pub id: i32,
    pub name: String,
    pub email: String,
}
 
impl GetUser {
    pub fn new(id: i32) -> Self {
        Self {
            id,
            name: Default::default(),
            email: Default::default(),
        }
    }
}
 
#[tokio::main]
async fn main() -> Result<(), Error> {
    let (client, connection) = tokio_postgres::connect(
        "host=localhost user=postgres dbname=test",
        NoTls,
    ).await?;
     
    tokio::spawn(async move {
        if let Err(e) = connection.await {
            eprintln!("Connection error: {}", e);
        }
    });
     
    // Insert a new user
    let insert_user = InsertUser {
        name: "John".to_string(),
        email: "john@example.com".to_string(),
    };
     
    // Extension method style
    let id = client.insert(insert_user).await?;
     
    // Get the user back
    let get_user = GetUser::new(id as i32);
    let user = client.fetch(get_user).await?;
     
    println!("User: {:?}", user);
    Ok(())
}

The trait-based extension methods can also be used with client objects from deadpool-postgres:

use parsql::tokio_postgres::CrudOps;
use deadpool_postgres::{Config, Pool};
use tokio_postgres::NoTls;

async fn example() -> Result<(), Box<dyn std::error::Error>> {
    let mut cfg = Config::new();
    cfg.host = Some("localhost".to_string());
    cfg.user = Some("postgres".to_string());
     
    let pool = cfg.create_pool(None, NoTls)?;
     
    // Get client from pool
    let client = pool.get().await?;
     
    // Use extension methods
    let users = client.fetch_all(active_users_query).await?;
     
    Ok(())
}

Re-exports§

pub use crate::crud_ops::insert;
pub use crate::crud_ops::update;
pub use crate::crud_ops::delete;
pub use crate::crud_ops::fetch;
pub use crate::crud_ops::fetch_all;
pub use crate::crud_ops::select;
pub use crate::crud_ops::select_all;
pub use crate::crud_ops::get;Deprecated
pub use crate::crud_ops::get_all;Deprecated
pub use transaction_ops as transactional;
pub use macros::*;

Modules§

crud_ops
macros
traits
transaction_ops
Transaction support module

Structs§

Client
An asynchronous PostgreSQL client.
Error
An error communicating with the Postgres server.
Row
A row of data returned from the database by a query.

Traits§

ToSql
A trait for types that can be converted into Postgres values.