Registry / database / database-query

database-query

JSON →
library1.1.22jsnpmunverified

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.

npm install database-query
INSTALL
IMPORT
SIG · DATABASE-QUERY
D
database-query
databasejavascriptv1.1.22
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DataBase
import { DataBase } from 'database-query';
const DataBase = require('database-query');
The primary class for managing database connections and queries. ESM import is recommended in modern Node.js environments; CommonJS is also supported.
IDataBaseConfig
import { IDataBaseConfig } from 'database-query';
TypeScript interface for configuring database connections, useful for type-safe configuration objects.
DataBaseType
import { DataBaseType } from 'database-query';
TypeScript enum or type for specifying the database type (e.g., 'mysql', 'pgsql') in the configuration.

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();
Debug
Known issues
gotchaThe `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.
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`.
affects: >=1.0.0
gotchaWhile `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.
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.
affects: >=1.0.0
gotchaThis 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.
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.
affects: >=1.0.0
gotchaAll 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`).
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.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'mysql' (or 'pg', 'mssql', etc.)
Although `database-query` lists all drivers as dependencies, sometimes a specific driver might fail to install or be missing from the environment.
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`.
Error: connect ECONNREFUSED <host>:<port>
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.
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.
Error: Access denied for user 'youruser'@'localhost' (using password: YES)
The provided username or password in the database configuration is incorrect, or the user lacks necessary permissions from the specified host.
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.
Upgrade
Version history
1.1.22latest on npm
Audit
Dependencies
mysqlrequiredRequired for connecting to MySQL databases.
pgrequiredRequired for connecting to PostgreSQL databases.
mssqlrequiredRequired for connecting to Microsoft SQL Server databases.
oracledbrequiredRequired for connecting to Oracle databases. Note: This driver often requires native build tools and Oracle Instant Client libraries.
mongodbrequiredRequired for connecting to MongoDB databases.
clickhouserequiredRequired for connecting to ClickHouse databases.
Agent activity
8 hits · last 30 days
node
8
Resources
database-query — npm install database-query · libregistry