Skip to content

Commit

Permalink
try to get wa-sqlite to compile with rust wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Jul 19, 2024
1 parent 5981d65 commit afca323
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 25 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
21 changes: 7 additions & 14 deletions bindings_ffi/Cargo.lock

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

8 changes: 8 additions & 0 deletions diesel-wasm-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
46 changes: 46 additions & 0 deletions diesel-wasm-sqlite/js-to-rust-test/index.js
Original file line number Diff line number Diff line change
@@ -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");
}
}
9 changes: 9 additions & 0 deletions diesel-wasm-sqlite/js-to-rust-test/package.json
Original file line number Diff line number Diff line change
@@ -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",
}
}
8 changes: 8 additions & 0 deletions diesel-wasm-sqlite/js-to-rust-test/yarn.lock
Original file line number Diff line number Diff line change
@@ -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==
21 changes: 16 additions & 5 deletions diesel-wasm-sqlite/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -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<i32, JsValue>;
}

/// 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<u8>);

#[wasm_bindgen]
pub fn sqlite3_result_null(context: i32);
}
34 changes: 30 additions & 4 deletions diesel-wasm-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -33,10 +38,12 @@ impl Connection for WasmSqliteConnection {
type TransactionManager = AnsiTransactionManager;

fn establish(database_url: &str) -> diesel::prelude::ConnectionResult<Self> {
ffi::establish(database_url)
let raw_conn = ffi::establish(database_url)
.map_err(WasmSqliteError::from)
.map_err(Into::<diesel::result::ConnectionError>::into)?;
Ok(WasmSqliteConnection {})
Ok(WasmSqliteConnection {
raw_db_pointer: raw_conn,
})
}

fn execute_returning_count<T>(&mut self, source: &T) -> QueryResult<usize>
Expand Down Expand Up @@ -80,3 +87,22 @@ impl From<JsValue> 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);
}
}
1 change: 0 additions & 1 deletion diesel-wasm-sqlite/src/query_builder/returning.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit afca323

Please sign in to comment.