Skip to content

Commit 964107f

Browse files
author
Christopher Jones
authored
Sync with node-oracledb 2.3 examples (#41)
Sync node-oracledb examples with https://github.com/oracle/node-oracledb/tree/v2.3.0/examples
1 parent 5991874 commit 964107f

Some content is hidden

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

52 files changed

+1726
-290
lines changed

javascript/node-oracledb/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Node-oracledb Examples
22

3-
This directory contains [node-oracledb 2.0](https://www.npmjs.com/package/oracledb) examples.
3+
This directory contains [node-oracledb 2.3](https://www.npmjs.com/package/oracledb) examples.
44

55
The node-oracledb add-on for Node.js powers high performance Oracle Database applications.
66

7-
[Node-oracledb documentation](https://github.com/oracle/node-oracledb/blob/master/doc/api.md)
7+
[Node-oracledb documentation](https://oracle.github.io/node-oracledb/doc/api.html)
88

99
[Issues and questions](https://github.com/oracle/node-oracledb/issues)
1010

1111
To run the examples:
1212

13-
- [Install node-oracledb](https://github.com/oracle/node-oracledb/blob/master/INSTALL.md).
13+
- [Install node-oracledb](https://oracle.github.io/node-oracledb/INSTALL.html).
1414

1515

1616
- Use `demo.sql` to create schema objects used by the samples. For

javascript/node-oracledb/blobhttp.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
1+
/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */
22

33
/******************************************************************************
44
*
@@ -74,8 +74,7 @@ function handleRequest(request, response) {
7474

7575
if (action == '/getimage') {
7676
oracledb.getConnection( // gets a connection from the 'default' connection pool
77-
function(err, connection)
78-
{
77+
function(err, connection) {
7978
if (err) {
8079
console.error(err.message);
8180
return;
@@ -84,8 +83,7 @@ function handleRequest(request, response) {
8483
connection.execute(
8584
"SELECT b FROM mylobs WHERE id = :id", // get the image
8685
{ id: 2 },
87-
function(err, result)
88-
{
86+
function(err, result) {
8987
if (err) {
9088
console.error(err.message);
9189
return;
@@ -104,24 +102,21 @@ function handleRequest(request, response) {
104102

105103
lob.on(
106104
'end',
107-
function()
108-
{
105+
function() {
109106
console.log("lob.on 'end' event");
110107
response.end();
111108
});
112109
lob.on(
113110
'close',
114-
function()
115-
{
111+
function() {
116112
console.log("lob.on 'close' event");
117113
connection.close(function(err) {
118114
if (err) console.error(err);
119115
});
120116
});
121117
lob.on(
122118
'error',
123-
function(err)
124-
{
119+
function(err) {
125120
console.log("lob.on 'error' event");
126121
console.error(err);
127122
connection.close(function(err) {

javascript/node-oracledb/clobexample.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ This is example text used for node-oracledb CLOB examples.
22

33
The Oracle Database Node.js driver powers high performance Node.js applications.
44

5-
The node-oracledb home page is at http://www.oracle.com/technetwork/database/database-technologies/scripting-languages/node_js/
5+
The node-oracledb home page is at http://oracle.github.io/node-oracledb/

javascript/node-oracledb/connect.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
1+
/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */
22

33
/******************************************************************************
44
*
@@ -33,17 +33,15 @@ oracledb.getConnection(
3333
password : dbConfig.password,
3434
connectString : dbConfig.connectString
3535
},
36-
function(err, connection)
37-
{
36+
function(err, connection) {
3837
if (err) {
3938
console.error(err.message);
4039
return;
4140
}
4241
console.log('Connection was successful!');
4342

4443
connection.close(
45-
function(err)
46-
{
44+
function(err) {
4745
if (err) {
4846
console.error(err.message);
4947
return;

javascript/node-oracledb/cqn1.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. */
2+
3+
/******************************************************************************
4+
*
5+
* You may not use the identified files except in compliance with the Apache
6+
* License, Version 2.0 (the "License.")
7+
*
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* NAME
19+
* cqn1.js
20+
*
21+
* DESCRIPTION
22+
* Shows query-level Continuous Query Notification, allowing a
23+
* method to be invoked when a data set changes.
24+
*
25+
* The user must have been granted CHANGE NOTIFICATION.
26+
* The node-oracledb host must be resolvable by the database host.
27+
*
28+
* This example uses Node 8 syntax, but could be written to use callbacks.
29+
*
30+
*****************************************************************************/
31+
32+
const oracledb = require("oracledb");
33+
let dbConfig = require('./dbconfig.js');
34+
35+
dbConfig.events = true; // CQN needs events mode
36+
37+
let interval = setInterval(function() {
38+
console.log("waiting...");
39+
}, 5000);
40+
41+
function myCallback(message)
42+
{
43+
// message.type is one of the oracledb.SUBSCR_EVENT_TYPE_* values
44+
console.log("Message type:", message.type);
45+
if (message.type == oracledb.SUBSCR_EVENT_TYPE_DEREG) {
46+
clearInterval(interval);
47+
console.log("Deregistration has taken place...");
48+
return;
49+
}
50+
console.log("Message database name:", message.dbName);
51+
console.log("Message transaction id:", message.txId);
52+
console.log("Message queries:");
53+
for (let i = 0; i < message.queries.length; i++) {
54+
let query = message.queries[i];
55+
for (let j = 0; j < query.tables.length; j++) {
56+
let table = query.tables[j];
57+
console.log("--> --> Table Name:", table.name);
58+
// Note table.operation and row.operation are masks of
59+
// oracledb.CQN_OPCODE_* values
60+
console.log("--> --> Table Operation:", table.operation);
61+
if (table.rows) {
62+
console.log("--> --> Table Rows:");
63+
for (let k = 0; k < table.rows.length; k++) {
64+
let row = table.rows[k];
65+
console.log("--> --> --> Row Rowid:", row.rowid);
66+
console.log("--> --> --> Row Operation:", row.operation);
67+
console.log(Array(61).join("-"));
68+
}
69+
}
70+
}
71+
console.log(Array(61).join("="));
72+
}
73+
}
74+
75+
const options = {
76+
callback : myCallback,
77+
sql: "SELECT * FROM cqntable WHERE k > :bv",
78+
binds: { bv : 100 },
79+
timeout : 60, // Stop after 60 seconds
80+
// SUBSCR_QOS_QUERY: generate notifications when rows with k > 100 are changed
81+
// SUBSCR_QOS_ROWIDS: Return ROWIDs in the notification message
82+
qos : oracledb.SUBSCR_QOS_QUERY | oracledb.SUBSCR_QOS_ROWIDS
83+
};
84+
85+
async function runTest() {
86+
let conn;
87+
88+
try {
89+
conn = await oracledb.getConnection(dbConfig);
90+
91+
await conn.subscribe('mysub', options);
92+
93+
console.log("Subscription created...");
94+
95+
} catch (err) {
96+
console.error(err);
97+
clearInterval(interval);
98+
} finally {
99+
if (conn) {
100+
try {
101+
await conn.close();
102+
} catch (err) {
103+
console.error(err);
104+
}
105+
}
106+
}
107+
}
108+
109+
process
110+
.on('SIGTERM', function() {
111+
console.log("\nTerminating");
112+
process.exit(0);
113+
})
114+
.on('SIGINT', function() {
115+
console.log("\nTerminating");
116+
process.exit(0);
117+
});
118+
119+
runTest();

javascript/node-oracledb/cqn2.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. */
2+
3+
/******************************************************************************
4+
*
5+
* You may not use the identified files except in compliance with the Apache
6+
* License, Version 2.0 (the "License.")
7+
*
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* NAME
19+
* cqn2.js
20+
*
21+
* DESCRIPTION
22+
* Shows object-level Continuous Query Notification (formerly known
23+
* as Database Change Notification), allowing a method to be invoked
24+
* when an object changes. Notification are grouped into intervals.
25+
*
26+
* The user must have been granted CHANGE NOTIFICATION.
27+
* The node-oracledb host must be resolvable by the database host.
28+
*
29+
* This example uses Node 8 syntax, but could be written to use callbacks.
30+
*
31+
*****************************************************************************/
32+
33+
const oracledb = require("oracledb");
34+
let dbConfig = require('./dbconfig.js');
35+
36+
dbConfig.events = true; // CQN needs events mode
37+
38+
let interval = setInterval(function() {
39+
console.log("waiting...");
40+
}, 5000);
41+
42+
function myCallback(message)
43+
{
44+
// message.type is one of the oracledb.SUBSCR_EVENT_TYPE_* values
45+
console.log("Message type:", message.type);
46+
if (message.type == oracledb.SUBSCR_EVENT_TYPE_DEREG) {
47+
clearInterval(interval);
48+
console.log("Deregistration has taken place...");
49+
return;
50+
}
51+
console.log("Message database name:", message.dbName);
52+
console.log("Message transaction id:", message.txId);
53+
for (let i = 0; i < message.tables.length; i++) {
54+
let table = message.tables[i];
55+
console.log("--> Table Name:", table.name);
56+
// Note table.operation and row.operation are masks of
57+
// oracledb.CQN_OPCODE_* values
58+
console.log("--> Table Operation:", table.operation);
59+
if (table.rows) {
60+
for (let j = 0; j < table.rows.length; j++) {
61+
let row = table.rows[j];
62+
console.log("--> --> Row Rowid:", row.rowid);
63+
console.log("--> --> Row Operation:", row.operation);
64+
console.log(Array(61).join("-"));
65+
}
66+
}
67+
console.log(Array(61).join("="));
68+
}
69+
}
70+
71+
const options = {
72+
callback : myCallback,
73+
sql: "SELECT * FROM cqntable",
74+
// Stop after 60 seconds
75+
timeout : 60,
76+
// Return ROWIDs in the notification message
77+
qos : oracledb.SUBSCR_QOS_ROWIDS,
78+
// Group notifications in batches covering 10 second
79+
// intervals, and send a summary
80+
groupingClass : oracledb.SUBSCR_GROUPING_CLASS_TIME,
81+
groupingValue : 10,
82+
groupingType : oracledb.SUBSCR_GROUPING_TYPE_SUMMARY
83+
};
84+
85+
async function runTest() {
86+
let conn;
87+
88+
try {
89+
conn = await oracledb.getConnection(dbConfig);
90+
91+
await conn.subscribe('mysub', options);
92+
93+
console.log("Subscription created...");
94+
95+
} catch (err) {
96+
console.error(err);
97+
clearInterval(interval);
98+
} finally {
99+
if (conn) {
100+
try {
101+
await conn.close();
102+
} catch (err) {
103+
console.error(err);
104+
}
105+
}
106+
}
107+
}
108+
109+
process
110+
.on('SIGTERM', function() {
111+
console.log("\nTerminating");
112+
process.exit(0);
113+
})
114+
.on('SIGINT', function() {
115+
console.log("\nTerminating");
116+
process.exit(0);
117+
});
118+
119+
runTest();

javascript/node-oracledb/dbconfig.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* to the database. Production applications should consider using
2424
* External Authentication to avoid hard coded credentials.
2525
*
26+
* To create a database user see https://www.youtube.com/watch?v=WDJacg0NuLo
27+
*
2628
* Applications can set the connectString value to an Easy Connect
2729
* string, or a Net Service Name from a tnsnames.ora file or
2830
* external naming service, or it can be the name of a local Oracle
@@ -75,10 +77,10 @@ module.exports = {
7577
password : process.env.NODE_ORACLEDB_PASSWORD || "welcome",
7678

7779
// For information on connection strings see:
78-
// https://github.com/oracle/node-oracledb/blob/master/doc/api.md#connectionstrings
80+
// https://oracle.github.io/node-oracledb/doc/api.html#connectionstrings
7981
connectString : process.env.NODE_ORACLEDB_CONNECTIONSTRING || "localhost/orclpdb",
8082

8183
// Setting externalAuth is optional. It defaults to false. See:
82-
// https://github.com/oracle/node-oracledb/blob/master/doc/api.md#extauth
84+
// https://oracle.github.io/node-oracledb/doc/api.html#extauth
8385
externalAuth : process.env.NODE_ORACLEDB_EXTERNALAUTH ? true : false
8486
};

0 commit comments

Comments
 (0)