georm 0.2.1

Georm, a simple, opiniated SQLx ORM for PostgreSQL
Documentation
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! # Georm
//!
//! A simple, type-safe PostgreSQL ORM built on SQLx with zero runtime overhead.
//!
//! ## Quick Start
//!
//! ```ignore
//! use georm::Georm;
//!
//! // Note: No need to derive FromRow - Georm generates it automatically
//! #[derive(Georm)]
//! #[georm(table = "users")]
//! pub struct User {
//!     #[georm(id)]
//!     id: i32,
//!     username: String,
//!     email: String,
//! }
//!
//! // Use generated methods
//! let user = User::find(&pool, &1).await?;      // Static method
//! let all_users = User::find_all(&pool).await?; // Static method
//! user.update(&pool).await?;                    // Instance method
//! ```
//!
//! ## Core CRUD Operations
//!
//! ### Static Methods (called on the struct type)
//! - `Entity::find(pool, &id)` - Find by primary key, returns `Option<Entity>`
//! - `Entity::find_all(pool)` - Get all records, returns `Vec<Entity>`
//! - `Entity::delete_by_id(pool, &id)` - Delete by ID, returns affected row count
//!
//! ### Instance Methods (called on entity objects)
//! - `entity.create(pool)` - Insert new record, returns created entity with database-generated values
//! - `entity.update(pool)` - Update existing record, returns updated entity with fresh database state
//! - `entity.create_or_update(pool)` - True PostgreSQL upsert using `ON CONFLICT`, returns final entity
//! - `entity.delete(pool)` - Delete this record, returns affected row count
//! - `entity.get_id()` - Get reference to the entity's ID (`&Id` for simple keys, owned for composite)
//!
//! ```ignore
//! // Static methods
//! let user = User::find(&pool, &1).await?.unwrap();
//! let all_users = User::find_all(&pool).await?;
//! let deleted_count = User::delete_by_id(&pool, &1).await?;
//!
//! // Instance methods
//! let new_user = User { id: 0, username: "alice".to_string(), email: "alice@example.com".to_string() };
//! let created = new_user.create(&pool).await?;        // Returns entity with actual generated ID
//! let updated = created.update(&pool).await?;         // Returns entity with fresh database state
//! let deleted_count = updated.delete(&pool).await?;
//! ```
//!
//! ### PostgreSQL Optimizations
//!
//! Georm leverages PostgreSQL-specific features for performance and reliability:
//!
//! - **RETURNING clause**: All `INSERT` and `UPDATE` operations use `RETURNING *` to capture database-generated values (sequences, defaults, triggers)
//! - **True upserts**: `create_or_update()` uses `INSERT ... ON CONFLICT ... DO UPDATE` for atomic upsert operations
//! - **Prepared statements**: All queries use parameter binding for security and performance
//! - **Compile-time verification**: SQLx macros verify all generated SQL against your database schema at compile time
//!
//! ## Primary Keys and Identifiers
//!
//! ### Simple Primary Keys
//!
//! Primary key fields can have any name (not just "id"):
//!
//! ```ignore
//! #[derive(Georm)]
//! #[georm(table = "books")]
//! pub struct Book {
//!     #[georm(id)]
//!     ident: i32,  // Custom field name for primary key
//!     title: String,
//! }
//!
//! // Works the same way
//! let book = Book::find(&pool, &1).await?;
//! ```
//!
//! ### Composite Primary Keys
//!
//! Mark multiple fields with `#[georm(id)]` for composite keys:
//!
//! ```ignore
//! #[derive(Georm)]
//! #[georm(table = "user_roles")]
//! pub struct UserRole {
//!     #[georm(id)]
//!     user_id: i32,
//!     #[georm(id)]
//!     role_id: i32,
//!     assigned_at: chrono::DateTime<chrono::Utc>,
//! }
//! ```
//!
//! This automatically generates a composite ID struct following the `{EntityName}Id` pattern:
//!
//! ```ignore
//! // Generated automatically by the macro
//! pub struct UserRoleId {
//!     pub user_id: i32,
//!     pub role_id: i32,
//! }
//! ```
//!
//! Usage with composite keys:
//!
//! ```ignore
//! // Static methods work with generated ID structs
//! let id = UserRoleId { user_id: 1, role_id: 2 };
//! let user_role = UserRole::find(&pool, &id).await?;
//! UserRole::delete_by_id(&pool, &id).await?;
//!
//! // Instance methods work the same way
//! let role = UserRole { user_id: 1, role_id: 2, assigned_at: chrono::Utc::now() };
//! let created = role.create(&pool).await?;
//! let id = created.get_id(); // Returns owned UserRoleId for composite keys
//! ```
//!
//! ### Composite Key Limitations
//!
//! - **Relationships not supported**: Entities with composite primary keys cannot
//!   yet define relationships (one-to-one, one-to-many, many-to-many)
//! - **ID struct naming**: Generated ID struct follows pattern `{EntityName}Id` (not customizable)
//!
//! ## Defaultable Fields
//!
//! Use `#[georm(defaultable)]` for fields with database defaults or auto-generated values:
//!
//! ```ignore
//! #[derive(Georm)]
//! #[georm(table = "posts")]
//! pub struct Post {
//!     #[georm(id, defaultable)]
//!     id: i32,                    // Auto-generated serial
//!     title: String,              // Required field
//!     #[georm(defaultable)]
//!     published: bool,            // Has database default
//!     #[georm(defaultable)]
//!     created_at: chrono::DateTime<chrono::Utc>, // DEFAULT NOW()
//!     #[georm(defaultable)]
//!     pub(crate) internal_note: String,          // Field visibility preserved
//!     author_id: i32,             // Required field
//! }
//! ```
//!
//! This generates a companion `PostDefault` struct where defaultable fields become `Option<T>`:
//!
//! ```ignore
//! // Generated automatically by the macro
//! pub struct PostDefault {
//!     pub id: Option<i32>,        // Can be None for auto-generation
//!     pub title: String,          // Required field stays the same
//!     pub published: Option<bool>, // Can be None to use database default
//!     pub created_at: Option<chrono::DateTime<chrono::Utc>>, // Can be None
//!     pub(crate) internal_note: Option<String>,  // Visibility preserved
//!     pub author_id: i32,         // Required field stays the same
//! }
//!
//! impl Defaultable<i32, Post> for PostDefault {
//!     async fn create(&self, pool: &sqlx::PgPool) -> sqlx::Result<Post>;
//! }
//! ```
//!
//! ### Usage Example
//!
//! ```ignore
//! use georm::{Georm, Defaultable};
//!
//! // Create a post with some fields using database defaults
//! let post_default = PostDefault {
//!     id: None,                   // Let database auto-generate
//!     title: "My Blog Post".to_string(),
//!     published: None,            // Use database default (e.g., false)
//!     created_at: None,           // Use database default (e.g., NOW())
//!     internal_note: Some("Draft".to_string()),
//!     author_id: 42,
//! };
//!
//! // Create the entity in the database (instance method on PostDefault)
//! let created_post = post_default.create(&pool).await?;
//! println!("Created post with ID: {}", created_post.id);
//! ```
//!
//! ### Defaultable Rules and Limitations
//!
//! - **Option fields cannot be marked as defaultable**: If a field is already
//!   `Option<T>`, you cannot mark it with `#[georm(defaultable)]`. This prevents
//!   `Option<Option<T>>` types and causes a compile-time error.
//! - **Field visibility is preserved**: The generated defaultable struct maintains
//!   the same field visibility (`pub`, `pub(crate)`, private) as the original struct.
//! - **ID fields can be defaultable**: It's common to mark ID fields as defaultable
//!   when they are auto-generated serials in PostgreSQL.
//! - **Only generates when needed**: The defaultable struct is only generated if
//!   at least one field is marked as defaultable.
//!
//! ## Relationships
//!
//! Georm supports comprehensive relationship modeling with two approaches: field-level
//! relationships for foreign keys and struct-level relationships for reverse lookups.
//! Each relationship method call executes a separate database query.
//!
//! ### Field-Level Relationships (Foreign Keys)
//!
//! Use the `relation` attribute on foreign key fields to generate lookup methods:
//!
//! ```ignore
//! #[derive(Georm)]
//! #[georm(table = "posts")]
//! pub struct Post {
//!     #[georm(id)]
//!     id: i32,
//!     title: String,
//!     #[georm(relation = {
//!         entity = Author,        // Target entity type
//!         table = "authors",      // Target table name
//!         name = "author",        // Method name (generates get_author)
//!         remote_id = "id",       // Target table's key column (default: "id")
//!         nullable = false        // Whether relationship can be null (default: false)
//!     })]
//!     author_id: i32,
//! }
//! ```
//!
//! **Generated instance method**: `post.get_author(pool).await? -> sqlx::Result<Author>`
//!
//! For nullable relationships:
//!
//! ```ignore
//! #[derive(Georm)]
//! #[georm(table = "posts")]
//! pub struct Post {
//!     #[georm(id)]
//!     id: i32,
//!     title: String,
//!     #[georm(relation = {
//!         entity = Category,
//!         table = "categories",
//!         name = "category",
//!         nullable = true         // Allows NULL values
//!     })]
//!     category_id: Option<i32>,
//! }
//! ```
//!
//! **Generated instance method**: `post.get_category(pool).await? -> sqlx::Result<Option<Category>>`
//!
//! Since `remote_id` and `nullable` have default values, this is equivalent:
//!
//! ```ignore
//! #[georm(relation = { entity = Author, table = "authors", name = "author" })]
//! author_id: i32,
//! ```
//!
//! #### Non-Standard Primary Key References
//!
//! Use `remote_id` to reference tables with non-standard primary key names:
//!
//! ```ignore
//! #[derive(Georm)]
//! #[georm(table = "reviews")]
//! pub struct Review {
//!     #[georm(id)]
//!     id: i32,
//!     #[georm(relation = {
//!         entity = Book,
//!         table = "books",
//!         name = "book",
//!         remote_id = "ident"     // Book uses 'ident' instead of 'id'
//!     })]
//!     book_id: i32,
//!     content: String,
//! }
//! ```
//!
//! #### Field-Level Relationship Attributes
//!
//! | Attribute    | Description                                          | Required | Default |
//! |--------------|------------------------------------------------------|----------|---------|
//! | `entity`     | Target entity type                                   | Yes      | N/A     |
//! | `name`       | Method name (generates `get_{name}`)                 | Yes      | N/A     |
//! | `table`      | Target table name                                    | Yes      | N/A     |
//! | `remote_id`  | Target table's key column                            | No       | `"id"`  |
//! | `nullable`   | Whether relationship can be null                     | No       | `false` |
//!
//! ### Struct-Level Relationships (Reverse Lookups)
//!
//! Define relationships at the struct level to query related entities that reference this entity.
//! These generate separate database queries for each method call.
//!
//! #### One-to-One Relationships
//!
//! ```ignore
//! #[derive(Georm)]
//! #[georm(
//!     table = "users",
//!     one_to_one = [{
//!         entity = Profile,       // Related entity type
//!         name = "profile",       // Method name (generates get_profile)
//!         table = "profiles",     // Related table name
//!         remote_id = "user_id",  // Foreign key in related table
//!     }]
//! )]
//! pub struct User {
//!     #[georm(id)]
//!     id: i32,
//!     username: String,
//! }
//! ```
//!
//! **Generated instance method**: `user.get_profile(pool).await? -> sqlx::Result<Option<Profile>>`
//!
//! #### One-to-Many Relationships
//!
//! ```ignore
//! #[derive(Georm)]
//! #[georm(
//!     table = "authors",
//!     one_to_many = [{
//!         entity = Post,          // Related entity type
//!         name = "posts",         // Method name (generates get_posts)
//!         table = "posts",        // Related table name
//!         remote_id = "author_id" // Foreign key in related table
//!     }, {
//!         entity = Comment,       // Multiple relationships allowed
//!         name = "comments",
//!         table = "comments",
//!         remote_id = "author_id"
//!     }]
//! )]
//! pub struct Author {
//!     #[georm(id)]
//!     id: i32,
//!     name: String,
//! }
//! ```
//!
//! **Generated instance methods**:
//! - `author.get_posts(pool).await? -> sqlx::Result<Vec<Post>>`
//! - `author.get_comments(pool).await? -> sqlx::Result<Vec<Comment>>`
//!
//! #### Many-to-Many Relationships
//!
//! For many-to-many relationships, specify the link table that connects the entities:
//!
//! ```sql
//! -- Example schema for books and genres
//! CREATE TABLE books (
//!     id SERIAL PRIMARY KEY,
//!     title VARCHAR(200) NOT NULL
//! );
//!
//! CREATE TABLE genres (
//!     id SERIAL PRIMARY KEY,
//!     name VARCHAR(100) NOT NULL
//! );
//!
//! CREATE TABLE book_genres (
//!     book_id INT NOT NULL REFERENCES books(id),
//!     genre_id INT NOT NULL REFERENCES genres(id),
//!     PRIMARY KEY (book_id, genre_id)
//! );
//! ```
//!
//! ```ignore
//! #[derive(Georm)]
//! #[georm(
//!     table = "books",
//!     many_to_many = [{
//!         entity = Genre,         // Related entity type
//!         name = "genres",        // Method name (generates get_genres)
//!         table = "genres",       // Related table name
//!         remote_id = "id",       // Primary key in related table (default: "id")
//!         link = {                // Link table configuration
//!             table = "book_genres",  // Join table name
//!             from = "book_id",       // Column referencing this entity
//!             to = "genre_id"         // Column referencing related entity
//!         }
//!     }]
//! )]
//! pub struct Book {
//!     #[georm(id)]
//!     id: i32,
//!     title: String,
//! }
//!
//! #[derive(Georm)]
//! #[georm(
//!     table = "genres",
//!     many_to_many = [{
//!         entity = Book,
//!         name = "books",
//!         table = "books",
//!         link = {
//!             table = "book_genres",
//!             from = "genre_id",      // Note: reversed perspective
//!             to = "book_id"
//!         }
//!     }]
//! )]
//! pub struct Genre {
//!     #[georm(id)]
//!     id: i32,
//!     name: String,
//! }
//! ```
//!
//! **Generated instance methods**:
//! - `book.get_genres(pool).await? -> sqlx::Result<Vec<Genre>>`
//! - `genre.get_books(pool).await? -> sqlx::Result<Vec<Book>>`
//!
//! #### Struct-Level Relationship Attributes
//!
//! | Attribute    | Description                                          | Required | Default |
//! |--------------|------------------------------------------------------|----------|---------|
//! | `entity`     | Target entity type                                   | Yes      | N/A     |
//! | `name`       | Method name (generates `get_{name}`)                 | Yes      | N/A     |
//! | `table`      | Target table name                                    | Yes      | N/A     |
//! | `remote_id`  | Target table's key column                            | No       | `"id"`  |
//! | `link.table` | Join table name (many-to-many only)                  | Yes*     | N/A     |
//! | `link.from`  | Column referencing this entity (many-to-many only)   | Yes*     | N/A     |
//! | `link.to`    | Column referencing target entity (many-to-many only) | Yes*     | N/A     |
//!
//! *Required for many-to-many relationships
//!
//! As with field-level relationships, `remote_id` is optional and defaults to `"id"`:
//!
//! ```ignore
//! #[georm(
//!     table = "users",
//!     one_to_many = [{ entity = Post, name = "posts", table = "posts" }]
//! )]
//! ```
//!
//! #### Complex Relationship Example
//!
//! Here's a comprehensive example showing multiple relationship types:
//!
//! ```ignore
//! #[derive(Georm)]
//! #[georm(
//!     table = "posts",
//!     one_to_many = [{
//!         entity = Comment,
//!         name = "comments",
//!         table = "comments",
//!         remote_id = "post_id"
//!     }],
//!     many_to_many = [{
//!         entity = Tag,
//!         name = "tags",
//!         table = "tags",
//!         link = {
//!             table = "post_tags",
//!             from = "post_id",
//!             to = "tag_id"
//!         }
//!     }]
//! )]
//! pub struct Post {
//!     #[georm(id)]
//!     id: i32,
//!     title: String,
//!     content: String,
//!
//!     // Field-level relationship (foreign key)
//!     #[georm(relation = {
//!         entity = Author,
//!         table = "authors",
//!         name = "author"
//!     })]
//!     author_id: i32,
//!
//!     // Nullable field-level relationship
//!     #[georm(relation = {
//!         entity = Category,
//!         table = "categories",
//!         name = "category",
//!         nullable = true
//!     })]
//!     category_id: Option<i32>,
//! }
//! ```
//!
//! **Generated instance methods**:
//! - `post.get_author(pool).await? -> sqlx::Result<Author>` (from field relation)
//! - `post.get_category(pool).await? -> sqlx::Result<Option<Category>>` (nullable field relation)
//! - `post.get_comments(pool).await? -> sqlx::Result<Vec<Comment>>` (one-to-many)
//! - `post.get_tags(pool).await? -> sqlx::Result<Vec<Tag>>` (many-to-many)
//!
//! ## Error Handling
//!
//! All Georm methods return `sqlx::Result<T>` which can contain:
//!
//! - **Database errors**: Connection issues, constraint violations, etc.
//! - **Not found errors**: When `find()` operations return `None`
//! - **Compile-time errors**: Invalid SQL, type mismatches, schema validation failures
//!
//! ### Compile-Time Validations
//!
//! Georm performs several validations at compile time:
//!
//! ```ignore
//! // ❌ Compile error: No ID field specified
//! #[derive(Georm)]
//! #[georm(table = "invalid")]
//! pub struct Invalid {
//!     name: String,  // Missing #[georm(id)]
//! }
//!
//! // ❌ Compile error: Option<T> cannot be defaultable
//! #[derive(Georm)]
//! #[georm(table = "invalid")]
//! pub struct Invalid {
//!     #[georm(id)]
//!     id: i32,
//!     #[georm(defaultable)]  // Error: would create Option<Option<String>>
//!     optional_field: Option<String>,
//! }
//! ```
//!
//! ## Attribute Reference
//!
//! ### Struct-Level Attributes
//!
//! ```ignore
//! #[georm(
//!     table = "table_name",                   // Required: database table name
//!     one_to_one = [{ /* ... */ }],           // Optional: one-to-one relationships
//!     one_to_many = [{ /* ... */ }],          // Optional: one-to-many relationships
//!     many_to_many = [{ /* ... */ }]          // Optional: many-to-many relationships
//! )]
//! ```
//!
//! ### Field-Level Attributes
//!
//! ```ignore
//! #[georm(id)]                               // Mark as primary key (required on at least one field)
//! #[georm(defaultable)]                      // Mark as defaultable field (database default/auto-generated)
//! #[georm(relation = { /* ... */ })]         // Define foreign key relationship
//! ```
//!
//! ## Performance Characteristics
//!
//! - **Zero runtime overhead**: All SQL is generated at compile time
//! - **No eager loading**: Each relationship method executes a separate query
//! - **Prepared statements**: All queries use parameter binding for optimal performance
//! - **Database round-trips**: CRUD operations use RETURNING clause to minimize round-trips
//! - **No N+1 prevention**: Built-in relationships don't prevent N+1 query patterns
//!
//! ## Limitations
//!
//! ### Database Support
//!
//! Georm is currently limited to PostgreSQL. Other databases may be supported in
//! the future, such as SQLite or MySQL, but that is not the case yet.
//!
//! ### Identifiers
//!
//! Identifiers, or primary keys from the point of view of the database, may
//! be simple types recognized by SQLx or composite keys (multiple fields marked
//! with `#[georm(id)]`). Single primary keys cannot be arrays, and optionals are
//! only supported in one-to-one relationships when explicitly marked as nullables.
//!
//! ### Current Limitations
//!
//! - **Composite key relationships**: Entities with composite primary keys cannot define relationships
//! - **Single table per entity**: No table inheritance or polymorphism support
//! - **No advanced queries**: No complex WHERE clauses or joins beyond relationships
//! - **No eager loading**: Each relationship call is a separate database query
//! - **No field-based queries**: No `find_by_{field_name}` methods generated automatically
//! - **PostgreSQL only**: No support for other database systems
//!
//! ## Generated Code
//!
//! Georm automatically generates:
//! - `sqlx::FromRow` implementation (no need to derive manually)
//! - Composite ID structs for multi-field primary keys
//! - Defaultable companion structs for entities with defaultable fields
//! - Relationship methods for accessing related entities
//! - All CRUD operations with proper PostgreSQL optimizations

pub use georm_macros::Georm;

mod georm;
pub use georm::Georm;
mod defaultable;
pub use defaultable::Defaultable;