Immutable MariaDB/MySQL Driver

1.3.47 · abandoned · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates connecting to a MariaDB/MySQL database and performing append-only operations using the async/await pattern, illustrating how to implement an 'immutable' log for user actions rather than in-place updates.

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();

view raw JSON →