feat: rename crates from gejdr-crud to georm

This commit renames gejdr-crud to georm for an easier name to remember
in case I publish `georm` and `georm-macros`. This commit extracts the
`Georm` (formerly `Crud`) trait from `gejdr-core` to its dedicated
crate `georm` on which gejdr-core now depends.

Currently writing tests
This commit is contained in:
2025-01-25 23:41:44 +01:00
parent 915bd8387e
commit 857b1d98d0
19 changed files with 285 additions and 180 deletions

View File

@@ -9,7 +9,7 @@ serde = "1.0.215"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["fmt", "std", "env-filter", "registry", "json", "tracing-log"] }
uuid = { version = "1.11.0", features = ["v4", "serde"] }
gejdr-macros = { path = "../gejdr-macros" }
georm = { path = "../georm" }
[dependencies.sqlx]
version = "0.8.3"

View File

@@ -0,0 +1,5 @@
DROP TABLE IF EXISTS book_genres;
DROP TABLE IF EXISTS books;
DROP TABLE IF EXISTS genres;
DROP TABLE IF EXISTS authors;
DROP SCHEMA IF EXISTS tests;

View File

@@ -0,0 +1,26 @@
CREATE SCHEMA IF NOT EXISTS tests;
CREATE TABLE tests.authors (
author_id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE tests.books (
id SERIAL PRIMARY KEY,
title VARCHAR(100) NOT NULL,
author_id INT NOT NULL,
FOREIGN KEY (author_id) REFERENCES tests.authors(author_id) ON DELETE CASCADE
);
CREATE TABLE tests.genres (
genre_id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE tests.book_genres (
book_id INT NOT NULL,
genre_id INT NOT NULL,
PRIMARY KEY (book_id, genre_id),
FOREIGN KEY (book_id) REFERENCES tests.books(id) ON DELETE CASCADE,
FOREIGN KEY (genre_id) REFERENCES tests.genres(genre_id) ON DELETE CASCADE
);

View File

@@ -1,4 +1,4 @@
use super::Crud;
use georm::Georm;
use sqlx::PgPool;
type Timestampz = chrono::DateTime<chrono::Utc>;
@@ -23,10 +23,10 @@ impl RemoteUser {
}
}
#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq, Eq, Default, Clone, Crud)]
#[crud(table = "users")]
#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq, Eq, Default, Clone, Georm)]
#[georm(table = "users")]
pub struct User {
#[crud(id)]
#[georm(id)]
pub id: String,
pub username: String,
pub email: Option<String>,

View File

@@ -1,76 +1 @@
pub mod accounts;
pub use gejdr_macros::Crud;
pub trait Crud<Id> {
/// Find the entiy in the database based on its identifier.
///
/// # Errors
/// Returns any error Postgres may have encountered
fn find(
pool: &sqlx::PgPool,
id: &Id,
) -> impl std::future::Future<Output = sqlx::Result<Option<Self>>> + Send
where
Self: Sized;
/// Create the entity in the database.
///
/// # Errors
/// Returns any error Postgres may have encountered
fn create(
&self,
pool: &sqlx::PgPool,
) -> impl std::future::Future<Output = sqlx::Result<Self>> + Send
where
Self: Sized;
/// Update an entity with a matching identifier in the database.
///
/// # Errors
/// Returns any error Postgres may have encountered
fn update(
&self,
pool: &sqlx::PgPool,
) -> impl std::future::Future<Output = sqlx::Result<Self>> + Send
where
Self: Sized;
/// Update an entity with a matching identifier in the database if
/// it exists, create it otherwise.
///
/// # Errors
/// Returns any error Postgres may have encountered
fn create_or_update(
&self,
pool: &sqlx::PgPool,
) -> impl std::future::Future<Output = sqlx::Result<Self>> + Send
where
Self: Sized;
/// Delete the entity from the database if it exists.
///
/// # Returns
/// Returns the amount of rows affected by the deletion.
///
/// # Errors
/// Returns any error Postgres may have encountered
fn delete(
&self,
pool: &sqlx::PgPool,
) -> impl std::future::Future<Output = sqlx::Result<u64>> + Send;
/// Delete any entity with the identifier `id`.
///
/// # Returns
/// Returns the amount of rows affected by the deletion.
///
/// # Errors
/// Returns any error Postgres may have encountered
fn delete_by_id(
pool: &sqlx::PgPool,
id: &Id,
) -> impl std::future::Future<Output = sqlx::Result<u64>> + Send;
/// Returns the identifier of the entity.
fn get_id(&self) -> &Id;
}