開發環境
OS:
Windows: Node v20.17.0 npm 10.8.2
macOS: Node v20.13.1 npm 10.5.2
Linux(Ubuntu 22.04): Node v16.19.1 npm 8.19.3
資料庫版本: 11.2.0.4.0
SQL: SELECT version FROM PRODUCT_COMPONENT_VERSION WHERE product LIKE ‘Oracle Database%’;
透過該指令得出版本
接下來根據三個環境做快速流程(官網都有,以下是走過後確認記錄下來給自己的,如有不同是可能的)
Windows 10
- 下載 Oracle官網下載連結 19_24
- 解壓縮之後有一個 instantclient_19_24資料夾
- 放置到專案下的 oracle資料夾裡頭
macOS 15.0.1
- 下載 Oracle官網下載連結 23 3
- 掛載後
- cd /Volumes/instantclient-basic-macos.arm64-23.3.0.23.09/
- sh ./install_ic.sh
- 之後建立出一個資料夾,路徑為 /Users//Downloads/instantclient_23_3
Linux(Ubuntu 22.04)
如權限夠無需 sudo
- sudo mkdir -p /opt/oracle
- cd /opt/oracle
- sudo wget https://download.oracle.com/otn_software/linux/instantclient/1925000/instantclient-basic-linux.x64-19.25.0.0.0dbru.zip
- unzip instantclient-basic-linux.x64-19.25.0.0.0dbru.zip (沒有 unzip 記得 sudo apt install zip)
- sudo sh -c “echo /opt/oracle/instantclient_19_25 > /etc/ld.so.conf.d/oracle-instantclient.conf”
- sudo ldconfi
專案之結構(節錄)
node-oracle/
├── node_modules/
├── oracle/instantclient_19_24 <- Only Windows Put Here
├── index.js
└── package.json
index.js 範例 (根據這份source code: https://node-oracledb.readthedocs.io/en/latest/user_guide/initialization.html#enabling-node-oracledb-thick-mode 並稍做修改 )
const oracledb = require('oracledb');
let clientOpts = {};
if (process.platform === 'win32') {
// Windows
// If you use backslashes in the libDir string, you will
// need to double them.
clientOpts = { libDir: './oracle/instantclient_19_24' };
} else if (process.platform === 'darwin') {
// macOS
clientOpts = { libDir: process.env.HOME + '/Downloads/instantclient_23_3' };
}
// else on other platforms like Linux the system library search path MUST always be
// set before Node.js is started, for example with ldconfig or LD_LIBRARY_PATH.
// enable node-oracledb Thick mode
oracledb.initOracleClient(clientOpts);
發佈留言