Database-js SQLite Driver
database-js-sqlite serves as a wrapper for the `node-sqlite3` package, providing SQLite database connectivity for applications utilizing the `database-js` framework. It enables a consistent API for interacting with SQLite databases, including support for prepared statements and async/await operations. Additionally, it offers an alternative driver option for `sql.js` for browser-compatible SQLite usage. The current stable version is 1.3.0, released in 2018. Given the lack of recent updates, the package appears to be in a maintenance or effectively abandoned state, meaning release cadence is irregular or ceased, and compatibility with very recent Node.js versions or security patches might be a concern. Its primary differentiator is its integration within the `database-js` ecosystem, providing a unified interface across different database types.
Common errors
-
Error: Cannot find module 'database-js2'
cause The core `database-js` library was incorrectly referred to as `database-js2` in older documentation or examples.fixChange your import statement from `require('database-js2')` to `require('database-js')` or `import { Connection } from 'database-js';`. -
Error: NAPI_VERSION_MISMATCH (node-sqlite3)
cause The native `node-sqlite3` module was compiled against a different Node.js N-API version than the one currently running, often due to Node.js version changes or corrupted `node_modules`.fixDelete `node_modules` and `package-lock.json`, then reinstall: `rm -rf node_modules package-lock.json && npm install`. If issues persist, ensure `node-gyp` is configured correctly or try rebuilding `node-sqlite3` specifically using `npm rebuild node-sqlite3`. -
Error: A database-js driver for 'sqlite' could not be found.
cause The `database-js-sqlite` package, which registers the 'sqlite' driver for `database-js`, is not installed or accessible in your project.fixEnsure `database-js-sqlite` is listed in your dependencies and properly installed: `npm install database-js-sqlite`.
Warnings
- gotcha The README and examples historically referenced `database-js2`. The correct package name for the core library is `database-js`. Using `database-js2` will result in a 'Cannot find module' error.
- breaking This package, and its core dependency `database-js`, have not seen significant updates since 2018-2019. This means it may not be compatible with newer Node.js versions (e.g., > v16) or may lack support for modern JavaScript features or API changes.
- gotcha The underlying `node-sqlite3` package is a native module, which can cause installation issues (e.g., `node-gyp` errors) if build tools are not properly configured on your system or if prebuilt binaries are unavailable for your specific OS/Node.js architecture.
- gotcha When specifying the `sql.js` driver, ensure `sql.js` is installed as a dependency. Without it, the connection attempt will fail when `driver=sql.js` is used.
Install
-
npm install database-js-sqlite -
yarn add database-js-sqlite -
pnpm add database-js-sqlite
Imports
- Connection
var Database = require('database-js2').Connection;import { Connection } from 'database-js'; - SQLiteConnection
import { SQLiteConnection } from 'database-js-sqlite';import SQLiteConnection from 'database-js-sqlite';
- Database (CJS)
const Database = require('database-js-sqlite');const { Connection: Database } = require('database-js');
Quickstart
import { Connection } from 'database-js';
// Ensure database-js-sqlite and node-sqlite3 are installed:
// npm install database-js database-js-sqlite node-sqlite3
(async () => {
let connection;
try {
// Connect to an SQLite database file. It will be created if it doesn't exist.
connection = new Connection('sqlite:///test.sqlite');
console.log('Database connection established.');
// Create a table if it doesn't exist
await connection.query(
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, user_name TEXT, email TEXT);"
);
console.log('Table 'users' ensured.');
// Insert data using a prepared statement
let statement = await connection.prepareStatement("INSERT INTO users (user_name, email) VALUES (?, ?)");
await statement.query('john_doe', 'john@example.com');
await statement.query('jane_smith', 'jane@example.com');
console.log('Users inserted.');
// Query data using a prepared statement
statement = await connection.prepareStatement("SELECT * FROM users WHERE user_name = ?");
const rows = await statement.query('john_doe');
console.log('Query result for john_doe:', rows);
} catch (error) {
console.error('Database operation failed:', error);
} finally {
if (connection) {
await connection.close();
console.log('Database connection closed.');
}
}
})();