Skip to content

Commit

Permalink
Expose sqlite struct and add settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Sep 10, 2024
1 parent 39247bf commit bb34010
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/modules/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use rquickjs::{
Class, Ctx, Result,
};

use self::argument::Argument;
use self::database::Database;
use self::statement::Statement;
use self::value::Value;
pub use self::argument::Argument;
pub use self::database::Database;
pub use self::open::open;
pub use self::statement::Statement;
pub use self::value::Value;
use crate::utils::module::export_default;

mod argument;
Expand Down
60 changes: 53 additions & 7 deletions src/modules/sqlite/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ static IN_MEMORY_DB_SEQ: AtomicUsize = AtomicUsize::new(0);
pub async fn open(options: OpenOptions) -> Result<Database> {
let mut connect_options = SqliteConnectOptions::new();
connect_options = connect_options
.foreign_keys(true)
.foreign_keys(options.foreign_keys)
.page_size(options.page_size)
.busy_timeout(Duration::from_millis(options.busy_timeout))
.thread_name(|id| format!("quickjs-sqlite-worker-{id}"));
if let Some(filename) = options.filename {
connect_options = connect_options.filename(filename).create_if_missing(true);
Expand All @@ -31,31 +33,75 @@ pub async fn open(options: OpenOptions) -> Result<Database> {

let mut pool_options = SqlitePoolOptions::new();
pool_options = pool_options
.idle_timeout(None)
.max_lifetime(Some(Duration::from_secs(60 * 60)))
.max_connections(5)
.min_connections(0);
.idle_timeout(options.idle_timeout.map(Duration::from_secs))
.max_lifetime(Some(Duration::from_secs(options.max_lifetime)))
.max_connections(options.max_connections)
.min_connections(options.min_connections);

let pool = pool_options.connect_with(connect_options).await.unwrap();
Ok(Database::new(pool))
}

#[derive(Debug, Clone)]
pub struct OpenOptions {
filename: Option<String>,
in_memory: bool,
wal: bool,
page_size: u32,
foreign_keys: bool,
max_connections: u32,
min_connections: u32,
idle_timeout: Option<u64>,
max_lifetime: u64,
busy_timeout: u64,
}

impl Default for OpenOptions {
fn default() -> Self {
Self {
filename: None,
in_memory: true,
wal: true,
page_size: 4096,
foreign_keys: true,
max_connections: 5,
min_connections: 0,
idle_timeout: None,
max_lifetime: 60 * 60,
busy_timeout: 5 * 1000,
}
}
}

impl<'js> FromJs<'js> for OpenOptions {
fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
let default = OpenOptions::default();
let obj = value.get::<Object<'js>>()?;
let filename = obj.get("filename")?;
let in_memory = obj.get("inMemory").unwrap_or(false);
let wal = obj.get("wal").unwrap_or(true);
let in_memory = obj.get("inMemory").unwrap_or(default.in_memory);
let wal = obj.get("wal").unwrap_or(default.wal);
let page_size = obj.get("pageSize").unwrap_or(default.page_size);
let foreign_keys = obj.get("foreignKeys").unwrap_or(default.foreign_keys);
let max_connections = obj.get("maxConnections").unwrap_or(default.max_connections);
let min_connections = obj.get("minConnections").unwrap_or(default.min_connections);
let idle_timeout = obj.get::<_, u64>("idleTimeout").ok();
let max_lifetime = obj
.get::<_, u64>("maxLifetime")
.unwrap_or(default.max_lifetime);
let busy_timeout = obj
.get::<_, u64>("busyTimeout")
.unwrap_or(default.busy_timeout);
Ok(Self {
filename,
in_memory,
wal,
page_size,
foreign_keys,
max_connections,
min_connections,
idle_timeout,
max_lifetime,
busy_timeout,
})
}
}

0 comments on commit bb34010

Please sign in to comment.