|
| 1 | +/* Copyright (c) 2024, Oracle and/or its affiliates. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * This software is dual-licensed to you under the Universal Permissive License |
| 6 | + * (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
| 7 | + * 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 8 | + * either license. |
| 9 | + * |
| 10 | + * If you elect to accept the software under the Apache License, Version 2.0, |
| 11 | + * the following applies: |
| 12 | + * |
| 13 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 | + * you may not use this file except in compliance with the License. |
| 15 | + * You may obtain a copy of the License at |
| 16 | + * |
| 17 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 18 | + * |
| 19 | + * Unless required by applicable law or agreed to in writing, software |
| 20 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 21 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | + * See the License for the specific language governing permissions and |
| 23 | + * limitations under the License. |
| 24 | + * |
| 25 | + * NAME |
| 26 | + * soda2.js |
| 27 | + * |
| 28 | + * DESCRIPTION |
| 29 | + * Simple Oracle Document Access (SODA) example with SodaDocumentCursor |
| 30 | + * asyncIterators. |
| 31 | + * |
| 32 | + * Requires Oracle Database and Oracle Client 18.3, or higher. |
| 33 | + * The user must have been granted the SODA_APP and CREATE TABLE privileges. |
| 34 | + * https://node-oracledb.readthedocs.io/en/latest/user_guide/soda.html#sodaoverview |
| 35 | + * |
| 36 | + *****************************************************************************/ |
| 37 | + |
| 38 | +'use strict'; |
| 39 | + |
| 40 | +Error.stackTraceLimit = 50; |
| 41 | + |
| 42 | +const oracledb = require('oracledb'); |
| 43 | +const dbConfig = require('./dbconfig.js'); |
| 44 | + |
| 45 | +// This example requires node-oracledb Thick mode. |
| 46 | +// |
| 47 | +// Thick mode requires Oracle Client or Oracle Instant Client libraries. On |
| 48 | +// Windows and macOS Intel you can specify the directory containing the |
| 49 | +// libraries at runtime or before Node.js starts. On other platforms (where |
| 50 | +// Oracle libraries are available) the system library search path must always |
| 51 | +// include the Oracle library path before Node.js starts. If the search path |
| 52 | +// is not correct, you will get a DPI-1047 error. See the node-oracledb |
| 53 | +// installation documentation. |
| 54 | +let clientOpts = {}; |
| 55 | +// On Windows and macOS Intel platforms, set the environment |
| 56 | +// variable NODE_ORACLEDB_CLIENT_LIB_DIR to the Oracle Client library path |
| 57 | +if (process.platform === 'win32' || (process.platform === 'darwin' && process.arch === 'x64')) { |
| 58 | + clientOpts = { libDir: process.env.NODE_ORACLEDB_CLIENT_LIB_DIR }; |
| 59 | +} |
| 60 | +oracledb.initOracleClient(clientOpts); // enable node-oracledb Thick mode |
| 61 | + |
| 62 | +// The general recommendation for simple SODA usage is to enable autocommit |
| 63 | +oracledb.autoCommit = true; |
| 64 | + |
| 65 | +async function run() { |
| 66 | + let connection, collection; |
| 67 | + try { |
| 68 | + connection = await oracledb.getConnection(dbConfig); |
| 69 | + if (oracledb.oracleClientVersion < 1803000000) { |
| 70 | + throw new Error('node-oracledb SODA requires Oracle Client libraries 18.3 or greater'); |
| 71 | + } |
| 72 | + |
| 73 | + if (connection.oracleServerVersion < 1803000000) { |
| 74 | + throw new Error('node-oracledb SODA requires Oracle Database 18.3 or greater'); |
| 75 | + } |
| 76 | + |
| 77 | + const soda = connection.getSodaDatabase(); |
| 78 | + collection = await soda.createCollection("Test"); |
| 79 | + console.log('Created a SODA collection\n'); |
| 80 | + const data = [ |
| 81 | + { name: "John", age: 57 }, |
| 82 | + { name: "Sally", age: 53 } |
| 83 | + ]; |
| 84 | + await collection.insertMany(data); |
| 85 | + const cursor = await collection.find().getCursor(); |
| 86 | + console.log('Retrieved SODA document contents as an object using SodaDocumentCursor:'); |
| 87 | + // Use the asyncIterator for the sodaDocumentCursor object |
| 88 | + for await (const doc of cursor) { |
| 89 | + console.log(doc.getContent()); |
| 90 | + } |
| 91 | + await cursor.close(); |
| 92 | + } catch (err) { |
| 93 | + console.error(err); |
| 94 | + } finally { |
| 95 | + if (collection) { |
| 96 | + // Drop the collection |
| 97 | + const res = await collection.drop(); |
| 98 | + if (res.dropped) { |
| 99 | + console.log('\nThe collection was dropped'); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (connection) { |
| 104 | + await connection.close(); |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | +} |
| 110 | + |
| 111 | +run(); |
0 commit comments