Node.js SDK for Oracle NoSQL Database
This is version 5.5 of the Node.js SDK for
the Oracle NoSQL Database.
The SDK provides interfaces, documentation, and examples to develop Node.js
applications that use the
Oracle NoSQL Database Cloud Service
and the On-Premise Oracle NoSQL Database. You can use the SDK to access Oracle NoSQL Database
in JavaScript or TypeScript.
You may install this SDK either as a dependency of your project:
npm install oracle-nosqldb --save
or globally:
sudo npm install -g oracle-nosqldb
See the API and user guide documentation.
When requesting help please be sure to include as much detail as possible,
including version of the SDK and simple, standalone example code as needed.
For detailed information and API documentation about using the SDK in different
environments see the documentation
The following is a quick start tutorial to run a simple program in the supported
environments. The same template source code is used for all environments. The
first step is to cut the program below and paste it into an editor for minor
modifications. The instructions assume that is stored as quickstart.js, but you
can use any name you like. The quickstart example supports 3 environments:
See running quickstart for instructions on how to edit and
run the quickstart program in different environments.
/*
* A simple example that
* - creates a table
* - inserts a row using the put() operation
* - reads a row using the get() operation
* - drops the table
*
* To run:
* 1. Edit for your target environment and credentials
* 2. Run it:
* node quickstart.js cloud|cloudsim|kvstore
*
* Use 'cloud' for the Oracle NoSQL Database Cloud Service
* Use 'cloudsim' for the Oracle NoSQL Cloud Simulator
* Use 'kvstore' for the Oracle NoSQL Database on-premise
*/
'use strict';
const NoSQLClient = require('oracle-nosqldb').NoSQLClient;
const Region = require('oracle-nosqldb').Region;
const ServiceType = require('oracle-nosqldb').ServiceType;
// Target table used by this example
const TABLE_NAME = 'NodeQuickstart';
const USAGE = 'Usage: node quickstart.js cloud|cloudsim|kvstore';
async function quickstart() {
let client;
try {
const args = process.argv;
let serviceType = args[2];
if (!serviceType) {
return console.error(USAGE);
}
// Set up access to the cloud service
client = createClient(serviceType);
console.log('Created NoSQLClient instance');
await run(client);
console.log('Success!');
} catch (err) {
console.error(' Error: ' + err.message);
console.error(' from: ');
console.error(err.operation.api.name);
} finally {
if (client) {
client.close();
}
}
}
/*
* This function encapsulates environmental differences and returns a
* client handle to use for data operations.
*/
function createClient(serviceType) {
switch(serviceType) {
case 'cloud':
return new NoSQLClient({
/*
* EDIT:
* 1. use desired region id
* 2. your tenancy's OCID, user's OCID
* 3. privateKeyFile path
* 4. fingerprint for uploaded public key
* 5. optional passphrase. If your key has none, delete this
* line (and the leading ',').
*/
region: Region.<your-region-here>,
auth: {
iam: {
tenantId: 'your tenancy OCID',
userId: 'your user OCID',
fingerprint: 'your public key fingerprint',
privateKeyFile: 'path to private key file',
passphrase: 'pass phrase if set for your private key'
}
}
});
case 'cloudsim':
/*
* EDIT: if the endpoint does not reflect how the Cloud
* Simulator has been started, modify it accordingly.
*/
return new NoSQLClient({
serviceType: ServiceType.CLOUDSIM,
endpoint: 'localhost:8080'
});
case 'kvstore':
/*
* EDIT: if the endpoint does not reflect how the Proxy
* Server has been started, modify it accordingly.
*/
return new NoSQLClient({
serviceType: ServiceType.KVSTORE,
endpoint: 'localhost:80'
});
default:
throw new Error('Unknown service type: ' + serviceType);
}
}
/*
* Create a table, read and write a record
*/
async function run(client) {
const createDDL = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} \
(cookie_id LONG, audience_data JSON, PRIMARY KEY(cookie_id))`;
console.log('Create table ' + TABLE_NAME);
let res = await client.tableDDL(createDDL, {
tableLimits: {
readUnits: 50,
writeUnits: 50,
storageGB: 25
}
});
console.log(' Creating table %s', res.tableName);
console.log(' Table state: %s', res.tableState.name);
// Wait for the operation completion
await client.forCompletion(res);
console.log(' Table %s is created', res.tableName);
console.log(' Table state: %s', res.tableState.name);
// Write a record
console.log('\nWrite a record');
res = await client.put(TABLE_NAME, {
cookie_id: 456,
audience_data: {
ipaddr: '10.0.00.yyy',
audience_segment: {
sports_lover: '2019-01-05',
foodie: '2018-12-31'
}
}
});
if (res.consumedCapacity) {
console.log(' Write used: %O', res.consumedCapacity);
}
// Read a record
console.log('\nRead a record');
res = await client.get(TABLE_NAME, { cookie_id: 456 });
console.log(' Got record: %O', res.row);
if (res.consumedCapacity) {
console.log(' Read used: %O', res.consumedCapacity);
}
// Drop the table
console.log('\nDrop table');
const dropDDL = `DROP TABLE ${TABLE_NAME}`;
res = await client.tableDDL(dropDDL);
console.log(' Dropping table %s', res.tableName);
// Wait for the table to be removed
await client.forCompletion(res);
console.log(' Operation completed');
console.log(' Table state is %s', res.tableState.name);
}
quickstart();
Running against the Cloud Service requires an Oracle Cloud account. See
Configuring for the Cloud Service for information on getting an account and acquiring required credentials.
Edit quickstart.js and add your information in the ‘cloud’ section of the
createClient() function.
Decide the region you want to use and add that in the same section in the
value for the region key.
Run the program:
node quickstart.js cloud
If you would prefer to create a configuration file for credentials instead of
modifying the program put credentials in a file (see
Using a Configuration File). Then modify quickstart.js to use the file:
Replace
iam: {
tenantId: 'your tenancy OCID',
userId: 'your user OCID',
fingerprint: 'your public key fingerprint',
privateKeyFile: 'path to private key file',
passphrase: 'pass phrase if set for your private key'
}
with
iam: {
configFile: 'path-to-config-file',
profileName: 'DEFAULT'
}
Running against the Oracle NoSQL Cloud Simulator requires a running Cloud
Simulator instance. See Using the Cloud Simulator for information on how to download
and start the Cloud Simulator.
Start the Cloud Simulator based on instructions above. Note the HTTP port
used. By default it is 8080 on localhost.
The quickstart.js program defaults to localhost:8080 so if the Cloud
Simulator was started using default values no editing is required.
Run the program:
node quickstart.js cloudsim
Running against the Oracle NoSQL Database on-premise requires a running
Oracle NoSQL Database instance as well as a running NoSQL Proxy server instance.
The program will connect to the proxy server.
See Connecting to an On-Premise Oracle NoSQL Database for information on how to download
and start the database instance and proxy server. The database and proxy should
be started without security enabled for this quickstart program to operate
correctly. A secure configuration requires a secure proxy and more complex
configuration.
Start the Oracle NoSQL Database and proxy server based on instructions above.
Note the HTTP port used. By default the endpoint is localhost:80.
The quickstart.js program defaults to localhost:80. If the proxy was started
using a different host or port edit the settings accordingly.
Run the program:
node quickstart.js kvstore
The examples are located in the examples directory. There are two sets of
examples: JavaScript in examples/javascript directory and TypeScript in
examples/typescript directory.
You can run the examples:
examples/config directory contains template JSON configuration files used to
run the examples:
Alternatively, when using cloud service with default configuration as
described in
Configuring the SDK, you may run examples without providing JSON
configuration file. This assumes that your credentials and your region
identifier are present in an OCI config file ~/.oci/config.
JavaScript examples are in examples/javascript directory. You can copy
all files in this directory to a separate directory. The SDK package
oracle-nosqldb is the only dependency for these examples. You may install
it via package.json in the same directory (alternatively, you may install
the SDK globally). To run an example:
npm install
node <example.js> [optinal_config_file.json]
E.g.
npm install
$ node basic_example.js config.json
TypeScript examples are in examples/typescript directory. There are 4
examples: table_ops.ts, single_row_ops.ts, multi_row_ops.ts and
query_ops.ts. They also share some common functionality (see setup.ts and
common.ts). package.json in the same directory contains scripts to build
and run the examples. You may copy all files in this directory to a separate
directory.
Use npm to install the dependencies, then you can run each example as
follows:
npm install
npx tsx <example.ts> [optional_config_file.json]
E.g.
npm install
npx tsx single_row_ops.ts config.json
The commands above use tsx which is
installed as one of the dependencies.
Alternatively, you can build the examples into JavaScript. Then
run the resulting .js files, which are created in the dist directory, e.g.:
npm run build
node dist/single_row_ops.js config.json
See package.json for more details.
Please see the LICENSE file included in the top-level directory of the
package for a copy of the license and additional information.
The THIRD_PARTY_LICENSES file contains third
party notices and licenses.
See CONTRIBUTING for details.
See SECURITY for details.