Skip to content

Commit 2d75dc3

Browse files
committed
fix: expose necessary types for Rust API usage
1 parent 1cb7b43 commit 2d75dc3

4 files changed

Lines changed: 59 additions & 19 deletions

File tree

README.md

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -571,35 +571,50 @@ tx.commit().await?;
571571

572572
### Cross-Database Operations
573573

574-
Attach other databases for cross-database queries:
574+
Attach other databases for cross-database queries. For Rust API usage, you need to load
575+
both databases first, then create `AttachedSpec` instances using their inner database
576+
references:
575577

576578
```rust
577-
use tauri_plugin_sqlite::AttachedDatabaseSpec;
579+
use tauri_plugin_sqlite::{DatabaseWrapper, AttachedSpec, AttachedMode};
580+
use std::sync::Arc;
581+
582+
// Load both databases
583+
let main_db = DatabaseWrapper::load("/path/to/main.db".into(), None).await?;
584+
let stats_db = DatabaseWrapper::load("/path/to/stats.db".into(), None).await?;
585+
586+
// Create attached spec using the inner database reference
587+
let stats_spec = AttachedSpec {
588+
database: Arc::clone(stats_db.inner()),
589+
schema_name: "stats".to_string(),
590+
mode: AttachedMode::ReadWrite,
591+
};
578592

579593
// Simple transaction with attached database
580-
let results = db.execute_transaction(vec![
594+
let results = main_db.execute_transaction(vec![
581595
("INSERT INTO main.orders (user_id) VALUES (?)", vec![json!(1)]),
582596
("UPDATE stats.order_count SET count = count + 1", vec![]),
583597
])
584-
.attach(vec![AttachedDatabaseSpec {
585-
database_path: "stats.db".into(),
586-
schema_name: "stats".into(),
587-
mode: tauri_plugin_sqlite::AttachedDatabaseMode::ReadWrite,
588-
}])
598+
.attach(vec![stats_spec])
589599
.await?;
590-
591600
println!("Cross-database transaction completed: {} statements", results.len());
592601

593602
// Interruptible transaction with attached database
603+
// Load the inventory database
604+
let inventory_db = DatabaseWrapper::load("/path/to/inventory.db".into(), None).await?;
605+
606+
// Create spec for inventory database
607+
let inv_spec = AttachedSpec {
608+
database: Arc::clone(inventory_db.inner()),
609+
schema_name: "inv".to_string(),
610+
mode: AttachedMode::ReadWrite,
611+
};
612+
594613
// Assuming product_id is defined in your application context
595614
let product_id = 789;
596615

597-
let _tx = db.begin_interruptible_transaction()
598-
.attach(vec![AttachedDatabaseSpec {
599-
database_path: "inventory.db".into(),
600-
schema_name: "inv".into(),
601-
mode: tauri_plugin_sqlite::AttachedDatabaseMode::ReadWrite,
602-
}])
616+
let _tx = main_db.begin_interruptible_transaction()
617+
.attach(vec![inv_spec])
603618
.execute(vec![
604619
("UPDATE inv.stock SET quantity = quantity - ? WHERE product_id = ?", vec![json!(1), json!(product_id)]),
605620
])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Placeholder migrations directory for doctest compilation
2+
# sqlx::migrate! requires a migrations directory to exist at compile time

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ mod transactions;
1515
mod wrapper;
1616

1717
pub use error::{Error, Result};
18-
pub use sqlx_sqlite_conn_mgr::Migrator as SqliteMigrator;
18+
pub use sqlx_sqlite_conn_mgr::{
19+
AttachedMode, AttachedSpec, Migrator as SqliteMigrator, SqliteDatabaseConfig,
20+
};
1921
pub use transactions::{ActiveInterruptibleTransactions, ActiveRegularTransactions, Statement};
2022
pub use wrapper::{
2123
DatabaseWrapper, InterruptibleTransaction, InterruptibleTransactionBuilder,

src/wrapper.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,28 @@ pub struct DatabaseWrapper {
2929

3030
impl DatabaseWrapper {
3131
/// Get the inner Arc<SqliteDatabase> for advanced usage
32-
pub(crate) fn inner(&self) -> &Arc<SqliteDatabase> {
32+
///
33+
/// This is useful when you need to create `AttachedSpec` instances for cross-database
34+
/// operations with interruptible transactions.
35+
///
36+
/// # Example
37+
///
38+
/// ```no_run
39+
/// # use tauri_plugin_sqlite::{DatabaseWrapper, AttachedSpec, AttachedMode};
40+
/// # use std::sync::Arc;
41+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
42+
/// # let db1: DatabaseWrapper = todo!();
43+
/// # let db2: DatabaseWrapper = todo!();
44+
/// // Create an attached spec using the inner database reference
45+
/// let spec = AttachedSpec {
46+
/// database: Arc::clone(db2.inner()),
47+
/// schema_name: "other".to_string(),
48+
/// mode: AttachedMode::ReadOnly,
49+
/// };
50+
/// # Ok(())
51+
/// # }
52+
/// ```
53+
pub fn inner(&self) -> &Arc<SqliteDatabase> {
3354
&self.inner
3455
}
3556

@@ -50,7 +71,7 @@ impl DatabaseWrapper {
5071
/// # Example
5172
///
5273
/// ```no_run
53-
/// # use tauri_plugin_sqlite::DatabaseWrapper;
74+
/// # use tauri_plugin_sqlite::{DatabaseWrapper, Statement};
5475
/// # use serde_json::json;
5576
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
5677
/// # let db: DatabaseWrapper = todo!();
@@ -64,7 +85,7 @@ impl DatabaseWrapper {
6485
///
6586
/// // Continue with more work
6687
/// let results = tx.continue_with(vec![
67-
/// crate::transactions::Statement {
88+
/// Statement {
6889
/// query: "INSERT INTO items (name) VALUES (?)".to_string(),
6990
/// values: vec![json!("item1")],
7091
/// }

0 commit comments

Comments
 (0)