[](https://github.com/rhashimoto/wa-sqlite/actions/workflows/ci.yml?branch=breaking-changes)
npm install wa-sqlite
IndexedDB and Origin Private File System virtual file systems and a virtual table module that accesses Javascript arrays are among the examples provided as proof of concept.
Try the demo or run benchmarks with a modern desktop web browser. More information is available in the FAQ, discussion forums, and API reference.
Minor build customization (e.g. changing build defines or flags) can be done with make arguments, and the helper project sqwab can be used to build without a local build environment.
If you do want to build yourself, here are the prerequisites:
* Building on Debian Linux is known to work, compatibility with other platforms is unknown.
* yarn - If you use a different package manager (e.g. npm) then file paths in the demo will need adjustment.
* Emscripten SDK 3.1.25+.
* curl, make, openssl, sed, tclsh, unzip
Here are the build steps:
* Make sure emcc works.
* git clone git@github.com:rhashimoto/wa-sqlite.git
* cd wa-sqlite
* yarn install
* make (or yarn prepack)
The default build produces ES6 modules + WASM, synchronous and asynchronous (using Asyncify) in dist/.
``javascript
import SQLiteESMFactory from 'wa-sqlite/dist/wa-sqlite.mjs';
import * as SQLite from 'wa-sqlite';
async function hello() {
const module = await SQLiteESMFactory();
const sqlite3 = SQLite.Factory(module);
const db = await sqlite3.open_v2('myDB');
await sqlite3.exec(db, SELECT 'Hello, world!', (row, columns) => {
console.log(row);
});
await sqlite3.close(db);
}
hello();
`
The implementation of sqlite3.exec may be of interest to anyone wanting more fine-grained use of SQLite statement objects (e.g. for binding parameters, explicit column datatypes, etc.).
* Open a browser on http://localhost:8000/demo/The demo page provides access to databases on multiple VFS implementations, including IndexedDB (which is the only one persistent across page loads and multiple tabs). In addition, in each database there is a SQLite module named "array" that provides some historical stock data from a common Javascript array - use it for virtual tables in SQL like this:
`sql
CREATE VIRTUAL TABLE IF NOT EXISTS goog USING array;-- Use it directly out of the Javascript array:
SELECT * FROM goog LIMIT 5;
-- Copy into a native table (on the current VFS):
CREATE TABLE IF NOT EXISTS tbl AS SELECT * FROM goog;
``For convenience, if any text region is selected in the editor, only that region will be executed. In addition, the editor contents are restored across page reloads using browser localStorage.