sqlite-wasm 0.1.1

A high-performance SQLite wrapper for WebAssembly with OPFS support
docs.rs failed to build sqlite-wasm-0.1.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.

SQLite WASM

crates.io docs.rs MIT License Rust WASM

A high-performance SQLite wrapper for WebAssembly with OPFS support
Zero-cost abstractions โ€ข Type-safe API โ€ข Persistent storage โ€ข Single-threaded by design


โœจ Features

Icon Feature Description
๐Ÿš€ Zero-cost abstractions Worker management with OnceLock and atomic counters
๐Ÿ”’ OPFS persistence Databases survive page reloads and browser restarts
๐Ÿงต Web Worker Database operations run in a separate thread
๐Ÿ“ฆ Auto-minification JS glue code automatically minified (Brotli/Gzip)
๐Ÿ” COOP/COEP Headers Proper headers for SharedArrayBuffer support
๐ŸŽฏ Type Safe Strongly typed Rust API with proper error handling
๐Ÿ”„ Async/Await Promise-based API for JavaScript
โšก Atomic operations Lock-free message passing with AtomicU32

๐Ÿš€ Test It Live

๐Ÿ‘‰ Try SQLite Studio Online
No installation needed. Opens directly in your browser.


๐Ÿ“ฆ Installation

Add to your Cargo.toml:

[dependencies]
sqlite-wasm = "0.1"

Or via CLI:

cargo add sqlite-wasm

๐Ÿ”จ Building

trunk build --release
trunk serve --release

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Your App      โ”‚
โ”‚   (Rust/JS)     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   sqlite-wasm   โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”โ”‚
โ”‚   โ”‚  Worker    โ”‚โ”‚  โ”€โ”€ Singleton, auto-managed
โ”‚   โ”‚  Manager   โ”‚โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”โ”‚
โ”‚   โ”‚   Message  โ”‚โ”‚  โ”€โ”€ Atomic counters, oneshot channels
โ”‚   โ”‚   Passing  โ”‚โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”โ”‚
โ”‚   โ”‚    OPFS    โ”‚โ”‚  โ”€โ”€ Persistent, high-performance
โ”‚   โ”‚   Storage  โ”‚โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  SQLite Worker  โ”‚
โ”‚  (JavaScript)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

API Reference

Function Description
autostart(path) Initializes the SQLite worker (call once)
open(name) Opens or creates a database
exec(sql, params) Executes SQL without returning rows
query(sql, params) Executes SELECT and returns rows
close() Closes the current database
is_open() Checks if a database is open
db_id() Returns current database ID (debug)

๐Ÿฆ€ Rust Usage

Quick Start

use sqlite_wasm::{autostart, open, close};

#[wasm_bindgen]
pub async fn example() -> Result<(), JsValue> {
    // 1. Initialize worker
    let db = autostart("/static/sqlite.org/sqlite3-worker1.js").await?;
    
    // 2. Open database
    open("myapp.sqlite3").await?;
    
    // 3. Create table
    db.exec(
        "CREATE TABLE IF NOT EXISTS users (id INTEGER, name TEXT)",
        vec![]
    ).await?;
    
    // 4. Insert data
    db.exec(
        "INSERT INTO users VALUES (?, ?)",
        vec![1.into(), "Alice".into()]
    ).await?;
    
    // 5. Query data
    let users = db.query("SELECT * FROM users", vec![]).await?;
    
    // 6. Close database
    close().await?;
    
    Ok(())
}

๐ŸŒ JavaScript Usage

After initialization, the global wasm object is available with all methods.

Quick Start

import init from './pkg/sqlite_wasm.js';

async function start() {
    // 1. Load WASM module
    await init();
    
    // 2. Initialize worker
    await wasm.autostart('/sqlite.org/sqlite3-worker1.js');
    
    // 3. Open database
    await wasm.open('app.db');
    
    // 4. Create table
    await wasm.exec(
        `CREATE TABLE IF NOT EXISTS users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL,
            email TEXT UNIQUE
        )`,
        []
    );
    
    // 5. Insert data
    await wasm.exec(
        "INSERT INTO users (name, email) VALUES (?, ?)",
        ["Alice", "alice@example.com"]
    );
    
    // 6. Query data
    const result = await wasm.query("SELECT * FROM users", []);
    console.log('Users:', result.resultRows);
    
    // 7. Close database
    await wasm.close();
}

start().catch(console.error);

JavaScript API

Method Description Example
autostart(path) Initializes worker await wasm.autostart('/sqlite.org/sqlite3-worker1.js')
open(name) Opens/creates database await wasm.open('mydb.sqlite3')
exec(sql, params) Executes SQL await wasm.exec("INSERT INTO users VALUES (?)", ["Joรฃo"])
query(sql, params) Runs SELECT const res = await wasm.query("SELECT * FROM users", [])
close() Closes database await wasm.close()

๐ŸŒ Browser Requirements

  • WebAssembly support
  • Web Workers
  • OPFS (Origin Private File System)
  • COOP/COEP headers (Cross-Origin Isolation)

Recommended: Latest Chrome, Edge, or other Chromium-based browsers.


๐Ÿ“„ License

MIT ยฉ adorno-dev


Built with ๐Ÿฆ€ and โค๏ธ for maximum performance
Made in Rust ยท Runs in Browser ยท Powered by SQLite