CouchDB Database Ensurer

2.1.0 · active · verified Wed Apr 22

The `couchdb-ensure` package provides a focused Node.js utility for managing CouchDB database existence. Its primary function is to check if a CouchDB database at a specified URL already exists and, if not, to create it. This makes it an ideal tool for initial application setup, automated testing environments, or any scenario requiring a guaranteed database presence before operations begin. The current stable version is 2.1.0, released in October 2021, indicating a mature and stable codebase with an infrequent release cadence, typically driven by updates to its core dependencies like the `nano` CouchDB client. Unlike full-featured ORMs or database management tools, `couchdb-ensure` differentiates itself by its single-purpose API and a convenient command-line interface (CLI) for rapid database initialization, providing a lightweight and efficient solution without unnecessary overhead. It primarily utilizes CommonJS for module exports.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `couchdb-ensure` with both a traditional callback and an `async/await` pattern (using `promisify`) to ensure a CouchDB database exists, including basic error handling.

const ensure = require('couchdb-ensure');

// Basic usage with callback
ensure('http://localhost:5984/my_app_db', (error, response) => {
  if (error) {
    console.error('Error ensuring database:', error);
    // Handle specific errors, e.g., connection issues, authentication failures
    if (error.code === 'ECONNREFUSED') {
      console.error('CouchDB server not running or wrong URL.');
    }
    return;
  }
  console.log('Database ensured:', response);
  // response will contain { ok: true } if created or if it already existed
});

// For an async/await pattern (requires promisify or wrapper)
const { promisify } = require('util');
const ensurePromise = promisify(ensure);

async function setupDatabase() {
  try {
    const response = await ensurePromise('http://localhost:5984/another_db');
    console.log('Another database ensured successfully:', response);
  } catch (error) {
    console.error('Failed to ensure another database:', error);
  }
}

// setupDatabase();

view raw JSON →