Skip to content

Commit

Permalink
Merge pull request #8 from JakenHerman/jaken/post-3
Browse files Browse the repository at this point in the history
Add initial database for dooly
  • Loading branch information
JakenHerman authored Oct 14, 2024
2 parents df89aa7 + 54db7db commit a1412a4
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 174 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target/
target/
*.sqlite
177 changes: 177 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ edition = "2021"

[dependencies]
rocket = { version = "0.5.1", features = ["json"] }
diesel = { version = "2.0", features = ["r2d2", "sqlite"] }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
9 changes: 9 additions & 0 deletions diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]

[migrations_directory]
dir = "./migrations"
Empty file added migrations/.keep
Empty file.
1 change: 1 addition & 0 deletions migrations/2024-10-14-063139_create_todos/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE todos;
5 changes: 5 additions & 0 deletions migrations/2024-10-14-063139_create_todos/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT 0
);
24 changes: 24 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use diesel::prelude::*;
use diesel::SqliteConnection;
use rocket::serde::{Serialize, Deserialize};

use crate::schema::todos;

#[derive(Queryable, Serialize, Deserialize)]
pub struct TodoItem {
pub id: i32,
pub title: String,
pub completed: bool,
}

#[derive(Insertable, Deserialize)]
#[diesel(table_name = todos)]
pub struct NewTodoItem<'a> {
pub title: &'a str,
pub completed: bool,
}

pub fn establish_connection() -> SqliteConnection {
let database_url = "db.sqlite";
SqliteConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
}
Loading

0 comments on commit a1412a4

Please sign in to comment.