OCI Database Service Client for Node.js

2.130.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart initializes the `DatabaseClient` using API key authentication from a configuration file and lists all databases within a specified compartment in OCI.

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();

view raw JSON →