WebSQL API Implementation for Node.js

2.0.3 · maintenance · verified Wed Apr 22

This `websql` package provides a Node.js implementation of the deprecated WebSQL Database API, leveraging `sqlite3` for its backend operations. It enables developers to run code originally designed for browser-based WebSQL environments within a Node.js context, and also falls back to `window.openDatabase` when used in a browser via bundlers like Browserify or Webpack. The current stable version is 2.0.3, with no explicit release cadence specified, indicating a focus on stability and compatibility rather than active feature development. Its primary differentiator is its strict adherence to the existing WebSQL API as implemented in browsers, acting as a bridge for legacy applications rather than extending the standard. It specifically *does not* aim to invent new APIs, support features like BLOBs or encryption, or enhance WebSQL beyond its original scope, focusing instead on accurate emulation for compatibility.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to open a WebSQL database, create a table, insert data, and query it using the `transaction` API.

import openDatabase from 'websql'; // Or `const openDatabase = require('websql');` for CommonJS

async function initializeDatabase() {
  // Create a SQLite3 database file called 'mydb.db'
  const db = openDatabase('mydb.db', '1.0', 'My Test Database', 1 * 1024 * 1024); // Size in bytes

  console.log('Database opened successfully.');

  // Perform a transaction to create a table and insert data
  return new Promise((resolve, reject) => {
    db.transaction((tx) => {
      tx.executeSql(
        'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)',
        [],
        () => {
          console.log('Table "users" created or already exists.');
          tx.executeSql(
            'INSERT INTO users (name, age) VALUES (?, ?)',
            ['Alice', 30],
            () => {
              console.log('Inserted Alice.');
              tx.executeSql(
                'INSERT INTO users (name, age) VALUES (?, ?)',
                ['Bob', 24],
                () => {
                  console.log('Inserted Bob.');
                  tx.executeSql(
                    'SELECT * FROM users',
                    [],
                    (_, result) => {
                      console.log('Users:', Array.from(result.rows).map(row => row));
                      resolve(true);
                    },
                    (_, error) => {
                      console.error('Error selecting users:', error.message);
                      reject(error);
                    }
                  );
                },
                (_, error) => {
                  console.error('Error inserting Bob:', error.message);
                  reject(error);
                }
              );
            },
            (_, error) => {
              console.error('Error inserting Alice:', error.message);
              reject(error);
            }
          );
        },
        (_, error) => {
          console.error('Error creating table:', error.message);
          reject(error);
        }
      );
    });
  });
}

initializeDatabase().catch(console.error);

view raw JSON →