Crates.io | diesel-wasm-sqlite |
lib.rs | diesel-wasm-sqlite |
version | 0.0.1 |
source | src |
created_at | 2024-08-27 19:36:30.580054 |
updated_at | 2024-08-27 19:36:30.580054 |
description | SQLite WebAssembly backend for Diesel |
homepage | https://xmtp.org/ |
repository | https://github.com/xmtp/libxmtp |
max_upload_size | |
id | 1353774 |
size | 1,673,923 |
add diesel-wasm-sqlite
to your project. SQLite is automatically bundled with
the library.
[dependencies]
diesel = { version = "2.2" }
diesel-wasm-sqlite = { git = "https://github.com/xmtp/libxmtp", branch = "wasm-backend" }
wasm-bindgen = "0.2"
use diesel_wasm_sqlite::{connection::WasmSqliteConnection, WasmSqlite};
use wasm_bindgen::prelude::*;
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./tests/web/migrations/");
mod schema {
diesel::table! {
books {
id -> Integer,
title -> Text,
author -> Nullable<Text>,
}
}
}
#[derive(Deserialize, Insertable, Debug, PartialEq, Clone)]
#[diesel(table_name = books)]
pub struct BookForm {
title: String,
author: Option<String>,
}
// SQLite must be instantiated in a web-worker
// to take advantage of OPFS
#[wasm_bindgen]
async fn code_in_web_worker() -> Result<i32, diesel::QueryResult<usize>> {
use schema::books::dsl::*;
// `init_sqlite` sets up OPFS and SQLite. It must be ran before anything else,
// or we crash once we start trying to do queries.
diesel_wasm_sqlite::init_sqlite().await;
// create a new persistent SQLite database with OPFS
let result = WasmSqliteConnection::establish(&format!("test-{}", rng));
let query = insert_into(books).values(vec![
BookForm {
title: "Game of Thrones".into(),
author: Some("George R.R".into()),
},
BookForm {
title: "The Hobbit".into(),
author: Some("J.R.R. Tolkien".into()),
},
]);
Ok(query.execute(conn)?)
}
look in tests/web.rs
for working example!
yarn install
yarn run build
package.json
if it changedcargo build --target wasm32-unknown-unknown
wasm-pack test --safari --features unsafe-debug-query
navigate to http://localhost:8000
to observe test output
wasm-pack test --safari --headless
rust-analyzer does not like crates with different targets in the same workspace.
If you want this to work well with your LSP, open diesel-wasm-sqlite
as it's
own project in VSCode.