Skip to content

Commit d1e266f

Browse files
authored
Update examples for node-oracledb 4.0 (oracle-samples#58)
1 parent 0064f41 commit d1e266f

Some content is hidden

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

75 files changed

+3777
-4162
lines changed

javascript/node-oracledb/README.md

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

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

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

@@ -15,7 +15,7 @@ Slack](https://node-oracledb.slack.com/join/shared_invite/enQtNDU4Mjc2NzM5OTA2LT
1515

1616
To run the examples:
1717

18-
- [Install node-oracledb](https://oracle.github.io/node-oracledb/INSTALL.html).
18+
- [Install node-oracledb](https://oracle.github.io/node-oracledb/INSTALL.html#quickstart).
1919

2020

2121
- Use `demo.sql` to create schema objects used by the samples. For
@@ -33,7 +33,7 @@ connection string:
3333
module.exports = {
3434
user: "hr",
3535
password: process.env.NODE_ORACLEDB_PASSWORD,
36-
connectString:"localhost/orclpdb"
36+
connectString:"localhost/orclpdb1"
3737
};
3838
```
3939

@@ -60,3 +60,77 @@ The demonstration objects can be dropped with `demodrop.sql`:
6060
```
6161
sqlplus hr/welcome@localhost/orclpdb @demodrop.sql
6262
```
63+
64+
## Example Overview
65+
66+
If this is your first time with node-oracledb, start with
67+
[`example.js`](example.js).
68+
69+
File Name | Description
70+
----------------------------|----------------------------------------------------------------------------------
71+
aqoptions.js | Oracle Advanced Queuing (AQ) example setting options and message attributes
72+
aqmulti.js | Oracle Advanced Queuing (AQ) example passing multiple messages
73+
aqobject.js | Oracle Advanced Queuing (AQ) example passing an Oracle Database object
74+
aqraw.js | Basic Oracle Advanced Queuing (AQ) example passing text messages
75+
blobhttp.js | Simple web app that streams an image
76+
calltimeout.js | Shows how to cancel a SQL statement if it doesn't complete in a specified time
77+
connect.js | Basic example for creating a standalone (non-pooled) connection
78+
connectionpool.js | Basic example creating a pool of connections
79+
cqn1.js | Basic Continuous Query Notification (CQN) example
80+
cqn2.js | Continuous Query Notification with notification grouping
81+
date.js | Show some DATE and TIMESTAMP behaviors
82+
dbconfig.js | Common file used by examples for setting connection credentials
83+
dbmsoutputgetline.js | Show fetching DBMS_OUTPUT by binding buffers
84+
dbmsoutputpipe.js | Show fetching DBMS_OUTPUT by using a pipelined table
85+
demo.sql | SQL script to create extra schema objects for the examples
86+
demodrop.sql | SQL script to drop the extra schema objects for the examples
87+
dmlrupd1.js | Example of DML RETURNING with a single row match
88+
dmlrupd2.js | Example of DML RETURNING where multiple rows are matched
89+
em_batcherrors.js | `executeMany()` example showing handling data errors
90+
em_dmlreturn1.js | `executeMany()` example of DML RETURNING that returns single values
91+
em_dmlreturn2.js | `executeMany()` example of DML RETURNING that returns multiple values
92+
em_insert1.js | Array DML example using `executeMany()` with bind-by-name syntax
93+
em_insert2.js | Array DML example using `executeMany()` with bind by position
94+
em_plsql.js | `executeMany()` example calling PL/SQL multiple times with one call
95+
em_rowcounts.js | `executeMany()` example showing how to find the number of rows affected by each input row
96+
endtoend.js | Example showing setting tracing attributes
97+
example.js | Basic example showing creating a table, inserting multiple rows, and querying rows
98+
fetchinfo.js | Show how numbers and dates can be returned as strings using `fetchAsString` and `fetchInfo`
99+
impres.js | Shows PL/SQL 'Implict Results' returning multiple query results from PL/SQL code.
100+
insert1.js | Basic example creating a table and inserting data. Shows DDL and DML
101+
insert2.js | Basic example showing auto commit behavior
102+
lobbinds.js | Demonstrates how to bind and query LOBs
103+
lobinsert1.js | Shows inserting a file into a CLOB column
104+
lobinsert2.js | Inserts text into a CLOB column using the RETURNING INTO method.
105+
lobinserttemp.js | Writes data to a Temporary CLOB and then inserts it into the database
106+
lobplsqltemp.js | Streams data into a Temporary CLOB and then passes it to PL/SQL
107+
lobselect.js | Shows basic, non-streaming CLOB and BLOB queries
108+
lobstream1.js | Shows how to stream LOBs to files
109+
lobstream2.js | Shows using Stream data events to fetch a CLOB
110+
metadata.js | Shows the metadata available after executing SELECT statements
111+
plsqlarray.js | Examples of binding PL/SQL "INDEX BY" tables
112+
plsqlfunc.js | How to call a PL/SQL function
113+
plsqlproc.js | How to call a PL/SQL procedure
114+
plsqlrecord.js | Shows binding of PL/SQL RECORDS
115+
plsqlvarrayrecord.js | Shows binding a VARRAY of RECORD in PL/SQL
116+
raw1.js | Shows using a Buffer to insert and select a RAW
117+
refcursor.js | Shows using a ResultSet to fetch rows from a REF CURSOR
118+
refcursortoquerystream.js | Converts a REF CURSOR returned from `execute()` to a query stream.
119+
resultset1.js | Executes a query and uses a ResultSet to fetch rows with `getRow()`
120+
resultset2.js | Executes a query and uses a ResultSet to fetch batches of rows with `getRows()`
121+
resultsettoquerystream.js | Converts a ResultSet returned from `execute()` into a Readable Stream.
122+
rowlimit.js | Shows ways to limit the number of records fetched by queries
123+
select1.js | Executes a basic query without using a connection pool or ResultSet
124+
select2.js | Executes queries to show array and object output formats
125+
selectgeometry.js | Insert and query Oracle Spatial geometries
126+
selectjson.js | Shows some JSON features of Oracle Database
127+
selectjsonblob.js | Shows how to use a BLOB as a JSON column store
128+
selectobject.js | Insert and query a named Oracle database object
129+
selectstream.js | Executes a basic query using a Readable Stream
130+
selectvarray.js | Shows inserting and selecting from a VARRAY column
131+
sessionfixup.js | Shows a pooled connection callback to efficiently set session state
132+
sessiontagging1.js | Simple pooled connection tagging for setting session state
133+
sessiontagging2.js | More complex example of pooled connection tagging for setting session state
134+
soda1.js | Basic Simple Oracle Document Access (SODA) example
135+
version.js | Shows the node-oracledb version attributes
136+
webappawait.js | A simple web application using a connection pool

javascript/node-oracledb/aqmulti.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* Copyright (c) 2019, 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+
* aqmulti.js
20+
*
21+
* DESCRIPTION
22+
* Oracle Advanced Queuing (AQ) example passing multiple messages.
23+
*
24+
* Before running this, a queue allowing RAW payloads must be
25+
* created, see https://oracle.github.io/node-oracledb/doc/api.html#aqrawexample
26+
*
27+
* This example requires node-oracledb 4 or later.
28+
*
29+
* This example uses Node 8's async/await syntax.
30+
*
31+
*****************************************************************************/
32+
33+
const oracledb = require('oracledb');
34+
const dbConfig = require('./dbconfig.js');
35+
36+
const queueName = "DEMO_RAW_QUEUE";
37+
38+
async function enq() {
39+
let connection;
40+
41+
try {
42+
connection = await oracledb.getConnection(dbConfig);
43+
const queue = await connection.getQueue(queueName);
44+
queue.enqOptions.visibility = oracledb.AQ_VISIBILITY_IMMEDIATE; // Send a message without requiring a commit
45+
46+
console.log('Enqueuing messages');
47+
48+
const messages = [
49+
"Message 1",
50+
"Message 2",
51+
{
52+
expiration: 5,
53+
payload: "Message 3"
54+
},
55+
"Message 4"
56+
];
57+
await queue.enqMany(messages); // !! Review the enqMany() documentation's caveat before using it !!
58+
59+
} catch (err) {
60+
console.error(err);
61+
} finally {
62+
if (connection) {
63+
try {
64+
await connection.close();
65+
} catch (err) {
66+
console.error(err);
67+
}
68+
}
69+
}
70+
}
71+
72+
async function deq() {
73+
let connection;
74+
75+
try {
76+
connection = await oracledb.getConnection(dbConfig);
77+
const queue = await connection.getQueue(queueName);
78+
queue.deqOptions.visibility = oracledb.AQ_VISIBILITY_IMMEDIATE; // Change the visibility so no explicit commit is required
79+
80+
const messages = await queue.deqMany(5); // get at most 5 messages
81+
console.log("Dequeued " + messages.length + " messages");
82+
for (const msg of messages) {
83+
console.log(msg.payload.toString());
84+
}
85+
86+
} catch (err) {
87+
console.error(err);
88+
} finally {
89+
if (connection) {
90+
try {
91+
await connection.close();
92+
} catch (err) {
93+
console.error(err);
94+
}
95+
}
96+
}
97+
}
98+
99+
enq();
100+
deq();

javascript/node-oracledb/aqobject.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* Copyright (c) 2019, 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+
* aqobject.js
20+
*
21+
* DESCRIPTION
22+
* Oracle Advanced Queuing (AQ) example passing an Oracle Database object
23+
*
24+
* Before running this, a queue allowing an Oracle Database object
25+
* payloads must be created, see
26+
* https://oracle.github.io/node-oracledb/doc/api.html#aqobjexample
27+
*
28+
* This example requires node-oracledb 4 or later.
29+
*
30+
* This example uses Node 8's async/await syntax.
31+
*
32+
*****************************************************************************/
33+
34+
const oracledb = require('oracledb');
35+
const dbConfig = require('./dbconfig.js');
36+
37+
const queueName = "ADDR_QUEUE";
38+
39+
async function enq() {
40+
let connection;
41+
42+
// The message to send.
43+
// The attributes correspond to the USER_ADDRESS_TYPE fields.
44+
const addrData = {
45+
NAME: "scott",
46+
ADDRESS: "The Kennel"
47+
};
48+
49+
try {
50+
connection = await oracledb.getConnection(dbConfig);
51+
const queue = await connection.getQueue(queueName, {payloadType: "USER_ADDRESS_TYPE"});
52+
const message = new queue.payloadTypeClass(addrData);
53+
console.log('Enqueuing: ', addrData);
54+
await queue.enqOne(message);
55+
await connection.commit();
56+
} catch (err) {
57+
console.error(err);
58+
} finally {
59+
if (connection) {
60+
try {
61+
await connection.close();
62+
} catch (err) {
63+
console.error(err);
64+
}
65+
}
66+
}
67+
}
68+
69+
async function deq() {
70+
let connection;
71+
72+
try {
73+
connection = await oracledb.getConnection(dbConfig);
74+
const queue = await connection.getQueue(queueName, {payloadType: "USER_ADDRESS_TYPE"});
75+
const msg = await queue.deqOne(); // wait for a message
76+
await connection.commit();
77+
if (msg) {
78+
console.log('Dequeued: ', msg.payload);
79+
}
80+
} catch (err) {
81+
console.error(err);
82+
} finally {
83+
if (connection) {
84+
try {
85+
await connection.close();
86+
} catch (err) {
87+
console.error(err);
88+
}
89+
}
90+
}
91+
}
92+
93+
enq();
94+
deq();

0 commit comments

Comments
 (0)