{"id":17200,"library":"database-query","title":"Multi-Database Query Library","description":"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.","status":"active","version":"1.1.22","language":"javascript","source_language":"en","source_url":"https://github.com/Apipost-Team/database-query","tags":["javascript","mysql","goldendb","mssql","Sql Server","oracle","clickhouse","mongodb","PostgreSQL"],"install":[{"cmd":"npm install database-query","lang":"bash","label":"npm"},{"cmd":"yarn add database-query","lang":"bash","label":"yarn"},{"cmd":"pnpm add database-query","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for connecting to MySQL databases.","package":"mysql","optional":false},{"reason":"Required for connecting to PostgreSQL databases.","package":"pg","optional":false},{"reason":"Required for connecting to Microsoft SQL Server databases.","package":"mssql","optional":false},{"reason":"Required for connecting to Oracle databases. Note: This driver often requires native build tools and Oracle Instant Client libraries.","package":"oracledb","optional":false},{"reason":"Required for connecting to MongoDB databases.","package":"mongodb","optional":false},{"reason":"Required for connecting to ClickHouse databases.","package":"clickhouse","optional":false}],"imports":[{"note":"The primary class for managing database connections and queries. ESM import is recommended in modern Node.js environments; CommonJS is also supported.","wrong":"const DataBase = require('database-query');","symbol":"DataBase","correct":"import { DataBase } from 'database-query';"},{"note":"TypeScript interface for configuring database connections, useful for type-safe configuration objects.","symbol":"IDataBaseConfig","correct":"import { IDataBaseConfig } from 'database-query';"},{"note":"TypeScript enum or type for specifying the database type (e.g., 'mysql', 'pgsql') in the configuration.","symbol":"DataBaseType","correct":"import { DataBaseType } from 'database-query';"}],"quickstart":{"code":"import { DataBase, DataBaseType } from 'database-query';\n\nconst runQuery = async () => {\n  const config = {\n    type: DataBaseType.MySql,\n    host: process.env.DB_MYSQL_HOST ?? 'localhost',\n    port: parseInt(process.env.DB_MYSQL_PORT ?? '3306', 10),\n    user: process.env.DB_MYSQL_USER ?? 'root',\n    password: process.env.DB_MYSQL_PASSWORD ?? 'password',\n    database: process.env.DB_MYSQL_DATABASE ?? 'testdb',\n    connectionLimit: 10 // Example for MySQL, other DBs might have different pool options\n  };\n\n  let db: DataBase | null = null;\n  try {\n    db = new DataBase(config);\n    await db.connect();\n    console.log('Successfully connected to MySQL.');\n\n    const query = 'SELECT * FROM users WHERE age > ?';\n    const params = [25];\n    const results = await db.query(query, params);\n    console.log('Query results:', results);\n\n    // Example for an insert operation\n    const insertQuery = 'INSERT INTO logs (message) VALUES (?)';\n    const insertParams = ['User query executed successfully.'];\n    const insertResult = await db.execute(insertQuery, insertParams);\n    console.log('Insert result:', insertResult);\n\n  } catch (error) {\n    console.error('Database operation failed:', error);\n  } finally {\n    if (db) {\n      await db.close();\n      console.log('Database connection closed.');\n    }\n  }\n};\n\nrunQuery();","lang":"typescript","description":"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."},"warnings":[{"fix":"Refer to the official `oracledb` documentation for detailed installation prerequisites and troubleshooting. Ensure your build environment has the necessary tools and libraries configured before installing `database-query`.","message":"The `oracledb` driver, included as a dependency, often requires native build tools (like `gcc` on Linux or Xcode command line tools on macOS) and the Oracle Instant Client libraries to be installed on the system where the package is used. This can lead to complex installation issues, particularly in containerized environments or on different operating systems.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always consult the official documentation for the specific database driver (e.g., `pg`, `mssql`, `mysql`) to understand database-specific configuration options. Test connections thoroughly with minimal configurations first.","message":"While `database-query` provides a unified API, the underlying connection configurations (e.g., specific options for connection pooling, SSL/TLS, timeouts) can still vary significantly between database types. Misconfigured options for a specific database can lead to connection failures or unexpected behavior.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use parameterized queries (`db.query('SELECT * FROM users WHERE id = ?', [userId])`) to prevent SQL injection. Avoid directly embedding user-provided input into SQL strings.","message":"This library primarily focuses on direct query execution. While it supports parameterized queries (which protect against SQL injection), constructing queries via string concatenation without proper escaping or parameterization is a major security vulnerability.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If you encounter installation issues or wish to minimize dependencies, consider managing specific database drivers yourself and manually passing their connection details or client instances if the library's API allows, or investigate alternatives that allow explicit installation of only desired drivers. Otherwise, accept the bundled dependencies.","message":"All supported database drivers (mysql, pg, mssql, oracledb, mongodb, clickhouse) are listed as direct dependencies in `package.json`. This means `npm install database-query` will attempt to install all of them, even if you only intend to use one. This can lead to increased install times, package bloat, and potential native module compilation failures for unused drivers (especially `oracledb`).","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Manually try `npm install <missing-driver-package>` (e.g., `npm install mysql`) to ensure the required database driver is present in your `node_modules`.","cause":"Although `database-query` lists all drivers as dependencies, sometimes a specific driver might fail to install or be missing from the environment.","error":"Error: Cannot find module 'mysql' (or 'pg', 'mssql', etc.)"},{"fix":"Verify the `host` and `port` in your database configuration. Ensure the database server is running and accessible from the machine running your application. Check firewall rules on both client and server.","cause":"The application could not establish a connection to the database server. This usually indicates an incorrect host, port, database server not running, or a firewall blocking the connection.","error":"Error: connect ECONNREFUSED <host>:<port>"},{"fix":"Double-check your `user` and `password` credentials. Ensure the database user has permissions to connect from the `host` where your application is running, and to the `database` you specified.","cause":"The provided username or password in the database configuration is incorrect, or the user lacks necessary permissions from the specified host.","error":"Error: Access denied for user 'youruser'@'localhost' (using password: YES)"}],"ecosystem":"npm","meta_description":null}