Database-js2 Common Database Interface
database-js2 is a JavaScript library providing a common, promise-based interface for accessing various database systems, inspired by Java's JDBC (Java Database Connectivity) pattern. It allows developers to interact with different databases (e.g., SQLite, MySQL, PostgreSQL) and even non-SQL data sources like Firebase, INI files, or Excel/CSV sheets using a unified API and connection string format. The library supports built-in prepared statements and integrates well with ES7 async/await syntax via Promises. This specific package, `database-js2`, is currently at version 1.4.2 but has been officially deprecated. All new feature development has ceased for this package, and future development and bug fixes are being published under the new `database-js` package (v3.x and above), which has claimed the original `database-js` npm package name.
Common errors
-
Error: Driver not found for scheme 'sqlite'
cause The specific database driver package (e.g., `database-js-sqlite`) has not been installed.fixInstall the required driver package for your database type: `npm install database-js-sqlite` (for SQLite), `npm install database-js-mysql` (for MySQL), etc. -
TypeError: Cannot read properties of undefined (reading 'Connection')
cause The `Connection` symbol was not correctly imported, likely due to mixing CommonJS `require` with incorrect property access, or attempting to use ESM `import`.fixEnsure you are using `const Connection = require('database-js2').Connection;` for CommonJS environments. -
UnhandledPromiseRejectionWarning: (some database error message)
cause A Promise returned by a database operation (`query`, `close`, etc.) was rejected, and the rejection was not handled with a `.catch()` block.fixAlways chain a `.catch()` method to your promise-based database operations, or use `try...catch` blocks with `async/await` to handle potential errors gracefully.
Warnings
- deprecated The `database-js2` package is officially deprecated. It will only receive critical bug fixes and no new features. All new development has moved to the `database-js` package (v3.x+).
- breaking This package is CommonJS-only. Direct `import` statements (ESM) are not supported. Attempting to use ESM imports will lead to module resolution errors.
- gotcha Drivers for specific databases (e.g., MySQL, PostgreSQL, SQLite) are not bundled with `database-js2`. You must install the relevant driver package separately (e.g., `npm install database-js-sqlite`) for your chosen database schema.
Install
-
npm install database-js2 -
yarn add database-js2 -
pnpm add database-js2
Imports
- Connection
import { Connection } from 'database-js2';const Connection = require('database-js2').Connection;
Quickstart
const Connection = require('database-js2').Connection;
const fs = require('fs');
const path = require('path');
const dbPath = path.join(__dirname, 'test.sqlite');
// Create a dummy SQLite database file if it doesn't exist
if (!fs.existsSync(dbPath)) {
fs.writeFileSync(dbPath, '');
}
// Ensure the SQLite driver is installed: npm install database-js-sqlite
// If using 'mysql' or 'postgres', install their respective drivers.
const conn = new Connection(`sqlite:///${dbPath}`);
async function runExample() {
try {
// Create a table if it doesn't exist
await conn.prepareStatement("CREATE TABLE IF NOT EXISTS states (id INTEGER PRIMARY KEY, name TEXT)").query();
console.log("Table 'states' ensured.");
// Insert some data
await conn.prepareStatement("INSERT INTO states (name) VALUES (?)").query("California");
await conn.prepareStatement("INSERT INTO states (name) VALUES (?)").query("Texas");
await conn.prepareStatement("INSERT INTO states (name) VALUES (?)").query("Florida");
console.log("Data inserted.");
// Query data with a prepared statement
const statement = conn.prepareStatement("SELECT * FROM states WHERE name = ?");
const results = await statement.query("Texas");
console.log("Query Results for 'Texas':", results);
// Close the connection
await conn.close();
console.log("Connection closed. Example finished.");
} catch (reason) {
console.error("Error during example execution:", reason);
if (conn) {
try {
await conn.close();
} catch (closeReason) {
console.error("Error closing connection:", closeReason);
}
}
}
}
runExample();