This commit is contained in:
Dominic Grimm 2023-02-07 07:02:51 +01:00
commit 94fb270008
No known key found for this signature in database
GPG key ID: 6F294212DEAAC530
22 changed files with 2424 additions and 0 deletions

28
backend/src/db/mod.rs Normal file
View file

@ -0,0 +1,28 @@
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 fn establish_connection() -> ConnectionResult<PgConnection> {
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();
}

67
backend/src/db/models.rs Normal file
View file

@ -0,0 +1,67 @@
use chrono::prelude::*;
use diesel::prelude::*;
use crate::db::schema;
#[derive(Identifiable, Queryable, Debug)]
#[diesel(table_name = schema::configs)]
pub struct Config {
pub id: i32,
pub active: bool,
pub name: String,
pub description: String,
pub copyright: String,
pub owner_name: String,
pub owner_email: String,
pub owner_website: Option<String>,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::configs)]
pub struct NewConfig<'a> {
pub active: bool,
pub name: &'a str,
pub description: &'a str,
pub copyright: &'a str,
pub owner_name: &'a str,
pub owner_email: &'a str,
pub owner_website: Option<&'a str>,
}
#[derive(Identifiable, Queryable, Debug)]
#[diesel(table_name = schema::tags)]
pub struct Tag {
pub id: i32,
pub name: String,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::tags)]
pub struct NewTag<'a> {
pub name: &'a str,
}
#[derive(Identifiable, Queryable, Debug)]
#[diesel(table_name = schema::posts)]
pub struct Post {
pub id: i32,
pub name: String,
pub slug: String,
pub description: String,
pub content: String,
pub published_at: NaiveDate,
pub edited_at: Option<NaiveDate>,
pub active: bool,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::posts)]
pub struct NewPost<'a> {
pub name: &'a str,
pub slug: &'a str,
pub description: &'a str,
pub content: &'a str,
pub published_at: NaiveDate,
pub edited_at: Option<NaiveDate>,
pub active: bool,
}

32
backend/src/db/schema.rs Normal file
View file

@ -0,0 +1,32 @@
diesel::table! {
configs {
id -> Integer,
active -> Bool,
name -> Text,
description -> Text,
copyright -> Text,
owner_name -> Text,
owner_email -> Text,
owner_website -> Nullable<Text>,
}
}
diesel::table! {
tags {
id -> Integer,
name -> Text,
}
}
diesel::table! {
posts {
id -> Integer,
name -> Text,
slug -> Text,
description -> Text,
content -> Text,
published_at -> Date,
edited_at -> Nullable<Date>,
active -> Bool,
}
}