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//!
37//! ## Traits Overview
38//!
39//! ### [`InsertableEntity`]
40//!
41//! For types that can be inserted into the database. An "Insertable" struct is generated for
42//! each entity, containing only the fields that should be provided on insert.
43//!
44//! Field annotated with the default annotation are wrappedin [`Defaultable`]. Indicating if the
45//! default value should be used, or the provided one
46//!
47//! ### [`FetchableEntity`]
48//!
49//! For types that can be fetched by primary key(s) from the database. A "Pk" struct is generated
50//! for each entity, containing only the primary key fields.
51//!
52//! ### [`StreamableEntity`]
53//!
54//! For types that can be streamed (selected) from the database. This trait is implemented for
55//! the entity struct, allowing you to stream all rows from the table.
56//!
57//! ### [`UpdatableEntity`]
58//!
59//! For types that can be updated in the database. An "Updatable" struct is generated for each
60//! entity, containing the primary key(s) and updatable fields.
61//!
62//! ### [`DeletableEntity`]
63//!
64//! For types that can be deleted from the database. This trait is implemented for the entity
65//! struct, allowing you to delete a row by its primary key(s).
66
67pub use futures::Stream;
68pub use gremlin_orm_macro::Entity;
69
70/// Used for inserting values, use either the default or the provided value
71pub enum Defaultable<T> {
72 /// Use the default value
73 Default,
74 /// Use the provided value
75 Value(T),
76}
77
78/// Trait for types that can be inserted into the database.
79/// An "Insertable" struct is generated for each entity, containing only the fields that should be provided on insert.
80pub trait InsertableEntity {
81 /// The entity type returned after insertion (typically the main entity struct).
82 type SourceEntity;
83
84 /// Insert the entity into the database.
85 ///
86 /// # Arguments
87 ///
88 /// * `pool` - A reference to a PostgreSQL connection pool.
89 ///
90 /// # Returns
91 ///
92 /// A future resolving to either the inserted entity or a SQLx error.
93 fn insert(
94 &self,
95 pool: &sqlx::PgPool,
96 ) -> impl Future<Output = Result<Self::SourceEntity, sqlx::Error>>;
97}
98
99/// Trait for types that can be fetched by primary key(s) from the database.
100/// A "Pk" struct is generated for each entity, containing only the primary key fields.
101pub trait FetchableEntity {
102 /// The entity type returned after fetching (typically the main entity struct).
103 type SourceEntity;
104
105 /// Fetch the entity from the database by its primary key(s).
106 ///
107 /// # Arguments
108 ///
109 /// * `pool` - A reference to a PostgreSQL connection pool.
110 ///
111 /// # Returns
112 ///
113 /// A future resolving to either `Some(entity)` if found, `None` if not found, or a SQLx error.
114 fn fetch(
115 &self,
116 pool: &sqlx::PgPool,
117 ) -> impl Future<Output = Result<Option<Self::SourceEntity>, sqlx::Error>>;
118}
119
120/// Trait for types that can be streamed (selected) from the database.
121/// This trait is implemented for the entity struct, allowing you to stream all rows from the table.
122pub trait StreamableEntity: Sized {
123 /// Stream all entities from the database table.
124 ///
125 /// # Arguments
126 ///
127 /// * `pool` - A reference to a PostgreSQL connection pool.
128 ///
129 /// # Returns
130 ///
131 /// An async stream of results, each being either the entity or a SQLx error.
132 fn stream(pool: &sqlx::PgPool) -> impl Stream<Item = Result<Self, sqlx::Error>>;
133}
134
135/// Trait for types that can be updated in the database.
136/// An "Updatable" struct is generated for each entity, containing the primary key(s) and updatable fields.
137pub trait UpdatableEntity {
138 /// The entity type returned after updating (typically the main entity struct).
139 type SourceEntity;
140
141 /// Update the entity in the database.
142 ///
143 /// # Arguments
144 ///
145 /// * `pool` - A reference to a PostgreSQL connection pool.
146 ///
147 /// # Returns
148 ///
149 /// A future resolving to either the updated entity or a SQLx error.
150 fn update(
151 &self,
152 pool: &sqlx::PgPool,
153 ) -> impl Future<Output = Result<Self::SourceEntity, sqlx::Error>>;
154}
155
156/// Trait for types that can be deleted from the database.
157/// This trait is implemented for the entity struct, allowing you to delete a row by its primary key(s).
158pub trait DeletableEntity {
159 /// Delete the entity from the database by its primary key(s).
160 ///
161 /// # Arguments
162 ///
163 /// * `pool` - A reference to a PostgreSQL connection pool.
164 ///
165 /// # Returns
166 ///
167 /// A future resolving to `()` if successful, or a SQLx error.
168 fn delete(&self, pool: &sqlx::PgPool) -> impl Future<Output = Result<(), sqlx::Error>>;
169}