gremlin_orm/
lib.rs

1//! # `gremlin-orm`
2//!
3//! This crate provides a lightweight, type-safe ORM for PostgreSQL in Rust, focusing on minimal boilerplate and compile-time query safety.
4//!
5//! ## Example
6//!
7//! ```rust
8//! use gremlin_orm::Entity;
9//!
10//! #[derive(Entity, sqlx::FromRow)]
11//! #[orm(table = "public.artist")]
12//! struct Artist {
13//!     #[orm(pk, generated)]
14//!     id: i32,
15//!     name: String,
16//!     #[orm(generated)]
17//!     slug: String,
18//! }
19//! ```
20//!
21//! ## Annotations
22//!
23//! The `#[orm(...)]` attribute supports both struct-level and field-level annotations to
24//! control code generation and database mapping.
25//!
26//! ### Struct-level Annotations
27//!
28//! - `#[orm(table = "schema.table")]`: Specifies the database table for the entity.
29//!
30//! ### Field-level Annotations
31//!
32//! - `#[orm(pk)]`: Marks the field as a primary key. Multiple fields can be marked as primary keys for composite keys.
33//! - `#[orm(generated)]`: Indicates the field is auto-generated by the database (e.g., auto-increment or computed columns). Such fields are excluded from inserts and updates.
34//! - `#[orm(deref)]`: Used for optional/reference types (e.g., `Option<T>`, `&str`, etc.), allowing the macro to handle dereferencing when generating queries.
35//! - `#[orm(default)]`: Allows the field to use a default value when inserting, by wrapping it in `Defaultable<T>`.
36//! - `#[orm(cast = "Type")]`: Casts the field to the specified SQL type in generated queries. This is useful when you want to explicitly cast a column in SQL (e.g., for custom types or to resolve type mismatches).
37//!
38//! ## Traits Overview
39//!
40//! ### [`InsertableEntity`]
41//!
42//! For types that can be inserted into the database. An "Insertable" struct is generated for
43//! each entity, containing only the fields that should be provided on insert.
44//!
45//! Field annotated with the default annotation are wrappedin [`Defaultable`]. Indicating if the
46//! default value should be used, or the provided one
47//!
48//! ### [`FetchableEntity`]
49//!
50//! For types that can be fetched by primary key(s) from the database. A "Pk" struct is generated
51//! for each entity, containing only the primary key fields.
52//!
53//! ### [`StreamableEntity`]
54//!
55//! For types that can be streamed (selected) from the database. This trait is implemented for
56//! the entity struct, allowing you to stream all rows from the table.
57//!
58//! ### [`UpdatableEntity`]
59//!
60//! For types that can be updated in the database. An "Updatable" struct is generated for each
61//! entity, containing the primary key(s) and updatable fields.
62//!
63//! ### [`DeletableEntity`]
64//!
65//! For types that can be deleted from the database. This trait is implemented for the entity
66//! struct, allowing you to delete a row by its primary key(s).
67
68pub use futures::Stream;
69pub use gremlin_orm_macro::Entity;
70
71/// Used for inserting values, use either the default or the provided value
72pub enum Defaultable<T> {
73    /// Use the default value
74    Default,
75    /// Use the provided value
76    Value(T),
77}
78
79/// Trait for types that can be inserted into the database.
80/// An "Insertable" struct is generated for each entity, containing only the fields that should be provided on insert.
81pub trait InsertableEntity {
82    /// The entity type returned after insertion (typically the main entity struct).
83    type SourceEntity;
84
85    /// Insert the entity into the database.
86    ///
87    /// # Arguments
88    ///
89    /// * `pool` - A reference to a PostgreSQL connection pool.
90    ///
91    /// # Returns
92    ///
93    /// A future resolving to either the inserted entity or a SQLx error.
94    fn insert(
95        &self,
96        pool: &sqlx::PgPool,
97    ) -> impl Future<Output = Result<Self::SourceEntity, sqlx::Error>>;
98}
99
100/// Trait for types that can be fetched by primary key(s) from the database.
101/// A "Pk" struct is generated for each entity, containing only the primary key fields.
102pub trait FetchableEntity {
103    /// The entity type returned after fetching (typically the main entity struct).
104    type SourceEntity;
105
106    /// Fetch the entity from the database by its primary key(s).
107    ///
108    /// # Arguments
109    ///
110    /// * `pool` - A reference to a PostgreSQL connection pool.
111    ///
112    /// # Returns
113    ///
114    /// A future resolving to either `Some(entity)` if found, `None` if not found, or a SQLx error.
115    fn fetch(
116        &self,
117        pool: &sqlx::PgPool,
118    ) -> impl Future<Output = Result<Option<Self::SourceEntity>, sqlx::Error>>;
119}
120
121/// Trait for types that can be streamed (selected) from the database.
122/// This trait is implemented for the entity struct, allowing you to stream all rows from the table.
123pub trait StreamableEntity: Sized {
124    /// Stream all entities from the database table.
125    ///
126    /// # Arguments
127    ///
128    /// * `pool` - A reference to a PostgreSQL connection pool.
129    ///
130    /// # Returns
131    ///
132    /// An async stream of results, each being either the entity or a SQLx error.
133    fn stream(pool: &sqlx::PgPool) -> impl Stream<Item = Result<Self, sqlx::Error>>;
134}
135
136/// Trait for types that can be updated in the database.
137/// An "Updatable" struct is generated for each entity, containing the primary key(s) and updatable fields.
138pub trait UpdatableEntity {
139    /// The entity type returned after updating (typically the main entity struct).
140    type SourceEntity;
141
142    /// Update the entity in the database.
143    ///
144    /// # Arguments
145    ///
146    /// * `pool` - A reference to a PostgreSQL connection pool.
147    ///
148    /// # Returns
149    ///
150    /// A future resolving to either the updated entity or a SQLx error.
151    fn update(
152        &self,
153        pool: &sqlx::PgPool,
154    ) -> impl Future<Output = Result<Self::SourceEntity, sqlx::Error>>;
155}
156
157/// Trait for types that can be deleted from the database.
158/// This trait is implemented for the entity struct, allowing you to delete a row by its primary key(s).
159pub trait DeletableEntity {
160    /// Delete the entity from the database by its primary key(s).
161    ///
162    /// # Arguments
163    ///
164    /// * `pool` - A reference to a PostgreSQL connection pool.
165    ///
166    /// # Returns
167    ///
168    /// A future resolving to `()` if successful, or a SQLx error.
169    fn delete(&self, pool: &sqlx::PgPool) -> impl Future<Output = Result<(), sqlx::Error>>;
170}