armature-framework 0.2.2

A modern, type-safe HTTP framework for Rust inspired by Angular and NestJS. Features dependency injection, decorators, middleware, authentication (JWT/OAuth2/SAML), validation, OpenAPI/Swagger, caching, job queues, and observability.
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
# SeaORM Integration Guide

SeaORM database integration for the Armature framework.

## Overview

The `armature-seaorm` crate provides:

- **Multiple Backends**: PostgreSQL, MySQL, and SQLite support
- **Connection Pooling**: Built-in connection pooling via SQLx
- **Transaction Management**: Easy-to-use transaction helpers
- **Active Record Pattern**: Entity-based CRUD operations
- **Pagination**: Built-in pagination utilities
- **Query Helpers**: Fluent query building

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
armature-seaorm = "0.1"
```

### Feature Flags

```toml
[dependencies]
# PostgreSQL with Tokio + Rustls (default)
armature-seaorm = "0.1"

# MySQL
armature-seaorm = { version = "0.1", default-features = false, features = ["runtime-tokio-rustls", "sqlx-mysql"] }

# SQLite
armature-seaorm = { version = "0.1", default-features = false, features = ["runtime-tokio-rustls", "sqlx-sqlite"] }

# With extra features
armature-seaorm = { version = "0.1", features = ["with-json", "with-chrono", "with-uuid"] }
```

Available features:
- `runtime-tokio-rustls` - Tokio + Rustls (default)
- `runtime-tokio-native-tls` - Tokio + Native TLS
- `sqlx-postgres` - PostgreSQL (default)
- `sqlx-mysql` - MySQL
- `sqlx-sqlite` - SQLite
- `with-json` - JSON column support
- `with-chrono` - Chrono datetime support
- `with-uuid` - UUID support
- `mock` - Mock database for testing
- `debug-print` - Print SQL queries

## Quick Start

### Basic Setup

```rust
use armature_seaorm::{Database, DatabaseConfig};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create configuration
    let config = DatabaseConfig::new("postgres://user:pass@localhost/mydb")
        .max_connections(10)
        .connect_timeout(Duration::from_secs(5));

    // Connect to database
    let db = Database::connect(config).await?;

    // Check connection
    db.ping().await?;

    Ok(())
}
```

### From Environment Variables

```rust
use armature_seaorm::Database;

// Uses DATABASE_URL and other DATABASE_* variables
let db = Database::connect_from_env().await?;
```

Environment variables:
- `DATABASE_URL` - Required database URL
- `DATABASE_MAX_CONNECTIONS` - Max connections (default: 10)
- `DATABASE_MIN_CONNECTIONS` - Min connections (default: 1)
- `DATABASE_CONNECT_TIMEOUT` - Connect timeout in seconds
- `DATABASE_SQLX_LOGGING` - Enable SQL logging (true/false)

## Configuration

### DatabaseConfig Options

```rust
use armature_seaorm::DatabaseConfig;
use std::time::Duration;

let config = DatabaseConfig::new("postgres://localhost/mydb")
    // Pool settings
    .max_connections(20)
    .min_connections(5)

    // Timeouts
    .connect_timeout(Duration::from_secs(30))
    .max_lifetime(Duration::from_secs(30 * 60))
    .idle_timeout(Duration::from_secs(10 * 60))

    // Logging
    .sqlx_logging(true)

    // PostgreSQL schema
    .schema("public");
```

## Entity Definition

Define your entities using SeaORM's derive macros:

```rust
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "users")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub name: String,
    pub email: String,
    pub created_at: DateTimeUtc,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(has_many = "super::post::Entity")]
    Posts,
}

impl Related<super::post::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Posts.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}
```

## CRUD Operations

### Create

```rust
use sea_orm::{ActiveModelTrait, Set};

let user = user::ActiveModel {
    name: Set("Alice".to_owned()),
    email: Set("alice@example.com".to_owned()),
    ..Default::default()
};

let user = user.insert(&db).await?;
```

### Read

```rust
use sea_orm::EntityTrait;

