chore: add example with diesel
This commit is contained in:
parent
5a2c810a0e
commit
6330ef0d00
16 changed files with 172 additions and 2 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -4,4 +4,4 @@
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
|
||||||
itconfig/src/main.rs
|
itconfig/src/main.rs
|
||||||
.env*
|
/.env*
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"itconfig",
|
"itconfig",
|
||||||
"itconfig_tests"
|
"itconfig_tests",
|
||||||
|
"examples/diesel",
|
||||||
]
|
]
|
||||||
|
|
1
examples/diesel/.env
Normal file
1
examples/diesel/.env
Normal file
|
@ -0,0 +1 @@
|
||||||
|
DATABASE_URL=postgres://user:test@localhost:5534:5432/db
|
12
examples/diesel/Cargo.toml
Normal file
12
examples/diesel/Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
[package]
|
||||||
|
name = "diesel"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
itconfig = { path = "../../itconfig" }
|
||||||
|
dotenv = "0.15.0"
|
||||||
|
diesel = { version = "1.4.3", features = ["postgres"] }
|
16
examples/diesel/README.md
Normal file
16
examples/diesel/README.md
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
# Diesel
|
||||||
|
|
||||||
|
This example shows how you can use itconfig with diesel.
|
||||||
|
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd examples/diesel
|
||||||
|
|
||||||
|
docker-compose -p itconfig-diesel-example -f docker-compose.example.yml up -d
|
||||||
|
|
||||||
|
diesel migration run
|
||||||
|
|
||||||
|
cargo run
|
||||||
|
```
|
5
examples/diesel/diesel.toml
Normal file
5
examples/diesel/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"
|
11
examples/diesel/docker-compose.example.yml
Normal file
11
examples/diesel/docker-compose.example.yml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
postgresql:
|
||||||
|
image: postgres:11-alpine
|
||||||
|
ports:
|
||||||
|
- 5534:5432
|
||||||
|
environment:
|
||||||
|
POSTGRES_PASSWORD: test
|
||||||
|
POSTGRES_USER: user
|
||||||
|
POSTGRES_DB: db
|
0
examples/diesel/migrations/.gitkeep
Normal file
0
examples/diesel/migrations/.gitkeep
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
|
||||||
|
DROP FUNCTION IF EXISTS diesel_set_updated_at();
|
|
@ -0,0 +1,36 @@
|
||||||
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Sets up a trigger for the given table to automatically set a column called
|
||||||
|
-- `updated_at` whenever the row is modified (unless `updated_at` was included
|
||||||
|
-- in the modified columns)
|
||||||
|
--
|
||||||
|
-- # Example
|
||||||
|
--
|
||||||
|
-- ```sql
|
||||||
|
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
|
||||||
|
--
|
||||||
|
-- SELECT diesel_manage_updated_at('users');
|
||||||
|
-- ```
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
|
||||||
|
BEGIN
|
||||||
|
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
|
||||||
|
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
|
||||||
|
BEGIN
|
||||||
|
IF (
|
||||||
|
NEW IS DISTINCT FROM OLD AND
|
||||||
|
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
|
||||||
|
) THEN
|
||||||
|
NEW.updated_at := current_timestamp;
|
||||||
|
END IF;
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
|
@ -0,0 +1,2 @@
|
||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
DROP TABLE posts;
|
|
@ -0,0 +1,12 @@
|
||||||
|
-- Your SQL goes here
|
||||||
|
CREATE TABLE posts (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
title VARCHAR NOT NULL,
|
||||||
|
body TEXT NOT NULL,
|
||||||
|
published BOOLEAN NOT NULL DEFAULT 'f'
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO posts (title, body, published)
|
||||||
|
VALUES ('First post', 'Interesting body', 't'),
|
||||||
|
('Second post', 'Very interesting post', 't'),
|
||||||
|
('Draft post', 'This is post will not be shown', 'f');
|
9
examples/diesel/src/db.rs
Normal file
9
examples/diesel/src/db.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
use super::cfg;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use diesel::pg::PgConnection;
|
||||||
|
|
||||||
|
pub fn establish_connection() -> PgConnection {
|
||||||
|
let database_url = cfg::DATABASE_URL();
|
||||||
|
PgConnection::establish(&database_url)
|
||||||
|
.expect(&format!("Error connecting to {}", database_url))
|
||||||
|
}
|
44
examples/diesel/src/main.rs
Normal file
44
examples/diesel/src/main.rs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
#[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! {
|
||||||
|
DATABASE_URL: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
7
examples/diesel/src/models.rs
Normal file
7
examples/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
examples/diesel/src/schema.rs
Normal file
8
examples/diesel/src/schema.rs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
table! {
|
||||||
|
posts (id) {
|
||||||
|
id -> Int4,
|
||||||
|
title -> Varchar,
|
||||||
|
body -> Text,
|
||||||
|
published -> Bool,
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue