PostgreSQL Wrapper for database-js

1.1.3 · abandoned · verified Wed Apr 22

database-js-postgres is a wrapper library designed to integrate PostgreSQL databases with the database-js framework. It utilizes the widely adopted node-postgres package under the hood to handle core database interactions, exposing its functionality through a consistent promise-based API, even when used independently of database-js. The library reached its latest stable version, 1.1.3, in late 2017. Given the lack of updates or commits since then, its release cadence is effectively ceased, and the project is considered unmaintained. Its primary differentiation was to provide a standardized interface within the database-js ecosystem, enabling a uniform approach to various SQL databases. However, its dependency on outdated Node.js versions and its CommonJS-only structure limit its applicability in modern JavaScript environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to a PostgreSQL database, executing a SELECT query with parameters, performing an INSERT operation, and properly closing the connection using the standalone promise-based API.

const postgres = require('database-js-postgres');

(async () => {
    let connection, rows;

    // Establish a connection to the PostgreSQL database
    connection = postgres.open({
        Hostname: 'localhost',
        Port: 5432,
        Username: 'my_secret_username',
        Password: 'my_secret_password',
        Database: 'my_top_secret_database'
    });
    
    try {
        // Execute a query and fetch results
        rows = await connection.query("SELECT id, name FROM users WHERE email = ?", ['example@email.com']);
        console.log('Query Results:', rows);

        // Example of an insert statement
        const insertResult = await connection.query("INSERT INTO logs (message) VALUES (?) RETURNING id", ['User logged in successfully']);
        console.log('Insert Result:', insertResult);

    } catch (error) {
        console.error('Database Operation Error:', error);
    } finally {
        // Always ensure the connection is closed
        await connection.close();
        console.log('Connection closed.');
    }
})();

view raw JSON →