diff --git a/Cargo.toml b/Cargo.toml index 1e7c4ad..2e53d44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,11 @@ all-features = true [workspace] members = ["examples/*"] + +[[example]] +name = "simple" +required-features = ["r2d2_postgres"] + +[[example]] +name = "transaction" +required-features = ["r2d2_postgres", "nightly"] diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..78b993e --- /dev/null +++ b/examples/README.md @@ -0,0 +1,19 @@ +# Examples + +By default, all examples in this directory require a postgres server, which you can run using `docker-compose.dev.yml` in the `examples/web` directory. + +## Simple + +This example initializes persistence, gets a connection and runs the query. + +```sh +cargo run --example simple --features r2d2_postgres +``` + +## Transaction + +This is extended simple example where we also upgrade existing connection to transaction and run query. Then we close transaction and use connection. + +```sh +cargo run --example transaction --features "nightly r2d2_postgres" +``` \ No newline at end of file diff --git a/examples/simple.rs b/examples/simple.rs new file mode 100644 index 0000000..7efdec2 --- /dev/null +++ b/examples/simple.rs @@ -0,0 +1,36 @@ +use ood_persistence::r2d2_postgres::{postgres, NoTlsManager}; +pub use ood_persistence::r2d2_postgres::{ + NoTlsConnection as PostgresConnection, NoTlsPersistence as PostgresPersistence, + NoTlsPool as PostgresPool, +}; +pub use ood_persistence::{ + error::Result as QueryResult, + syn::{ConnectionClient, PersistencePool}, +}; + +const DATABASE_URL: &str = "postgres://postgres:test@localhost:5577/x"; + +fn main() -> Result<(), Box> { + // create pool to our database + let pool = create_postgres_pool(); + // initialize persistence + let persistence = ood_persistence::r2d2_postgres::new(&pool); + // get connection to database + let mut conn = persistence.get_connection()?; + // we can query something + let res: i32 = conn.inner().query_one("select 1", &[])?.get(0); + assert_eq!(res, 1); + + Ok(()) +} + +pub fn create_postgres_pool() -> PostgresPool { + let db_conn_config = DATABASE_URL + .parse() + .expect("Failed to convert database url to database config"); + + let manager = NoTlsManager::new(db_conn_config, postgres::NoTls); + PostgresPool::builder() + .build(manager) + .expect("Failed to create database pool") +} diff --git a/examples/transaction.rs b/examples/transaction.rs new file mode 100644 index 0000000..c7b23d0 --- /dev/null +++ b/examples/transaction.rs @@ -0,0 +1,49 @@ +use ood_persistence::r2d2_postgres::{postgres, NoTlsManager}; +pub use ood_persistence::r2d2_postgres::{ + NoTlsConnection as PostgresConnection, NoTlsPersistence as PostgresPersistence, + NoTlsPool as PostgresPool, +}; +pub use ood_persistence::{ + error::Result as QueryResult, + syn::{ConnectionClient, PersistencePool, TransactionClient}, +}; + +const DATABASE_URL: &str = "postgres://postgres:test@localhost:5577/x"; + +fn main() -> Result<(), Box> { + // create pool to our database + let pool = create_postgres_pool(); + // initialize persistence + let persistence = ood_persistence::r2d2_postgres::new(&pool); + // get connection to database + let mut conn = persistence.get_connection()?; + // we can query something + let res: i32 = conn.inner().query_one("select 1", &[])?.get(0); + assert_eq!(res, 1); + + // and we can upgrade connection to transaction + let mut trx = conn.start_transaction()?; + // we can query or execute something in transaction + let res: i32 = trx.inner().query_one("select 2", &[])?.get(0); + assert_eq!(res, 2); + // now we close transaction + trx.rollback()?; + // or trx.commit()?; + + // and we can use connection again as well + let res: i32 = conn.inner().query_one("select 3", &[])?.get(0); + assert_eq!(res, 3); + + Ok(()) +} + +pub fn create_postgres_pool() -> PostgresPool { + let db_conn_config = DATABASE_URL + .parse() + .expect("Failed to convert database url to database config"); + + let manager = NoTlsManager::new(db_conn_config, postgres::NoTls); + PostgresPool::builder() + .build(manager) + .expect("Failed to create database pool") +}