Skip to main content

Crate cross_model_search

Crate cross_model_search 

Source
Expand description

Search through several models at once, and get one ordered and paginated list of mixed models.

This is Cross Model Search for Laravel Rust. Declare an enum with a variant for each model, add each model’s query and the columns to search through, and run the search:

use cross_model_search::{SearchResult, through};
use serde::Serialize;

#[derive(SearchResult, Serialize)]
#[serde(tag = "type")]
enum Hit {
    Post(Post),
    Video(Video),
}

let hits = Hit::search("laravel eloquent")
    .add_query(Post::query().with(Post::comments), (Post::title, through(Post::comments, Comment::body)))
    .add(Video::title)
    .order_by_desc()
    .paginate(15)
    .await?;

for hit in hits.items() {
    match hit {
        Hit::Post(post) => println!("post {}", post.title),
        Hit::Video(video) => println!("video {}", video.title),
    }
}

The documentation in docs/ covers each feature in Laravel PHP and Laravel Rust.

Structs§

Key
The key of a model of one of a SearchResult type’s variants, read from a search’s combined rows.
MissingModelError
A model that the search found was gone when the search loaded it, because another request deleted or changed it between the two queries.
NoModelsError
A search needs at least one model to search through.
PaginatedSearch
A Search that returns a page of results with a Paginator.
Search
A search through several models at once, which returns one list of H, ordered and optionally paginated.
SearchColumn
A column that a search looks through for a model M.
SimplePaginatedSearch
A Search that returns a page of results with a SimplePaginator.

Traits§

FromModel
A SearchResult type that holds models of type M in the variant numbered VARIANT.
IntoColumns
One or more columns that a search looks through for the model M.
ModelOrder
A tuple of models, in the order that Search::order_by_model orders the results by.
SearchResult
A type that holds one search result, which is a model of one of several types.

Functions§

parse_terms
Split terms into the words to search for.
through
Search columns of the models that relation relates to, such as the bodies of a post’s comments with through(Post::comments, Comment::body).

Derive Macros§

SearchResult
Derive SearchResult, FromModel, and a search function on an enum whose variants each hold one model.