This repository has been archived on 2022-07-24. You can view files and clone it, but cannot push or open issues or pull requests.
itconfig/examples/diesel/src/main.rs

44 lines
782 B
Rust
Raw Normal View History

2019-12-29 01:11:34 +03:00
#[macro_use]
extern crate itconfig;
#[macro_use]
extern crate diesel;
mod db;
mod models;
mod schema;
use dotenv::dotenv;
use diesel::prelude::*;
use crate::models::*;
config! {
2020-02-08 22:11:37 +03:00
DATABASE_URL,
2019-12-29 01:11:34 +03:00
}
fn main() {
dotenv().ok();
cfg::init();
let connection = db::establish_connection();
let posts = get_posts(&connection);
println!("Displaying {} posts", posts.len());
for post in posts {
print!("\n");
println!("{}", post.title);
println!("----------");
println!("{}", post.body);
}
}
fn get_posts(connection: &PgConnection) -> Vec<Post> {
use crate::schema::posts::dsl::*;
posts.filter(published.eq(true))
.limit(5)
.get_results::<Post>(connection)
.expect("Error loading posts")
}