Immutable MariaDB/MySQL Driver
This package is a Node.js database driver designed to interface with MariaDB and MySQL, specifically adapted for 'immutable-core' concepts, meaning it encourages or facilitates an append-only data pattern. It requires Node.js v7.6.0 or higher for native async/await support. However, `immutable-database-mariasql` appears to be a wrapper around the `mariasql` package, which itself is highly outdated, having been last published over a decade ago (version 0.2.6, April 2016). Due to its reliance on a deprecated and unmaintained underlying library, this package is effectively abandoned and should not be used for new development. Its 'immutable' aspect is likely implemented at the application or wrapper level, as MariaDB/MySQL are not inherently immutable databases. For modern Node.js applications connecting to MariaDB or MySQL, the actively maintained `@mariadb/mariadb-connector-nodejs` (npm `mariadb`) is the recommended alternative.
Common errors
-
TypeError: client.connect is not a function
cause The `Client` instance was not properly initialized or the method name is incorrect for the version/wrapper.fixEnsure `new Client()` is used and refer to the specific API of `immutable-database-mariasql` if it deviates from standard `mariasql` connection methods. The original `mariasql` uses `client.connect()` with a callback, or implicitly connects on query if auto-connect is enabled. -
Error: Packets out of order. Got: X Expected: Y
cause This error often indicates issues with the MariaDB/MySQL server connection, network instability, or protocol mismatches, especially with older drivers on newer servers or vice versa.fixCheck network connectivity to the database server. Ensure the database server is running and accessible. Verify firewall rules. Consider upgrading or downgrading Node.js and/or the database server version to find a compatible combination if sticking with this legacy driver. -
Error: Cannot find module 'mariasql'
cause The underlying `mariasql` package, which `immutable-database-mariasql` depends on, is not installed.fixRun `npm install mariasql` alongside `npm install immutable-database-mariasql` if `mariasql` is a peer dependency or not properly declared in the wrapper's `package.json`. It's safer to avoid this package entirely and use a modern driver.
Warnings
- breaking This package relies on the `mariasql` library, which was last updated in April 2016 and is no longer maintained. It has known vulnerabilities and lacks support for modern database features or Node.js versions.
- gotcha The package officially requires Node.js v7.6.0 or greater. Running it on modern Node.js versions (e.g., Node.js 16+) may encounter compatibility issues, unexpected behavior, or errors due to breaking changes in Node.js core or underlying dependencies.
- gotcha Despite 'immutable' in its name, this package does not provide native immutable database features. It is a driver for MariaDB/MySQL (mutable by default) and facilitates an application-level immutable pattern (e.g., append-only logs) rather than enforcing cryptographic immutability like dedicated immutable databases (e.g., immudb, Dolt).
- deprecated The `immutable-database-mariasql` package itself is considered abandoned due to the deprecation of its core `mariasql` dependency and lack of recent updates or maintenance. It poses security risks and functional limitations for modern applications.
Install
-
npm install immutable-database-mariasql -
yarn add immutable-database-mariasql -
pnpm add immutable-database-mariasql
Imports
- Client
import { Client } from 'immutable-database-mariasql';const { Client } = require('immutable-database-mariasql'); - ConnectionOptions
// Type definitions are generally not available for this package. Refer to documentation for object structure.
Quickstart
const { Client } = require('immutable-database-mariasql');
const client = new Client({
host: process.env.DB_HOST ?? 'localhost',
user: process.env.DB_USER ?? 'root',
password: process.env.DB_PASSWORD ?? '',
db: process.env.DB_NAME ?? 'test_db',
charset: 'utf8mb4'
});
async function runImmutableExample() {
try {
await client.connect();
console.log('Connected to MariaDB/MySQL.');
// Create a table for immutable logs if it doesn't exist (example for 'immutable' pattern)
await client.query(`
CREATE TABLE IF NOT EXISTS audit_log (
id INT AUTO_INCREMENT PRIMARY KEY,
action VARCHAR(255) NOT NULL,
user_id INT NOT NULL,
timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
data JSON,
superseded_id INT NULL
);
`);
console.log('Ensured audit_log table exists.');
// Demonstrate 'immutable' insert: adding a new record instead of updating
const insertResult = await client.query(
'INSERT INTO audit_log (action, user_id, data) VALUES (?, ?, ?)',
['USER_CREATED', 101, JSON.stringify({ username: 'alice', email: 'alice@example.com' })]
);
console.log('New user log entry:', insertResult.info.affectedRows);
// When data conceptually 'changes', a new log entry is created.
// The previous entry can be referenced as 'superseded'.
const previousLog = (await client.query('SELECT id FROM audit_log WHERE user_id = ? ORDER BY id DESC LIMIT 1', [101]))[0];
const updateLogResult = await client.query(
'INSERT INTO audit_log (action, user_id, data, superseded_id) VALUES (?, ?, ?, ?)',
['USER_EMAIL_UPDATED', 101, JSON.stringify({ email: 'alice.new@example.com' }), previousLog ? previousLog.id : null]
);
console.log('User email update log entry:', updateLogResult.info.affectedRows);
// Querying the full history for a user
const history = await client.query('SELECT * FROM audit_log WHERE user_id = ? ORDER BY timestamp ASC', [101]);
console.log('\nFull audit history for user 101:');
history.forEach(entry => console.log(entry));
} catch (err) {
console.error('Database operation failed:', err);
} finally {
if (client.connected) {
await client.end();
console.log('Connection closed.');
}
}
}
runImmutableExample();