feat(server): start db query abstraction
This commit is contained in:
parent
aef2c823e5
commit
428bf6ec4c
7 changed files with 176 additions and 72 deletions
61
server/src/db/conn.rs
Normal file
61
server/src/db/conn.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
use super::RieterDb;
|
||||
use sea_orm::{DbBackend, DbErr, ExecResult, QueryResult, Statement};
|
||||
use std::{future::Future, pin::Pin};
|
||||
|
||||
// Allows RieterDb objects to be passed to ORM functions
|
||||
impl sea_orm::ConnectionTrait for RieterDb {
|
||||
fn get_database_backend(&self) -> DbBackend {
|
||||
self.conn.get_database_backend()
|
||||
}
|
||||
fn execute<'life0, 'async_trait>(
|
||||
&'life0 self,
|
||||
stmt: Statement,
|
||||
) -> Pin<Box<dyn Future<Output = std::result::Result<ExecResult, DbErr>> + Send + 'async_trait>>
|
||||
where
|
||||
Self: 'async_trait,
|
||||
'life0: 'async_trait,
|
||||
{
|
||||
self.conn.execute(stmt)
|
||||
}
|
||||
fn execute_unprepared<'life0, 'life1, 'async_trait>(
|
||||
&'life0 self,
|
||||
sql: &'life1 str,
|
||||
) -> Pin<Box<dyn Future<Output = std::result::Result<ExecResult, DbErr>> + Send + 'async_trait>>
|
||||
where
|
||||
Self: 'async_trait,
|
||||
'life0: 'async_trait,
|
||||
'life1: 'async_trait,
|
||||
{
|
||||
self.conn.execute_unprepared(sql)
|
||||
}
|
||||
fn query_one<'life0, 'async_trait>(
|
||||
&'life0 self,
|
||||
stmt: Statement,
|
||||
) -> Pin<
|
||||
Box<
|
||||
dyn Future<Output = std::result::Result<Option<QueryResult>, DbErr>>
|
||||
+ Send
|
||||
+ 'async_trait,
|
||||
>,
|
||||
>
|
||||
where
|
||||
Self: 'async_trait,
|
||||
'life0: 'async_trait,
|
||||
{
|
||||
self.conn.query_one(stmt)
|
||||
}
|
||||
fn query_all<'life0, 'async_trait>(
|
||||
&'life0 self,
|
||||
stmt: Statement,
|
||||
) -> Pin<
|
||||
Box<
|
||||
dyn Future<Output = std::result::Result<Vec<QueryResult>, DbErr>> + Send + 'async_trait,
|
||||
>,
|
||||
>
|
||||
where
|
||||
Self: 'async_trait,
|
||||
'life0: 'async_trait,
|
||||
{
|
||||
self.conn.query_all(stmt)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +1,69 @@
|
|||
mod conn;
|
||||
pub mod entities;
|
||||
mod migrator;
|
||||
|
||||
use migrator::Migrator;
|
||||
use sea_orm::ColumnTrait;
|
||||
use sea_orm::ConnectOptions;
|
||||
use sea_orm::Database;
|
||||
use sea_orm::DatabaseConnection;
|
||||
use sea_orm::EntityTrait;
|
||||
use sea_orm::PaginatorTrait;
|
||||
use sea_orm::QueryFilter;
|
||||
use sea_orm::QueryOrder;
|
||||
use sea_orm_migration::MigratorTrait;
|
||||
|
||||
pub async fn init<C: Into<ConnectOptions>>(
|
||||
opt: C,
|
||||
) -> Result<sea_orm::DatabaseConnection, sea_orm::DbErr> {
|
||||
let db = Database::connect(opt).await?;
|
||||
pub use entities::prelude::*;
|
||||
pub use entities::*;
|
||||
|
||||
Migrator::up(&db, None).await?;
|
||||
type Result<T> = std::result::Result<T, sea_orm::DbErr>;
|
||||
|
||||
Ok(db)
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RieterDb {
|
||||
pub conn: DatabaseConnection,
|
||||
}
|
||||
|
||||
impl RieterDb {
|
||||
pub async fn connect<C: Into<ConnectOptions>>(opt: C) -> Result<Self> {
|
||||
let db = Database::connect(opt).await?;
|
||||
|
||||
Migrator::up(&db, None).await?;
|
||||
|
||||
Ok(Self { conn: db })
|
||||
}
|
||||
|
||||
pub async fn repos(&self, per_page: u64, page: u64) -> Result<(u64, Vec<repo::Model>)> {
|
||||
let paginator = Repo::find()
|
||||
.order_by_asc(repo::Column::Id)
|
||||
.paginate(&self.conn, per_page);
|
||||
let repos = paginator.fetch_page(page).await?;
|
||||
let total_pages = paginator.num_pages().await?;
|
||||
|
||||
Ok((total_pages, repos))
|
||||
}
|
||||
|
||||
pub async fn repo(&self, id: i32) -> Result<Option<repo::Model>> {
|
||||
repo::Entity::find_by_id(id).one(&self.conn).await
|
||||
}
|
||||
|
||||
pub async fn repo_by_name(&self, name: &str) -> Result<Option<repo::Model>> {
|
||||
Repo::find()
|
||||
.filter(repo::Column::Name.eq(name))
|
||||
.one(&self.conn)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn packages(&self, per_page: u64, page: u64) -> Result<(u64, Vec<package::Model>)> {
|
||||
let paginator = Package::find()
|
||||
.order_by_asc(package::Column::Id)
|
||||
.paginate(&self.conn, per_page);
|
||||
let packages = paginator.fetch_page(page).await?;
|
||||
let total_pages = paginator.num_pages().await?;
|
||||
|
||||
Ok((total_pages, packages))
|
||||
}
|
||||
|
||||
pub async fn package(&self, id: i32) -> Result<Option<package::Model>> {
|
||||
package::Entity::find_by_id(id).one(&self.conn).await
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue