sea-orm-turso 0.1.0

A universal SeaORM adapter for Turso Database (MVCC, in-process, async)
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
use std::collections::HashMap;
use std::sync::Arc;
use async_trait::async_trait;
use sea_orm::{
    ConnectionTrait, DbBackend, Statement, ExecResult, QueryResult,
    TransactionTrait, IsolationLevel, AccessMode, TransactionError, TransactionOptions,
    ProxyExecResult, ProxyRow,
};
use tokio::sync::Mutex;
use turso::{Builder, Connection, Database};

use crate::error::TursoError;
use crate::row::{row_to_proxy_row, statement_to_sql};

/// SQLite column type from PRAGMA table_info
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ColumnType {
    Boolean,
    Integer,
    Real,
    Text,
    Blob,
    DateTime,
    Unknown(String),
}

impl ColumnType {
    pub fn from_sqlite_decl(decl: &str) -> Self {
        let upper = decl.to_uppercase();
        if upper.contains("BOOL") {
            ColumnType::Boolean
        } else if upper.contains("INT") {
            ColumnType::Integer
        } else if upper.contains("REAL") || upper.contains("FLOAT") || upper.contains("DOUBLE") || upper.contains("NUMERIC") {
            ColumnType::Real
        } else if upper.contains("TEXT") || upper.contains("CLOB") || upper.contains("CHAR") {
            ColumnType::Text
        } else if upper.contains("BLOB") {
            ColumnType::Blob
        } else if upper.contains("DATE") || upper.contains("TIME") {
            ColumnType::DateTime
        } else {
            ColumnType::Unknown(decl.to_string())
        }
    }
}

/// A SeaORM-compatible connection backed by Turso Database
///
/// This struct implements SeaORM's [`ConnectionTrait`] and [`TransactionTrait`],
/// allowing you to use Turso as a drop-in replacement for any SeaORM-supported
/// database.
///
/// # Features
///
/// - Full MVCC support via Turso engine
/// - In-memory performance with WAL persistence
/// - Async/await native support
/// - Compatible with all SeaORM entities and queries
/// - Schema-aware type conversion (booleans, datetimes, etc.)
#[derive(Clone)]
pub struct TursoConnection {
    db_path: String,
    db: Arc<Database>,
    conn: Arc<Mutex<Connection>>,
    schema_cache: Arc<Mutex<HashMap<String, HashMap<String, ColumnType>>>>,
}

impl TursoConnection {
    /// Connect to a Turso database file
    ///
    /// Creates the file if it doesn't exist. The database uses MVCC
    /// with WAL mode for concurrent access and persistence.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the database file (e.g., "app.db")
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use sea_orm_turso::TursoConnection;
    ///
    /// let db = TursoConnection::connect("my_database.db").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn connect(path: &str) -> Result<Self, TursoError> {
        let db = Builder::new_local(path)
            .build()
            .await
            .map_err(|e| TursoError::Connection(e.to_string()))?;

        let conn = db.connect()
            .map_err(|e| TursoError::Connection(e.to_string()))?;

