Database-js SQLite Driver

1.3.0 · maintenance · verified Wed Apr 22

database-js-sqlite serves as a wrapper for the `node-sqlite3` package, providing SQLite database connectivity for applications utilizing the `database-js` framework. It enables a consistent API for interacting with SQLite databases, including support for prepared statements and async/await operations. Additionally, it offers an alternative driver option for `sql.js` for browser-compatible SQLite usage. The current stable version is 1.3.0, released in 2018. Given the lack of recent updates, the package appears to be in a maintenance or effectively abandoned state, meaning release cadence is irregular or ceased, and compatibility with very recent Node.js versions or security patches might be a concern. Its primary differentiator is its integration within the `database-js` ecosystem, providing a unified interface across different database types.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to an SQLite database, creating a table, inserting data, and querying using prepared statements with the `database-js` API.

import { Connection } from 'database-js';

// Ensure database-js-sqlite and node-sqlite3 are installed:
// npm install database-js database-js-sqlite node-sqlite3

(async () => {
    let connection;
    try {
        // Connect to an SQLite database file. It will be created if it doesn't exist.
        connection = new Connection('sqlite:///test.sqlite');
        console.log('Database connection established.');

        // Create a table if it doesn't exist
        await connection.query(
            "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, user_name TEXT, email TEXT);"
        );
        console.log('Table 'users' ensured.');

        // Insert data using a prepared statement
        let statement = await connection.prepareStatement("INSERT INTO users (user_name, email) VALUES (?, ?)");
        await statement.query('john_doe', 'john@example.com');
        await statement.query('jane_smith', 'jane@example.com');
        console.log('Users inserted.');

        // Query data using a prepared statement
        statement = await connection.prepareStatement("SELECT * FROM users WHERE user_name = ?");
        const rows = await statement.query('john_doe');
        console.log('Query result for john_doe:', rows);

    } catch (error) {
        console.error('Database operation failed:', error);
    } finally {
        if (connection) {
            await connection.close();
            console.log('Database connection closed.');
        }
    }
})();

view raw JSON →