40 lines
830 B
Markdown
40 lines
830 B
Markdown
|
|
# 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)?;
|
||
|
|
```
|