any-db
raw JSON → 2.2.1 verified Sat Apr 25 auth: no javascript maintenance
any-db is a database-agnostic abstraction layer for Node.js that provides consistent APIs for connection pooling, querying, and transactions across multiple SQL database backends including PostgreSQL, MySQL, SQLite3, and MSSQL. Version 2.2.1 is the latest stable release. The library emphasizes minimal opinionation, preserving driver-specific behavior while offering a unified interface. It supports streaming query results, bound parameters via ?, and event-based error handling. Unlike ORMs, any-db focuses on low-level database access with flexible connection and pool management. Its adapters are installed separately and have any-db as a peer dependency. The library ships only CommonJS modules and does not provide TypeScript definitions.
Common errors
error Error: Cannot find module 'any-db-postgres' ↓
cause The required adapter is not installed.
fix
npm install any-db-postgres
error TypeError: anyDB.createConnection is not a function ↓
cause Using wrong import style: named export instead of default export.
fix
Use import anyDB from 'any-db' instead of import { anyDB } from 'any-db'.
error Error: listen EADDRINUSE: address already in use :::5432 ↓
cause Port conflict, often due to multiple instances or leftover connections.
fix
Kill existing processes or use a different port in the connection URL.
Warnings
deprecated any-db is no longer actively maintained; consider using knex or sequelize. ↓
fix Migrate to a modern database abstraction library like knex.
gotcha Adapter packages must be installed separately; any-db alone does not connect to any database. ↓
fix Install an adapter like 'any-db-postgres' and ensure it's in your package.json.
gotcha The library ships only CommonJS; ESM imports may require bundling or Node --experimental-require-module. ↓
fix Use require() or configure a bundler to handle CJS.
breaking Version 2.0.0 changed stream behavior: query() now returns a readable stream instead of emitting 'row' events. ↓
fix Use .on('data') or .pipe() to consume rows; for old event listeners, upgrade code.
gotcha Connection URL parsing may fail for non-standard ports or special characters; use object syntax for reliability. ↓
fix Pass an object with adapter, host, port, database, user, password instead of a URL string.
Install
npm install any-db yarn add any-db pnpm add any-db Imports
- default wrong
const anyDB = require('any-db')correctimport anyDB from 'any-db' - createConnection wrong
const { createConnection } = require('any-db')correctimport { createConnection } from 'any-db' - createPool wrong
import createPool from 'any-db'correctimport { createPool } from 'any-db'
Quickstart
import anyDB from 'any-db';
// Assumes any-db-postgres is installed
const url = 'postgres://user:password@localhost/mydb';
const conn = anyDB.createConnection(url);
conn.query('SELECT * FROM users WHERE id = ?', [1], (err, result) => {
if (err) throw err;
console.log(result.rows);
});
conn.on('error', (err) => console.error('Connection error:', err));
const pool = anyDB.createPool(url, { min: 2, max: 5 });
pool.query('SELECT 1', (err, res) => {
console.log(res.rows);
pool.close();
});