← 中文资源

简体中文版本

node-oracledb

Oracle 官方 Node.js 驱动,支持连接池、Promise 与批量操作。

阅读英文原文

概述

Oracle 官方 Node.js 驱动,支持连接池、Promise 与批量操作。

核心 API

  • oracledb.getConnection:从连接池获取连接以执行语句。
  • connection.execute:使用绑定变量执行 SQL 或 PL/SQL 语句。
  • connection.executeMany:单次往返中对每行绑定执行一次语句。

使用注意

连接必须归还到池中,否则连接数会迅速耗尽,务必在 finally 中 close。SQL 一律使用绑定变量,禁止字符串拼接以防注入。批量写入用 executeMany 能显著减少往返。事务需显式 commit,默认不自动提交。

验证方式

验证绑定变量查询结果正确、executeMany 影响行数符合预期,并确认连接在 close 后回到池中可被再次获取。

版本提示

以上签名与用法以 v6 为基准,升级大版本前请对照官方迁移说明确认 API 是否变更。

代码示例

import oracledb from 'oracledb';
const conn = await oracledb.getConnection({
  user: 'scott', password: 'tiger', connectString: 'localhost/orcl',
});
const result = await conn.execute(
  'SELECT * FROM users WHERE id = :id',
  { id: 1 },
);
console.log(result.rows);
await conn.executeMany(
  'INSERT INTO t (id, name) VALUES (:id, :name)',
  [{ id: 1, name: 'Ada' }, { id: 2, name: 'Bob' }],
);
npm install oracledb