PouchDB HTTP Request Adapter

4.2.0 · active · verified Wed Apr 22

`pouchdb-req-http-query` is a utility within the PouchDB ecosystem designed to bridge CouchDB-style request objects with PouchDB's internal HTTP request handling. It allows developers to construct a request object formatted similarly to a CouchDB request and then execute it against a given PouchDB database instance as an HTTP operation. This package is currently at version 4.2.0 and receives maintenance updates for bug fixes and dependency management, indicating an active but mature status. Its key differentiators include its role in facilitating lower-level HTTP interactions with PouchDB, particularly when integrating with systems expecting CouchDB's request structures, and its inclusion in the PouchDB monorepo.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pouchdb-req-http-query` to perform CouchDB-style GET and POST requests against a local PouchDB instance, including fetching all documents and inserting a new one.

import PouchDB from 'pouchdb-node';
import httpQuery from 'pouchdb-req-http-query';

async function runHttpQueryExample() {
  const db = new PouchDB('http-query-test-db');

  try {
    // Example 1: Fetch all documents (equivalent to GET /db/_all_docs)
    const allDocsRequest = {
      path: '_all_docs',
      method: 'GET',
      query: {
        include_docs: true,
      },
      headers: {
        'Accept': 'application/json'
      }
    };

    console.log('Executing CouchDB-style GET _all_docs request...');
    const allDocsResponse = await httpQuery(db, allDocsRequest);
    console.log('GET _all_docs response:', JSON.stringify(allDocsResponse, null, 2));

    // Example 2: Insert a new document (equivalent to POST /db)
    const insertDocRequest = {
      path: '', // Empty path for POST to create new doc at DB root
      method: 'POST',
      body: {
        message: 'Hello from http-query!',
        timestamp: new Date().toISOString()
      },
      headers: {
        'Content-Type': 'application/json'
      }
    };

    console.log('\nExecuting CouchDB-style POST request to insert document...');
    const insertResponse = await httpQuery(db, insertDocRequest);
    console.log('POST insert response:', JSON.stringify(insertResponse, null, 2));

    // Example 3: Get a specific document (equivalent to GET /db/doc_id)
    if (insertResponse.id) {
      const getDocRequest = {
        path: insertResponse.id,
        method: 'GET',
        headers: {
          'Accept': 'application/json'
        }
      };
      console.log(`\nExecuting CouchDB-style GET request for document '${insertResponse.id}'...`);
      const getResponse = await httpQuery(db, getDocRequest);
      console.log(`GET document '${insertResponse.id}' response:`, JSON.stringify(getResponse, null, 2));
    }

  } catch (error) {
    console.error('Error during http query example:', error);
  } finally {
    await db.destroy();
    console.log('\nDatabase destroyed.');
  }
}

runHttpQueryExample();

view raw JSON →