Multi-Database Query Library

1.1.22 · active · verified Wed Apr 22

The `database-query` package (current stable version 1.1.22) provides a unified, lightweight interface for connecting to and executing queries against a variety of popular SQL and NoSQL databases. It supports MySQL, PostgreSQL, MS SQL Server, Oracle, MongoDB, and ClickHouse. This library acts as an abstraction layer, aiming to simplify interaction with diverse database systems through a consistent API, rather than requiring developers to learn the specific nuances of each underlying database driver. Its core functionality focuses on direct query execution and connection management, distinguishing it from full-fledged ORMs or complex query builders. The project, maintained by the Apipost-Team, has a moderate release cadence, with the last significant update in October 2023, indicating an active or maintenance status. Its primary differentiator lies in offering broad multi-database support under a single, streamlined API, facilitating integration in applications that require connectivity to multiple data stores.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `database-query` for a MySQL connection, execute a parameterized SELECT query, and perform an INSERT operation, including proper connection management with environment variables.

import { DataBase, DataBaseType } from 'database-query';

const runQuery = async () => {
  const config = {
    type: DataBaseType.MySql,
    host: process.env.DB_MYSQL_HOST ?? 'localhost',
    port: parseInt(process.env.DB_MYSQL_PORT ?? '3306', 10),
    user: process.env.DB_MYSQL_USER ?? 'root',
    password: process.env.DB_MYSQL_PASSWORD ?? 'password',
    database: process.env.DB_MYSQL_DATABASE ?? 'testdb',
    connectionLimit: 10 // Example for MySQL, other DBs might have different pool options
  };

  let db: DataBase | null = null;
  try {
    db = new DataBase(config);
    await db.connect();
    console.log('Successfully connected to MySQL.');

    const query = 'SELECT * FROM users WHERE age > ?';
    const params = [25];
    const results = await db.query(query, params);
    console.log('Query results:', results);

    // Example for an insert operation
    const insertQuery = 'INSERT INTO logs (message) VALUES (?)';
    const insertParams = ['User query executed successfully.'];
    const insertResult = await db.execute(insertQuery, insertParams);
    console.log('Insert result:', insertResult);

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

runQuery();

view raw JSON →