WebSQL API for Node.js (Configurable)

3.0.3 · active · verified Wed Apr 22

websql-configurable is a JavaScript/TypeScript library (version 3.0.3) that implements the WebSQL Database API for Node.js environments, leveraging `node-sqlite3` as its backend. It is a fork of `node-websql` which provides additional configuration options and incorporates TypeScript types from `@types/websql`. This package allows developers to reuse legacy WebSQL-based code in Node.js, facilitating quick testing and bridging existing WebSQL implementations to a Node.js context. When used in a browser environment via bundlers like Browserify or Webpack, it gracefully falls back to the native `window.openDatabase` implementation, subject to current browser support. The library aims for strict compatibility with the existing WebSQL API as found in browsers, focusing on bridging the gap rather than expanding the deprecated standard. Its release cadence is primarily driven by maintenance needs and fixes for its specific niche. A key differentiator is its enhanced configurability for the underlying `sqlite3` driver and the ability to swap custom SQLite3 implementations.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open a file-based or in-memory WebSQL database in Node.js, create a table, insert data, and query it within a transaction using the `websql-configurable` API.

import openDatabase from 'websql-configurable';

// Create a file-based SQLite3 database named 'mydb.db'
const db = openDatabase('mydb.db', '1.0', 'My WebSQL Database', 1024 * 1024);

// Alternatively, create an in-memory database that is temporary
const inMemoryDb = openDatabase(':memory:', '1.0', 'In-memory DB', 1);

console.log('Database opened (or created):', db);

db.transaction((tx) => {
  tx.executeSql(
    'CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, quantity INTEGER)',
    [],
    () => console.log('Table "items" created or already exists.'),
    (tx, error) => console.error('Error creating table:', error.message)
  );

  tx.executeSql(
    'INSERT INTO items (name, quantity) VALUES (?, ?)',
    ['Apple', 5],
    () => console.log('Inserted: Apple'),
    (tx, error) => console.error('Error inserting Apple:', error.message)
  );

  tx.executeSql(
    'INSERT INTO items (name, quantity) VALUES (?, ?)',
    ['Orange', 3],
    () => console.log('Inserted: Orange'),
    (tx, error) => console.error('Error inserting Orange:', error.message)
  );

  tx.executeSql(
    'SELECT * FROM items WHERE quantity > ?', [4],
    (tx, results) => {
      console.log('Items with quantity > 4:');
      for (let i = 0; i < results.rows.length; i++) {
        console.log(`  ID: ${results.rows.item(i).id}, Name: ${results.rows.item(i).name}, Quantity: ${results.rows.item(i).quantity}`);
      }
    },
    (tx, error) => console.error('Error selecting items:', error.message)
  );
}, (error) => {
  console.error('Transaction failed:', error.message);
}, () => {
  console.log('Transaction completed successfully.');
});

view raw JSON →