mirror of
https://github.com/Phundrak/georm.git
synced 2025-12-13 16:31:51 +01:00
fix: simple ORM for one struct and foreign references work
Currently, all methods declared in the Georm trait are available. If a struct has an ID pointing towards another entity, the user can create a get method to get the entity pointed at from the database too (local one-to-one relationship). I still need to implement remote one-to-one relationships (one-to-one relationships when the ID of the remote object is not available locally). I still need to also test and debug one-to-many relationships (ID of the remote entiies not available locally) and many-to-many relationships (declared in a dedicated table). For now, IDs in all cases are simple types recognized by SQLx that are not arrays. Options are only supported when explicitely specified for one-to-one relationships.
This commit is contained in:
63
tests/models.rs
Normal file
63
tests/models.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use georm::Georm;
|
||||
|
||||
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq, Default)]
|
||||
#[georm(table = "biographies")]
|
||||
pub struct Biography {
|
||||
#[georm(id)]
|
||||
pub id: i32,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq, Default)]
|
||||
#[georm(table = "authors")]
|
||||
pub struct Author {
|
||||
#[georm(id)]
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
#[georm(relation = {entity = Biography, table = "biographies", name = "biography", nullable = true})]
|
||||
pub biography_id: Option<i32>,
|
||||
}
|
||||
|
||||
impl PartialOrd for Author {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.id.cmp(&other.id))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Author {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.id.cmp(&other.id)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq, Default)]
|
||||
#[georm(table = "books")]
|
||||
pub struct Book {
|
||||
#[georm(id)]
|
||||
ident: i32,
|
||||
title: String,
|
||||
#[georm(relation = {entity = Author, table = "authors", name = "author"})]
|
||||
author_id: i32,
|
||||
}
|
||||
|
||||
impl PartialOrd for Book {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.ident.cmp(&other.ident))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Book {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.ident.cmp(&other.ident)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq)]
|
||||
#[georm(table = "reviews")]
|
||||
pub struct Review {
|
||||
#[georm(id)]
|
||||
pub id: i32,
|
||||
#[georm(relation = {entity = Book, table = "books", remote_id = "ident", name = "book"})]
|
||||
pub book_id: i32,
|
||||
pub review: String
|
||||
}
|
||||
Reference in New Issue
Block a user