#[cfg(feature = "portal")]
pub mod models;
#[cfg(feature = "portal")]
pub mod pool;
#[cfg(feature = "portal")]
pub mod queries;
#[cfg(feature = "portal")]
pub use pool::DatabasePool;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum DbError {
#[error("Record not found")]
NotFound,
#[error("Duplicate entry: {0}")]
DuplicateEntry(String),
#[error("Invalid data: {0}")]
ValidationError(String),
#[error("Connection error: {0}")]
ConnectionError(String),
#[error("Query error: {0}")]
QueryError(String),
#[error("Migration error: {0}")]
MigrationError(String),
}
#[cfg(feature = "portal")]
impl From<sqlx::Error> for DbError {
fn from(err: sqlx::Error) -> Self {
match err {
sqlx::Error::RowNotFound => DbError::NotFound,
sqlx::Error::Database(db_err) => {
if db_err.is_unique_violation() {
DbError::DuplicateEntry(db_err.message().to_string())
} else {
DbError::QueryError(db_err.message().to_string())
}
}
_ => DbError::QueryError(err.to_string()),
}
}
}