30 lines
752 B
Rust
30 lines
752 B
Rust
use anyhow::Result;
|
|
use diesel::pg::PgConnection;
|
|
use diesel::prelude::*;
|
|
use diesel::r2d2::{ConnectionManager, PooledConnection};
|
|
use lazy_static::lazy_static;
|
|
|
|
use crate::CONFIG;
|
|
|
|
pub mod models;
|
|
pub mod schema;
|
|
|
|
pub type Pool = diesel::r2d2::Pool<ConnectionManager<PgConnection>>;
|
|
pub type Connection = PgConnection;
|
|
pub type PoolConnection = PooledConnection<ConnectionManager<PgConnection>>;
|
|
|
|
pub fn establish_connection() -> ConnectionResult<PgConnection> {
|
|
use diesel::Connection;
|
|
|
|
PgConnection::establish(&CONFIG.db_url)
|
|
}
|
|
|
|
pub fn pool() -> Result<Pool> {
|
|
Ok(diesel::r2d2::Pool::builder()
|
|
.build(ConnectionManager::<PgConnection>::new(&*CONFIG.db_url))?)
|
|
}
|
|
|
|
lazy_static! {
|
|
pub static ref POOL: Pool = pool().unwrap();
|
|
}
|