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;
70use sqlx::PgExecutor;
71
72/// Used for inserting values, use either the default or the provided value
73pub enum Defaultable<T> {
74 /// Use the default value
75 Default,
76 /// Use the provided value
77 Value(T),
78}
79
80/// Trait for types that can be inserted into the database.
81/// An "Insertable" struct is generated for each entity, containing only the fields that should be provided on insert.
82pub trait InsertableEntity {
83 /// The entity type returned after insertion (typically the main entity struct).
84 type SourceEntity;
85
86 /// Insert the entity into the database.
87 ///
88 /// # Arguments
89 ///
90 /// * `pool` - A reference to a PostgreSQL connection pool.
91 ///
92 /// # Returns
93 ///
94 /// A future resolving to either the inserted entity or a SQLx error.
95 fn insert<'a>(
96 &self,
97 executor: impl PgExecutor<'a>,
98 ) -> impl Future<Output = Result<Self::SourceEntity, sqlx::Error>>;
99}
100
101/// Trait for types that can be fetched by primary key(s) from the database.
102/// A "Pk" struct is generated for each entity, containing only the primary key fields.
103pub trait FetchableEntity {
104 /// The entity type returned after fetching (typically the main entity struct).
105 type SourceEntity;
106
107 /// Fetch the entity from the database by its primary key(s).
108 ///
109 /// # Arguments
110 ///
111 /// * `pool` - A reference to a PostgreSQL connection pool.
112 ///
113 /// # Returns
114 ///
115 /// A future resolving to either `Some(entity)` if found, `None` if not found, or a SQLx error.
116 fn fetch<'a>(
117 &self,
118 executor: impl PgExecutor<'a>,
119 ) -> impl Future<Output = Result<Option<Self::SourceEntity>, sqlx::Error>>;
120}
121
122/// Trait for types that can be streamed (selected) from the database.
123/// This trait is implemented for the entity struct, allowing you to stream all rows from the table.
124pub trait StreamableEntity: Sized {
125 /// Stream all entities from the database table.
126 ///
127 /// # Arguments
128 ///
129 /// * `pool` - A reference to a PostgreSQL connection pool.
130 ///
131 /// # Returns
132 ///
133 /// An async stream of results, each being either the entity or a SQLx error.
134 fn stream<'a>(
135 executor: impl PgExecutor<'a> + 'a,
136 ) -> impl Stream<Item = Result<Self, sqlx::Error>>;
137}
138
139/// Trait for types that can be updated in the database.
140/// An "Updatable" struct is generated for each entity, containing the primary key(s) and updatable fields.
141pub trait UpdatableEntity {
142 /// The entity type returned after updating (typically the main entity struct).
143 type SourceEntity;
144
145 /// Update the entity in the database.
146 ///
147 /// # Arguments
148 ///
149 /// * `pool` - A reference to a PostgreSQL connection pool.
150 ///
151 /// # Returns
152 ///
153 /// A future resolving to either the updated entity or a SQLx error.
154 fn update<'a>(
155 &self,
156 executor: impl PgExecutor<'a>,
157 ) -> impl Future<Output = Result<Self::SourceEntity, sqlx::Error>>;
158}
159
160/// Trait for types that can be deleted from the database.
161/// This trait is implemented for the entity struct, allowing you to delete a row by its primary key(s).
162pub trait DeletableEntity {
163 /// Delete the entity from the database by its primary key(s).
164 ///
165 /// # Arguments
166 ///
167 /// * `pool` - A reference to a PostgreSQL connection pool.
168 ///
169 /// # Returns
170 ///
171 /// A future resolving to `()` if successful, or a SQLx error.
172 fn delete<'a>(
173 &self,
174 executor: impl PgExecutor<'a>,
175 ) -> impl Future<Output = Result<(), sqlx::Error>>;
176}