Skip to content

Commit c690fc5

Browse files
authored
Update examples for node-oracledb 4.1 (oracle-samples#84)
1 parent 1a06c63 commit c690fc5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1283
-982
lines changed

javascript/node-oracledb/README.md

Lines changed: 79 additions & 94 deletions
Large diffs are not rendered by default.

javascript/node-oracledb/aqmulti.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ async function deq() {
7777
const queue = await connection.getQueue(queueName);
7878
queue.deqOptions.visibility = oracledb.AQ_VISIBILITY_IMMEDIATE; // Change the visibility so no explicit commit is required
7979

80+
console.log('Dequeuing messages');
81+
8082
const messages = await queue.deqMany(5); // get at most 5 messages
8183
console.log("Dequeued " + messages.length + " messages");
8284
for (const msg of messages) {

javascript/node-oracledb/aqobject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Oracle Advanced Queuing (AQ) example passing an Oracle Database object
2323
*
2424
* Before running this, a queue allowing an Oracle Database object
25-
* payloads must be created, see
25+
* payload must be created, see
2626
* https://oracle.github.io/node-oracledb/doc/api.html#aqobjexample
2727
*
2828
* This example requires node-oracledb 4 or later.

javascript/node-oracledb/blobhttp.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,33 @@
2222
* Listens for an HTTP request and returns an image queried from a BLOB column
2323
* Also shows the connection pool's caching using a 'default' pool.
2424
*
25-
* Use demo.sql to create the required table or do:
26-
* DROP TABLE mylobs;
27-
* CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
28-
*
29-
* Run lobinsert1.js to load an image before running this example.
30-
*
3125
* Start the listener with 'node blobhttp.js' and then use a browser
3226
* to load http://localhost:7000/getimage
3327
*
3428
* This example uses Node 8's async/await syntax.
3529
*
3630
*****************************************************************************/
3731

38-
var url = require('url');
39-
var http = require('http');
40-
var oracledb = require('oracledb');
41-
var dbConfig = require('./dbconfig.js');
32+
const url = require('url');
33+
const http = require('http');
34+
const oracledb = require('oracledb');
35+
const dbConfig = require('./dbconfig.js');
36+
const demoSetup = require('./demosetup.js');
4237

43-
var httpPort = 7000;
38+
const httpPort = 7000;
4439

4540
// Main entry point. Creates a connection pool which becomes the
4641
// 'default' pool, and then creates an HTTP server.
4742
async function init() {
4843
try {
49-
await oracledb.createPool(
50-
{
51-
user: dbConfig.user,
52-
password: dbConfig.password,
53-
connectString: dbConfig.connectString
54-
});
44+
await oracledb.createPool(dbConfig);
5545
console.log('Connection pool started');
5646

47+
// create the demo table
48+
const connection = await oracledb.getConnection();
49+
await demoSetup.setupLobs(connection, true);
50+
await connection.close();
51+
5752
// Create HTTP server and listen on port httpPort
5853
const server = http.createServer();
5954
server.on('error', (err) => {
@@ -63,7 +58,7 @@ async function init() {
6358
handleRequest(request, response);
6459
});
6560
await server.listen(httpPort);
66-
console.log("Server running. Try requesting: http://localhost:" + httpPort + "/getimage");
61+
console.log("Server is running. Try loading http://localhost:" + httpPort + "/getimage");
6762

6863
} catch (err) {
6964
console.error('init() error: ' + err.message);
@@ -84,14 +79,14 @@ async function handleRequest(request, response) {
8479
connection = await oracledb.getConnection(); // gets a connection from the 'default' connection pool
8580

8681
const result = await connection.execute(
87-
"SELECT b FROM mylobs WHERE id = :id", // get the image
82+
"SELECT b FROM no_lobs WHERE id = :id", // get the image
8883
{ id: 2 }
8984
);
9085
if (result.rows.length === 0) {
91-
throw new Error("No results. Did you run lobinsert1.js?");
86+
throw new Error("No data selected from table.");
9287
}
9388

94-
var lob = result.rows[0][0];
89+
const lob = result.rows[0][0];
9590
if (lob === null) {
9691
throw new Error("BLOB was NULL");
9792
}
@@ -117,6 +112,7 @@ async function handleRequest(request, response) {
117112

118113
} catch (err) {
119114
console.error(err);
115+
await closePoolAndExit();
120116
} finally {
121117
if (connection) {
122118
try {
@@ -137,9 +133,9 @@ async function closePoolAndExit() {
137133
console.log('\nTerminating');
138134
try {
139135
// Get the pool from the pool cache and close it when no
140-
// connections are in use, or force it closed after 10 seconds
136+
// connections are in use, or force it closed after 2 seconds
141137
// If this hangs, you may need DISABLE_OOB=ON in a sqlnet.ora file
142-
await oracledb.getPool().close(10);
138+
await oracledb.getPool().close(2);
143139
console.log('Pool closed');
144140
process.exit(0);
145141
} catch(err) {

javascript/node-oracledb/calltimeout.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
* Shows how to time out long running database calls.
2323
* See https://oracle.github.io/node-oracledb/doc/api.html#dbcalltimeouts
2424
*
25-
* This example requires node-oracledb 3 or later.
26-
* Node-oracledb must be using Oracle Client 18c libraries, or greater.
25+
* This example requires node-oracledb 3 (or later) and Oracle Client 18c
26+
* libraries (or later).
2727
*
2828
* This example uses Node 8's async/await syntax.
2929
*
@@ -40,6 +40,11 @@ async function run() {
4040
let connection;
4141

4242
try {
43+
44+
if (oracledb.oracleClientVersion < 1800000000) {
45+
throw new Error("Oracle Client libraries must be 18c or later");
46+
}
47+
4348
connection = await oracledb.getConnection(dbConfig);
4449

4550
connection.callTimeout = timeout * 1000; // milliseconds
@@ -66,4 +71,14 @@ async function run() {
6671
}
6772
}
6873

74+
process
75+
.on('SIGTERM', function() {
76+
console.log("\nTerminating");
77+
process.exit(0);
78+
})
79+
.on('SIGINT', function() {
80+
console.log("\nTerminating");
81+
process.exit(0);
82+
});
83+
6984
run();

javascript/node-oracledb/connectionpool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async function init() {
4545
password: dbConfig.password,
4646
connectString: dbConfig.connectString
4747
// edition: 'ORA$BASE', // used for Edition Based Redefintion
48-
// events: true, // whether to handle Oracle Database FAN and RLB events or support CQN
48+
// events: false, // whether to handle Oracle Database FAN and RLB events or support CQN
4949
// externalAuth: false, // whether connections should be established using External Authentication
5050
// homogeneous: true, // all connections in the pool have the same credentials
5151
// poolAlias: 'default', // set an alias to allow access to the pool via a name.

javascript/node-oracledb/cqn1.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* Run this script and when the subscription has been created, run
2929
* these statements in a SQL*Plus session:
30-
* INSERT INTO CQNTABLE VALUES (101);
30+
* INSERT INTO NO_CQNTABLE VALUES (101);
3131
* COMMIT;
3232
*
3333
* This example requires node-oracledb 2.3 or later.
@@ -81,20 +81,40 @@ function myCallback(message)
8181

8282
const options = {
8383
callback : myCallback,
84-
sql: "SELECT * FROM cqntable WHERE k > :bv",
84+
sql: `SELECT * FROM no_cqntable WHERE k > :bv`,
8585
binds: { bv : 100 },
8686
timeout : 60, // Stop after 60 seconds
87+
// ipAddress: '127.0.0.1',
8788
// SUBSCR_QOS_QUERY: generate notifications when rows with k > 100 are changed
8889
// SUBSCR_QOS_ROWIDS: Return ROWIDs in the notification message
8990
qos : oracledb.SUBSCR_QOS_QUERY | oracledb.SUBSCR_QOS_ROWIDS
9091
};
9192

93+
async function setup(connection) {
94+
const stmts = [
95+
`DROP TABLE no_cqntable`,
96+
97+
`CREATE TABLE no_cqntable (k NUMBER)`
98+
];
99+
100+
for (const s of stmts) {
101+
try {
102+
await connection.execute(s);
103+
} catch(e) {
104+
if (e.errorNum != 942)
105+
console.error(e);
106+
}
107+
}
108+
}
109+
92110
async function runTest() {
93111
let connection;
94112

95113
try {
96114
connection = await oracledb.getConnection(dbConfig);
97115

116+
await setup(connection);
117+
98118
await connection.subscribe('mysub', options);
99119

100120
console.log("Subscription created...");

javascript/node-oracledb/cqn2.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* Run this script and when the subscription has been created, run
3030
* these statements in a SQL*Plus session:
31-
* INSERT INTO CQNTABLE VALUES (1);
31+
* INSERT INTO NO_CQNTABLE VALUES (1);
3232
* COMMIT;
3333
*
3434
* This example requires node-oracledb 2.3 or later.
@@ -40,7 +40,7 @@
4040
const oracledb = require("oracledb");
4141
const dbConfig = require('./dbconfig.js');
4242

43-
// dbConfig.events = true; // CQN needs events mode, which is true by default in 4.0
43+
dbConfig.events = true; // CQN needs events mode
4444

4545
const interval = setInterval(function() {
4646
console.log("waiting...");
@@ -77,7 +77,8 @@ function myCallback(message)
7777

7878
const options = {
7979
callback : myCallback,
80-
sql: "SELECT * FROM cqntable",
80+
sql: "SELECT * FROM no_cqntable",
81+
// ipAddress: '127.0.0.1',
8182
// Stop after 60 seconds
8283
timeout : 60,
8384
// Return ROWIDs in the notification message
@@ -89,12 +90,31 @@ const options = {
8990
groupingType : oracledb.SUBSCR_GROUPING_TYPE_SUMMARY
9091
};
9192

93+
async function setup(connection) {
94+
const stmts = [
95+
`DROP TABLE no_cqntable`,
96+
97+
`CREATE TABLE no_cqntable (k NUMBER)`
98+
];
99+
100+
for (const s of stmts) {
101+
try {
102+
await connection.execute(s);
103+
} catch(e) {
104+
if (e.errorNum != 942)
105+
console.error(e);
106+
}
107+
}
108+
}
109+
92110
async function runTest() {
93111
let connection;
94112

95113
try {
96114
connection = await oracledb.getConnection(dbConfig);
97115

116+
await setup(connection);
117+
98118
await connection.subscribe('mysub', options);
99119

100120
console.log("Subscription created...");

javascript/node-oracledb/date.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,32 @@ async function run() {
4747
connection = await oracledb.getConnection(dbConfig);
4848

4949
console.log('Creating table');
50-
await connection.execute(
51-
`BEGIN
52-
DECLARE
53-
e_table_exists EXCEPTION;
54-
PRAGMA EXCEPTION_INIT(e_table_exists, -00942);
55-
BEGIN
56-
EXECUTE IMMEDIATE ('DROP TABLE datetest');
57-
EXCEPTION
58-
WHEN e_table_exists
59-
THEN NULL;
60-
END;
61-
END;`);
62-
63-
await connection.execute(
64-
`CREATE TABLE datetest(
50+
51+
const stmts = [
52+
`DROP TABLE no_datetab`,
53+
54+
`CREATE TABLE no_datetab(
6555
id NUMBER,
6656
timestampcol TIMESTAMP,
6757
timestamptz TIMESTAMP WITH TIME ZONE,
6858
timestampltz TIMESTAMP WITH LOCAL TIME ZONE,
69-
datecol DATE)`);
59+
datecol DATE)`
60+
];
61+
62+
for (const s of stmts) {
63+
try {
64+
await connection.execute(s);
65+
} catch(e) {
66+
if (e.errorNum != 942)
67+
console.error(e);
68+
}
69+
}
7070

7171
// When bound, JavaScript Dates are inserted using TIMESTAMP WITH LOCAL TIMEZONE
7272
date = new Date();
7373
console.log('Inserting JavaScript date: ' + date);
7474
result = await connection.execute(
75-
`INSERT INTO datetest (id, timestampcol, timestamptz, timestampltz, datecol)
75+
`INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
7676
VALUES (1, :ts, :tstz, :tsltz, :td)`,
7777
{ ts: date, tstz: date, tsltz: date, td: date });
7878
console.log('Rows inserted: ' + result.rowsAffected );
@@ -81,7 +81,7 @@ async function run() {
8181
result = await connection.execute(
8282
`SELECT id, timestampcol, timestamptz, timestampltz, datecol,
8383
TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
84-
FROM datetest
84+
FROM no_datetab
8585
ORDER BY id`);
8686
console.log(result.rows);
8787

@@ -91,7 +91,7 @@ async function run() {
9191
date = new Date();
9292
console.log('Inserting JavaScript date: ' + date);
9393
result = await connection.execute(
94-
`INSERT INTO datetest (id, timestampcol, timestamptz, timestampltz, datecol)
94+
`INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
9595
VALUES (2, :ts, :tstz, :tsltz, :td)`,
9696
{ ts: date, tstz: date, tsltz: date, td: date });
9797
console.log('Rows inserted: ' + result.rowsAffected );
@@ -100,7 +100,7 @@ async function run() {
100100
result = await connection.execute(
101101
`SELECT id, timestampcol, timestamptz, timestampltz, datecol,
102102
TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
103-
FROM datetest
103+
FROM no_datetab
104104
ORDER BY id`);
105105
console.log(result.rows);
106106

0 commit comments

Comments
 (0)