diff --git a/Cargo.lock b/Cargo.lock index fd0c847b4..025898ae1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1077,8 +1077,11 @@ name = "diesel-wasm-sqlite" version = "0.1.0" dependencies = [ "diesel", + "getrandom", "log", + "rand", "wasm-bindgen", + "wasm-bindgen-test", ] [[package]] @@ -5267,6 +5270,21 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" +[[package]] +name = "test-wasm" +version = "0.1.0" +dependencies = [ + "chrono", + "diesel", + "diesel-wasm-sqlite", + "getrandom", + "rand", + "serde", + "wasm-bindgen", + "wasm-bindgen-test", + "web-sys", +] + [[package]] name = "thiserror" version = "1.0.63" diff --git a/Cargo.toml b/Cargo.toml index 1fc4ec3c6..262d5f577 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ members = [ "xmtp_user_preferences", "xmtp_v2", "xmtp_mls", - "xmtp_id", "diesel-wasm-sqlite", + "xmtp_id", "diesel-wasm-sqlite", "examples/test-wasm", ] exclude = [ diff --git a/bindings_ffi/Cargo.lock b/bindings_ffi/Cargo.lock index ac3c0372e..83af96ba0 100644 --- a/bindings_ffi/Cargo.lock +++ b/bindings_ffi/Cargo.lock @@ -978,8 +978,7 @@ dependencies = [ [[package]] name = "diesel" version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35b696af9ff4c0d2a507db2c5faafa8aa0205e297e5f11e203a24226d5355e7a" +source = "git+https://github.com/xmtp/diesel?branch=insipx/wasm-backend#a3fa32d4291bbae566cf544ca63a1aa2543da943" dependencies = [ "diesel_derives", "libsqlite3-sys", @@ -990,8 +989,7 @@ dependencies = [ [[package]] name = "diesel_derives" version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d6fdd83d5947068817016e939596d246e5367279453f2a3433287894f2f2996" +source = "git+https://github.com/xmtp/diesel?branch=insipx/wasm-backend#a3fa32d4291bbae566cf544ca63a1aa2543da943" dependencies = [ "diesel_table_macro_syntax", "dsl_auto_type", @@ -1003,8 +1001,7 @@ dependencies = [ [[package]] name = "diesel_migrations" version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a73ce704bad4231f001bff3314d91dce4aba0770cee8b233991859abc15c1f6" +source = "git+https://github.com/xmtp/diesel?branch=insipx/wasm-backend#a3fa32d4291bbae566cf544ca63a1aa2543da943" dependencies = [ "diesel", "migrations_internals", @@ -1014,8 +1011,7 @@ dependencies = [ [[package]] name = "diesel_table_macro_syntax" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "209c735641a413bc68c4923a9d6ad4bcb3ca306b794edaa7eb0b3228a99ffb25" +source = "git+https://github.com/xmtp/diesel?branch=insipx/wasm-backend#a3fa32d4291bbae566cf544ca63a1aa2543da943" dependencies = [ "syn 2.0.48", ] @@ -1083,8 +1079,7 @@ dependencies = [ [[package]] name = "dsl_auto_type" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab32c18ea6760d951659768a3e35ea72fc1ba0916d665a88dfe048b2a41e543f" +source = "git+https://github.com/xmtp/diesel?branch=insipx/wasm-backend#a3fa32d4291bbae566cf544ca63a1aa2543da943" dependencies = [ "darling", "either", @@ -2607,8 +2602,7 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "migrations_internals" version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd01039851e82f8799046eabbb354056283fb265c8ec0996af940f4e85a380ff" +source = "git+https://github.com/xmtp/diesel?branch=insipx/wasm-backend#a3fa32d4291bbae566cf544ca63a1aa2543da943" dependencies = [ "serde", "toml 0.8.8", @@ -2617,8 +2611,7 @@ dependencies = [ [[package]] name = "migrations_macros" version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb161cc72176cb37aa47f1fc520d3ef02263d67d661f44f05d05a079e1237fd" +source = "git+https://github.com/xmtp/diesel?branch=insipx/wasm-backend#a3fa32d4291bbae566cf544ca63a1aa2543da943" dependencies = [ "migrations_internals", "proc-macro2", diff --git a/diesel-wasm-sqlite/Cargo.toml b/diesel-wasm-sqlite/Cargo.toml index 5f68163b6..c8fba4f7f 100644 --- a/diesel-wasm-sqlite/Cargo.toml +++ b/diesel-wasm-sqlite/Cargo.toml @@ -7,3 +7,11 @@ edition = "2021" diesel = { git = "https://github.com/xmtp/diesel", branch = "insipx/wasm-backend", features = ["r2d2", "i-implement-a-third-party-backend-and-opt-into-breaking-changes"] } wasm-bindgen = "0.2" log = "0.4" + +[dev-dependencies] +rand = "0.8" +getrandom = { version = "0.2", features = ["js"] } +wasm-bindgen-test = "0.3" + +[lib] +crate-type = ["cdylib", "rlib"] diff --git a/diesel-wasm-sqlite/js-to-rust-test/index.js b/diesel-wasm-sqlite/js-to-rust-test/index.js new file mode 100644 index 000000000..fa23e127f --- /dev/null +++ b/diesel-wasm-sqlite/js-to-rust-test/index.js @@ -0,0 +1,46 @@ +import * as SQLite from "@xmtp/wa-sqlite"; +import initModule from "@xmtp/wa-sqlite/build"; + +const sqlite3 = SQLite.Factory(module); + +export function sqlite3_result_text(context, value) { + sqlite3.result_text(context, value); +} + +export function sqlite3_result_int(context, value) { + sqlite3.result_int(context, value); +} + +export function sqlite3_result_int64(context, value) { + sqlite3.result_int64(context, value); +} + +export function sqlite3_result_double(context, value) { + sqlite3.result_double(context, value); +} + +export function sqlite3_result_blob(context, value) { + sqlite3.result_blob(context, value); +} + +export function sqlite3_result_null(context) { + sqlite3.result_null(context); +} + +export function establish(database_url) { + try { + console.log("Opening database!"); + return sqlite3.open_v2(database_url); + } catch { + console.log("establish err"); + } +} + +export function batch_execute(database, query) { + try { + sqlite3.exec(database, query); + console.log("Batch exec'ed"); + } catch { + console.log("exec err"); + } +} diff --git a/diesel-wasm-sqlite/js-to-rust-test/package.json b/diesel-wasm-sqlite/js-to-rust-test/package.json new file mode 100644 index 000000000..3b5389021 --- /dev/null +++ b/diesel-wasm-sqlite/js-to-rust-test/package.json @@ -0,0 +1,9 @@ +{ + "name": "js-to-rust-test", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "dependencies": { + "@xmtp/wa-sqlite": "^1.0.1", + } +} diff --git a/diesel-wasm-sqlite/js-to-rust-test/yarn.lock b/diesel-wasm-sqlite/js-to-rust-test/yarn.lock new file mode 100644 index 000000000..a9e0b5b70 --- /dev/null +++ b/diesel-wasm-sqlite/js-to-rust-test/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@xmtp/wa-sqlite@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@xmtp/wa-sqlite/-/wa-sqlite-1.0.1.tgz#3ecab8cf3ea15386e3e1d152526d6190898f7529" + integrity sha512-/MVsgo5HG7FVdMMNUKsf5HTIDnOncSK0ljrOBr+xqkfH6qonQwqmUsjcW2Z4GyyCIVvhQlxOSy+1W+7Z/WH0DA== diff --git a/diesel-wasm-sqlite/src/ffi.rs b/diesel-wasm-sqlite/src/ffi.rs index 1f4be65be..aa164a9cb 100644 --- a/diesel-wasm-sqlite/src/ffi.rs +++ b/diesel-wasm-sqlite/src/ffi.rs @@ -1,22 +1,33 @@ use wasm_bindgen::{prelude::*, JsValue}; /// Simple Connection -#[wasm_bindgen] +#[wasm_bindgen(module = "/js-to-rust-test/index.js")] extern "C" { #[wasm_bindgen(catch)] - pub fn batch_execute(query: &str) -> Result<(), JsValue>; + pub fn batch_execute(database: i32, query: &str) -> Result<(), JsValue>; #[wasm_bindgen(catch)] - pub fn establish(database_url: &str) -> Result<(), JsValue>; + pub fn establish(database_url: &str) -> Result; } -/// Direct Shim for was-sqlite -#[wasm_bindgen] +/// Direct Shim for wa-sqlite +#[wasm_bindgen(module = "/js-to-rust-test/index.js")] extern "C" { + #[wasm_bindgen] pub fn sqlite3_result_text(context: i32, value: String); + + #[wasm_bindgen] pub fn sqlite3_result_int(context: i32, value: i32); + + #[wasm_bindgen] pub fn sqlite3_result_int64(context: i32, value: i64); + + #[wasm_bindgen] pub fn sqlite3_result_double(context: i32, value: f64); + + #[wasm_bindgen] pub fn sqlite3_result_blob(context: i32, value: Vec); + + #[wasm_bindgen] pub fn sqlite3_result_null(context: i32); } diff --git a/diesel-wasm-sqlite/src/lib.rs b/diesel-wasm-sqlite/src/lib.rs index f6033799f..0d3f07afe 100644 --- a/diesel-wasm-sqlite/src/lib.rs +++ b/diesel-wasm-sqlite/src/lib.rs @@ -14,13 +14,18 @@ use diesel::{ use wasm_bindgen::JsValue; pub use backend::{SqliteType, WasmSqlite}; -pub struct WasmSqliteConnection {} + +#[derive(Debug)] +pub struct WasmSqliteConnection { + raw_db_pointer: i32, +} + #[derive(Debug)] pub struct WasmSqliteError(JsValue); impl SimpleConnection for WasmSqliteConnection { fn batch_execute(&mut self, query: &str) -> diesel::prelude::QueryResult<()> { - ffi::batch_execute(query) + ffi::batch_execute(self.raw_db_pointer, query) .map_err(WasmSqliteError::from) .map_err(Into::into) } @@ -33,10 +38,12 @@ impl Connection for WasmSqliteConnection { type TransactionManager = AnsiTransactionManager; fn establish(database_url: &str) -> diesel::prelude::ConnectionResult { - ffi::establish(database_url) + let raw_conn = ffi::establish(database_url) .map_err(WasmSqliteError::from) .map_err(Into::::into)?; - Ok(WasmSqliteConnection {}) + Ok(WasmSqliteConnection { + raw_db_pointer: raw_conn, + }) } fn execute_returning_count(&mut self, source: &T) -> QueryResult @@ -80,3 +87,22 @@ impl From for WasmSqliteError { WasmSqliteError(err) } } + +#[cfg(test)] +mod test { + use super::*; + use wasm_bindgen_test::wasm_bindgen_test; + wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + + #[wasm_bindgen_test] + fn test_establish() { + let rng: u16 = rand::random(); + let url = format!( + "{}/wasmtest-{}.db3", + std::env::temp_dir().to_str().unwrap(), + rng + ); + let mut conn = WasmSqliteConnection::establish(&url).unwrap(); + println!("{:?}", conn); + } +} diff --git a/diesel-wasm-sqlite/src/query_builder/returning.rs b/diesel-wasm-sqlite/src/query_builder/returning.rs index 8709c1787..edc466034 100644 --- a/diesel-wasm-sqlite/src/query_builder/returning.rs +++ b/diesel-wasm-sqlite/src/query_builder/returning.rs @@ -1,5 +1,4 @@ use crate::backend::{SqliteReturningClause, WasmSqlite}; -// use diesel::backend::Backend; use diesel::query_builder::ReturningClause; use diesel::query_builder::{AstPass, QueryFragment}; use diesel::result::QueryResult;