Skip to content

Commit

Permalink
latest
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Jul 22, 2024
1 parent 655d8f7 commit 709a8d1
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 72 deletions.
114 changes: 63 additions & 51 deletions Cargo.lock

Large diffs are not rendered by default.

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", "examples/test-wasm",
"xmtp_id", "diesel-wasm-sqlite"
]

exclude = [
Expand Down
11 changes: 10 additions & 1 deletion diesel-wasm-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
[package]
name = "diesel-wasm-sqlite"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[dependencies]
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"
rand = "0.8"
getrandom = { version = "0.2", features = ["js"] }
wee_alloc = { version = "0.4.2", optional = true }
console_error_panic_hook = { version = "0.1", optional = true }

[dev-dependencies]
rand = "0.8"
getrandom = { version = "0.2", features = ["js"] }
wasm-bindgen-test = "0.3"
web-sys = { version = "0.3", features = ["console"] }


[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]
12 changes: 12 additions & 0 deletions diesel-wasm-sqlite/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# Custom Diesel Backend for Wasm wa-sqlite

#### Bundle the javascript in `package.js` to rust

`npm run esbuild`

#### Build the JS WASM interface

`wasm-pack build`

#### Run the Wasm Tests

wasm-pack test --safari --headless
4 changes: 4 additions & 0 deletions diesel-wasm-sqlite/esbuild.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const esbuild = require("esbuild");
const { wasmLoader } = require("esbuild-plugin-wasm");

esbuild.build({
entryPoints: ["package.js"],
bundle: true,
outfile: "src/package.js",
plugins: [
wasmLoader(),
],
format: "esm",
minify: true,
}).catch(() => process.exit(1));
15 changes: 14 additions & 1 deletion diesel-wasm-sqlite/package-lock.json

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

3 changes: 3 additions & 0 deletions diesel-wasm-sqlite/package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as SQLite from "@xmtp/wa-sqlite";
import initModule from "@xmtp/wa-sqlite/build";

const module = await initModule();
const sqlite3 = SQLite.Factory(module);
const vfs = await OPFSCoopSyncVFS.create("test", module);
sqlite3.vfs_register(vfs, true);

export function sqlite3_result_text(context, value) {
sqlite3.result_text(context, value);
Expand Down
3 changes: 2 additions & 1 deletion diesel-wasm-sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"@xmtp/wa-sqlite": "^1.0.1",
"esbuild": "^0.23.0"
"esbuild": "^0.23.0",
"esbuild-plugin-wasm": "^1.1.0"
}
}
46 changes: 31 additions & 15 deletions diesel-wasm-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ pub mod connection;
pub mod ffi;
pub mod query_builder;
pub mod sqlite_types;
pub mod utils;

use diesel::{
connection::{AnsiTransactionManager, Instrumentation, SimpleConnection, TransactionManager},
query_builder::{QueryFragment, QueryId},
result::QueryResult,
Connection,
};
use wasm_bindgen::JsValue;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};

pub use backend::{SqliteType, WasmSqlite};

Expand Down Expand Up @@ -68,6 +69,18 @@ impl Connection for WasmSqliteConnection {
}
}

#[wasm_bindgen(js_name = establishDbConnection)]
pub fn establish_db_connection() {
let rng: u16 = rand::random();
let url = format!(
"{}/wasmtest-{}.db3",
std::env::temp_dir().to_str().unwrap(),
rng
);
let conn = WasmSqliteConnection::establish(&url).unwrap();
println!("{:?}", conn);
}

impl From<WasmSqliteError> for diesel::result::Error {
fn from(value: WasmSqliteError) -> diesel::result::Error {
log::error!("NOT IMPLEMENTED, {:?}", value);
Expand All @@ -88,21 +101,24 @@ impl From<JsValue> for WasmSqliteError {
}
}

#[cfg(test)]
mod test {
/*
mod tests {
use super::*;
use wasm_bindgen_test::wasm_bindgen_test;
use web_sys::console;
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);
}
/*
#[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);
}
*/
}
*/
4 changes: 2 additions & 2 deletions diesel-wasm-sqlite/src/package.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions diesel-wasm-sqlite/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
27 changes: 27 additions & 0 deletions diesel-wasm-sqlite/tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![cfg(target_arch = "wasm32")]

use wasm_bindgen_test::*;
use web_sys::console;
use diesel_wasm_sqlite::WasmSqliteConnection;
wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn test_console_log() {
diesel_wasm_sqlite::utils::set_panic_hook();
console::log_1(&"TEST THIS WORKS????".into());
assert_eq!(1, 2);
}

/*
#[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);
}
*/

0 comments on commit 709a8d1

Please sign in to comment.