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).
; async { const sql = await ; // From here on, the SQL.js API can be used... const db = ;};
On the web wasmUrl
defaults to /sqlite3.wasm
.
On NodeJS wasmUrl
defaults to ${__dirname}/sqlite3.wasm
SQL.js usage examples:
// Create a databasevar db = ;// NOTE: You can also use new sql.Database(data) where// data is an Uint8Array representing an SQLite database file // Execute some sqlsqlstr = "CREATE TABLE hello (a int, b char);";sqlstr += "INSERT INTO hello VALUES (0, 'hello');"sqlstr += "INSERT INTO hello VALUES (1, 'world');"db; // Run the query without returning anything var res = db;/*[ {columns:['a','b'], values:[[0,'hello'],[1,'world']]}]*/ // Prepare an sql statementvar stmt = db; // Bind values to the parameters and fetch the results of the queryvar result = stmt;console; // Will print {a:1, b:'world'} // Bind other valuesstmt;while stmt console; // Will print [0, 'hello'] // You can also use javascript functions inside your SQL code// Create the js function you need {return a+b;}// Specifies the SQL function's name, the number of it's arguments, and the js function to usedb;// Run a query in which the function is useddb; // Inserts 10 and 'Hello world' // free the memory used by the statementstmt;// You can not use your statement anymore once it has been freed.// But not freeing your statements causes memory leaks. You don't want that. // Export the database to an Uint8Array containing the SQLite database filevar binaryArray = db;
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