{"id":17196,"library":"database-js2","title":"Database-js2 Common Database Interface","description":"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.","status":"deprecated","version":"1.4.2","language":"javascript","source_language":"en","source_url":"https://github.com/mlaanderson/database-js","tags":["javascript","database","database-js","adodb","firebase","ini","mysql","postgres"],"install":[{"cmd":"npm install database-js2","lang":"bash","label":"npm"},{"cmd":"yarn add database-js2","lang":"bash","label":"yarn"},{"cmd":"pnpm add database-js2","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for connecting to SQLite databases. Must be installed separately.","package":"database-js-sqlite","optional":true},{"reason":"Required for connecting to MySQL databases. Must be installed separately.","package":"database-js-mysql","optional":true},{"reason":"Required for connecting to PostgreSQL databases. Must be installed separately.","package":"database-js-postgresql","optional":true}],"imports":[{"note":"This package (`database-js2` v1.x) is CommonJS-only. Attempting to use ESM `import` will result in a runtime error or unexpected behavior, as it does not export named ESM modules.","wrong":"import { Connection } from 'database-js2';","symbol":"Connection","correct":"const Connection = require('database-js2').Connection;"}],"quickstart":{"code":"const Connection = require('database-js2').Connection;\nconst fs = require('fs');\nconst path = require('path');\n\nconst dbPath = path.join(__dirname, 'test.sqlite');\n\n// Create a dummy SQLite database file if it doesn't exist\nif (!fs.existsSync(dbPath)) {\n    fs.writeFileSync(dbPath, '');\n}\n\n// Ensure the SQLite driver is installed: npm install database-js-sqlite\n// If using 'mysql' or 'postgres', install their respective drivers.\n\nconst conn = new Connection(`sqlite:///${dbPath}`);\n\nasync function runExample() {\n    try {\n        // Create a table if it doesn't exist\n        await conn.prepareStatement(\"CREATE TABLE IF NOT EXISTS states (id INTEGER PRIMARY KEY, name TEXT)\").query();\n        console.log(\"Table 'states' ensured.\");\n\n        // Insert some data\n        await conn.prepareStatement(\"INSERT INTO states (name) VALUES (?)\").query(\"California\");\n        await conn.prepareStatement(\"INSERT INTO states (name) VALUES (?)\").query(\"Texas\");\n        await conn.prepareStatement(\"INSERT INTO states (name) VALUES (?)\").query(\"Florida\");\n        console.log(\"Data inserted.\");\n\n        // Query data with a prepared statement\n        const statement = conn.prepareStatement(\"SELECT * FROM states WHERE name = ?\");\n        const results = await statement.query(\"Texas\");\n        console.log(\"Query Results for 'Texas':\", results);\n\n        // Close the connection\n        await conn.close();\n        console.log(\"Connection closed. Example finished.\");\n    } catch (reason) {\n        console.error(\"Error during example execution:\", reason);\n        if (conn) {\n            try {\n                await conn.close();\n            } catch (closeReason) {\n                console.error(\"Error closing connection:\", closeReason);\n            }\n        }\n    }\n}\n\nrunExample();\n","lang":"javascript","description":"This example demonstrates how to establish a connection to an SQLite database, create a table, insert data using prepared statements, query specific data, and properly close the connection using async/await."},"warnings":[{"fix":"Migrate your projects to use the `database-js` package (npm install database-js) and update your code to its API, which is largely compatible but may have minor breaking changes in newer major versions.","message":"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+).","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Use `const MySymbol = require('database-js2').MySymbol;` for all imports. For new projects, consider migrating to the `database-js` package which offers better ESM support.","message":"This package is CommonJS-only. Direct `import` statements (ESM) are not supported. Attempting to use ESM imports will lead to module resolution errors.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Before establishing a connection, ensure the corresponding driver package for your database type is installed via npm. For example, for `sqlite:///`, ensure `database-js-sqlite` is installed.","message":"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.","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":"Install the required driver package for your database type: `npm install database-js-sqlite` (for SQLite), `npm install database-js-mysql` (for MySQL), etc.","cause":"The specific database driver package (e.g., `database-js-sqlite`) has not been installed.","error":"Error: Driver not found for scheme 'sqlite'"},{"fix":"Ensure you are using `const Connection = require('database-js2').Connection;` for CommonJS environments.","cause":"The `Connection` symbol was not correctly imported, likely due to mixing CommonJS `require` with incorrect property access, or attempting to use ESM `import`.","error":"TypeError: Cannot read properties of undefined (reading 'Connection')"},{"fix":"Always chain a `.catch()` method to your promise-based database operations, or use `try...catch` blocks with `async/await` to handle potential errors gracefully.","cause":"A Promise returned by a database operation (`query`, `close`, etc.) was rejected, and the rejection was not handled with a `.catch()` block.","error":"UnhandledPromiseRejectionWarning: (some database error message)"}],"ecosystem":"npm","meta_description":null}