PouchDB HTTP Adapter
pouchdb-adapter-http is a PouchDB plugin that enables the core PouchDB library to use HTTP as its data store, allowing it to seamlessly communicate and synchronize with remote CouchDB or CouchDB-like databases. This adapter is crucial for applications requiring offline-first capabilities combined with cloud synchronization. Currently stable at version 9.0.0, PouchDB maintains a consistent release cadence with frequent patch updates and less frequent major/minor versions. Key differentiators include its ability to abstract away the complexities of HTTP requests and responses, providing a familiar PouchDB API for remote data operations, and its design for use in both Node.js and browser environments, making it a versatile component within the PouchDB ecosystem.
Common errors
-
Error: Adapter 'http' is not installed.
cause The `pouchdb-adapter-http` plugin was not registered with the main PouchDB instance before being used.fixAdd `import HttpPouch from 'pouchdb-adapter-http'; PouchDB.plugin(HttpPouch);` to your application's initialization code. Ensure it runs before creating any PouchDB instances that use the 'http' or 'https' adapter. -
Access to fetch at 'http://127.0.0.1:5984/mydb' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
cause The remote CouchDB server is not configured to allow cross-origin requests from the origin where your PouchDB client application is running.fixConfigure the CouchDB server to enable CORS. This typically involves modifying `local.ini` or `default.ini` to set `[httpd]` bind address and `[cors]` options (origins, methods, headers). For development, you might use a proxy.
Warnings
- breaking PouchDB 9.0.0 is a major release with 202 PRs merged. While specific changes for 'pouchdb-adapter-http' are not detailed in the excerpt, major version bumps typically introduce breaking changes across the ecosystem. Users should consult the full PouchDB changelog before upgrading from 8.x.
- breaking PouchDB 8.0.0 initiated a transition to modern ES6+ JavaScript syntax, including refactors to use native JS classes instead of prototypes. While 'pouchdb-adapter-http' primarily acts as a plugin, direct interaction with its internal structure or extending it with older patterns may be affected.
- gotcha When connecting a browser-based PouchDB client to a remote CouchDB server, Cross-Origin Resource Sharing (CORS) policies can block requests. This is a common security feature that prevents unauthorized cross-origin data access.
- gotcha Failing to register the `pouchdb-adapter-http` plugin with `PouchDB.plugin()` before attempting to create a database instance using the 'http' or 'https' adapter will result in an error indicating the adapter is not found.
Install
-
npm install pouchdb-adapter-http -
yarn add pouchdb-adapter-http -
pnpm add pouchdb-adapter-http
Imports
- PouchDB
const PouchDB = require('pouchdb');import PouchDB from 'pouchdb';
- HttpPouch
const HttpPouch = require('pouchdb-adapter-http');import HttpPouch from 'pouchdb-adapter-http';
- PouchDB.plugin
const PouchDB = require('pouchdb'); require('pouchdb-adapter-http'); // Adapter is not registeredimport PouchDB from 'pouchdb'; import HttpPouch from 'pouchdb-adapter-http'; PouchDB.plugin(HttpPouch);
Quickstart
import PouchDB from 'pouchdb';
import HttpPouch from 'pouchdb-adapter-http';
PouchDB.plugin(HttpPouch);
// Create a PouchDB instance that connects to a remote CouchDB via HTTP
const remoteDbUrl = process.env.COUCHDB_URL ?? 'http://127.0.0.1:5984/mydb';
const db = new PouchDB(remoteDbUrl);
async function runExample() {
try {
// Add a document
const doc = {
_id: 'mydoc-1',
title: 'Hello PouchDB HTTP',
description: 'This document is stored remotely.'
};
const response = await db.put(doc);
console.log('Document added:', response);
// Get the document
const fetchedDoc = await db.get('mydoc-1');
console.log('Document fetched:', fetchedDoc);
// You can also listen to changes, replicate, etc.
console.log(`PouchDB adapter in use: ${db.adapter}`);
} catch (err) {
console.error('Error:', err);
}
}
runExample();