database-js: Unified Database Access Interface

3.0.11 · active · verified Tue Apr 21

database-js provides a common, promise-based interface for SQL database access in JavaScript, inspired by the Java Database Connectivity (JDBC) API. It abstracts away the specifics of various underlying database drivers, allowing developers to interact with different databases (such as MySQL, PostgreSQL, SQLite, MS SQL Server, Firebase, CSV, Excel, JSON, and INI files) using a consistent API and connection string format. The library includes built-in support for prepared statements, even for drivers that don't natively offer them, and is designed to integrate seamlessly with ES7 async/await patterns. The current stable version is 3.0.11, and it receives regular maintenance updates for bug fixes. Its key differentiators include driver agnosticism via connection strings and a consistent promise-based API across heterogeneous data sources.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a database connection, execute DDL, INSERT, UPDATE, and SELECT operations using prepared statements with the promise-based API, and properly close the connection. It highlights the unified interface across different database types.

import { Connection } from 'database-js';
// IMPORTANT: You must also install the specific driver package, e.g., 'npm install database-js-sqlite'

async function runDatabaseOperations() {
  // Using SQLite as an example. Change the connection string
  // and install the relevant driver for other databases.
  const connectionString = "sqlite:///path/to/test.sqlite"; // Or mysql://user:password@localhost/test
  const conn = new Connection(connectionString);

  try {
    // COMMAND: Create a table
    await conn.prepareStatement(
      "CREATE TABLE IF NOT EXISTS city (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, population INTEGER)"
    ).execute();
    console.log('Table created or already exists.');

    // COMMAND: Insert data using a prepared statement
    const insertStmt = conn.prepareStatement("INSERT INTO city (name, population) VALUES (?, ?)");
    await insertStmt.execute("Rio de Janeiro", 6747815);
    console.log('Inserted Rio de Janeiro.');

    // QUERY: Select data using a prepared statement
    const queryStmt = conn.prepareStatement("SELECT * FROM city WHERE name = ?");
    const results = await queryStmt.query("New York"); // Assuming 'New York' might exist from previous runs
    console.log('Query results for New York:', results);

    // ANOTHER COMMAND: Update data
    const updateStmt = conn.prepareStatement("UPDATE city SET population = population + ? WHERE name = ?");
    await updateStmt.execute(1, "Rio de Janeiro");
    console.log('Updated Rio de Janeiro population.');

    // QUERY: Select all data
    const allCities = await conn.prepareStatement("SELECT * FROM city").query();
    console.log('All cities:', allCities);

  } catch (reason) {
    console.error('An error occurred:', reason);
  } finally {
    // CLOSING THE CONNECTION
    await conn.close();
    console.log('Connection closed.');
  }
}

runDatabaseOperations();

view raw JSON →