building db support with diesel.rs
This commit is contained in:
1
cookbook/.env
Normal file
1
cookbook/.env
Normal file
@@ -0,0 +1 @@
|
|||||||
|
DATABASE_URL=db.sqlite3
|
||||||
@@ -5,3 +5,6 @@ authors = ["artus <artus@landoftheunicorn.hd.free.fr>"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
libsqlite3-sys = { version = "*", features = ["bundled"] }
|
||||||
|
diesel = { version = "1.4.1", features = ["sqlite"] }
|
||||||
|
dotenv = "0.9.0"
|
||||||
|
|||||||
BIN
cookbook/db.sqlite3
Normal file
BIN
cookbook/db.sqlite3
Normal file
Binary file not shown.
5
cookbook/diesel.toml
Normal file
5
cookbook/diesel.toml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# For documentation on how to configure this file,
|
||||||
|
# see diesel.rs/guides/configuring-diesel-cli
|
||||||
|
|
||||||
|
[print_schema]
|
||||||
|
file = "src/schema.rs"
|
||||||
0
cookbook/migrations/.gitkeep
Normal file
0
cookbook/migrations/.gitkeep
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
DROP TABLE recipes
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
-- Your SQL goes here
|
||||||
|
CREATE TABLE recipes (
|
||||||
|
id INTEGER PRIMARY KEY NOT NULL,
|
||||||
|
title VARCHAR NOT NULL,
|
||||||
|
category INTEGER NOT NULL,
|
||||||
|
ingredients TEXT NOT NULL,
|
||||||
|
preparation TEXT NOT NULL
|
||||||
|
)
|
||||||
23
cookbook/src/bin/show_recipes.rs
Normal file
23
cookbook/src/bin/show_recipes.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
extern crate cookbook;
|
||||||
|
extern crate diesel;
|
||||||
|
|
||||||
|
use self::cookbook::*;
|
||||||
|
use self::models::*;
|
||||||
|
use self::diesel::prelude::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
use cookbook::schema::recipes::dsl::*;
|
||||||
|
|
||||||
|
let conn = establish_connection();
|
||||||
|
let result = recipes
|
||||||
|
.limit(5)
|
||||||
|
.load::<Recipe>(&conn)
|
||||||
|
.expect("Error loading recipes");
|
||||||
|
|
||||||
|
println!("Here are {} recipes :", result.len());
|
||||||
|
for rec in result {
|
||||||
|
println!("{}", rec.title);
|
||||||
|
println!("-------------\n");
|
||||||
|
println!("{}", rec.ingredients);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,28 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate diesel;
|
||||||
|
extern crate dotenv;
|
||||||
|
|
||||||
|
pub mod schema;
|
||||||
|
pub mod models;
|
||||||
|
|
||||||
mod recipe;
|
mod recipe;
|
||||||
mod storage;
|
mod storage;
|
||||||
mod importer;
|
mod importer;
|
||||||
|
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use dotenv::dotenv;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
pub fn establish_connection() -> SqliteConnection {
|
||||||
|
dotenv().ok();
|
||||||
|
|
||||||
|
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set !");
|
||||||
|
SqliteConnection::establish(&db_url)
|
||||||
|
.expect(&format!("Error connecting to {}", db_url))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
8
cookbook/src/models.rs
Normal file
8
cookbook/src/models.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#[derive(Queryable)]
|
||||||
|
pub struct Recipe {
|
||||||
|
pub id: i32,
|
||||||
|
pub title: String,
|
||||||
|
pub category: i32,
|
||||||
|
pub ingredients: String,
|
||||||
|
pub preparation: String,
|
||||||
|
}
|
||||||
@@ -40,8 +40,7 @@ impl Ingredient {
|
|||||||
pub(super) fn new(name: String) -> Ingredient {
|
pub(super) fn new(name: String) -> Ingredient {
|
||||||
Ingredient {
|
Ingredient {
|
||||||
alias: vec![name],
|
alias: vec![name],
|
||||||
..
|
..Default::default()
|
||||||
Default::default()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
9
cookbook/src/schema.rs
Normal file
9
cookbook/src/schema.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
table! {
|
||||||
|
recipes (id) {
|
||||||
|
id -> Integer,
|
||||||
|
title -> Text,
|
||||||
|
category -> Integer,
|
||||||
|
ingredients -> Text,
|
||||||
|
preparation -> Text,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -122,8 +122,7 @@ impl<'a, V> ProblemBuilder<'a, V> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_variable(
|
pub fn add_variable(mut self,
|
||||||
mut self,
|
|
||||||
name: String,
|
name: String,
|
||||||
domain: &'a Domain<V>,
|
domain: &'a Domain<V>,
|
||||||
value: Option<&'a V>,
|
value: Option<&'a V>,
|
||||||
@@ -153,8 +152,7 @@ mod tests {
|
|||||||
let mut problem: Problem<_> = Problem::build()
|
let mut problem: Problem<_> = Problem::build()
|
||||||
.add_variable(String::from("Left"), &domain, None)
|
.add_variable(String::from("Left"), &domain, None)
|
||||||
.add_variable(String::from("Right"), &domain, None)
|
.add_variable(String::from("Right"), &domain, None)
|
||||||
.add_constraint(
|
.add_constraint(|assign: &Variables<i32>| {
|
||||||
|assign: &Variables<i32>| {
|
|
||||||
assign.get("Left").unwrap() == assign.get("Right").unwrap()
|
assign.get("Left").unwrap() == assign.get("Right").unwrap()
|
||||||
})
|
})
|
||||||
.finish();
|
.finish();
|
||||||
|
|||||||
30
web/index.html
Normal file
30
web/index.html
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.22/dist/vue.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app">
|
||||||
|
<h1>Cook Assistant</h1>
|
||||||
|
<p>Fetch value from wasm : {{ value }}</p>
|
||||||
|
<strong>{{ message }}</strong>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
var getWasmValue = function() {
|
||||||
|
return 42
|
||||||
|
};
|
||||||
|
|
||||||
|
var app = new Vue({
|
||||||
|
el: '#app',
|
||||||
|
data: {
|
||||||
|
message: 'Hello ! We are trying out here...',
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
value: getWasmValue,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user