DMDB Database Driver for Node.js

1.0.48286 · active · verified Wed Apr 22

DMDB is a native Node.js driver for the Dameng 8 (DM8) relational database, providing direct database connectivity and interaction. The current stable version is 1.0.48286 (as of March 2026), with a frequent release cadence, often monthly or bi-monthly, addressing bugs, performance, and new features. Key differentiators include its tight integration with the DM8 ecosystem, official support for Node.js versions 12 and above, and extensions for popular ORMs like TypeORM and Knex via `typeorm-dm` and `knex-dm` packages. The driver supports features like connection pooling, statement caching, and optional Snappy compression for internal communication. It also aims for compatibility with OracleDB-like API patterns, which is a significant aspect for developers migrating or working with similar database drivers.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection pool, obtain a connection, execute a simple `SELECT` query, perform an `INSERT` with bind parameters, and properly close connections and the pool. It also includes a basic table setup/teardown.

import dmdb from 'dmdb';

interface MyQueryResult {
  id: number;
  name: string;
}

async function runDbOperations() {
  let connection: dmdb.Connection | undefined;
  let pool: dmdb.Pool | undefined;

  try {
    // Create a connection pool
    pool = await dmdb.createPool({
      user: process.env.DB_USER ?? 'SYSDBA',
      password: process.env.DB_PASSWORD ?? 'SYSDBA',
      connectString: process.env.DB_CONNECT_STRING ?? 'localhost:5236/DAMENG',
      poolMin: 2,
      poolMax: 4,
      poolIncrement: 1,
      poolAlias: 'default'
    });
    console.log('Connection pool created successfully.');

    // Get a connection from the pool
    connection = await pool.getConnection();
    console.log('Connection obtained from pool.');

    // Execute a simple query
    const querySql = 'SELECT 1 AS id, \'Hello DMDB\' AS name FROM DUAL';
    const result: dmdb.Result<MyQueryResult> = await connection.execute(querySql);
    console.log('Query Result:', result.rows?.[0]);

    // Execute a DML statement with bind parameters
    const insertSql = 'INSERT INTO my_test_table (id, value) VALUES (:1, :2)';
    const bindParams = [1001, 'Test Value'];
    const insertResult = await connection.execute(insertSql, bindParams, { autoCommit: true });
    console.log('Rows inserted:', insertResult.rowsAffected); 

    // Execute a query to fetch data from the inserted table
    const fetchSql = 'SELECT id, value FROM my_test_table WHERE id = :id';
    const fetchResult: dmdb.Result<{ id: number; value: string }> = await connection.execute(fetchSql, { id: 1001 });
    console.log('Fetched data:', fetchResult.rows?.[0]);

  } catch (err: any) {
    console.error('Database operation failed:', err.message);
  } finally {
    if (connection) {
      try {
        await connection.close();
        console.log('Connection released back to pool.');
      } catch (closeErr: any) {
        console.error('Error closing connection:', closeErr.message);
      }
    }
    if (pool) {
      try {
        await pool.close();
        console.log('Connection pool closed.');
      } catch (poolCloseErr: any) {
        console.error('Error closing pool:', poolCloseErr.message);
      }
    }
  }
}

// A small helper to create the table if it doesn't exist
async function setupTable() {
  let connection: dmdb.Connection | undefined;
  try {
    connection = await dmdb.getConnection({
      user: process.env.DB_USER ?? 'SYSDBA',
      password: process.env.DB_PASSWORD ?? 'SYSDBA',
      connectString: process.env.DB_CONNECT_STRING ?? 'localhost:5236/DAMENG',
    });
    await connection.execute(`
      BEGIN
        EXECUTE IMMEDIATE 'DROP TABLE my_test_table';
      EXCEPTION
        WHEN OTHERS THEN
          IF SQLCODE != -942 THEN
            RAISE;
          END IF;
      END;
    `);
    await connection.execute(`
      CREATE TABLE my_test_table (
        id NUMBER(10) PRIMARY KEY,
        value VARCHAR2(50)
      )
    `);
    console.log('Table my_test_table ensured.');
  } catch (err: any) {
    console.error('Table setup failed:', err.message);
  } finally {
    if (connection) {
      await connection.close();
    }
  }
}

setupTable().then(() => runDbOperations());

view raw JSON →