        Ok(Self {
            db_path: path.to_string(),
            db: Arc::new(db),
            conn: Arc::new(Mutex::new(conn)),
            schema_cache: Arc::new(Mutex::new(HashMap::new())),
        })
    }

    /// Connect to an in-memory Turso database (no persistence)
    ///
    /// Useful for testing or ephemeral data.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use sea_orm_turso::TursoConnection;
    ///
    /// let db = TursoConnection::connect_memory().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn connect_memory() -> Result<Self, TursoError> {
        Self::connect(":memory:").await
    }

    /// Create a TursoConnection from an existing Turso database and connection
    ///
    /// This is useful when you need to share a Turso database instance
    /// or create multiple connections to the same database.
    pub fn from_existing(db: Database, conn: Connection) -> Self {
        Self {
            db_path: String::new(),
            db: Arc::new(db),
            conn: Arc::new(Mutex::new(conn)),
            schema_cache: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Get a reference to the underlying Turso database
    pub fn database(&self) -> &Database {
        &self.db
    }

    /// Flush cached data to disk (WAL checkpoint)
    ///
    /// Call this after write operations to ensure data persists to disk.
    /// The turso engine buffers writes in memory; cacheflush forces them to the WAL file.
    pub async fn cacheflush(&self) -> Result<(), TursoError> {
        let conn = self.conn.lock().await;
        conn.cacheflush()
            .map_err(|e| TursoError::Query(format!("cacheflush failed: {}", e)))?;
        Ok(())
    }

    /// Persist data to disk by creating a temporary Database on the same path and dropping it.
    ///
    /// Turso's local engine only syncs the WAL to disk when a Database is dropped.
    /// This creates a second Database instance (which reads existing data), then drops it
    /// to trigger the WAL sync. The main Database continues to work normally.
    pub async fn persist(&self) -> Result<(), TursoError> {
        if self.db_path.is_empty() || self.db_path == ":memory:" {
            return Ok(());
        }

        // Create a temporary Database on the same path — this opens the existing WAL
        let tmp_db = Builder::new_local(&self.db_path)
            .build()
            .await
            .map_err(|e| TursoError::Connection(format!("persist failed: {}", e)))?;

        let tmp_conn = tmp_db.connect()
            .map_err(|e| TursoError::Connection(format!("persist failed: {}", e)))?;

        // Run cacheflush on the temp connection to ensure all dirty pages are written
        tmp_conn.cacheflush()
            .map_err(|e| TursoError::Query(format!("persist cacheflush failed: {}", e)))?;

        // Dropping tmp_db triggers WAL sync to disk
        drop(tmp_conn);
        drop(tmp_db);

        Ok(())
    }

    /// Load schema info for a table via PRAGMA table_info
    async fn load_table_schema(&self, table_name: &str) -> Result<(), TursoError> {
        let mut cache = self.schema_cache.lock().await;
        if cache.contains_key(table_name) {
            return Ok(());
        }

        let conn = self.conn.lock().await;
        let mut prepared = conn.prepare(&format!("PRAGMA table_info({})", table_name))
            .await
            .map_err(|e| TursoError::Query(e.to_string()))?;
        let mut rows = prepared.query(())
            .await
            .map_err(|e| TursoError::Query(e.to_string()))?;

        let mut columns = HashMap::new();
        while let Some(row) = rows.next().await.map_err(|e| TursoError::Query(e.to_string()))? {
            let name = row.get_value(1)
                .map_err(|e| TursoError::Query(e.to_string()))?;
            let type_str = row.get_value(2)
                .map_err(|e| TursoError::Query(e.to_string()))?;

            if let turso::Value::Text(col_name) = name {
                if let turso::Value::Text(col_type) = type_str {
                    let upper = col_type.to_uppercase();
                    // SQLite stores booleans as INTEGER. Check common patterns:
                    // 1. Explicit BOOLEAN/BOOL type declaration
                    // 2. We'll also check column name patterns in the row converter
                    let ct = if upper.contains("BOOL") {
                        ColumnType::Boolean
                    } else {
                        ColumnType::from_sqlite_decl(&col_type)
                    };
                    columns.insert(col_name, ct);
                }
            }
        }

        cache.insert(table_name.to_string(), columns);
        Ok(())
    }

    /// Extract table names from a SQL query (simple parser)
    fn extract_table_names(sql: &str) -> Vec<String> {
        let mut tables = Vec::new();
        let upper = sql.to_uppercase();

        // Look for FROM and JOIN clauses
        for keyword in &["FROM", "JOIN"] {
            if let Some(pos) = upper.find(keyword) {
                let rest = &sql[pos + keyword.len()..].trim_start();
                // Extract the table name (before any space, comma, or WHERE)
                let name: String = rest.chars()
                    .take_while(|c| c.is_alphanumeric() || *c == '_' || *c == '"')
                    .collect();
                let name = name.trim_matches('"').to_string();
                if !name.is_empty() && !name.eq_ignore_ascii_case("DUAL") {
                    tables.push(name);
                }
            }
        }
        tables
    }

    /// Execute a SeaORM Statement and return rows as ProxyRow
    async fn execute_statement(&self, stmt: &Statement) -> Result<Vec<ProxyRow>, TursoError> {
        let (sql, params) = statement_to_sql(stmt);

        // Load schema for referenced tables
        for table in Self::extract_table_names(&sql) {
            self.load_table_schema(&table).await?;
        }

        let conn = self.conn.lock().await;

        // Use prepared statement to get column names
        let mut prepared = conn.prepare(&sql)
            .await
            .map_err(|e| TursoError::Query(e.to_string()))?;

        let column_names: Vec<String> = prepared.columns()
            .iter()
            .map(|c| c.name().to_string())
            .collect();

        // Execute query
        let mut rows = if params.is_empty() {
            prepared.query(())
                .await
                .map_err(|e| TursoError::Query(e.to_string()))?
        } else {
            prepared.query(params)
                .await
                .map_err(|e| TursoError::Query(e.to_string()))?
        };

        // Collect results
        let schema_cache = self.schema_cache.lock().await;
        let mut results = Vec::new();
        while let Some(row) = rows.next().await.map_err(|e| TursoError::Query(e.to_string()))? {
            let proxy_row = row_to_proxy_row(&row, &column_names, &schema_cache)
                .map_err(TursoError::Conversion)?;
            results.push(proxy_row);
        }

        Ok(results)
    }
}

#[async_trait]
impl ConnectionTrait for TursoConnection {
    fn get_database_backend(&self) -> DbBackend {
        DbBackend::Sqlite
    }

    async fn execute_raw(&self, stmt: Statement) -> Result<ExecResult, sea_orm::DbErr> {
        let (sql, params) = statement_to_sql(&stmt);

        let (last_insert_id, rows_affected) = {
            let conn = self.conn.lock().await;

            let rows_affected = if params.is_empty() {
                conn.execute(&sql, ())
                    .await
                    .map_err(|e| sea_orm::DbErr::Custom(e.to_string()))?
            } else {
                conn.execute(&sql, params)
                    .await
                    .map_err(|e| sea_orm::DbErr::Custom(e.to_string()))?
            };

            let last_insert_id = conn.last_insert_rowid() as u64;
            (last_insert_id, rows_affected)
        };

        // Auto-flush + persist to disk after write operations
        let _ = self.cacheflush().await;
        let _ = self.persist().await;

        Ok(ExecResult::from(ProxyExecResult::new(last_insert_id, rows_affected)))
    }

    async fn execute_unprepared(&self, sql: &str) -> Result<ExecResult, sea_orm::DbErr> {
        let (last_insert_id, rows_affected) = {
            let conn = self.conn.lock().await;

            let rows_affected = conn.execute(sql, ())
                .await
                .map_err(|e| sea_orm::DbErr::Custom(e.to_string()))?;

            let last_insert_id = conn.last_insert_rowid() as u64;
            (last_insert_id, rows_affected)
        };

        // Auto-flush + persist to disk after write operations
        let _ = self.cacheflush().await;
        let _ = self.persist().await;

        Ok(ExecResult::from(ProxyExecResult::new(last_insert_id, rows_affected)))
    }

    async fn query_one_raw(&self, stmt: Statement) -> Result<Option<QueryResult>, sea_orm::DbErr> {
        let mut results = self.execute_statement(&stmt).await
            .map_err(|e| sea_orm::DbErr::Custom(e.to_string()))?;

        Ok(results.pop().map(|r| r.into()))
    }

    async fn query_all_raw(&self, stmt: Statement) -> Result<Vec<QueryResult>, sea_orm::DbErr> {
        let results = self.execute_statement(&stmt).await
            .map_err(|e| sea_orm::DbErr::Custom(e.to_string()))?;

        Ok(results.into_iter().map(|r| r.into()).collect())
    }
}

/// Transaction wrapper for Turso
pub struct TursoTransaction {
    conn: TursoConnection,
}

#[async_trait]
impl ConnectionTrait for TursoTransaction {
    fn get_database_backend(&self) -> DbBackend {
        DbBackend::Sqlite
    }

    async fn execute_raw(&self, stmt: Statement) -> Result<ExecResult, sea_orm::DbErr> {
        self.conn.execute_raw(stmt).await
    }

    async fn execute_unprepared(&self, sql: &str) -> Result<ExecResult, sea_orm::DbErr> {
        self.conn.execute_unprepared(sql).await
    }

    async fn query_one_raw(&self, stmt: Statement) -> Result<Option<QueryResult>, sea_orm::DbErr> {
        self.conn.query_one_raw(stmt).await
    }

    async fn query_all_raw(&self, stmt: Statement) -> Result<Vec<QueryResult>, sea_orm::DbErr> {
        self.conn.query_all_raw(stmt).await
    }
}

#[async_trait]
impl sea_orm::TransactionSession for TursoTransaction {
    async fn commit(self) -> Result<(), sea_orm::DbErr> {
        {
            let conn = self.conn.conn.lock().await;
            conn.execute("COMMIT", ())
                .await
                .map_err(|e| sea_orm::DbErr::Custom(format!("Failed to commit: {}", e)))?;
        }
        // Flush committed transaction to disk
        let _ = self.conn.cacheflush().await;
        let _ = self.conn.persist().await;
        Ok(())
    }

    async fn rollback(self) -> Result<(), sea_orm::DbErr> {
        let conn = self.conn.conn.lock().await;
        conn.execute("ROLLBACK", ())
            .await
            .map_err(|e| sea_orm::DbErr::Custom(format!("Failed to rollback: {}", e)))?;
        Ok(())
    }
}

#[async_trait]
impl TransactionTrait for TursoConnection {
    type Transaction = TursoTransaction;

    async fn begin(&self) -> Result<Self::Transaction, sea_orm::DbErr> {
        let conn = self.conn.lock().await;
        conn.unchecked_transaction()
            .await
            .map_err(|e| sea_orm::DbErr::Custom(format!("Failed to begin transaction: {}", e)))?;
        drop(conn);

        Ok(TursoTransaction { conn: self.clone() })
    }

    async fn begin_with_config(
        &self,
        _isolation_level: Option<IsolationLevel>,
        _access_mode: Option<AccessMode>,
    ) -> Result<Self::Transaction, sea_orm::DbErr> {
        self.begin().await
    }

    async fn begin_with_options(
        &self,
        _options: TransactionOptions,
    ) -> Result<Self::Transaction, sea_orm::DbErr> {
        self.begin().await
    }

    async fn transaction<F, T, E>(&self, callback: F) -> Result<T, TransactionError<E>>
    where
        F: for<'c> FnOnce(&'c Self::Transaction) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<T, E>> + Send + 'c>>
            + Send,
        T: Send,
        E: std::fmt::Display + std::fmt::Debug + Send,
    {
        let txn = self.begin().await.map_err(TransactionError::Connection)?;
        let result = callback(&txn).await.map_err(TransactionError::Transaction)?;
        Ok(result)
    }

    async fn transaction_with_config<F, T, E>(
        &self,
        callback: F,
        _isolation_level: Option<IsolationLevel>,
        _access_mode: Option<AccessMode>,
    ) -> Result<T, TransactionError<E>>
    where
        F: for<'c> FnOnce(&'c Self::Transaction) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<T, E>> + Send + 'c>>
            + Send,
        T: Send,
        E: std::fmt::Display + std::fmt::Debug + Send,
    {
        let txn = self.begin().await.map_err(TransactionError::Connection)?;
        let result = callback(&txn).await.map_err(TransactionError::Transaction)?;
        Ok(result)
    }
}

