Section 3: Installing Node.js on IBM i
Node.js on its own doesn't know a Db2 table from a data area. We need a driver. This is where ODBC steps in.
Install the ODBC pieces with yum
unixODBCis the ODBC driver manager.ibm-iaccessis the IBM i Access ODBC Driver for PASE.
Check the driver is registered:
You should see [IBM i Access ODBC Driver]. On IBM i there's also a predefined DSN called *LOCAL that connects to the local database.
Test the connection with isql
At the SQL> prompt try:
Type quit to leave.
Install the npm package
Inside your project directory (we'll create the real one in Section 5):
npm init -y
npm install odbc
Build tools:
odbc is a native addon. If npm tries to compile it and fails, install the build chain: yum install gcc10 gcc10-cplusplus make-gnu python39 and try again.
Quick test - odbc
Inside your project directory (we'll create the real one in Section 5):
const odbc = require('odbc');
async function main() {
const conn = await odbc.connect(
`DSN=*LOCAL;UID=${process.env.DB_USER};PWD=${process.env.DB_PASS};NAM=1`
);
const rows = await conn.query(
'SELECT CURRENT_SERVER AS SERVER, CURRENT_USER AS USR FROM SYSIBM.SYSDUMMY1'
);
console.log(rows[0]);
await conn.close();
}
main().catch(err => { console.error(err); process.exit(1); });
NAM=1 sets system naming (LIBRARY/TABLE and your library list), which feels like home. Leave it off for SQL naming (SCHEMA.TABLE).
Alternative - idb-pconnector (on-box only)
IBM's native connector uses the job's own authority, so no user/password is needed when running on the IBM i:
const { Connection, Statement } = require('idb-pconnector');
async function main() {
const conn = new Connection({ url: '*LOCAL' });
const stmt = new Statement(conn);
const rows = await stmt.exec(
'SELECT CURRENT_SERVER AS SERVER FROM SYSIBM.SYSDUMMY1'
);
console.log(rows);
await stmt.close();
conn.disconn();
conn.close();
}
main().catch(console.error);
From here on, the course uses odbc. You've now got Node.js, npm and a working database connection. High five!
