Struct teil::Connection

source ·
pub struct Connection { /* private fields */ }
Expand description

Wrapper structure around a postgres connection to allow for connection re-use

Implementations§

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 here

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 here

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;
    }
};

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);
    }
};

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);
    }
};

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);
    }
};

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);
    }
};

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);
    }
};

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);
    }
};

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);
    }
};

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);
    }
};

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();

Trait Implementations§

The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more