Diesel Filter
Diesel filter is a quick way to add filters and pagination to your diesel models.
Works with Diesel and Postgres.
Crate features
rocketDerivesFromFormon the generated filter struct (See this example)actixDerivesDeserializeon the generated filter struct (See this example)paginationAdds thePaginatetrait (See this example)serializewithpaginationAdds thePaginatedPayloadtrait that can directly be sent to your client
Usage & Examples
Cargo.toml
= { = "../../diesel_filter/core", = ["pagination", "serialize", "rocket"] }
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 #[filter] annotation can receive the kinds of filter you want to apply on it, for the moment, there is only substring and insensitive.
A struct for the filtering data will be generated with the name [YourStructName]Filters, e.g: ProjectFilters.
Two methods will be generated (let's keep Project as an example):
and
The filter method can be used in conjunction with other diesel methods like inner_join and such.
filter
.inner_join
.select
.
With Rocket
With the rocket feature, the generated struct can be obtained from the request query parameters (dot notation ?filters.name=xxx)
use PaginatedPayload;
async
With Actix
With the actix feature, the generated struct can be obtained from the request query parameters
N.B: unlike the rocket integration, the query parameters must be sent unscopped. e.g ?field=xxx&other=1
use PaginatedPayload;
async
With Pagination
With the pagination feature, you have access to the methods paginate, per_page and load_and_count
use Paginate;
filter
.inner_join
.select
.paginate
.per_page
.
These are independent of the #[pagination] annotation that you can add on your struct to add page and per_page to your generated filter struct and change the signature of the filtered method.
To convert this into Json, with the feature flag serialize you can use PaginatedPayload.
async
License
Diesel filter is licensed under either of the following, at your option:
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or https://opensource.org/licenses/MIT)