Struct teil::Connection
source · pub struct Connection { /* private fields */ }Expand description
Wrapper structure around a postgres connection to allow for connection re-use
Implementations§
source§impl Connection
impl Connection
sourcepub async fn new() -> Result<Connection, Error>
pub async fn new() -> Result<Connection, Error>
Creates a new connection to the database
use teil::Connection;
let connection = match Connection::new().await {
Ok(v) => v,
Err(e) => {
println!("could not get connection to the database, {}", e);
return;
}
};
// Use the connection heresourcepub async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error>
pub async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error>
Obtains a transaction from the connection.
use teil::Connection;
let mut connection = Connection::new().await.unwrap();
let transaction = match connection.transaction().await {
Ok(v) => v,
Err(e) => {
println!("could not get transaction from the connection, {}", e);
return;
}
};
// Use the connection heresourcepub async fn retrieve<T: Teil, A: Into<<T as Teil>::Id> + Send + Sync>(
&self,
id: A
) -> Result<Option<T>, Error>
pub async fn retrieve<T: Teil, A: Into<<T as Teil>::Id> + Send + Sync>(
&self,
id: A
) -> Result<Option<T>, Error>
Performs a retrieve operation using the connection.
use teil::{Teil, Connection};
#[derive(Teil)]
struct User {
#[teil(primary_key)]
name: String
}
let connection = Connection::new().await.unwrap();
let user: User = match connection.retrieve("some_name").await {
Ok(Some(v)) => v,
Ok(None) => {
println!("user with name some_name not found");
return;
},
Err(e) => {
println!("could not retrieve object from db, {}", e);
return;
}
};sourcepub async fn save<T: Teil>(&self, teil: &mut T) -> Result<<T as Teil>::Id, Error>
pub async fn save<T: Teil>(&self, teil: &mut T) -> Result<<T as Teil>::Id, Error>
Performs a save operation using the connection.
use teil::{Teil, Connection};
#[derive(Teil)]
struct User {
#[teil(primary_key)]
name: String
}
let connection = Connection::new().await.unwrap();
let mut user = User{name: "some_name".to_string()};
match connection.save(&mut user).await {
Ok(_id) => {
println!("saved user to the database!");
},
Err(e) => {
println!("could not save object to db, {}", e);
}
};sourcepub async fn delete<T: Teil, A: Into<<T as Teil>::Id> + Send + Sync>(
&self,
id: A
) -> Result<Option<T>, Error>
pub async fn delete<T: Teil, A: Into<<T as Teil>::Id> + Send + Sync>(
&self,
id: A
) -> Result<Option<T>, Error>
Performs a delete operation using the connection.
use teil::{Teil, Connection};
#[derive(Teil)]
struct User {
#[teil(primary_key)]
name: String
}
let connection = Connection::new().await.unwrap();
// Using the turbofish...
match connection.delete::<User, _>("some_name").await {
Ok(Some(_)) => (),
Ok(None) => {
println!("user with name some_name not found");
},
Err(e) => {
println!("could not retrieve object from db, {}", e);
}
};sourcepub async fn update_all<T: Teil>(&self, teil: &T) -> Result<bool, Error>
pub async fn update_all<T: Teil>(&self, teil: &T) -> Result<bool, Error>
Performs a full update to an object using the connection.
use teil::{Teil, Connection};
#[derive(Teil)]
struct User {
#[teil(primary_key)]
name: String,
amount: i32
}
let connection = Connection::new().await.unwrap();
let mut user = User{name: "some_name".to_string(), amount: 10};
// Will update all non-primary keys (amount)
match connection.update_all(&user).await {
Ok(_) => {
println!("saved user to the database!");
},
Err(e) => {
println!("could not save object to db, {}", e);
}
};sourcepub async fn execute_update<U: Update>(&self, update: U) -> Result<bool, Error>
pub async fn execute_update<U: Update>(&self, update: U) -> Result<bool, Error>
Performs a partial update to an object using the connection.
use teil::{Teil, Connection};
#[derive(Teil)]
struct User {
#[teil(primary_key)]
name: String,
amount: i32,
base_amount: i32
}
let connection = Connection::new().await.unwrap();
let user_update = User::update("some_name").set_base_amount(100);
// Will update only the base_amount
match connection.execute_update(user_update).await {
Ok(_) => {
println!("updated user object in the database");
},
Err(e) => {
println!("could not save object to db, {}", e);
}
};sourcepub async fn query_vec<T: Teil>(
&self,
filters: Option<Vec<<T as Teil>::Filter>>,
sort: Option<Vec<<T as Teil>::Sort>>,
offset: Option<usize>,
limit: Option<usize>
) -> Result<Vec<T>, Error>
pub async fn query_vec<T: Teil>(
&self,
filters: Option<Vec<<T as Teil>::Filter>>,
sort: Option<Vec<<T as Teil>::Sort>>,
offset: Option<usize>,
limit: Option<usize>
) -> Result<Vec<T>, Error>
Performs a query with optional parameters using the connection.
This function is cumbersome to use, you should prefer execute_query instead.
use teil::{Teil, Connection};
#[derive(Teil)]
struct User {
#[teil(primary_key)]
name: String
}
let connection = Connection::new().await.unwrap();
// Gets all elements from the user table
match connection.query_vec::<User>(None, None, None, None).await {
Ok(_vals) => {
println!("retrieved objects from the database");
// Do something with the objects...
},
Err(e) => {
println!("could not save object to db, {}", e);
}
};sourcepub async fn execute_query<T: Teil>(
&self,
teil_query: TeilQuery<T>
) -> Result<Vec<T>, Error>
pub async fn execute_query<T: Teil>(
&self,
teil_query: TeilQuery<T>
) -> Result<Vec<T>, Error>
Executes a TeilQuery object using the connection
use teil::{Teil, Connection};
#[derive(Teil)]
struct User {
#[teil(primary_key)]
name: String,
amount: i32,
base_amount: i32
}
let connection = Connection::new().await.unwrap();
let user_query = User::query();
// We'll query all values from the database
match connection.execute_query(user_query).await {
Ok(_elems) => {
println!("got elements from the database");
// Do something with the elements
},
Err(e) => {
println!("could not save object to db, {}", e);
}
};sourcepub async fn count<T: Teil>(
&self,
filters: Vec<<T as Teil>::Filter>
) -> Result<i64, Error>
pub async fn count<T: Teil>(
&self,
filters: Vec<<T as Teil>::Filter>
) -> Result<i64, Error>
Counts the number of elements in the database with the given filters, using the connection
use teil::{Teil, Connection};
#[derive(Teil)]
struct User {
#[teil(primary_key)]
name: String
}
let connection = Connection::new().await.unwrap();
// Counts all the elements
match connection.count::<User>(vec![]).await {
Ok(num_elems) => {
println!("there are {} elems!", num_elems);
},
Err(e) => {
println!("could not get object count from the db, {}", e);
}
};sourcepub async fn save_iter<'b, T: Teil, A: IntoIterator<Item = &'b mut T> + Send>(
&mut self,
elements: A
) -> Result<(), Error>
pub async fn save_iter<'b, T: Teil, A: IntoIterator<Item = &'b mut T> + Send>(
&mut self,
elements: A
) -> Result<(), Error>
Saves a group of elements to the database using the connection
use teil::{Teil, Connection};
#[derive(Teil)]
struct User {
#[teil(primary_key)]
name: String
}
let mut connection = Connection::new().await.unwrap();
let mut elems = vec![User{name: "Foo".to_string()}, User{name: "Bar".to_string()}];
// Counts all the elements
match connection.save_iter(&mut elems).await {
Ok(_) => {
println!("saved all elements to the database");
},
Err(e) => {
println!("could not save elements, {}", e);
}
};sourcepub fn into_inner(self) -> Object<Manager>
pub fn into_inner(self) -> Object<Manager>
Returns the inner deadpool connection
use teil::Connection;
let connection = Connection::new().await.unwrap().into_inner();
// Use the pool transaction, for example...
connection.execute("DELETE FROM some_table;", &[]).await.unwrap();