Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database #4

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
server: add insert and get database queries
  • Loading branch information
marijanp committed Sep 19, 2024
commit f3ac6a113eed3451772a9895dd8fe5ab60ff17b3
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
{
devShells.default = pkgs.mkShell {
inputsFrom = [ self'.packages.default ];
packages = [ pkgs.sqlx-cli ];
DATABASE_URL = "sqlite:items.db";
};

packages = {
Expand Down

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

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

1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ clap = { version = "4", features = ["derive"] }
lazy_static = "1.5.0"
metrics = "0.23"
metrics-exporter-prometheus = "0.15"
sqlx = { version = "0.8", features = [ "runtime-tokio-native-tls", "sqlite", "uuid"] }
tokio = { version = "1", features = [ "full", "tracing"] }
tracing = "0.1"
tracing-subscriber = "0.3"
Expand Down
6 changes: 6 additions & 0 deletions server/migrations/20240916162916_create-items.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

CREATE TABLE items (
id TEXT PRIMARY KEY NOT NULL, -- Store UUID as TEXT in SQLite
name TEXT NOT NULL,
price TEXT NOT NULL -- Store u128 as TEXT
);
43 changes: 43 additions & 0 deletions server/src/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::item::Item;
use sqlx::{query_as, FromRow, SqlitePool};
use uuid::Uuid;

#[derive(FromRow)]
struct DbItem {
id: String,
name: String,
price: String,
}

impl From<DbItem> for Item {
fn from(db_item: DbItem) -> Item {
Item {
id: Uuid::parse_str(&db_item.id).expect(""),
name: db_item.name,
price: db_item.price.parse::<u128>().expect(""),
}
}
}

pub async fn new_item(pool: &SqlitePool, name: &str, price: u128) -> Result<Item, sqlx::Error> {
let id = Uuid::new_v4().to_string();
let price = price.to_string();
query_as!(
DbItem,
"insert into items (id, name, price) values ($1, $2, $3) returning *",
id,
name,
price
)
.fetch_one(pool)
.await
.map(Into::into)
}

/// Returns all known items
pub async fn get_items(pool: &SqlitePool) -> Result<Vec<Item>, sqlx::Error> {
query_as!(DbItem, "select * from items")
.fetch_all(pool)
.await
.map(|items| items.into_iter().map(Into::into).collect())
}
1 change: 1 addition & 0 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod cli;
pub mod db;
pub mod item;
pub mod routes;

Expand Down