sql_js_httpvfs_rs/
db.rs

1use wasm_bindgen::prelude::*;
2
3#[wasm_bindgen(module = "/src/build/index.js")]
4extern "C" {
5    #[cfg(feature = "bundled")]
6    #[wasm_bindgen(js_name = createBundledDbWorker)]
7    async fn create_db_blob_worker(configs: Vec<JsValue>, worker_blob: &[u8], wasm_blob: &[u8]);
8
9    /// Creates a new SQLite DB worker given a config, and the `str` paths to the worker file URL and the WASM file URL.
10    ///
11    /// Note that `wasm_url`, if a relative path, is relative to the `worker_url` file!
12    #[wasm_bindgen(js_name = createUrlDbWorker)]
13    pub async fn create_db_worker(configs: Vec<JsValue>, worker_url: &str, wasm_url: &str);
14
15    /// Executes an SQL query, assuming the SQLite DB worker is initialized. If not, this function will return an
16    /// `Error`.
17    #[wasm_bindgen(catch, js_name = execQuery)]
18    pub async fn exec_query(query: String) -> Result<JsValue, JsValue>;
19
20    /// A check to see if the SQLite DB worker is initialized.
21    #[wasm_bindgen(js_name = isInitialized)]
22    pub fn is_worker_initialized() -> bool;
23
24}
25
26/// Creates a new SQLite DB worker given a config.
27///
28/// Uses bundled blobs of the worker and WASM files to avoid needing to manually bundle them along with
29/// your application. This is enabled by enabling the `bundled` feature.
30#[cfg(feature = "bundled")]
31pub async fn create_bundled_db_worker(configs: Vec<JsValue>) {
32    const WORKER_BLOB: &[u8] = include_bytes!("./build/assets/sqlite.worker.js");
33    const WASM_BLOB: &[u8] = include_bytes!("./build/assets/sql-wasm.wasm");
34
35    create_db_blob_worker(configs, WORKER_BLOB, WASM_BLOB).await;
36}