#[async_trait]
impl TransactionTrait for TursoTransaction {
    type Transaction = TursoTransaction;

    async fn begin(&self) -> Result<Self::Transaction, sea_orm::DbErr> {
        Err(sea_orm::DbErr::Custom(
            "Nested transactions not supported".to_string(),
        ))
    }

    async fn begin_with_config(
        &self,
        _isolation_level: Option<IsolationLevel>,
        _access_mode: Option<AccessMode>,
    ) -> Result<Self::Transaction, sea_orm::DbErr> {
        self.begin().await
    }

    async fn begin_with_options(
        &self,
        _options: TransactionOptions,
    ) -> Result<Self::Transaction, sea_orm::DbErr> {
        self.begin().await
    }

    async fn transaction<F, T, E>(&self, callback: F) -> Result<T, TransactionError<E>>
    where
        F: for<'c> FnOnce(&'c Self::Transaction) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<T, E>> + Send + 'c>>
            + Send,
        T: Send,
        E: std::fmt::Display + std::fmt::Debug + Send,
    {
        let result = callback(self).await.map_err(TransactionError::Transaction)?;
        Ok(result)
    }

    async fn transaction_with_config<F, T, E>(
        &self,
        callback: F,
        _isolation_level: Option<IsolationLevel>,
        _access_mode: Option<AccessMode>,
    ) -> Result<T, TransactionError<E>>
    where
        F: for<'c> FnOnce(&'c Self::Transaction) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<T, E>> + Send + 'c>>
            + Send,
        T: Send,
        E: std::fmt::Display + std::fmt::Debug + Send,
    {
        let result = callback(self).await.map_err(TransactionError::Transaction)?;
        Ok(result)
    }
}