Skip to content

Commit ed8b816

Browse files
committed
Add async/await examples
1 parent cd9c6fa commit ed8b816

File tree

5 files changed

+335
-10
lines changed

5 files changed

+335
-10
lines changed

INSTALL.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ module.exports = {
358358
Run one of the examples:
359359

360360
```
361-
node select1.js
361+
node example.js
362362
```
363363

364364
*Note:* Remember to set `LD_LIBRARY_PATH` or equivalent first.
@@ -493,7 +493,7 @@ module.exports = {
493493
Run one of the examples:
494494

495495
```
496-
node select1.js
496+
node example.js
497497
```
498498

499499
*Note:* Remember to set `LD_LIBRARY_PATH` or equivalent first.
@@ -608,7 +608,7 @@ module.exports = {
608608
Run one of the examples:
609609

610610
```
611-
node select1.js
611+
node example.js
612612
```
613613

614614
### <a name="instosx"></a> 3.5 Node-oracledb Installation on macOS
@@ -726,7 +726,7 @@ module.exports = {
726726
Run one of the examples:
727727

728728
```
729-
node select1.js
729+
node example.js
730730
```
731731

732732
### <a name="instwin"></a> 3.6 Node-oracledb Installation on Windows with Instant Client ZIP files
@@ -853,7 +853,7 @@ module.exports = {
853853
Run one of the examples:
854854

855855
```
856-
node select1.js
856+
node example.js
857857
```
858858

859859
### <a name="instwinoh"></a> 3.7 Node-oracledb Installation on Windows with a Local Database or Full Client
@@ -942,7 +942,7 @@ module.exports = {
942942
Run one of the examples:
943943

944944
```
945-
node select1.js
945+
node example.js
946946
```
947947

948948
### <a name="winbins"></a> 3.8 Copying node-oracledb Binaries on Windows
@@ -1097,7 +1097,7 @@ module.exports = {
10971097
Run one of the examples:
10981098

10991099
```
1100-
node select1.js
1100+
node example.js
11011101
```
11021102

11031103
### <a name="instsolarisx8664"></a> 3.10 Node-oracledb Installation on Oracle Solaris x86-64 (64-Bit) with Instant Client ZIP files
@@ -1216,7 +1216,7 @@ module.exports = {
12161216
Run one of the examples:
12171217

12181218
```
1219-
node select1.js
1219+
node example.js
12201220
```
12211221

12221222
### <a name="github"></a> 3.11 Node-oracledb Installation from Source Code

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ See [Documentation for the Oracle Database Node.js Add-on][32] and the [CHANGELO
2323
## <a name="examples"></a> Examples
2424

2525
See the [examples][30] directory. Start with
26-
[examples/select1.js][31].
26+
[examples/example.js][31].
2727

2828
## <a name="help"></a> Help
2929

@@ -65,7 +65,7 @@ limitations under the License.
6565
[3]: https://github.com/oracle/node-oracledb/issues
6666
[4]: https://oracle.github.io/node-oracledb
6767
[30]: https://github.com/oracle/node-oracledb/blob/master/examples
68-
[31]: https://github.com/oracle/node-oracledb/blob/master/examples/select1.js#L35
68+
[31]: https://github.com/oracle/node-oracledb/blob/master/examples/example.js#L32
6969
[32]: https://oracle.github.io/node-oracledb/doc/api.html
7070
[33]: https://github.com/oracle/node-oracledb/blob/master/CHANGELOG.md
7171
[34]: https://github.com/oracle/node-oracledb/blob/master/test/README.md

examples/example.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
* example.js
20+
*
21+
* DESCRIPTION
22+
* A basic node-oracledb example using Node.js 8's async/await syntax.
23+
*
24+
* For a connection pool example see webapp.js
25+
* For a ResultSet example see resultset2.js
26+
* For a query stream example see selectstream.js
27+
* For a Promise example see promises.js
28+
* For a callback example see select1.js
29+
*
30+
*****************************************************************************/
31+
32+
var oracledb = require('oracledb');
33+
var dbConfig = require('./dbconfig.js');
34+
35+
async function run() {
36+
let connection;
37+
38+
try {
39+
40+
let sql, binds, options, result;
41+
42+
connection = await oracledb.getConnection( {
43+
user : dbConfig.user,
44+
password : dbConfig.password,
45+
connectString : dbConfig.connectString
46+
});
47+
48+
// Create a table
49+
50+
await connection.execute(
51+
`BEGIN
52+
EXECUTE IMMEDIATE 'DROP TABLE mytab';
53+
EXCEPTION
54+
WHEN OTHERS THEN
55+
IF SQLCODE NOT IN (-00942) THEN
56+
RAISE;
57+
END IF;
58+
END;`);
59+
60+
await connection.execute(
61+
`CREATE TABLE mytab (id NUMBER, data VARCHAR2(20))`);
62+
63+
// Insert some data
64+
65+
sql = `INSERT INTO mytab VALUES (:1, :2)`;
66+
67+
binds = [ [101, "Alpha" ], [102, "Beta" ], [103, "Gamma" ] ];
68+
69+
// For a complete list of options see the documentation.
70+
options = {
71+
autoCommit: true,
72+
// batchErrors: true, // continue processing even if there are data errors
73+
bindDefs: [
74+
{ type: oracledb.NUMBER },
75+
{ type: oracledb.STRING, maxSize: 20 }
76+
]
77+
};
78+
79+
result = await connection.executeMany(sql, binds, options);
80+
81+
console.log("Number of rows inserted:", result.rowsAffected);
82+
83+
// Query the data
84+
85+
sql = `SELECT * FROM mytab`;
86+
87+
binds = {};
88+
89+
// For a complete list of options see the documentation.
90+
options = {
91+
outFormat: oracledb.OBJECT // query result format
92+
// extendedMetaData: true, // get extra metadata
93+
// fetchArraySize: 100 // internal buffer allocation size for tuning
94+
};
95+
96+
result = await connection.execute(sql, binds, options);
97+
98+
console.log("Column metadata: ", result.metaData);
99+
console.log("Query results: ");
100+
console.log(result.rows);
101+
102+
} catch (err) {
103+
console.error(err);
104+
} finally {
105+
if (connection) {
106+
try {
107+
await connection.close();
108+
} catch (err) {
109+
console.error(err);
110+
}
111+
}
112+
}
113+
}
114+
115+
run();

examples/select1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* Scripts to create the HR schema can be found at:
2626
* https://github.com/oracle/db-sample-schemas
2727
*
28+
* For an async/await example see selectawait.js
2829
* For a connection pool example see webapp.js
2930
* For a ResultSet example see resultset2.js
3031
* For a query stream example see selectstream.js

0 commit comments

Comments
 (0)