Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Change examples to match diesel-2 format, add pagination to example
Browse files Browse the repository at this point in the history
  • Loading branch information
popen2 committed Dec 26, 2022
1 parent 63bbb23 commit 510f337
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ diesel_filter = { path = "../../diesel_filter/core", features = ["pagination", "
```

Derive your struct with `DieselFilter` and annotate the fields that will be used as filters.
The top level annotation `#[table_name = "db_table"]` is mandatory.
The top level annotation `#[diesel(table_name = db_table)]` is mandatory.

```rust
#[derive(Queryable, DieselFilter)]
#[table_name = "projects"]
#[diesel(table_name = projects)]
pub struct Project {
pub id: Uuid,
#[filter(substring, insensitive)]
Expand Down Expand Up @@ -117,7 +117,7 @@ These are independent of the `#[pagination]` annotation that you can add on your

```rust
#[derive(Queryable, DieselFilter)]
#[table_name = "projects"]
#[diesel(table_name = projects)]
#[pagination]
pub struct Project
```
Expand Down
2 changes: 1 addition & 1 deletion core/src/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<T> Paginated<T> {
Paginated {
per_page,
offset: (self.page - 1) * per_page,
..self,
..self
}
}

Expand Down
10 changes: 6 additions & 4 deletions examples/simple/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[package]
edition = "2021"
name = "simple-example"
version = "0.1.0"
publish = false
version = "0.1.0"

[dependencies]
diesel_filter = { path = "../../core" }
serde = { version = "1.0" }
diesel = { version = "1.4", features = ["postgres"] }
diesel = {version = "2", features = ["postgres", "uuid"]}
diesel_filter = {path = "../../core", features = ["pagination"]}
serde = {version = "1.0"}
uuid = "1.2.2"
26 changes: 18 additions & 8 deletions examples/simple/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,40 @@ extern crate diesel;

use crate::schema::thingies;
use diesel::prelude::*;
use diesel_filter::Paginate;
use std::env;
use uuid::Uuid;

mod schema;

#[derive(DieselFilter, Queryable, Debug)]
#[table_name = "thingies"]
struct Thingy {
pub id: i32,
#[diesel(table_name = thingies)]
#[pagination]
pub struct Thingy {
pub id: Uuid,
#[filter(insensitive)]
pub name: String,
#[filter(multiple)]
pub category: String,
pub other: String,
#[filter]
pub other: Option<String>,
}

fn main() {
// Get a postgres DB connection
let conn = todo!();
let database_url = env::var("DATABASE_URL").expect("Please set DATABASE_URL");
let mut conn = PgConnection::establish(&database_url).expect("Could not connect to database");

let mut filters = ThingyFilters {
name: "coucou",
let filters = ThingyFilters {
name: Some("coucou".to_owned()),
category: None,
other: None,
page: None,
per_page: None,
};

let results = ThingyFilters::filtered(&filters, &conn);
let results = Thingy::filtered(&filters, &mut conn);

println!("{:?}", filters);
println!("{:?}", results);
}
4 changes: 2 additions & 2 deletions examples/simple/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
table! {
thingies (id) {
id -> Integer,
id -> Uuid,
name -> Varchar,
category -> Varchar,
other -> Varchar,
other -> Nullable<Varchar>,
}
}

0 comments on commit 510f337

Please sign in to comment.