Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions javascript/node-oracledb/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Node-oracledb Examples

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

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

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

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

To run the examples:

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


- Use `demo.sql` to create schema objects used by the samples. For
Expand Down
17 changes: 6 additions & 11 deletions javascript/node-oracledb/blobhttp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */

/******************************************************************************
*
Expand Down Expand Up @@ -74,8 +74,7 @@ function handleRequest(request, response) {

if (action == '/getimage') {
oracledb.getConnection( // gets a connection from the 'default' connection pool
function(err, connection)
{
function(err, connection) {
if (err) {
console.error(err.message);
return;
Expand All @@ -84,8 +83,7 @@ function handleRequest(request, response) {
connection.execute(
"SELECT b FROM mylobs WHERE id = :id", // get the image
{ id: 2 },
function(err, result)
{
function(err, result) {
if (err) {
console.error(err.message);
return;
Expand All @@ -104,24 +102,21 @@ function handleRequest(request, response) {

lob.on(
'end',
function()
{
function() {
console.log("lob.on 'end' event");
response.end();
});
lob.on(
'close',
function()
{
function() {
console.log("lob.on 'close' event");
connection.close(function(err) {
if (err) console.error(err);
});
});
lob.on(
'error',
function(err)
{
function(err) {
console.log("lob.on 'error' event");
console.error(err);
connection.close(function(err) {
Expand Down
2 changes: 1 addition & 1 deletion javascript/node-oracledb/clobexample.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ This is example text used for node-oracledb CLOB examples.

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

The node-oracledb home page is at http://www.oracle.com/technetwork/database/database-technologies/scripting-languages/node_js/
The node-oracledb home page is at http://oracle.github.io/node-oracledb/
8 changes: 3 additions & 5 deletions javascript/node-oracledb/connect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */

/******************************************************************************
*
Expand Down Expand Up @@ -33,17 +33,15 @@ oracledb.getConnection(
password : dbConfig.password,
connectString : dbConfig.connectString
},
function(err, connection)
{
function(err, connection) {
if (err) {
console.error(err.message);
return;
}
console.log('Connection was successful!');

connection.close(
function(err)
{
function(err) {
if (err) {
console.error(err.message);
return;
Expand Down
119 changes: 119 additions & 0 deletions javascript/node-oracledb/cqn1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. */

/******************************************************************************
*
* You may not use the identified files except in compliance with the Apache
* License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NAME
* cqn1.js
*
* DESCRIPTION
* Shows query-level Continuous Query Notification, allowing a
* method to be invoked when a data set changes.
*
* The user must have been granted CHANGE NOTIFICATION.
* The node-oracledb host must be resolvable by the database host.
*
* This example uses Node 8 syntax, but could be written to use callbacks.
*
*****************************************************************************/

const oracledb = require("oracledb");
let dbConfig = require('./dbconfig.js');

dbConfig.events = true; // CQN needs events mode

let interval = setInterval(function() {
console.log("waiting...");
}, 5000);

function myCallback(message)
{
// message.type is one of the oracledb.SUBSCR_EVENT_TYPE_* values
console.log("Message type:", message.type);
if (message.type == oracledb.SUBSCR_EVENT_TYPE_DEREG) {
clearInterval(interval);
console.log("Deregistration has taken place...");
return;
}
console.log("Message database name:", message.dbName);
console.log("Message transaction id:", message.txId);
console.log("Message queries:");
for (let i = 0; i < message.queries.length; i++) {
let query = message.queries[i];
for (let j = 0; j < query.tables.length; j++) {
let table = query.tables[j];
console.log("--> --> Table Name:", table.name);
// Note table.operation and row.operation are masks of
// oracledb.CQN_OPCODE_* values
console.log("--> --> Table Operation:", table.operation);
if (table.rows) {
console.log("--> --> Table Rows:");
for (let k = 0; k < table.rows.length; k++) {
let row = table.rows[k];
console.log("--> --> --> Row Rowid:", row.rowid);
console.log("--> --> --> Row Operation:", row.operation);
console.log(Array(61).join("-"));
}
}
}
console.log(Array(61).join("="));
}
}

