PouchDB is a pocket-sized database inspired by Apache CouchDB that runs in the browser and Node.js.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core database lifecycle operations including creation, configuration, information retrieval, and cleanup operations for PouchDB databases.
Creates a new PouchDB database instance with configurable options for different environments and use cases.
/**
* Creates a new PouchDB database instance
* @param name - Database name or remote URL
* @param options - Configuration options for the database
* @returns PouchDB instance
*/
new PouchDB(name: string, options?: PouchDBOptions): PouchDB;
interface PouchDBOptions {
adapter?: string; // Storage adapter ('leveldb', 'idb', 'http', etc.)
auto_compaction?: boolean; // Enable automatic compaction
revs_limit?: number; // Maximum number of revisions to store
view_adapter?: string; // Adapter for view storage
purged_infos_limit?: number; // Limit for purged info storage (default: 1000)
[key: string]: any; // Additional adapter-specific options
}Usage Examples:
// Local database with default adapter
const localDb = new PouchDB('my-local-db');
// Remote database
const remoteDb = new PouchDB('http://localhost:5984/my-remote-db');
// Database with specific adapter
const memoryDb = new PouchDB('temp-db', { adapter: 'memory' });
// Database with custom options
const db = new PouchDB('my-db', {
adapter: 'leveldb',
auto_compaction: true,
revs_limit: 10
});Retrieves comprehensive information about the database including document count, storage size, and metadata.
/**
* Get database information and statistics
* @returns Promise resolving to database information
*/
db.info(): Promise<DatabaseInfo>;
interface DatabaseInfo {
db_name: string; // Database name
doc_count: number; // Number of documents
update_seq: number | string; // Current update sequence
purge_seq?: number | string; // Current purge sequence
compact_running: boolean; // Whether compaction is running
disk_size?: number; // Database size on disk (bytes)
data_size?: number; // Actual data size (bytes)
instance_start_time: string; // When database instance was created
host: string; // Host information
auto_compaction?: boolean; // Auto-compaction status
}Usage Examples:
// Get basic database information
const info = await db.info();
console.log(`Database: ${info.db_name}`);
console.log(`Documents: ${info.doc_count}`);
console.log(`Size: ${info.disk_size} bytes`);
// Check if compaction is running
if (info.compact_running) {
console.log('Database compaction in progress');
}Permanently deletes a database and all its data. This operation cannot be undone.
/**
* Destroy the database and all its data permanently
* @param options - Destruction options
* @returns Promise resolving when destruction is complete
*/
db.destroy(options?: DestroyOptions): Promise<void>;
interface DestroyOptions {
[key: string]: any; // Adapter-specific options
}Usage Examples:
// Destroy a database
await db.destroy();
console.log('Database destroyed');
// Destroy with options (adapter-specific)
await db.destroy({
force: true // Example adapter-specific option
});Closes the database connection and releases resources. The database can be reopened by creating a new instance.
/**
* Close the database connection and release resources
* @returns Promise resolving when database is closed
*/
db.close(): Promise<void>;Usage Examples:
// Close database connection
await db.close();
console.log('Database connection closed');
// Proper cleanup pattern
try {
// Use database
await db.put({ _id: 'doc1', data: 'value' });
} finally {
// Always close when done
await db.close();
}Reduces database size by removing old document revisions and reclaiming space from deleted documents.
/**
* Compact the database to reduce size and improve performance
* @param options - Compaction options
* @returns Promise resolving to compaction result
*/
db.compact(options?: CompactOptions): Promise<CompactResponse>;
interface CompactOptions {
interval?: number; // Compaction interval for continuous compaction
[key: string]: any; // Adapter-specific options
}
interface CompactResponse {
ok: boolean; // Whether compaction succeeded
[key: string]: any; // Additional adapter-specific response data
}Usage Examples:
// Basic compaction
const result = await db.compact();
console.log('Compaction completed:', result.ok);
// Compaction with options
const result = await db.compact({
interval: 5000 // Compact every 5 seconds
});
// Check database size before and after
const infoBefore = await db.info();
await db.compact();
const infoAfter = await db.info();
console.log(`Size reduced from ${infoBefore.disk_size} to ${infoAfter.disk_size}`);Global configuration methods for setting up PouchDB defaults and preferences.
/**
* Create a PouchDB constructor with default options
* @param options - Default options to apply to all instances
* @returns New PouchDB constructor with defaults
*/
PouchDB.defaults(options: PouchDBOptions): typeof PouchDB;
/**
* Database name prefix for local databases
*/
PouchDB.prefix: string; // Default: '_pouch_'
/**
* Array of preferred adapters in order of preference
*/
PouchDB.preferredAdapters: string[];Usage Examples:
// Create PouchDB constructor with defaults
const CustomPouchDB = PouchDB.defaults({
adapter: 'memory',
auto_compaction: true
});
// All instances created with CustomPouchDB will use these defaults
const db1 = new CustomPouchDB('db1'); // Uses memory adapter
const db2 = new CustomPouchDB('db2'); // Uses memory adapter
// Change database prefix
PouchDB.prefix = 'myapp_';
const db = new PouchDB('test'); // Actually creates 'myapp_test'
// Check preferred adapters
console.log(PouchDB.preferredAdapters); // ['idb', 'leveldb', 'websql']Database management operations can fail for various reasons. Handle errors appropriately:
try {
const info = await db.info();
console.log('Database is accessible');
} catch (error) {
if (error.status === 404) {
console.log('Database does not exist');
} else if (error.status === 401) {
console.log('Authentication required');
} else {
console.log('Database error:', error.message);
}
}Common error codes:
404 - Database not found401 - Authentication required403 - Permission denied500 - Internal server error (for remote databases)Install with Tessl CLI
npx tessl i tessl/npm-pouchdb