PouchDB is a pocket-sized database with comprehensive replication functionality for data synchronization between local and remote CouchDB-compatible databases
npx @tessl/cli install tessl/npm-pouchdb-replication@9.0.0PouchDB is a pocket-sized database designed to run both in web browsers and Node.js environments. It provides a complete database solution with built-in replication functionality that enables seamless data synchronization between local and remote CouchDB-compatible databases. PouchDB allows applications to work reliably offline and online with automatic conflict resolution, incremental data transfer, and comprehensive event handling.
npm install pouchdbimport PouchDB from 'pouchdb';CommonJS:
const PouchDB = require('pouchdb');Browser (via CDN):
<script src="https://cdn.jsdelivr.net/npm/pouchdb@9.0.0/dist/pouchdb.min.js"></script>import PouchDB from 'pouchdb';
// Create a local database
const localDB = new PouchDB('my-local-database');
// Create documents
await localDB.put({
_id: 'user_001',
name: 'Alice Johnson',
email: 'alice@example.com',
active: true
});
// Retrieve documents
const doc = await localDB.get('user_001');
console.log(doc.name); // "Alice Johnson"
// List all documents
const allDocs = await localDB.allDocs({
include_docs: true
});
// Set up replication to a remote database
const remoteDB = 'https://myserver.com/mydb';
const replication = PouchDB.replicate(localDB, remoteDB, {
live: true,
retry: true
});
replication.on('change', (info) => {
console.log('Replication change:', info);
});PouchDB is built around several key architectural components:
Core CRUD operations for document management, including creation, retrieval, updates, and deletion with full conflict resolution.
/**
* PouchDB constructor creates a new database instance
* @param name - Database name (local) or URL (remote)
* @param options - Configuration options
*/
function PouchDB(name, options);
/**
* Create or update a document
* @param doc - Document object with _id and optional _rev
* @param options - Optional parameters like batch mode
* @returns Promise resolving to operation result
*/
db.put(doc, options);
/**
* Retrieve a document by ID
* @param docId - Document identifier
* @param options - Optional parameters like rev, revs
* @returns Promise resolving to document
*/
db.get(docId, options);
/**
* Create a new document with auto-generated ID
* @param doc - Document object without _id
* @param options - Optional parameters
* @returns Promise resolving to operation result with generated ID
*/
db.post(doc, options);
/**
* Delete a document
* @param docOrId - Document object or document ID
* @param optsOrRev - Options object or revision string
* @returns Promise resolving to operation result
*/
db.remove(docOrId, optsOrRev);Comprehensive replication system supporting one-way and bidirectional sync with extensive configuration options, filtering, and conflict resolution.
/**
* Static method to replicate from source to target
* @param source - Source database (PouchDB instance or URL)
* @param target - Target database (PouchDB instance or URL)
* @param options - Replication configuration
* @returns Replication object with event emitter interface
*/
PouchDB.replicate(source, target, options);
/**
* Static method for bidirectional synchronization
* @param source - First database (PouchDB instance or URL)
* @param target - Second database (PouchDB instance or URL)
* @param options - Sync configuration
* @returns Sync object with event emitter interface
*/
PouchDB.sync(source, target, options);
/**
* Instance method to replicate this database to target
* @param target - Target database (PouchDB instance or URL)
* @param options - Replication configuration
* @returns Replication object with event emitter interface
*/
db.replicate.to(target, options);
/**
* Instance method to replicate from source to this database
* @param source - Source database (PouchDB instance or URL)
* @param options - Replication configuration
* @returns Replication object with event emitter interface
*/
db.replicate.from(source, options);
/**
* Instance method for bidirectional sync with target
* @param target - Target database (PouchDB instance or URL)
* @param options - Sync configuration
* @returns Sync object with event emitter interface
*/
db.sync(target, options);Real-time change monitoring with filtering, live updates, and comprehensive event handling for both database changes and replication events.
/**
* Listen to database changes
* @param options - Changes configuration including live, since, filter
* @returns Changes object with event emitter interface
*/
db.changes(options);
/**
* Add event listener
* @param event - Event name (change, complete, error, etc.)
* @param handler - Event handler function
*/
db.on(event, handler);
/**
* Add one-time event listener
* @param event - Event name
* @param handler - Event handler function
*/
db.once(event, handler);Efficient bulk document operations and comprehensive querying capabilities including all documents retrieval with extensive filtering options.
/**
* Create, update, or delete multiple documents in a single operation
* @param docs - Array of document objects or object with docs array
* @param options - Bulk operation configuration
* @returns Promise resolving to array of operation results
*/
db.bulkDocs(docs, options);
/**
* Retrieve all documents with optional filtering and pagination
* @param options - Query options including keys, limit, skip, include_docs
* @returns Promise resolving to query results
*/
db.allDocs(options);Binary attachment management for storing and retrieving files associated with documents, supporting various content types.
/**
* Attach binary data to a document
* @param docId - Document ID
* @param attachmentId - Attachment identifier
* @param rev - Document revision
* @param blob - Binary data (Blob, Buffer, or string)
* @param type - MIME type
* @returns Promise resolving to operation result
*/
db.putAttachment(docId, attachmentId, rev, blob, type);
/**
* Retrieve an attachment
* @param docId - Document ID
* @param attachmentId - Attachment identifier
* @param options - Retrieval options
* @returns Promise resolving to attachment data
*/
db.getAttachment(docId, attachmentId, options);
/**
* Remove an attachment from a document
* @param docId - Document ID
* @param attachmentId - Attachment identifier
* @param rev - Document revision
* @returns Promise resolving to operation result
*/
db.removeAttachment(docId, attachmentId, rev);/**
* Registry of available database adapters
*/
PouchDB.adapters: { [key: string]: any };
/**
* List of preferred adapters in order of preference
*/
PouchDB.preferredAdapters: string[];
/**
* Default prefix for database names
*/
PouchDB.prefix: string;
/**
* PouchDB version string
*/
PouchDB.version: string;
/**
* Register a database adapter
* @param name - Adapter name
* @param adapter - Adapter implementation
* @param preferred - Add to preferred adapters list
*/
PouchDB.adapter(name, adapter, preferred);
/**
* Add plugin functionality
* @param plugin - Plugin function or object
*/
PouchDB.plugin(plugin);
/**
* Create PouchDB constructor with default options
* @param defaultOptions - Default configuration
* @returns PouchDB constructor with defaults
*/
PouchDB.defaults(defaultOptions);interface PouchDBOptions {
/** Database adapter to use */
adapter?: string;
/** Auto-compaction enabled */
auto_compaction?: boolean;
/** Database prefix */
prefix?: string;
/** Additional adapter-specific options */
[key: string]: any;
}
interface DocumentResponse {
/** Operation success status */
ok: boolean;
/** Document ID */
id: string;
/** New document revision */
rev: string;
}
interface AllDocsOptions {
/** Include full document content */
include_docs?: boolean;
/** Array of specific document IDs to retrieve */
keys?: string[];
/** Maximum number of documents to return */
limit?: number;
/** Number of documents to skip */
skip?: number;
/** Reverse result order */
descending?: boolean;
/** Start key for range queries */
startkey?: any;
/** End key for range queries */
endkey?: any;
}
interface ReplicationOptions {
/** Continuous replication */
live?: boolean;
/** Retry on failure */
retry?: boolean;
/** Document filter function */
filter?: string | Function;
/** Specific document IDs to replicate */
doc_ids?: string[];
/** Batch size for replication */
batch_size?: number;
/** Number of batches to process in parallel */
batches_limit?: number;
/** Use checkpoints */
checkpoint?: boolean;
}