1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! # `gremlin-orm`
//!
//! This crate provides a lightweight, type-safe ORM for PostgreSQL in Rust, focusing on minimal boilerplate and compile-time query safety.
//!
//! ## Example
//!
//! ```rust
//! use gremlin_orm::Entity;
//!
//! #[derive(Entity, sqlx::FromRow)]
//! #[orm(table = "public.artist")]
//! struct Artist {
//! #[orm(pk, generated)]
//! id: i32,
//! name: String,
//! #[orm(generated)]
//! slug: String,
//! }
//!
//! // Example with soft delete support
//! #[derive(Entity, sqlx::FromRow)]
//! #[orm(table = "public.soft_delete", soft_delete = "deleted_at")]
//! struct SoftDelete {
//! #[orm(pk, generated)]
//! id: i32,
//! value: i32,
//! #[orm(default)]
//! deleted_at: Option<chrono::NaiveDateTime>,
//! }
//! ```
//!
//! ## Annotations
//!
//! The `#[orm(...)]` attribute supports both struct-level and field-level annotations to
//! control code generation and database mapping.
//!
//! ### Struct-level Annotations
//!
//! - `#[orm(table = "schema.table")]`: Specifies the database table for the entity.
//! - `#[orm(soft_delete = "column_name")]`: Enables soft delete support for the entity. The given column (typically an `Option<chrono::NaiveDateTime>`) will be set to the current timestamp instead of deleting the row. Entities with a non-NULL value in this column are considered deleted and will be excluded from fetch, stream, and update operations.
//!
//! ### Field-level Annotations
//!
//! - `#[orm(pk)]`: Marks the field as a primary key. Multiple fields can be marked as primary keys for composite keys.
//! - `#[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.
//! - `#[orm(deref)]`: Used for optional/reference types (e.g., `Option<T>`, `&str`, etc.), allowing the macro to handle dereferencing when generating queries.
//! - `#[orm(as_ref)]`: Used for optional primitive types (e.g., `Option<i32>`, `Option<bool>`), calling `.as_ref()` instead of `.as_deref()` when generating update queries. This is useful for optional primitive values that don't need dereferencing.
//! - `#[orm(default)]`: Allows the field to use a default value when inserting, by wrapping it in `Defaultable<T>`.
//! - `#[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).
//!
//! ## Traits Overview
//!
//! ### [`InsertableEntity`]
//! For types that can be inserted into the database. An "Insertable" struct is generated for
//! each entity, containing only the fields that should be provided on insert.
//!
//! Fields annotated with the default annotation are wrapped in [`Defaultable`], indicating if the
//! default value should be used, or the provided one.
//!
//! ### [`FetchableEntity`]
//!
//! For types that can be fetched by primary key(s) from the database. A "Pk" struct is generated
//! for each entity, containing only the primary key fields.
//!
//! > If the entity uses soft delete, fetch operations will return `None` for rows where the soft delete column is set (i.e., not NULL).
//!
//! ### [`StreamableEntity`]
//!
//! For types that can be streamed (selected) from the database. This trait is implemented for
//! the entity struct, allowing you to stream all rows from the table.
//!
//! > If the entity uses soft delete, only rows where the soft delete column is NULL will be streamed.
//!
//! ### [`UpdatableEntity`]
//!
//! For types that can be updated in the database. An "Updatable" struct is generated for each
//! entity, containing the primary key(s) and updatable fields.
//!
//! > Updates will only affect rows that are not soft deleted (i.e., where the soft delete column is NULL).
//!
//! ### [`DeletableEntity`]
//!
//! For types that can be deleted from the database. This trait is implemented for the entity
//! struct, allowing you to delete a row by its primary key(s).
//!
//! > If the entity uses soft delete, calling `delete` will set the soft delete column to the current timestamp instead of removing the row from the database.
//!
pub use Stream;
pub use Entity;
use PgExecutor;
/// Used for inserting values, use either the default or the provided value
/// Trait for types that can be inserted into the database.
/// An "Insertable" struct is generated for each entity, containing only the fields that should be provided on insert.
/// Trait for types that can be fetched by primary key(s) from the database.
/// A "Pk" struct is generated for each entity, containing only the primary key fields.
/// Trait for types that can be streamed (selected) from the database.
/// This trait is implemented for the entity struct, allowing you to stream all rows from the table.
/// Trait for types that can be updated in the database.
/// An "Updatable" struct is generated for each entity, containing the primary key(s) and updatable fields.
/// Trait for types that can be deleted from the database.
/// This trait is implemented for the entity struct, allowing you to delete a row by its primary key(s).