Embedded SurrealDB connection and schema
Browse files at this commit0b4bc103c6ed6b1db5ae6434f2785ee0504553c8Parent: d6dd881- feat
- embedded SurrealDB connection and schema
1 changed file +91 -0
- src/database.rs +91 -0
src/database.rsadded+91 -0
| @@ -0,0 +1,91 @@ | |||
| 1 | +//! Embedded SurrealDB (surrealkv, file-backed) connection + schema. | ||
| 2 | + | ||
| 3 | +use surrealdb::Surreal; | ||
| 4 | +use surrealdb::engine::local::{Db, Mem, SurrealKv}; | ||
| 5 | + | ||
| 6 | +use crate::config::Config; | ||
| 7 | +use crate::error::AppError; | ||
| 8 | + | ||
| 9 | +pub type Database = Surreal<Db>; | ||
| 10 | + | ||
| 11 | +pub const USER_TABLE: &str = "user"; | ||
| 12 | +pub const SESSION_TABLE: &str = "session"; | ||
| 13 | +pub const NOTE_TABLE: &str = "note"; | ||
| 14 | +pub const EVENT_TABLE: &str = "event"; | ||
| 15 | +pub const ATTENDANCE_TABLE: &str = "attendance"; | ||
| 16 | +pub const EXAM_TABLE: &str = "exam"; | ||
| 17 | +pub const EXAM_RESULT_TABLE: &str = "exam_result"; | ||
| 18 | + | ||
| 19 | +/// SCHEMAFULL schema: every column is typed, references use `record<..>`. | ||
| 20 | +/// Idempotent — safe to run on every boot. | ||
| 21 | +const MIGRATION: &str = " | ||
| 22 | + DEFINE TABLE IF NOT EXISTS user SCHEMAFULL; | ||
| 23 | + DEFINE FIELD IF NOT EXISTS username ON user TYPE string; | ||
| 24 | + DEFINE FIELD IF NOT EXISTS password_hash ON user TYPE string; | ||
| 25 | + DEFINE FIELD IF NOT EXISTS role ON user TYPE string DEFAULT 'student'; | ||
| 26 | + DEFINE INDEX IF NOT EXISTS user_username ON user FIELDS username UNIQUE; | ||
| 27 | + UPDATE user SET role = 'student' WHERE role = NONE; | ||
| 28 | + | ||
| 29 | + DEFINE TABLE IF NOT EXISTS session SCHEMAFULL; | ||
| 30 | + DEFINE FIELD IF NOT EXISTS user ON session TYPE record<user>; | ||
| 31 | + DEFINE FIELD IF NOT EXISTS token ON session TYPE string; | ||
| 32 | + DEFINE FIELD IF NOT EXISTS expires_at ON session TYPE int; | ||
| 33 | + DEFINE INDEX IF NOT EXISTS session_token ON session FIELDS token UNIQUE; | ||
| 34 | + DEFINE INDEX IF NOT EXISTS session_expires ON session FIELDS expires_at; | ||
| 35 | + | ||
| 36 | + DEFINE TABLE IF NOT EXISTS note SCHEMAFULL; | ||
| 37 | + DEFINE FIELD IF NOT EXISTS user ON note TYPE record<user>; | ||
| 38 | + DEFINE FIELD IF NOT EXISTS title ON note TYPE string; | ||
| 39 | + DEFINE FIELD IF NOT EXISTS content ON note TYPE string; | ||
| 40 | + DEFINE INDEX IF NOT EXISTS note_user ON note FIELDS user; | ||
| 41 | + | ||
| 42 | + DEFINE TABLE IF NOT EXISTS event SCHEMAFULL; | ||
| 43 | + DEFINE FIELD IF NOT EXISTS creator ON event TYPE record<user>; | ||
| 44 | + DEFINE FIELD IF NOT EXISTS title ON event TYPE string; | ||
| 45 | + DEFINE FIELD IF NOT EXISTS description ON event TYPE string; | ||
| 46 | + DEFINE FIELD IF NOT EXISTS starts_at ON event TYPE option<int>; | ||
| 47 | + DEFINE FIELD IF NOT EXISTS ends_at ON event TYPE option<int>; | ||
| 48 | + | ||
| 49 | + DEFINE TABLE IF NOT EXISTS attendance SCHEMAFULL; | ||
| 50 | + DEFINE FIELD IF NOT EXISTS event ON attendance TYPE record<event>; | ||
| 51 | + DEFINE FIELD IF NOT EXISTS user ON attendance TYPE record<user>; | ||
| 52 | + DEFINE FIELD IF NOT EXISTS status ON attendance TYPE string; | ||
| 53 | + DEFINE FIELD IF NOT EXISTS marked_by ON attendance TYPE record<user>; | ||
| 54 | + DEFINE INDEX IF NOT EXISTS attendance_event_user ON attendance FIELDS event, user UNIQUE; | ||
| 55 | + | ||
| 56 | + DEFINE TABLE IF NOT EXISTS exam SCHEMAFULL; | ||
| 57 | + DEFINE FIELD IF NOT EXISTS creator ON exam TYPE record<user>; | ||
| 58 | + DEFINE FIELD IF NOT EXISTS title ON exam TYPE string; | ||
| 59 | + DEFINE FIELD IF NOT EXISTS description ON exam TYPE string; | ||
| 60 | + DEFINE FIELD IF NOT EXISTS kind ON exam TYPE string; | ||
| 61 | + | ||
| 62 | + DEFINE TABLE IF NOT EXISTS exam_result SCHEMAFULL; | ||
| 63 | + DEFINE FIELD IF NOT EXISTS exam ON exam_result TYPE record<exam>; | ||
| 64 | + DEFINE FIELD IF NOT EXISTS user ON exam_result TYPE record<user>; | ||
| 65 | + DEFINE FIELD IF NOT EXISTS mark ON exam_result TYPE int; | ||
| 66 | + DEFINE FIELD IF NOT EXISTS graded_by ON exam_result TYPE record<user>; | ||
| 67 | + DEFINE INDEX IF NOT EXISTS exam_result_exam_user ON exam_result FIELDS exam, user UNIQUE; | ||
| 68 | +"; | ||
| 69 | + | ||
| 70 | +/// Open the file-backed database and apply the schema. | ||
| 71 | +pub async fn init(cfg: &Config) -> Result<Database, AppError> { | ||
| 72 | + let db = Surreal::new::<SurrealKv>(cfg.db_path.clone()).await?; | ||
| 73 | + db.use_ns(cfg.db_ns.clone()) | ||
| 74 | + .use_db(cfg.db_name.clone()) | ||
| 75 | + .await?; | ||
| 76 | + migrate(&db).await?; | ||
| 77 | + Ok(db) | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +/// A fresh in-memory database with the schema applied. For tests. | ||
| 81 | +pub async fn init_mem() -> Result<Database, AppError> { | ||
| 82 | + let db = Surreal::new::<Mem>(()).await?; | ||
| 83 | + db.use_ns("hezarfen").use_db("hezarfen").await?; | ||
| 84 | + migrate(&db).await?; | ||
| 85 | + Ok(db) | ||
| 86 | +} | ||
| 87 | + | ||
| 88 | +async fn migrate(db: &Database) -> Result<(), AppError> { | ||
| 89 | + db.query(MIGRATION).await?.check()?; | ||
| 90 | + Ok(()) | ||
| 91 | +} | ||