Make Rust models for database schema

This commit is contained in:
Lucien Cartier-Tilet 2023-01-03 16:11:43 +01:00
parent a9e300ede2
commit 60988cff24
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
10 changed files with 142 additions and 7 deletions

View File

@ -6,5 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
diesel = { version = "2.0", features = ["postgres"] } diesel = { version = "2.0", features = ["postgres", "chrono"] }
diesel-derive-enum = { version = "2.0.0-rc.0", features = ["postgres"] }
chrono = "0.4.23"
dotenvy = "0.15" dotenvy = "0.15"

View File

@ -2,7 +2,7 @@
# see https://diesel.rs/guides/configuring-diesel-cli # see https://diesel.rs/guides/configuring-diesel-cli
[print_schema] [print_schema]
file = "src/schema.rs" file = "src/db/schema.rs"
[migrations_directory] [migrations_directory]
dir = "migrations" dir = "migrations"

View File

@ -6,11 +6,11 @@ CREATE TYPE AgentLanguageRelation as ENUM ('publisher', 'author');
CREATE TABLE Languages ( CREATE TABLE Languages (
name VARCHAR(255) PRIMARY KEY, name VARCHAR(255) PRIMARY KEY,
native VARCHAR(255), native VARCHAR(255),
relese Release NOT NULL, release Release NOT NULL,
targetLanguage TEXT[] NOT NULL, targetLanguage TEXT[] NOT NULL,
genre DictGenre[] NOT NULL, genre DictGenre[] NOT NULL,
abstract TEXT, abstract TEXT,
created TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
description TEXT, description TEXT,
rights TEXT, rights TEXT,
license TEXT, license TEXT,

2
src/db/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod models;
pub mod schema;

View File

@ -0,0 +1,53 @@
use diesel::prelude::*;
use super::super::schema::{languages, langandagents};
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Release"]
pub enum Release {
Public,
NonCommercial,
Research,
Private
}
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Dictgenre"]
pub enum DictGenre {
General,
Learning,
Etymology,
Specialized,
Historical,
Orthography,
Terminology
}
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Agentlanguagerelation"]
pub enum AgentLanguageRelation {
Publisher,
Author
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
pub struct Language {
release: Release,
created: chrono::NaiveDateTime,
name: String,
owner: String,
targetlanguage: Vec<String>,
genre: Vec<DictGenre>,
native: Option<String>,
abstract_: Option<String>,
description: Option<String>,
rights: Option<String>,
license: Option<String>,
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
#[diesel(table_name = langandagents)]
pub struct LangAndAgent {
id: i32,
agent: String,
language: String,
}

3
src/db/models/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod users;
pub mod languages;
pub mod words;

16
src/db/models/users.rs Normal file
View File

@ -0,0 +1,16 @@
use diesel::prelude::*;
use super::super::schema::{userfollows, users};
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
pub struct User {
pub id: String,
pub username: String,
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
#[diesel(table_name = userfollows)]
pub struct UserFollow {
pub id: i32,
pub follower: String,
pub following: String,
}

56
src/db/models/words.rs Normal file
View File

@ -0,0 +1,56 @@
use diesel::prelude::*;
use super::super::schema::{wordrelation, words};
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Wordrelationship"]
pub enum WordRelationship {
Definition,
Related
}
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
#[DieselTypePath = "crate::db::schema::sql_types::Partofspeech"]
pub enum PartOfSpeech {
Adjective,
Adposition,
Adverb,
Auxilliary,
CoordConj,
Determiner,
Interjection,
Noun,
Numeral,
Particle,
Pronoun,
ProperNoun,
Punctuation,
SubjConj,
Symbol,
Verb,
Other
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
struct Word {
norm: String,
native: Option<String>,
lemma: Option<String>,
language: String,
partofspeech: PartOfSpeech,
audio: Option<String>,
video: Option<String>,
image: Option<String>,
description: Option<String>,
etymology: Option<String>,
lusage: Option<String>,
morphology: Option<String>,
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
#[diesel(table_name = wordrelation)]
struct WordRelation {
id: i32,
wordsource: String,
wordtarget: String,
relationship: WordRelationship,
}

View File

@ -42,12 +42,12 @@ diesel::table! {
languages (name) { languages (name) {
name -> Varchar, name -> Varchar,
native -> Nullable<Varchar>, native -> Nullable<Varchar>,
relese -> Release, release -> Release,
targetlanguage -> Array<Nullable<Text>>, targetlanguage -> Array<Nullable<Text>>,
genre -> Array<Nullable<Dictgenre>>, genre -> Array<Nullable<Dictgenre>>,
#[sql_name = "abstract"] #[sql_name = "abstract"]
abstract_ -> Nullable<Text>, abstract_ -> Nullable<Text>,
created -> Nullable<Timestamptz>, created -> Timestamp,
description -> Nullable<Text>, description -> Nullable<Text>,
rights -> Nullable<Text>, rights -> Nullable<Text>,
license -> Nullable<Text>, license -> Nullable<Text>,

View File

@ -1,3 +1,5 @@
mod db;
fn main() { fn main() {
println!("Hello, world!"); println!("Hello, world!");
} }