Turso WASM Implementation
The Turso WASM implementation uses the @tursodatabase/database-wasm library, providing SQLite functionality in web browsers and other WASM-compatible environments.
INFO
This implementation uses SharedArrayBuffer and OPFS which require the following server headers to be set:
Cross-Origin-Opener-Policy: same-originCross-Origin-Embedder-Policy: require-corp
Installation
npm install applesauce-sqlite @tursodatabase/database-wasmBasic Usage
Unlike other implementations, Turso WASM requires you to create and initialize the database instance yourself before passing it to the TursoWasmEventDatabase constructor:
import { EventStore } from "applesauce-core";
import { TursoWasmEventDatabase } from "applesauce-sqlite/turso-wasm";
import { connect } from "@tursodatabase/database-wasm";
// Create a database connection
const db = await connect("my-database.db");
// Create and initialize the event database in one step
const database = await TursoWasmEventDatabase.fromDatabase(db);
// Create EventStore with Turso WASM backend
const eventStore = new EventStore(database);
// Use as normal
eventStore.add(someNostrEvent);Options
The TursoWasmEventDatabase.fromDatabase() method accepts the following options:
const database = await TursoWasmEventDatabase.fromDatabase(db, {
search: true, // Enable full-text search (default: false)
searchContentFormatter: customFormatter, // Custom search content formatter
});Search Support
Enable full-text search using SQLite's FTS5 extension:
const database = await TursoWasmEventDatabase.fromDatabase(db, {
search: true,
});
// Now you can search events
const results = eventStore.getByFilters({
search: "bitcoin lightning",
});Custom Search Formatter
Customize how content is indexed for search:
const database = await TursoWasmEventDatabase.fromDatabase(db, {
search: true,
searchContentFormatter: (event) => {
// Extract searchable content from event
return event.content + " " + event.tags.map((t) => t[1]).join(" ");
},
});Advanced Usage
Database Connection Management
The Turso WASM implementation requires you to manage the database connection lifecycle:
import { connect } from "@tursodatabase/database-wasm";
// Create connection
const db = await connect("my-database.db");
// Create and initialize event database in one step
const database = await TursoWasmEventDatabase.fromDatabase(db, { search: true });
// ... use the database
// Close when done
await database.close();Using with Existing Database Instance
You can use any existing Turso WASM Database instance:
import { connect } from "@tursodatabase/database-wasm";
import { TursoWasmEventDatabase } from "applesauce-sqlite/turso-wasm";
// Your existing database setup
const db = await connect("existing-database.db");
// Create and initialize event database in one step
const database = await TursoWasmEventDatabase.fromDatabase(db, { search: true });Rebuilding Search Index
If you need to rebuild the search index (e.g., after changing the search formatter):
await database.rebuildSearchIndex();Closing the Database
Always close the database when done:
await database.close();Or use automatic cleanup with Symbol.dispose:
{
const database = await TursoWasmEventDatabase.fromDatabase(db);
// ... use database
} // Automatically closed when out of scope