sql-wasm
SQLite compiled to WebAssembly through Emscripten.
This project is a port of the existing SQL.js library. The API methods and syntax are almost identical
Usage
The entry point to the library is the only difference between sql-wasm and SQL.js. The library is loaded asynchronously by downloading the .wasm file from the network (Web) or filesystem (NodeJS).
import createSqlWasm from "sql-wasm";
(async () => {
const sql = await createSqlWasm({ wasmUrl: "/path/to/sqlite3.wasm" });
const db = new sql.Database();
})();
On the web wasmUrl defaults to /sqlite3.wasm.
On NodeJS wasmUrl defaults to ${__dirname}/sqlite3.wasm
SQL.js usage examples:
var db = new sql.Database();
sqlstr = "CREATE TABLE hello (a int, b char);";
sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
sqlstr += "INSERT INTO hello VALUES (1, 'world');"
db.run(sqlstr);
var res = db.exec("SELECT * FROM hello");
var stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");
var result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});
console.log(result);
stmt.bind([0, 'hello']);
while (stmt.step()) console.log(stmt.get());
function add(a, b) {return a+b;}
db.create_function("add_js", add);
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));");
stmt.free();
var binaryArray = db.export();
Web Worker
Web Worker functionality has been omitted from this implementation.
Development
You'll need to install the Emscripten SDK to make any modifications to this package.
Compile the SQLite WebAssembly wrapper on it's own using:
npm run make-wasm
This project uses TypeScript + Babel. You can compile those by using the build command:
npm run build
Tests
The unit tests here are a direct port from the unit tests in SQL.js to ensure the two libraries are compatible.
These tests are written using Jest and can be launched using the NPM command:
npm test