libsql-orm 0.2.5

A powerful, async-first ORM for Turso Database with first-class support for Cloudflare Workers and WebAssembly environments. Features include automatic boolean conversion, upsert operations, built-in logging, migrations, and comprehensive query building.
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
update # libsql-orm

[![Crates.io](https://img.shields.io/crates/v/libsql-orm.svg)](https://crates.io/crates/libsql-orm)
[![Documentation](https://docs.rs/libsql-orm/badge.svg)](https://docs.rs/libsql-orm)
[![License](https://img.shields.io/crates/l/libsql-orm.svg)](LICENSE)
[![Build Status](https://github.com/ayonsaha2011/libsql-orm/workflows/CI/badge.svg)](https://github.com/ayonsaha2011/libsql-orm/actions)

A powerful, async-first ORM for [Turso Database](https://github.com/tursodatabase) with first-class support for **Cloudflare Workers** and WebAssembly environments.

> ⚠️ **Disclaimer**: This library is in early development and not fully tested in production environments. Use at your own risk. Please report any issues you encounter and feel free to contribute via pull requests - we're happy to address them and welcome community contributions!

## ✨ Features

- 🚀 **Cloudflare Workers Ready** - Built specifically for edge computing environments
- 🔄 **Async/Await Support** - Fully async API with excellent performance
- 🎯 **Type-Safe** - Leverages Rust's type system for compile-time safety
- 📊 **Rich Query Builder** - Fluent API for complex queries
- 🔍 **Advanced Filtering** - Search, pagination, sorting, and aggregations
- 🎨 **Derive Macros** - Automatic model generation with `#[derive(Model)]`
- 📦 **Bulk Operations** - Efficient batch inserts, updates, and deletes
- 🌐 **WASM Compatible** - Optimized for WebAssembly targets
- 🔧 **Custom Table Names** - `#[table_name("custom")]` attribute support
-**Boolean Type Safety** - Automatic SQLite integer ↔ Rust boolean conversion
- 🏷️ **Column Attributes** - `#[orm_column(...)]` for column customization
- 🔄 **Upsert Operations** - Smart create_or_update and upsert methods
- 🔌 **MCP Support** - Model Context Protocol integration for AI-powered database interactions

## 🚀 Quick Start

Add this to your `Cargo.toml`:

```toml
[dependencies]
libsql-orm = { version = "0.2.4", features = ["cloudflare"] }
serde = { version = "1.0", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }

# Required for Cloudflare Workers support - use git version of libsql with newer worker dependency
[patch.crates-io]
libsql = { git = "https://github.com/ayonsaha2011/libsql", features = ["cloudflare"] }
```

### Why the Git Patch?

For Cloudflare Workers compatibility, you need to use a patched version of libsql that includes:
- Updated `worker` dependency compatibility
- Enhanced WASM support for edge environments
- Cloudflare-specific optimizations

The patch ensures seamless integration with Cloudflare Workers' runtime environment.

### Basic Usage

```rust
use libsql_orm::{Model, Database, FilterOperator, Filter, Value};
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};

#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[table_name("users")]  // Custom table name (optional)
struct User {
    pub id: Option<i64>,
    pub name: String,
    pub email: String,
    pub age: Option<i32>,
    pub is_active: bool,        // ✅ Automatic boolean conversion
    pub is_verified: bool,      // ✅ Works with any boolean field
    pub created_at: DateTime<Utc>,
}

// In your async function
async fn example() -> Result<(), Box<dyn std::error::Error>> {
    // Connect to database
    let db = Database::new_connect("turso://your-db.turso.io", "your-auth-token").await?;
    
    // Create a user
    let user = User {
        id: None,
        name: "Alice".to_string(),
        email: "alice@example.com".to_string(),
        age: Some(30),
        is_active: true,
        created_at: Utc::now(),
    };
    
    // Save to database
    let saved_user = user.create(&db).await?;
    
    // Find users
    let users = User::find_all(&db).await?;
    
    // Query with conditions
    let active_users = User::find_where(
        FilterOperator::Single(Filter::eq("is_active", true)),
        &db
    ).await?;
    
    Ok(())
}
```

### Cloudflare Workers Integration

First, ensure your `Cargo.toml` includes the necessary features and patches:

```toml
[dependencies]
libsql-orm = { version = "0.2.4", features = ["cloudflare"] }
worker = ">=0.7.0"
serde = { version = "1.0", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }

# Use git version of libsql with newer worker dependency
[patch.crates-io]
libsql = { git = "https://github.com/ayonsaha2011/libsql", features = ["cloudflare"] }
```

Then in your worker code:

```rust
use worker::*;
use libsql_orm::{Model, Database};
use serde::{Serialize, Deserialize};
use chrono::{DateTime, Utc};

#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[table_name("blog_posts")]  // Custom table name
struct Post {
    pub id: Option<i64>,
    pub title: String,
    pub content: String,
    pub published: bool,       // ✅ Boolean automatically converted from SQLite
    pub featured: bool,        // ✅ Multiple boolean fields supported
    pub created_at: DateTime<Utc>,
}

#[event(fetch)]
async fn fetch(req: Request, env: Env, _ctx: Context) -> Result<Response> {
    console_error_panic_hook::set_once();
    
    // Get database credentials from environment
    let database_url = env.var("TURSO_DATABASE_URL")?.to_string();
    let auth_token = env.var("TURSO_AUTH_TOKEN")?.to_string();
    
    // Connect to database
    let db = Database::new_connect(&database_url, &auth_token).await
        .map_err(|e| format!("Database connection failed: {}", e))?;
    
    // Handle the request
    match req.method() {
        Method::Get => {
            let posts = Post::find_all(&db).await
                .map_err(|e| format!("Query failed: {}", e)))?;
            Response::from_json(&posts)
        }
        Method::Post => {
            let post: Post = req.json().await?;
            let saved_post = post.create(&db).await
                .map_err(|e| format!("Create failed: {}", e)))?;
            Response::from_json(&saved_post)
        }
        _ => Response::error("Method not allowed", 405)
    }
}
```

### Advanced Cloudflare Workers with Axum Integration

For more complex applications, you can integrate libsql-orm with Axum for better routing and state management.

**Key Requirements:**
- 🏗️ **Library crate**: Use `crate-type = ["cdylib"]` for Cloudflare Workers
- 🔧 **Worker features**: Enable `http` and `axum` features for the worker crate
- 🎯 **Axum config**: Use `default-features = false` for WASM compatibility
- 🔗 **Tower service**: Required for Axum routing integration

**Setup:**

```toml
[package]
name = "my-cloudflare-app"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
worker = { version = "0.7", features = ['http', 'axum'] }
worker-macros = { version = "0.7", features = ['http'] }
axum = { version = "0.8", default-features = false, features = ["json", "macros"] }
tower-service = "0.3.3"
libsql-orm = { version = "0.2.4", features = ["cloudflare"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
chrono = { version = "0.4", features = ["serde"] }
console_error_panic_hook = "0.1"

# Use git version of libsql with newer worker dependency
[patch.crates-io]
libsql = { git = "https://github.com/ayonsaha2011/libsql", features = ["cloudflare"] }
```

```rust
use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::Json,
    routing::{get, post, put, delete},
    Router,
};
use tower_service::Service;
use worker::*;
use std::result::Result;
use libsql_orm::{Model, Database, FilterOperator, Filter};
use serde::{Serialize, Deserialize};
use chrono::{DateTime, Utc};
use std::sync::Arc;

// Application state
#[derive(Clone)]
pub struct AppState {
    pub db: Arc<Database>,
}

impl AppState {
    pub async fn new(env: &Env) -> worker::Result<Self> {
        // Get database credentials from environment
        let database_url = env.var("TURSO_DATABASE_URL")?.to_string();
        let auth_token = env.var("TURSO_AUTH_TOKEN")?.to_string();

        // Connect to database
        let db = Database::new_connect(&database_url, &auth_token).await
            .map_err(|e| format!("Database connection failed: {}", e))?;

        Ok(Self {
            db: Arc::new(db),
        })
    }
}

// User model
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[table_name("users")]
struct User {
    pub id: Option<i64>,
    pub name: String,
    pub email: String,
    pub is_active: bool,
    pub created_at: DateTime<Utc>,
}

// Request/Response DTOs
#[derive(Deserialize)]
struct CreateUserRequest {
    pub name: String,
    pub email: String,
}

#[derive(Serialize)]
struct ApiResponse<T> {
    pub success: bool,
    pub data: Option<T>,
    pub error: Option<String>,
}

impl<T> ApiResponse<T> {
    fn success(data: T) -> Self {
        Self { success: true, data: Some(data), error: None }
    }

    fn error(error: String) -> Self {
        Self { success: false, data: None, error: Some(error) }
    }
}

#[derive(Serialize)]
struct ErrorResponse {
    pub error: String,
    pub message: String,
}

// Route handlers
#[worker::send]
async fn get_users(State(state): State<AppState>) -> Result<Json<ApiResponse<Vec<User>>>, (StatusCode, Json<ErrorResponse>)> {
    match User::find_all(&state.db).await {
        Ok(users) => Ok(Json(ApiResponse::success(users))),
        Err(e) => {
            console_log!("Error fetching users: {}", e);
            Err((StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse {
                error: "internal_server_error".to_string(),
                message: "Internal server error".to_string()
            })))
        }
    }
}

#[worker::send]
async fn get_user_by_id(
    State(state): State<AppState>,
    Path(id): Path<i64>
) -> Result<Json<ApiResponse<User>>, (StatusCode, Json<ErrorResponse>)> {
    match User::find_by_id(id, &state.db).await {
        Ok(Some(user)) => Ok(Json(ApiResponse::success(user))),
        Ok(None) => Err((StatusCode::NOT_FOUND, Json(ErrorResponse {
            error: "user_not_found".to_string(),
            message: "User not found".to_string()
        }))),
        Err(e) => {
            console_log!("Error fetching user {}: {}", id, e);
            Err((StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse {
                error: "internal_server_error".to_string(),
                message: "Internal server error".to_string()
            })))
        }
    }
}

#[worker::send]
async fn create_user(
    State(state): State<AppState>,
    Json(req): Json<CreateUserRequest>
) -> Result<(StatusCode, Json<ApiResponse<User>>), (StatusCode, Json<ErrorResponse>)> {
    let user = User {
        id: None,
        name: req.name,
        email: req.email,
        is_active: true,
        created_at: Utc::now(),
    };

    match user.create(&state.db).await {
        Ok(created_user) => Ok((
            StatusCode::CREATED,
            Json(ApiResponse::success(created_user))
        )),
        Err(e) => {
            console_log!("Error creating user: {}", e);
            Err((StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse {
                error: "internal_server_error".to_string(),
                message: "Internal server error".to_string()
            })))
        }
    }
}

#[worker::send]
async fn update_user(
    State(state): State<AppState>,
    Path(id): Path<i64>,
    Json(mut user): Json<User>
) -> Result<Json<ApiResponse<User>>, (StatusCode, Json<ErrorResponse>)> {
    user.id = Some(id);

    match user.update(&state.db).await {
        Ok(updated_user) => Ok(Json(ApiResponse::success(updated_user))),
        Err(e) => {
            console_log!("Error updating user {}: {}", id, e);
            Err((StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse {
                error: "internal_server_error".to_string(),
                message: "Internal server error".to_string()
            })))
        }
    }
}

#[worker::send]
async fn delete_user(
    State(state): State<AppState>,
    Path(id): Path<i64>
) -> Result<Json<ApiResponse<String>>, (StatusCode, Json<ErrorResponse>)> {
    let filter = FilterOperator::Single(Filter::eq("id", id));

    match User::delete_where(filter, &state.db).await {
        Ok(_) => Ok(Json(ApiResponse::success("User deleted successfully".to_string()))),
        Err(e) => {
            console_log!("Error deleting user {}: {}", id, e);
            Err((StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse {
                error: "internal_server_error".to_string(),
                message: "Internal server error".to_string()
            })))
        }
    }
}

#[worker::send]
async fn get_active_users(State(state): State<AppState>) -> Result<Json<ApiResponse<Vec<User>>>, (StatusCode, Json<ErrorResponse>)> {
    let filter = FilterOperator::Single(Filter::eq("is_active", true));

    match User::find_where(filter, &state.db).await {
        Ok(users) => Ok(Json(ApiResponse::success(users))),
        Err(e) => {
            console_log!("Error fetching active users: {}", e);
            Err((StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse {
                error: "internal_server_error".to_string(),
                message: "Internal server error".to_string()
            })))
        }
    }
}

// Create router with all routes
fn create_router() -> Router<AppState> {
    Router::new()
        .route("/users", get(get_users).post(create_user))
        .route("/users/active", get(get_active_users))
        .route("/users/:id", get(get_user_by_id).put(update_user).delete(delete_user))
}

// Main Cloudflare Workers handler
#[event(fetch)]
async fn fetch(
    req: HttpRequest,
    env: Env,
    _ctx: Context,
) -> worker::Result<axum::http::Response<axum::body::Body>> {
    console_error_panic_hook::set_once();

    // Initialize application state
    let app_state = match AppState::new(&env).await {
        Ok(state) => state,
        Err(e) => {
            console_log!("Failed to initialize application state: {}", e);
            return Ok(axum::http::Response::builder()
                .status(500)
                .header("content-type", "application/json")
                .body(axum::body::Body::from(
                    "{\"error\":\"initialization_failed\",\"message\":\"Failed to initialize application\"}"
                ))?
            );
        }
    };

    // Create router
    let mut router = create_router().with_state(app_state);

    // Handle the request
    Ok(router.call(req).await?)
}
```

This example demonstrates:

- **🏗️ Clean Architecture**: Separating models, DTOs, and handlers
- **🔄 State Management**: Using Axum's state system for database sharing
- **🛣️ RESTful Routing**: Complete CRUD operations with proper HTTP methods
- **📊 Error Handling**: Comprehensive error handling with tuple returns
- **🎯 Type Safety**: Strong typing with request/response DTOs
- **🚀 Performance**: Efficient database connection sharing with `AppState::new()`
- **📝 Logging**: Built-in `console_log!` macro for debugging
- **🔍 Advanced Queries**: Filtering and conditional operations
- **⚡ Worker Integration**: `#[worker::send]` attributes for optimal performance

### API Endpoints

| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/users` | Get all users |
| POST | `/users` | Create a new user |
| GET | `/users/:id` | Get user by ID |
| PUT | `/users/:id` | Update user |
| DELETE | `/users/:id` | Delete user |
| GET | `/users/active` | Get all active users |

### 🚀 Deployment

To deploy this Cloudflare Workers application, you'll need a `wrangler.toml` configuration:

```toml
name = "my-cloudflare-app"
main = "build/worker/shim.mjs"
compatibility_date = "2023-05-18"

[env.production.vars]
TURSO_DATABASE_URL = "your-database-url"
TURSO_AUTH_TOKEN = "your-auth-token"

[[env.production.rules]]
type = "CompiledWasm"
globs = ["**/*.wasm"]
fallthrough = true
```

**Deploy commands:**
```bash
# Install dependencies
npm install -g wrangler

# Deploy to Cloudflare Workers
wrangler deploy
```

## 📚 Advanced Features

### Custom Table Names

Use the `#[table_name("custom_name")]` attribute to specify custom table names:

```rust
#[derive(Model, Serialize, Deserialize)]
#[table_name("user_accounts")]  // Custom table name
struct User {
    pub id: Option<i64>,
    pub username: String,
    pub email: String,
}

// Default table name would be "user" (struct name lowercase)
// With attribute, table name is "user_accounts"
assert_eq!(User::table_name(), "user_accounts");
```

**Benefits:**
- 🏷️ **Legacy Integration** - Map to existing database tables
- 🎯 **Naming Control** - Override default naming conventions  
- 📁 **Multi-tenant** - Use prefixes like `tenant_users`
- 🔄 **Migration Friendly** - Rename tables without changing structs

### Boolean Type Safety

libsql-orm automatically handles boolean conversion between SQLite and Rust:

```rust
use libsql_orm::{Model, FilterOperator, Filter, Value};
use serde::{Serialize, Deserialize};

#[derive(Model, Serialize, Deserialize)]
struct User {
    pub id: Option<i64>,
    pub is_active: bool,      // ✅ SQLite INTEGER(0/1) ↔ Rust bool
    pub is_verified: bool,    // ✅ Automatic conversion
    pub has_premium: bool,    // ✅ Works with any boolean field name
    pub can_edit: bool,       // ✅ No configuration needed
    pub enabled: bool,        // ✅ Type-safe operations
}

// All boolean operations work seamlessly
let user = User::find_where(
    FilterOperator::Single(Filter::eq("is_active", true)),
    &db
).await?;

// JSON serialization works correctly
let json = serde_json::to_string(&user)?;  // ✅ Booleans as true/false
let deserialized: User = serde_json::from_str(&json)?;  // ✅ No errors
```

**Key Features:**
- **Automatic Detection** - Boolean fields identified at compile time
-**Zero Configuration** - Works with any boolean field name
-**Type Safety** - No runtime errors or invalid conversions
-**Performance** - Conversion logic generated at compile time
-**JSON Compatible** - Seamless serialization/deserialization

### Column Attributes

Customize column properties with `#[orm_column(...)]`:

```rust
use libsql_orm::Model;
use serde::{Serialize, Deserialize};

#[derive(Model, Serialize, Deserialize)]
struct Product {
    #[orm_column(type = "INTEGER PRIMARY KEY AUTOINCREMENT")]
    pub id: Option<i64>,
    
    #[orm_column(not_null, unique)]
    pub sku: String,
    
    #[orm_column(type = "REAL CHECK(price >= 0)")]
    pub price: f64,
    
    #[orm_column(type = "BOOLEAN DEFAULT TRUE")]
    pub is_available: bool,     // ✅ Boolean with DEFAULT constraint
}
```

### Query Builder

```rust
use libsql_orm::{QueryBuilder, FilterOperator, Filter, Sort, SortOrder, Pagination};

// Complex query with filtering and pagination
let query = QueryBuilder::new("users")
    .select(&["id", "name", "email"])
    .r#where(FilterOperator::Single(Filter::ge("age", 18i64)))
    .order_by(Sort::new("created_at", SortOrder::Desc))
    .limit(10)
    .offset(20);

let (sql, params) = query.build()?;
```

### Pagination

```rust
use libsql_orm::{Pagination, PaginatedResult};

let pagination = Pagination::new(1, 10); // page 1, 10 items per page
let result: PaginatedResult<User> = User::find_paginated(&pagination, &db).await?;

// Access pagination info
// Page: result.pagination.page
// Total pages: result.pagination.total_pages.unwrap_or(0)
// Total items: result.pagination.total.unwrap_or(0)
for user in result.data {
    // Process user: user.name
}
```

### Bulk Operations

```rust
// Bulk insert
let users = vec![
    User { /* ... */ },
    User { /* ... */ },
    User { /* ... */ },
];
let saved_users = User::bulk_create(&users, &db).await?;

// Bulk delete
let ids_to_delete = vec![1, 2, 3, 4, 5];
let deleted_count = User::bulk_delete(&ids_to_delete, &db).await?;
```

### Aggregations

```rust
use libsql_orm::Aggregate;

// Count users
let total_users = User::count(&db).await?;

// Average age
let avg_age = User::aggregate(
    Aggregate::Avg,
    "age",
    None,
    &db
).await?;

// Count with filter
let active_users_count = User::count_where(
    FilterOperator::Single(Filter::eq("is_active", true)),
    &db
).await?;
```

### Search

```rust
use libsql_orm::{SearchFilter, Pagination};

let search = SearchFilter::new(
    "john",
    vec!["name", "email"]
);

// Optional pagination
let pagination = Pagination::new(1, 10);
let results = User::search(&search, Some(&pagination), &db).await?;
```

### Upsert Operations

libsql-orm provides intelligent create-or-update operations:

```rust
use libsql_orm::{Model, Database};
use chrono::{DateTime, Utc};

// Create or update based on primary key
let mut user = User {
    id: Some(123),  // If record exists, it will be updated
    name: "John Doe".to_string(),
    email: "john@example.com".to_string(),
    is_active: true,
    created_at: Utc::now(),
};

// Automatically decides whether to create or update
let saved_user = user.create_or_update(&db).await?;

// Upsert based on unique constraints (e.g., email)
let user = User {
    id: None,  // Primary key not set
    name: "Jane Smith".to_string(),
    email: "jane@example.com".to_string(),  // Unique field
    is_active: true,
    created_at: Utc::now(),
};

// Will update existing record with this email, or create new if not found
let saved_user = user.upsert(&["email"], &db).await?;

// Multiple unique constraints
let saved_user = user.upsert(&["email", "username"], &db).await?;
```

## 🔌 MCP (Model Context Protocol) Support

libsql-orm provides comprehensive MCP server integration for AI-powered database interactions. The MCP protocol enables seamless communication between AI assistants and your Turso database.

### MCP Features

- **40+ Tools**: Complete CRUD operations, queries, migrations, and more
- **Type-Safe Queries**: Automatic validation and type checking
- **Advanced Filtering**: Complex AND/OR/NOT filter combinations
- **Pagination Support**: Both offset-based and cursor-based pagination
- **Migration Management**: Create, execute, and track database migrations
- **Bulk Operations**: Efficient batch processing
- **Search Capabilities**: Multi-column text search
- **Aggregations**: COUNT, SUM, AVG, MIN, MAX operations

### MCP Resources

The MCP server exposes:
- Database connection resources
- Model resources (table-based)
- Query builder resources
- Migration tracking resources

### Quick MCP Example

```json
{
  "tool": "model_find_where",
  "arguments": {
    "model": "User",
    "filter": {
      "type": "And",
      "filters": [
        {
          "type": "Single",
          "filter": {"column": "is_active", "operator": "Eq", "value": true}
        },
        {
          "type": "Single",
          "filter": {"column": "age", "operator": "Gt", "value": 18}
        }
      ]
    }
  }
}
```

📖 **[Complete MCP Documentation](MCP_DOCUMENTATION.md)** - Comprehensive guide with all 40+ tools, examples, and best practices

## 🏗️ Architecture

### WASM Compatibility

libsql-orm is built from the ground up for WebAssembly environments:

- Uses `libsql` WASM bindings for database connectivity
- Optimized async runtime for edge computing
- Minimal binary size with selective feature compilation
- Compatible with Cloudflare Workers, Deno Deploy, and other edge platforms


## 🔗 Ecosystem

libsql-orm works great with:

- **[Turso Database]https://github.com/tursodatabase** - The database platform
- **[Turso]https://turso.tech/** - Managed Turso hosting
- **[Cloudflare Workers]https://workers.cloudflare.com/** - Edge computing platform
- **[worker-rs]https://github.com/cloudflare/workers-rs** - Cloudflare Workers Rust SDK

## 🤝 Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

## ☕ Support the Project

If you find this library helpful and would like to support its development, consider making a donation:

### 💰 Donation Options

- **GitHub Sponsors**: [Sponsor on GitHub]https://github.com/sponsors/ayonsaha2011
- **Buy Me a Coffee**: [Buy me a coffee]https://coff.ee/ayonsaha2011
- **PayPal**: [PayPal Donation]https://paypal.me/ayonsaha

### 🎯 What Your Support Helps With

- 🚀 **Feature Development** - Building new capabilities and improvements
- 🐛 **Bug Fixes** - Maintaining stability and reliability  
- 📚 **Documentation** - Creating better guides and examples
- 🔧 **Maintenance** - Keeping the library up-to-date with dependencies
- ☁️ **Infrastructure** - Hosting costs for CI/CD and testing

Every contribution, no matter the size, helps make this library better for everyone! 🙏

## 📄 License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## 🙏 Acknowledgments

- [Turso team]https://github.com/tursodatabase for the excellent database platform
- [Cloudflare]https://cloudflare.com for the Workers platform
- Rust community for the amazing ecosystem

---
**Need help?**
- 📚 [Documentation]https://docs.rs/libsql-orm
- 🔌 [MCP Documentation]MCP_DOCUMENTATION.md
- 💬 [Discussions]https://github.com/ayonsaha2011/libsql-orm/discussions
- 🐛 [Issues]https://github.com/ayonsaha2011/libsql-orm/issues