// Find by ID
let user = User::find_by_id(1).one(&db).await?;

// Find all
let users = User::find().all(&db).await?;

// Find with filter
use sea_orm::QueryFilter;
let admins = User::find()
    .filter(user::Column::Role.eq("admin"))
    .all(&db)
    .await?;
```

### Update

```rust
use sea_orm::{ActiveModelTrait, Set, IntoActiveModel};

let mut user: user::ActiveModel = user.into_active_model();
user.name = Set("New Name".to_owned());
let user = user.update(&db).await?;
```

### Delete

```rust
use sea_orm::{EntityTrait, ModelTrait};

// Delete by model
user.delete(&db).await?;

// Delete by ID
User::delete_by_id(1).exec(&db).await?;

// Delete with filter
User::delete_many()
    .filter(user::Column::Active.eq(false))
    .exec(&db)
    .await?;
```

## Transactions

### Basic Transactions

```rust
use armature_seaorm::TransactionExt;

db.transaction(|txn| async move {
    let user = user::ActiveModel {
        name: Set("Alice".to_owned()),
        email: Set("alice@example.com".to_owned()),
        ..Default::default()
    };
    user.insert(&txn).await?;

    let profile = profile::ActiveModel {
        user_id: Set(1),
        ..Default::default()
    };
    profile.insert(&txn).await?;

    Ok::<_, sea_orm::DbErr>(())
}).await?;
```

### With Isolation Level

```rust
use armature_seaorm::{TransactionExt, IsolationLevel};

db.transaction_with_isolation(
    IsolationLevel::Serializable,
    |txn| async move {
        // Critical operations
        Ok::<_, sea_orm::DbErr>(())
    }
).await?;
```

## Pagination

### Offset Pagination

```rust
use armature_seaorm::{Paginate, PaginationOptions};

let options = PaginationOptions::new(1, 20); // page 1, 20 per page

let result = User::find()
    .paginate(&db, &options)
    .await?;

println!("Page: {}", result.meta.page);
println!("Total: {}", result.meta.total_items);
println!("Has next: {}", result.meta.has_next);

for user in result.items {
    println!("{}", user.name);
}
```

### Custom Pagination

```rust
use armature_seaorm::{Paginated, PaginationMeta};

let paginated = Paginated::new(items, page, per_page, total_items);

// Map to different type
let dto_paginated = paginated.map(|user| UserDto::from(user));
```

## Query Helpers

### QueryExt Trait

```rust
use armature_seaorm::QueryExt;

let users = User::find()
    .where_eq(user::Column::Role, "admin")
    .where_gt(user::Column::Age, 18)
    .where_like(user::Column::Email, "%@example.com")
    .where_not_null(user::Column::VerifiedAt)
    .order_desc(user::Column::CreatedAt)
    .all(&db)
    .await?;
```

Available methods:
- `where_eq`, `where_ne` - Equality
- `where_gt`, `where_gte`, `where_lt`, `where_lte` - Comparisons
- `where_like` - Pattern matching
- `where_null`, `where_not_null` - Null checks
- `where_in` - IN clause
- `where_between` - Range queries
- `order_asc`, `order_desc` - Ordering

### Search Filters

```rust
use armature_seaorm::SearchFilters;

// Parse from query parameters
let filters: SearchFilters = serde_json::from_str(r#"{
    "q": "alice",
    "sort": "created_at",
    "order": "desc",
    "page": 1,
    "per_page": 20
}"#)?;

let pagination = filters.pagination();
```

## Health Checks

```rust
// Simple ping
db.ping().await?;

// Full health check
let health = db.health_check().await;

println!("Healthy: {}", health.is_healthy);
println!("Response time: {}ms", health.response_time_ms);
println!("Backend: {}", health.backend.name());

if let Some(error) = health.error {
    eprintln!("Error: {}", error);
}
```

## With Armature DI

```rust
use armature_framework::prelude::*;
use armature_seaorm::{Database, DatabaseConfig};

