db

Provides a unified interface for database operations, allowing dynamic loading of different database adapters. This module supports multiple database types by loading the appropriate adapter based on configuration. It provides methods for querying, executing commands, and managing transactions. IMPORTANT: Requires explicit permission to use the module. See docs/permissions-guide for more details.
Description:
  • Provides a unified interface for database operations, allowing dynamic loading of different database adapters. This module supports multiple database types by loading the appropriate adapter based on configuration. It provides methods for querying, executing commands, and managing transactions. IMPORTANT: Requires explicit permission to use the module. See docs/permissions-guide for more details.

Methods

(static) execute(dbName, sql, params) → {Promise.<Object>}

Description:
  • Executes a SQL update/insert/delete command against the specified database.
Example
const result = await db.execute('myDatabase', 'UPDATE users SET active = ? WHERE id = ?', [false, userId]);
console.log(`Rows affected: ${result.rowCount}`);
Parameters:
Name Type Description
dbName string The name of the database to execute the command on.
sql string The SQL insert/update/delete command string.
params Array The parameters for the command.
Throws:
  • If the database connection is not configured or the command fails.
    Type
    Error
  • If the SQL command is invalid or the parameters do not match.
    Type
    Error
Returns:
The result of the execution, typically containing row count or status.
Type
Promise.<Object>

(static) query(dbName, sql, params) → {Promise.<Object>}

Description:
  • Executes a SQL query against the specified database.
Example
const result = await db.query('myDatabase', 'SELECT * FROM users WHERE id = ?', [userId]);
console.log(result);
Parameters:
Name Type Description
dbName string The name of the database to query.
sql string The SQL query string.
params Array The parameters for the query.
Throws:
  • If the database connection is not configured or the query fails.
    Type
    Error
  • If the SQL query is invalid or the parameters do not match.
    Type
    Error
Returns:
The result of the query.
Type
Promise.<Object>

(static) transaction(dbName, callback) → {Promise.<any>}

Description:
  • Executes a transaction with the provided callback function.
Example
await db.transaction('myDatabase', async (client) => {
    await client.execute('INSERT INTO users (name) VALUES (?)', ['Alice']);
});
Parameters:
Name Type Description
dbName string The name of the database to use for the transaction.
callback function The function to execute within the transaction context.
Throws:
  • If the transaction fails or the callback throws an error.
    Type
    Error
  • If the database connection is not configured.
    Type
    Error
Returns:
The result of the transaction callback.
Type
Promise.<any>