const options = {
callback : myCallback,
sql: "SELECT * FROM cqntable WHERE k > :bv",
binds: { bv : 100 },
timeout : 60, // Stop after 60 seconds
// SUBSCR_QOS_QUERY: generate notifications when rows with k > 100 are changed
// SUBSCR_QOS_ROWIDS: Return ROWIDs in the notification message
qos : oracledb.SUBSCR_QOS_QUERY | oracledb.SUBSCR_QOS_ROWIDS
};

async function runTest() {
let conn;

try {
conn = await oracledb.getConnection(dbConfig);

await conn.subscribe('mysub', options);

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

} catch (err) {
console.error(err);
clearInterval(interval);
} finally {
if (conn) {
try {
await conn.close();
} catch (err) {
console.error(err);
}
}
}
}

process
.on('SIGTERM', function() {
console.log("\nTerminating");
process.exit(0);
})
.on('SIGINT', function() {
console.log("\nTerminating");
process.exit(0);
});

runTest();
119 changes: 119 additions & 0 deletions javascript/node-oracledb/cqn2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. */

/******************************************************************************
*
* You may not use the identified files except in compliance with the Apache
* License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NAME
* cqn2.js
*
* DESCRIPTION
* Shows object-level Continuous Query Notification (formerly known
* as Database Change Notification), allowing a method to be invoked
* when an object changes. Notification are grouped into intervals.
*
* The user must have been granted CHANGE NOTIFICATION.
* The node-oracledb host must be resolvable by the database host.
*
* This example uses Node 8 syntax, but could be written to use callbacks.
*
*****************************************************************************/

const oracledb = require("oracledb");
let dbConfig = require('./dbconfig.js');

dbConfig.events = true; // CQN needs events mode

let interval = setInterval(function() {
console.log("waiting...");
}, 5000);

function myCallback(message)
{
// message.type is one of the oracledb.SUBSCR_EVENT_TYPE_* values
console.log("Message type:", message.type);
if (message.type == oracledb.SUBSCR_EVENT_TYPE_DEREG) {
clearInterval(interval);
console.log("Deregistration has taken place...");
return;
}
console.log("Message database name:", message.dbName);
console.log("Message transaction id:", message.txId);
for (let i = 0; i < message.tables.length; i++) {
let table = message.tables[i];
console.log("--> Table Name:", table.name);
// Note table.operation and row.operation are masks of
// oracledb.CQN_OPCODE_* values
console.log("--> Table Operation:", table.operation);
if (table.rows) {
for (let j = 0; j < table.rows.length; j++) {
let row = table.rows[j];
console.log("--> --> Row Rowid:", row.rowid);
console.log("--> --> Row Operation:", row.operation);
console.log(Array(61).join("-"));
}
}
console.log(Array(61).join("="));
}
}

const options = {
callback : myCallback,
sql: "SELECT * FROM cqntable",
// Stop after 60 seconds
timeout : 60,
// Return ROWIDs in the notification message
qos : oracledb.SUBSCR_QOS_ROWIDS,
// Group notifications in batches covering 10 second
// intervals, and send a summary
groupingClass : oracledb.SUBSCR_GROUPING_CLASS_TIME,
groupingValue : 10,
groupingType : oracledb.SUBSCR_GROUPING_TYPE_SUMMARY
};

async function runTest() {
let conn;

try {
conn = await oracledb.getConnection(dbConfig);

await conn.subscribe('mysub', options);

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

} catch (err) {
console.error(err);
clearInterval(interval);
} finally {
if (conn) {
try {
await conn.close();
} catch (err) {
console.error(err);
}
}
}
}

process
.on('SIGTERM', function() {
console.log("\nTerminating");
process.exit(0);
})
.on('SIGINT', function() {
console.log("\nTerminating");
process.exit(0);
});

runTest();
6 changes: 4 additions & 2 deletions javascript/node-oracledb/dbconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* to the database. Production applications should consider using
* External Authentication to avoid hard coded credentials.
*
* To create a database user see https://www.youtube.com/watch?v=WDJacg0NuLo
*
* Applications can set the connectString value to an Easy Connect
* string, or a Net Service Name from a tnsnames.ora file or
* external naming service, or it can be the name of a local Oracle
Expand Down Expand Up @@ -75,10 +77,10 @@ module.exports = {
password : process.env.NODE_ORACLEDB_PASSWORD || "welcome",

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

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