Registry / database / rtree-sql.js

rtree-sql.js

JSON →
library1.7.0jsnpmunverified

A fork of sql.js with R-Tree spatial indexing support. Version 1.7.0 (stable). This library compiles SQLite to JavaScript via Emscripten, enabling in-memory SQL databases in browsers and Node.js. Key differentiator: includes SQLite R-Tree extension for spatial queries, which the upstream sql.js does not enable by default. Same API as sql.js: create, query, export databases using pure JavaScript. Release cadence is sporadic, tied to upstream updates and patches for the R-Tree feature. Not to be confused with native SQLite3 bindings for Node.js; this is a pure in-memory implementation loading entire DB into memory.

npm install rtree-sql.js
INSTALL
IMPORT
SIG · RTREE-SQL.JS
R
rtree-sql.js
databasejavascriptv1.7.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

initSqlJs
✓ const initSqlJs = require('rtree-sql.js');
✗ import initSqlJs from 'rtree-sql.js';
Default export is a factory function. CommonJS require works; ESM import may need default interop. In browsers, use window.initSqlJs after loading CDN script.
SQL.Database
✓ const db = new SQL.Database();
✗ new Database();
Database is a class under the SQL namespace returned by initSqlJs(). You must await initSqlJs() first.
SQL.Statement
✓ const stmt = db.prepare('SELECT * FROM t');
✗ new SQL.Statement();
Statement objects are created via Database.prepare(), not directly instantiated.

Shows how to load the library, create an in-memory database, create a table, insert data, and query using prepared statements.

const initSqlJs = require('rtree-sql.js'); async function main() { const SQL = await initSqlJs({ locateFile: file => `https://cdn.jsdelivr.net/npm/rtree-sql.js@1.7/dist/${file}` }); const db = new SQL.Database(); db.run("CREATE TABLE points (id INTEGER, x REAL, y REAL);"); db.run("INSERT INTO points VALUES (1, 1.0, 2.0);"); const stmt = db.prepare("SELECT * FROM points;"); while (stmt.step()) { const row = stmt.getAsObject(); console.log(row); } stmt.free(); db.close(); } main();
Debug
Known issues
gotchaDatabase is stored in memory; changes are lost unless you explicitly export the database to a Uint8Array and save it.
fix
Call db.export() to get Uint8Array and persist via localStorage or file download.
affects: >=0.0.0
gotchainitSqlJs() must be called and awaited before creating any database. Calls before initialization will fail.
fix
Ensure you await initSqlJs() at the top level before using SQL.Database.
affects: >=0.0.0
gotchaThe .wasm file must be served alongside the JS. In Node.js it's loaded automatically, but in browsers you must provide locateFile option.
fix
Set locateFile to point to the directory containing sql-wasm.wasm (e.g., from node_modules or CDN).
affects: >=0.0.0
breakingBreaking: In version 1.0, the API changed from synchronous to asynchronous init (initSqlJs returns a Promise). Older code using synchronous init will break.
fix
Wrap code in async function and await initSqlJs(config).
affects: >=1.0
gotchaSQL.Database() constructor can throw if given invalid Uint8Array. Always catch errors.
fix
try { new SQL.Database(data) } catch(e) { ... }
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: SQL is not a constructor
Calling new SQL.Database() before initSqlJs resolves or using uninitialized SQL variable.
fix
Ensure initSqlJs() is awaited: const SQL = await initSqlJs();
Error: Cannot find module 'rtree-sql.js'
Package not installed or import path incorrect.
fix
Run npm install rtree-sql.js and use require('rtree-sql.js').
TypeError: db.prepare is not a function
db was created before initSqlJs completed, or db is already closed.
fix
Always await initSqlJs() first; do not reuse a closed database.
Upgrade
Version history
1.7.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
rtree-sql.js — npm install rtree-sql.js · libregistry