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