PouchDB
PouchDB is an open-source JavaScript database designed for offline-first web applications, enabling data storage directly in the user's browser (or Node.js environment) and seamless synchronization with CouchDB-compatible servers. Currently at stable version 9.0.0 (released May 24, 2024), the project releases major versions periodically, with patch releases addressing bugs and minor enhancements more frequently. Its key differentiators include its robust replication engine, automatic offline data handling, and its ability to work across various browser and Node.js environments without requiring a persistent network connection, providing a highly resilient and performant user experience even without network connectivity.
Common errors
-
Uncaught Error: Adapter is not installed. Did you remember to add pouchdb-adapter-idb or pouchdb-adapter-websql?
cause PouchDB couldn't find a suitable storage adapter for the environment.fixInstall and register the appropriate adapter (e.g., `npm install pouchdb-adapter-indexeddb` then `PouchDB.plugin(PouchdbAdapterIndexedDB);`) or use a preset like `pouchdb-browser` which includes adapters. -
TypeError: PouchDB is not a constructor
cause Attempting to instantiate PouchDB incorrectly, often due to CommonJS vs. ES module import mismatch, or trying to access it via a named import when it's a default export.fixFor ES Modules, use `import PouchDB from 'pouchdb';`. For CommonJS (older Node.js), use `const PouchDB = require('pouchdb');`. Avoid destructuring for the main `PouchDB` constructor. -
ReferenceError: require is not defined
cause Using CommonJS `require()` syntax in a browser environment without a bundler, or in a pure ES module context in Node.js.fixIf in a browser, use a bundler (Webpack, Rollup) to transpile CommonJS, or use the pre-built browser-friendly bundles. If in a Node.js ES module, switch to `import` statements. -
Error: Cannot use "indexeddb" adapter without manual data migration from "idb" (legacy)
cause Trying to open a database created with the old 'idb' adapter using the newer 'indexeddb' adapter without migrating data, as of PouchDB 7.2.2.fixImplement a manual data migration process to move data from databases created with the 'idb' adapter to the 'indexeddb' adapter, or stick to the 'idb' adapter if backward compatibility with existing data is critical and you don't need the new features/performance.
Warnings
- breaking PouchDB 9.0.0 introduces a default limit of 25 to the `.find()` method, which is a backwards-incompatible change. It also includes other internal refactors and improvements to the IndexedDB adapter's stability and performance.
- breaking PouchDB 8.0.0 began a significant refactor to embrace modern ES6+ JavaScript syntax, utilizing native JS classes instead of prototypes. This can lead to compatibility issues with older build tools, Node.js versions, or environments expecting CommonJS prototypes.
- breaking PouchDB 7.2.2 introduced a new 'indexeddb' adapter that uses native indexes. This new adapter is NOT backwards compatible with data stored using the older 'idb' adapter, and data migration is a manual process left to the developer.
- breaking Starting with PouchDB 7.0.0 (released June 21, 2018), the WebSQL adapter was removed from the default builds to reduce package size and focus development efforts. It is no longer included by default.
- gotcha PouchDB is an abstraction layer that requires an underlying storage adapter. While the main `pouchdb` package includes default adapters (IndexedDB in browser, LevelDB in Node.js), certain environments or specific needs might require explicit adapter installation and plugin registration.
Install
-
npm install pouchdb -
yarn add pouchdb -
pnpm add pouchdb
Imports
- PouchDB
const PouchDB = require('pouchdb');import PouchDB from 'pouchdb';
- PouchDB (browser-only build)
import { PouchDB } from 'pouchdb-browser';import PouchDB from 'pouchdb-browser';
- PouchDB.plugin
import { plugin } from 'pouchdb'; PouchDB.plugin(require('pouchdb-adapter-indexeddb'));import PouchDB from 'pouchdb'; import pouchdbAdapterIndexedDB from 'pouchdb-adapter-indexeddb'; PouchDB.plugin(pouchdbAdapterIndexedDB);
Quickstart
import PouchDB from 'pouchdb';
async function initializeAndUseDB() {
// Open or create a database named 'my_local_db'
// In browsers, this defaults to IndexedDB. In Node.js, it uses LevelDB.
const db = new PouchDB('my_local_db');
console.log('Database opened successfully!');
try {
// Create a new document
const newDoc = {
_id: 'dave@example.com',
name: 'David Z.',
occupation: 'Developer',
age: 30
};
const response = await db.put(newDoc);
console.log('Document created:', response);
// Fetch the document
const fetchedDoc = await db.get('dave@example.com');
console.log('Document fetched:', fetchedDoc);
// Update the document
fetchedDoc.age = 31;
const updateResponse = await db.put(fetchedDoc);
console.log('Document updated:', updateResponse);
// Delete the database (for cleanup or testing)
// await db.destroy();
// console.log('Database destroyed.');
} catch (err) {
console.error('Error interacting with PouchDB:', err);
}
}
initializeAndUseDB();