#[module_impl]
impl DatabaseModule {
    #[provider(singleton)]
    async fn database() -> Arc<Database> {
        let config = DatabaseConfig::from_env().expect("DATABASE_URL not set");
        Arc::new(Database::connect(config).await.unwrap())
    }
}

#[controller("/users")]
struct UserController {
    db: Arc<Database>,
}

impl UserController {
    #[get("")]
    async fn list(&self) -> Result<Json<Vec<user::Model>>, Error> {
        let users = User::find().all(self.db.as_ref()).await?;
        Ok(Json(users))
    }

    #[get("/:id")]
    async fn get(&self, req: HttpRequest) -> Result<Json<user::Model>, Error> {
        let id: i32 = req.param("id").unwrap().parse()?;
        let user = User::find_by_id(id)
            .one(self.db.as_ref())
            .await?
            .ok_or(Error::NotFound)?;
        Ok(Json(user))
    }
}
```

## Relations

### Define Relations

```rust
// In user entity
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(has_many = "super::post::Entity")]
    Posts,
}

// In post entity
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::user::Entity",
        from = "Column::UserId",
        to = "super::user::Column::Id"
    )]
    User,
}
```

### Load Relations

```rust
// Find with related
let user = User::find_by_id(1)
    .find_with_related(Post)
    .all(&db)
    .await?;

// Eager loading
let users_with_posts = User::find()
    .find_with_related(Post)
    .all(&db)
    .await?;
```

## Error Handling

```rust
use armature_seaorm::{SeaOrmError, SeaOrmResult};

fn handle_error(err: SeaOrmError) {
    match err {
        SeaOrmError::Connection(msg) => eprintln!("Connection failed: {}", msg),
        SeaOrmError::Query(msg) => eprintln!("Query error: {}", msg),
        SeaOrmError::Database(e) => eprintln!("Database error: {}", e),
        SeaOrmError::Transaction(msg) => eprintln!("Transaction failed: {}", msg),
        SeaOrmError::NotFound(msg) => eprintln!("Not found: {}", msg),
        SeaOrmError::Validation(msg) => eprintln!("Validation error: {}", msg),
        SeaOrmError::Migration(msg) => eprintln!("Migration failed: {}", msg),
        SeaOrmError::Config(msg) => eprintln!("Config error: {}", msg),
        SeaOrmError::Serialization(msg) => eprintln!("Serialization error: {}", msg),
    }
}
```

## Migrations

Use SeaORM's migration tool:

```bash
# Install CLI
cargo install sea-orm-cli

# Generate migration
sea-orm-cli migrate generate create_users_table

# Run migrations
sea-orm-cli migrate up

# Rollback
sea-orm-cli migrate down
```

## Best Practices

### 1. Use Connection Pooling

```rust
let config = DatabaseConfig::new(url)
    .max_connections(10)
    .min_connections(2);
```

### 2. Set Appropriate Timeouts

```rust
let config = DatabaseConfig::new(url)
    .connect_timeout(Duration::from_secs(5))
    .idle_timeout(Duration::from_secs(300));
```

### 3. Use Transactions for Multi-Step Operations

```rust
db.transaction(|txn| async move {
    // All operations succeed or fail together
    Ok::<_, sea_orm::DbErr>(())
}).await?;
```

### 4. Use Pagination for Large Result Sets

```rust
let options = PaginationOptions::new(page, 100);
let result = User::find().paginate(&db, &options).await?;
```

### 5. Enable SQL Logging in Development

```rust
let config = DatabaseConfig::new(url)
    .sqlx_logging(cfg!(debug_assertions));
```

## Summary

| Feature | Description |
|---------|-------------|
| Backends | PostgreSQL, MySQL, SQLite |
| ORM style | Active Record pattern |
| Pooling | Built-in via SQLx |
| Transactions | Full support with isolation |
| Pagination | Offset and cursor-based |
| Query helpers | Fluent query building |
| Relations | Has many, belongs to, many-to-many |
| Migrations | SeaORM CLI |