Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/sql/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion plugins/sql/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export interface QueryResult {
lastInsertId?: number
}

export interface ConnectionOptions {
sqlite?: {
pool?: {
max_connections?: number
min_connections?: number
}
}
}

/**
* **Database**
*
Expand Down Expand Up @@ -45,8 +54,12 @@ export default class Database {
* const db = await Database.load("sqlite:test.db");
* ```
*/
static async load(path: string): Promise<Database> {
static async load(
path: string,
options?: ConnectionOptions
): Promise<Database> {
const _path = await invoke<string>('plugin:sql|load', {
options,
db: path
})

Expand Down
5 changes: 3 additions & 2 deletions plugins/sql/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ use serde_json::Value as JsonValue;
use sqlx::migrate::Migrator;
use tauri::{command, AppHandle, Runtime, State};

use crate::{DbInstances, DbPool, Error, LastInsertId, Migrations};
use crate::{wrapper::ConnectionOptions, DbInstances, DbPool, Error, LastInsertId, Migrations};

#[command]
pub(crate) async fn load<R: Runtime>(
app: AppHandle<R>,
db_instances: State<'_, DbInstances>,
migrations: State<'_, Migrations>,
db: String,
options: Option<ConnectionOptions>,
) -> Result<String, crate::Error> {
let pool = DbPool::connect(&db, &app).await?;
let pool = DbPool::connect(&db, &app, options).await?;

if let Some(migrations) = migrations.0.lock().await.remove(&db) {
let migrator = Migrator::new(migrations).await?;
Expand Down
2 changes: 1 addition & 1 deletion plugins/sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Builder {
let mut lock = instances.0.write().await;

for db in config.preload {
let pool = DbPool::connect(&db, app).await?;
let pool = DbPool::connect(&db, app, None).await?;

if let Some(migrations) =
self.migrations.as_mut().and_then(|mm| mm.remove(&db))
Expand Down
39 changes: 38 additions & 1 deletion plugins/sql/src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ pub enum DbPool {
None,
}

#[cfg(feature = "sqlite")]
#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct SqlitePoolOptions {
max_connections: Option<u32>,
min_connections: Option<u32>,
}

#[cfg(feature = "sqlite")]
#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct SqliteOptions {
pub pool: Option<SqlitePoolOptions>,
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct ConnectionOptions {
#[cfg(feature = "sqlite")]
pub sqlite: Option<SqliteOptions>,
}

// public methods
/* impl DbPool {
/// Get the inner Sqlite Pool. Returns None for MySql and Postgres pools.
Expand Down Expand Up @@ -68,6 +87,7 @@ impl DbPool {
pub(crate) async fn connect<R: Runtime>(
conn_url: &str,
_app: &AppHandle<R>,
#[allow(unused_variables)] options: Option<ConnectionOptions>,
) -> Result<Self, crate::Error> {
match conn_url
.split_once(':')
Expand All @@ -88,7 +108,24 @@ impl DbPool {
if !Sqlite::database_exists(conn_url).await.unwrap_or(false) {
Sqlite::create_database(conn_url).await?;
}
Ok(Self::Sqlite(Pool::connect(conn_url).await?))

let mut pool_options = sqlx::sqlite::SqlitePoolOptions::new();

let sqlite_pool_options = options
.and_then(|opts| opts.sqlite)
.and_then(|sqlite_opts| sqlite_opts.pool);

if let Some(custom_pool_opts) = sqlite_pool_options {
if let Some(max_connections) = custom_pool_opts.max_connections {
pool_options = pool_options.max_connections(max_connections);
}

if let Some(min_connections) = custom_pool_opts.min_connections {
pool_options = pool_options.min_connections(min_connections);
}
}

Ok(Self::Sqlite(pool_options.connect(conn_url).await?))
}
#[cfg(feature = "mysql")]
"mysql" => {
Expand Down
Loading