PouchDB

9.0.0 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates how to install PouchDB, create a new local database, add, retrieve, and update documents using the async/await pattern.

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

view raw JSON →