OCI Database Service Client for Node.js
The `oci-database` package provides a Node.js client for interacting with the Oracle Cloud Infrastructure (OCI) Database service. It allows developers to programmatically manage database resources such as Autonomous Databases, DB Systems, and various database operations within OCI. This package is part of the larger OCI TypeScript SDK, offering type-safe access to OCI services. Currently at version 2.130.0, the OCI SDK typically follows a frequent release cadence, often aligning with OCI service updates, leading to multiple minor or patch releases per month. Key differentiators include first-party support from Oracle, comprehensive TypeScript type definitions, and integration with OCI's native authentication mechanisms, providing a robust and official way to automate database management tasks in the OCI ecosystem.
Common errors
-
ServiceError: 401 Unauthorized. The provided authentication is invalid. Review the setup documentation for API keys or instance principals.
cause Incorrect API key setup (missing private key, wrong fingerprint, invalid user OCID) or expired session for instance principal.fixVerify `~/.oci/config` entries, API key fingerprint, and `private_key_file` path. Ensure your user OCID is correct. For instance principals, check associated dynamic groups and policy permissions. -
ServiceError: 403 Forbidden. Authorization failed or requested resource not found.
cause The IAM user or instance principal lacks the necessary permissions to perform the requested operation on the target resource or compartment.fixReview OCI IAM policies in the OCI Console. Add the required `allow` statements for the user's group or dynamic group to `manage` or `read` the specific resource type (e.g., `databases`) in the target compartment. -
TypeError: DatabaseClient is not a constructor
cause Incorrect import statement (e.g., `require` for ESM-only package) or `DatabaseClient` is not directly exported as a default.fixEnsure you are using ES module imports (`import { DatabaseClient } from 'oci-database';`) in an environment configured for ESM, or if using CommonJS, verify the export structure of the package. It's usually a named export. -
ServiceError: 404 Not Found. The resource you are trying to access does not exist.
cause The OCID of the resource (e.g., database, compartment) is incorrect, or the client is configured for a different OCI region where the resource does not exist.fixDouble-check the OCID of the resource you are targeting. Verify that the OCI client's configured region matches the region where the resource resides. OCI OCIDs are globally unique but resources are region-bound.
Warnings
- gotcha OCI SDKs heavily rely on proper IAM policies. Operations will fail with 403 Forbidden errors if the user or instance principal lacks the necessary permissions for the specific API calls. Always verify your OCI IAM policies.
- gotcha Authentication details (API keys, config file, instance principals) must be correctly configured. Common issues include incorrect file paths, invalid fingerprints, or missing entries in the OCI config file.
- gotcha Many list operations in OCI SDKs are paginated. If you're expecting more than a single page of results, you must manually implement pagination logic using `opcNextPage` tokens to fetch all items.
- gotcha The `oci-database` client is region-specific. If the client is initialized for a different region than where your resources reside, operations will often result in 'Not Found' errors (404) or 'Unauthorized' (401) if the region configuration is completely off.
Install
-
npm install oci-database -
yarn add oci-database -
pnpm add oci-database
Imports
- DatabaseClient
const DatabaseClient = require('oci-database').DatabaseClient;import { DatabaseClient } from 'oci-database'; - ListDatabasesRequest
import { ListDatabasesRequest } from 'oci-database';import { requests } from 'oci-database'; const request: requests.ListDatabasesRequest = {}; - ConfigFileAuthenticationDetailsProvider
import { ConfigFileAuthenticationDetailsProvider } from 'oci-database';import { auth } from 'oci-common'; const provider = new auth.ConfigFileAuthenticationDetailsProvider();
Quickstart
import { DatabaseClient, requests } from 'oci-database';
import { auth, common } from 'oci-common';
async function listOciDatabases() {
try {
// Load authentication details from the default OCI config file (~/.oci/config)
// Ensure your OCI config file is set up with a profile (e.g., 'DEFAULT') and API key.
const provider = new auth.ConfigFileAuthenticationDetailsProvider();
// The region is typically read from the config file, but can be overridden
// For example, common.Region.US_PHOENIX_1
const client = new DatabaseClient({ authenticationDetailsProvider: provider });
// Specify the compartment to list databases from.
// Replace 'YOUR_COMPARTMENT_OCID' with an actual Compartment OCID.
const compartmentId = process.env.OCI_COMPARTMENT_OCID ?? 'YOUR_COMPARTMENT_OCID';
if (compartmentId === 'YOUR_COMPARTMENT_OCID') {
console.warn('WARNING: Please set OCI_COMPARTMENT_OCID environment variable or replace placeholder.');
}
const listRequest: requests.ListDatabasesRequest = {
compartmentId: compartmentId,
// You can add filters like lifecycleState, dbHomeId, etc.
// lifecycleState: requests.ListDatabasesRequest.LifecycleState.Available
};
console.log(`Listing databases in compartment: ${compartmentId}...`);
const response = await client.listDatabases(listRequest);
if (response.items && response.items.length > 0) {
console.log(`Found ${response.items.length} databases:`)
response.items.forEach(db => {
console.log(`- ${db.dbName} (OCID: ${db.id}, State: ${db.lifecycleState})`);
});
} else {
console.log('No databases found in the specified compartment.');
}
} catch (error) {
console.error('Error listing databases:', error);
if (error instanceof common.ServiceError) {
console.error(`Service Error Details: Status=${error.statusCode}, Code=${error.serviceCode}, Message=${error.message}`);
}
}
}
listOciDatabases();