feat: one-to-many relation helper
Allow one to specify that a field of a model is a foreign key. It will generate a bunch of helper methods to query related entities from one entity.
This commit is contained in:
parent
32ef1f7b33
commit
5f45671b74
25 changed files with 764 additions and 140 deletions
39
DRAFT.md
Normal file
39
DRAFT.md
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# Design draft
|
||||
|
||||
This document is at the attention of the developers of sqlxgentools.
|
||||
|
||||
## Implementing basic relation ship
|
||||
|
||||
### Issues
|
||||
|
||||
Problems with Struct non-flexibility
|
||||
|
||||
### Turning the problem around: Views
|
||||
|
||||
### hasMany / belongsTo relationship
|
||||
|
||||
So we can implements a method
|
||||
|
||||
```rs
|
||||
use repositories::impls::post::RelationShips;
|
||||
|
||||
let post = PostRepository::new(db).get_one_by_id("id_machin")?;
|
||||
post.first_name // OK
|
||||
let authors = post.get_authors()? // we need to require the implementation
|
||||
|
||||
```
|
||||
|
||||
.relations() => give you a RelationFetcherBuilder
|
||||
.of(entity) => give you a RelationFetcher
|
||||
.author
|
||||
|
||||
```rs
|
||||
|
||||
let post_repo = PostRepository::new(db);
|
||||
let author: User = post_repo.relations()
|
||||
.of(post)
|
||||
.author().ok_or(Err)?;
|
||||
let comments: Vec<Comment> = post_repo.relations()
|
||||
.of(post)
|
||||
.comments().ok_or(Err)?;
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue