feat: add diesel, but not works
This commit is contained in:
37
__database/diesel/src/main.rs
Normal file
37
__database/diesel/src/main.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
|
||||
pub mod schema;
|
||||
pub mod models;
|
||||
|
||||
use diesel::prelude::*;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
use crate::models::Post;
|
||||
|
||||
fn main() {
|
||||
use crate::schema::posts::dsl::*;
|
||||
|
||||
let connection = establish_connection();
|
||||
let results = posts.filter(published.eq(true))
|
||||
.limit(5)
|
||||
.load::<Post>(&connection)
|
||||
.expect("Error loading posts");
|
||||
|
||||
println!("Displaying {} posts", results.len());
|
||||
for post in results {
|
||||
println!("{}", post.title);
|
||||
println!("----------\n");
|
||||
println!("{}", post.body);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn establish_connection() -> SqliteConnection {
|
||||
dotenv().ok();
|
||||
|
||||
let database_url = env::var("DATABASE_URL")
|
||||
.expect("DATABASE_URL must be set");
|
||||
SqliteConnection::establish(&database_url)
|
||||
.expect(&format!("Error connecting to {}", database_url))
|
||||
}
|
||||
7
__database/diesel/src/models.rs
Normal file
7
__database/diesel/src/models.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
#[derive(Queryable)]
|
||||
pub struct Post {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub body: String,
|
||||
pub published: bool,
|
||||
}
|
||||
8
__database/diesel/src/schema.rs
Normal file
8
__database/diesel/src/schema.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
table! {
|
||||
posts (id) {
|
||||
id -> Nullable<Integer>,
|
||||
title -> Text,
|
||||
body -> Text,
|
||||
published -> Bool,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user