Skip to content

Commit

Permalink
[breaking] move query builder to query::Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Sep 3, 2023
1 parent 1292a93 commit a2a8002
Show file tree
Hide file tree
Showing 11 changed files with 836 additions and 838 deletions.
22 changes: 11 additions & 11 deletions ensemble/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Once you have created a model and its associated database table, you are ready t
# id: u64,
# name: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
for flight in Flight::all().await? {
println!("{}", flight.name);
}
Expand All @@ -173,7 +173,7 @@ The Ensemble `all` method will return all of the results in the model's table. H
# id: u64,
# name: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let flights: Vec<Flight> = Flight::query()
.r#where("active", '=', 1)
.order_by("name", "asc")
Expand All @@ -194,7 +194,7 @@ If you already have an instance of an Ensemble model that was retrieved from the
# id: u64,
# name: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let flight: Flight = Flight::query()
.r#where("number", '=', "FR 900")
.first().await?
Expand All @@ -216,7 +216,7 @@ In addition to retrieving all of the records matching a given query, you may als
# id: u64,
# name: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
// Retrieve a model by its primary key...
let flight = Flight::find(1).await?;

Expand Down Expand Up @@ -272,7 +272,7 @@ Alternatively, you may use the `create` method to "save" a new model using a sin
# id: u64,
# name: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let flight = Flight::create(Flight {
name: "London to Paris".to_string(),
..Flight::default()
Expand All @@ -292,7 +292,7 @@ The `save` method may also be used to update models that already exist in the da
# id: u64,
# name: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let mut flight = Flight::find(1).await?;

flight.name = "Paris to London".to_string();
Expand All @@ -312,7 +312,7 @@ Updates can also be performed against models that match a given query. In this e
# struct Flight {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
Flight::query()
.r#where("active", '=', 1)
.r#where("destination", '=', "San Diego")
Expand All @@ -334,7 +334,7 @@ To delete a model, you may call the delete method on the model instance:
# struct Flight {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let flight = Flight::find(1).await?;

flight.delete().await?;
Expand All @@ -350,7 +350,7 @@ You may call the truncate method to delete all of the model's associated databas
# struct Flight {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
Flight::query().truncate().await?;
# Ok(())
# }
Expand All @@ -366,7 +366,7 @@ Of course, you may build an Ensemble query to delete all models matching your qu
# struct Flight {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
Flight::query()
.r#where("active", '=', 0)
.delete().await?;
Expand All @@ -384,7 +384,7 @@ To convert a model to JSON, you should use the `json` method. This will return a
# struct User {
# pub id: u64
# }
# async fn test() -> Result<serde_json::Value, ensemble::query::Error> {
# async fn test() -> Result<serde_json::Value, ensemble::Error> {
let user = User::find(1).await?;

return Ok(user.json());
Expand Down
32 changes: 16 additions & 16 deletions ensemble/docs/relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use ensemble::relationships::Relationship;
# struct Post {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
# let mut user = User::find(1).await?;
let posts = user.posts().await?;

Expand Down Expand Up @@ -70,7 +70,7 @@ The [`HasOne`] type expects two generics: the type of the current model, and the
# struct Phone {
# id: u64,
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let mut user = User::find(1).await?;

let phone = user.phone().await?;
Expand Down Expand Up @@ -179,7 +179,7 @@ Once the relationship method has been defined, we can access the list of related
# id: u64,
# text: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let mut post = Post::find(1).await?;

for comment in post.comments().await? {
Expand All @@ -202,7 +202,7 @@ Since all relationships also serve as query builders, you may add further constr
# struct Comment {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let post = Post::find(1).await?;

let comment: Option<Comment> = post.comments.query()
Expand Down Expand Up @@ -266,7 +266,7 @@ Once the relationship has been defined, we can retrieve a comment's parent post
# id: u64,
# title: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let mut comment = Comment::find(1).await?;

let post_title = &comment.post().await?.title;
Expand Down Expand Up @@ -355,7 +355,7 @@ Once the relationship is defined, you may access the user's roles using the `rol
# id: u64,
# name: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let mut user = User::find(1).await?;

for role in user.roles().await? {
Expand All @@ -378,7 +378,7 @@ Since all relationships also serve as query builders, you may add further constr
# struct Role {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let user = User::find(1).await?;

let roles: Vec<Role> = user.roles.query()
Expand Down Expand Up @@ -479,7 +479,7 @@ You may query the `posts` relationship and add additional constraints to the rel
# struct Post {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let user = User::find(1).await?;

let posts: Vec<Post> = user.posts.query()
Expand All @@ -504,7 +504,7 @@ As demonstrated in the example above, you are free to add additional constraints
# struct Post {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
# let mut user = User::find(1).await?;
let posts: Vec<Post> = user.posts.query()
.r#where("active", '=', 1)
Expand Down Expand Up @@ -535,7 +535,7 @@ In most situations, you should use logical groups to group the conditional check
# struct Post {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
# let mut user = User::find(1).await?;
let posts: Vec<Post> = user.posts.query()
.r#where("active", '=', 1)
Expand Down Expand Up @@ -571,7 +571,7 @@ If you do not need to add additional constraints to an Ensemble relationship que
# id: u64,
# title: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let mut user = User::find(1).await?;

for post in user.posts().await? {
Expand All @@ -598,7 +598,7 @@ Sometimes you may want to count the number of related models for a given relatio
# struct Post {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let user = User::find(1).await?;

let posts_count = user.posts.query().count().await?;
Expand Down Expand Up @@ -639,7 +639,7 @@ Now, let's retrieve all books and their authors:
# id: u64,
# name: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
for mut book in Book::all().await? {
let author = book.author().await?;

Expand All @@ -665,7 +665,7 @@ Thankfully, we can use eager loading to reduce this operation to just two querie
# id: u64,
# name: String
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
for mut book in Book::with("author").get::<Book>().await? {
let author = book.author().await?;

Expand Down Expand Up @@ -693,7 +693,7 @@ Sometimes you may need to eager load several different relationships. To do so,
# struct Book {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
let books: Vec<Book> = Book::with(vec!["author", "publisher"]).get().await?;
# Ok(())
# }
Expand All @@ -709,7 +709,7 @@ Sometimes you may need to eager load a relationship after the parent model has a
# struct Book {
# id: u64
# }
# async fn example() -> Result<(), ensemble::query::Error> {
# async fn example() -> Result<(), ensemble::Error> {
# let someCondition = true;
let mut book = Book::find(1).await?;

Expand Down
Loading

0 comments on commit a2a8002

Please sign in to comment.