Oracle NoSQL Database Node.js Driver
The `oracle-nosqldb` package provides the Node.js SDK for interacting with Oracle NoSQL Database, supporting both the Oracle NoSQL Database Cloud Service and on-premise deployments. Currently at version 5.5.3, the library offers comprehensive interfaces for developing JavaScript and TypeScript applications, enabling operations like table creation, data insertion, retrieval, and schema management. While a strict release cadence isn't specified, it appears to follow major Node.js ecosystem updates and NoSQL database feature releases. A key differentiator is its dual support for cloud and and on-premise environments, offering a consistent API across deployment models. Developers need Node.js 12 or higher, and for TypeScript projects, TypeScript 5.0.x or higher is recommended.
Common errors
-
Error: Cannot find module 'oracle-nosqldb'
cause The `oracle-nosqldb` package has not been installed or is not accessible in the current project's `node_modules`.fixRun `npm install oracle-nosqldb` in your project directory. -
Error: Missing authentication credentials.
cause When connecting to Oracle NoSQL Database Cloud Service, the `auth` configuration in `NoSQLClient` is incomplete or incorrect, or OCI environment variables are not set.fixProvide all required OCI credentials (user OCID, tenant OCID, private key, fingerprint) in the `auth` object or ensure they are correctly sourced as environment variables. Check OCI IAM policies for necessary permissions. -
Error: connect ECONNREFUSED <address>:<port>
cause The application failed to connect to the NoSQL Database proxy server, often due to the server not running, being on a different host/port, or firewall issues.fixVerify that the Oracle NoSQL Database proxy server is running, listening on the specified host and port (e.g., `localhost:8080`), and network firewalls are not blocking the connection. -
TypeError: Class constructor NoSQLClient cannot be invoked without 'new'
cause The `NoSQLClient` class is being called as a function (e.g., `NoSQLClient(config)`) instead of instantiated with the `new` keyword.fixAlways create an instance of `NoSQLClient` using `const client = new NoSQLClient(config);`.
Warnings
- gotcha The SDK requires Node.js version 12.0.0 or higher. Using older versions will result in runtime errors.
- gotcha When using TypeScript, version 5.0.x or higher is required for optimal type inference and compatibility with the shipped type declarations.
- gotcha Connecting to Oracle NoSQL Database Cloud Service necessitates proper Oracle Cloud Infrastructure (OCI) authentication configuration, including a user, group policy, and API signing keys. Incorrect credentials or policies will lead to authentication failures.
- gotcha For on-premise Oracle NoSQL Database deployments, a running Oracle NoSQL Database instance and a separate NoSQL Proxy server are required for the Node.js SDK to connect. The SDK communicates via the HTTP proxy, not directly with the database nodes.
- gotcha The quickstart examples and initial setup often assume a non-secure configuration for simplicity. Secure configurations for both cloud and on-premise environments are more complex and require additional setup for certificates, secure proxies, and authentication.
Install
-
npm install oracle-nosqldb -
yarn add oracle-nosqldb -
pnpm add oracle-nosqldb
Imports
- NoSQLClient
const NoSQLClient = require('oracle-nosqldb');import { NoSQLClient } from 'oracle-nosqldb'; - Region
import Region from 'oracle-nosqldb';
import { Region } from 'oracle-nosqldb'; - ServiceType
const ServiceType = require('oracle-nosqldb');import { ServiceType } from 'oracle-nosqldb';
Quickstart
/*
* 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;
// --- Configuration (replace with your actual credentials and endpoint) ---
// For Oracle NoSQL Database Cloud Service:
// const config = {
// serviceType: ServiceType.CLOUD,
// region: Region.US_PHOENIX_1, // e.g., Region.US_PHOENIX_1
// auth: {
// userName: process.env.OCI_USER_OCID ?? '',
// tenantId: process.env.OCI_TENANT_OCID ?? '',
// privateKey: process.env.OCI_PRIVATE_KEY_PATH ?? '',
// fingerprint: process.env.OCI_FINGERPRINT ?? '',
// passPhrase: process.env.OCI_PRIVATE_KEY_PASSPHRASE ?? '' // Optional
// }
// };
// For Oracle NoSQL Cloud Simulator or On-Premise:
const config = {
serviceType: ServiceType.KVSTORE,
endpoint: process.env.NOSQL_ENDPOINT ?? 'localhost:8080' // Default for simulator/on-prem
};
async function runQuickstart() {
let client;
try {
client = new NoSQLClient(config);
const tableName = 'quickstartTable';
console.log(`Creating table: ${tableName}`);
await client.tableDDL(`CREATE TABLE ${tableName} (id LONG, name STRING, PRIMARY KEY (id))`);
console.log('Table created.');
const row = { id: 1, name: 'Hello, Oracle NoSQL!' };
console.log(`Putting row: ${JSON.stringify(row)}`);
await client.put(tableName, row);
console.log('Row put.');
console.log(`Getting row with id: ${row.id}`);
const result = await client.get(tableName, { id: row.id });
console.log(`Got row: ${JSON.stringify(result.value)}`);
console.log(`Dropping table: ${tableName}`);
await client.tableDDL(`DROP TABLE ${tableName}`);
console.log('Table dropped.');
} catch (error) {
console.error('Error:', error.message);
} finally {
if (client) {
client.close();
}
}
}
runQuickstart();