Docs.rs
  • parsql-tokio-postgres-0.4.0
    • parsql-tokio-postgres 0.4.0
    • Docs.rs crate page
    • MIT OR Apache-2.0
    • Links
    • Repository
    • crates.io
    • Source
    • Owners
    • yazdostum-nettr
    • Dependencies
      • async-trait ^0.1.88 normal
      • parsql-macros ^0.4.0 normal
      • postgres ^0.19.10 normal
      • tokio-postgres ^0.7.13 normal
      • criterion ^0.5.1 dev
      • tokio ^1.41.1 dev
    • Versions
    • 87.5% of the crate is documented
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate parsql_tokio_postgres

parsql_tokio_postgres0.4.0

  • All Items

Sections

  • parsql-tokio-postgres
    • Features
    • Usage

Crate Items

  • Re-exports
  • Modules
  • Structs
  • Traits

Crates

  • parsql_tokio_postgres

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.

Results

Settings
Help
    trait
    parsql_tokio_postgres::traits::FromRow
    Trait for converting database rows to Rust structs. This …
    derive macro
    parsql_tokio_postgres::macros::FromRow
    trait method
    parsql_tokio_postgres::traits::FromRow::from_row
    Converts a database row to a Rust struct.
    function
    parsql_tokio_postgres::crud_ops::fetch
    &Client, T -> Result<T, Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    fetch
    trait method
    parsql_tokio_postgres::traits::CrudOps::fetch
    &CrudOps, T -> Pin<Box<Future>>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    Retrieves a single record from the database and converts …
    method
    parsql_tokio_postgres::Client::fetch
    &Client, T -> Pin<Box<Future>>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    trait method
    parsql_tokio_postgres::traits::CrudOps::fetch_all
    &CrudOps, T -> Pin<Box<Future>>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    Retrieves multiple records from the database and converts …
    method
    parsql_tokio_postgres::Client::fetch_all
    &Client, T -> Pin<Box<Future>>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    function
    parsql_tokio_postgres::crud_ops::get
    &Client, T -> Result<T, Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    get
    method
    parsql_tokio_postgres::traits::CrudOps::get
    &CrudOps, T -> Pin<Box<Future>>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    method
    parsql_tokio_postgres::traits::CrudOps::get_all
    &CrudOps, T -> Pin<Box<Future>>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    function
    parsql_tokio_postgres::transaction_ops::tx_fetch
    Transaction, T -> Result<(Transaction, T), Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    Retrieves a single record within a transaction.
    function
    parsql_tokio_postgres::crud_ops::fetch_all
    &Client, T -> Result<Vec<T>, Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    fetch_all
    function
    parsql_tokio_postgres::transaction_ops::tx_get
    Transaction, T -> Result<(Transaction, T), Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    Retrieves a single record within a transaction.
    function
    parsql_tokio_postgres::crud_ops::get_all
    &Client, T -> Result<Vec<T>, Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    get_all
    function
    parsql_tokio_postgres::transaction_ops::tx_fetch_all
    Transaction, T -> Result<(Transaction, Vec<T>), Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    Retrieves multiple records within a transaction.
    function
    parsql_tokio_postgres::transaction_ops::tx_get_all
    Transaction, T -> Result<(Transaction, Vec<T>), Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    Retrieves multiple records within a transaction.
    trait method
    parsql_tokio_postgres::traits::FromRow::from_row
    &Row -> Result<FromRow, Error>
    Converts a database row to a Rust struct.
    function
    parsql_tokio_postgres::crud_ops::fetch
    &Client, T -> Result<T, Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    fetch
    function
    parsql_tokio_postgres::crud_ops::get
    &Client, T -> Result<T, Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    get
    function
    parsql_tokio_postgres::transaction_ops::tx_fetch
    Transaction, T -> Result<(Transaction, T), Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    Retrieves a single record within a transaction.
    function
    parsql_tokio_postgres::transaction_ops::tx_get
    Transaction, T -> Result<(Transaction, T), Error>
    where
    T: SqlQuery + FromRow + SqlParams + Send + Sync
    Retrieves a single record within a transaction.