49 lines
1.4 KiB
Rust
49 lines
1.4 KiB
Rust
pub mod user;
|
|
pub mod error;
|
|
pub mod schema;
|
|
pub mod gamenight;
|
|
pub mod gamenight_participants;
|
|
pub mod game;
|
|
pub mod owned_game;
|
|
|
|
use diesel::r2d2::ConnectionManager;
|
|
use diesel::r2d2::ManageConnection;
|
|
use diesel::r2d2::Pool;
|
|
use diesel::r2d2::PooledConnection;
|
|
use diesel::PgConnection;
|
|
use diesel_migrations::embed_migrations;
|
|
use diesel_migrations::EmbeddedMigrations;
|
|
|
|
use diesel_migrations::MigrationHarness;
|
|
pub use user::login;
|
|
pub use user::register;
|
|
pub use gamenight::gamenights;
|
|
pub use gamenight_participants::get_participants;
|
|
pub use game::games;
|
|
|
|
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
|
pub type DbConnection = PgConnection;
|
|
pub type DbPool = Pool<ConnectionManager<DbConnection>>;
|
|
|
|
pub fn run_migration(conn: &mut DbConnection) {
|
|
conn.run_pending_migrations(MIGRATIONS).unwrap();
|
|
}
|
|
|
|
pub fn get_connection_pool(url: &str) -> DbPool {
|
|
let manager = ConnectionManager::<PgConnection>::new(url);
|
|
// Refer to the `r2d2` documentation for more methods to use
|
|
// when building a connection pool
|
|
Pool::builder()
|
|
.test_on_check_out(true)
|
|
.build(manager)
|
|
.expect("Could not build connection pool")
|
|
}
|
|
pub trait GetConnection<T> where T: ManageConnection {
|
|
fn get_conn(&self) -> PooledConnection<T>;
|
|
}
|
|
|
|
impl<T: ManageConnection> GetConnection<T> for Pool<T> {
|
|
fn get_conn(&self) -> PooledConnection<T> {
|
|
self.get().expect("Couldn't get db connection from pool")
|
|
}
|
|
} |