chore: add simple examples

Closes #3
This commit is contained in:
Dmitriy Pleshevskiy 2021-10-21 00:55:08 +03:00
parent e844d8d3e2
commit 529a16ec40
4 changed files with 112 additions and 0 deletions

View file

@ -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"]

19
examples/README.md Normal file
View file

@ -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"
```

36
examples/simple.rs Normal file
View file

@ -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<dyn std::error::Error>> {
// 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")
}

49
examples/transaction.rs Normal file
View file

@ -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<dyn std::error::Error>> {
// 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")
}