PouchDB Users Database Plugin
pouchdb-users is a PouchDB plugin designed to simulate the behavior of CouchDB's `_users` database. It handles password hashing (using PBKDF2) and ensures user document formats adhere to CouchDB's specifications. This allows developers to use any PouchDB adapter, including in-memory or LevelDB-based local storage, for persisting user accounts, thereby decoupling user management from a live CouchDB instance for basic operations. The current stable version is 1.0.6, released in June 2017. The package has seen no updates since then, indicating it is no longer actively maintained. Its key differentiator is enabling offline-first user account management within PouchDB environments without requiring a constant connection to a CouchDB `_users` database for operations like user creation and retrieval. It explicitly does *not* implement access control, focusing solely on document format and password security for user records.
Common errors
-
TypeError: PouchDB.plugin is not a function
cause PouchDB was not loaded or aliased correctly, or the plugin was attempted to be installed on a database instance instead of the PouchDB constructor.fixEnsure `const PouchDB = require('pouchdb')` is called before attempting to install the plugin, and that `PouchDB.plugin()` is used, not `db.plugin()`. -
TypeError: db.installUsersBehavior is not a function
cause The `pouchdb-users` plugin was not successfully installed on the PouchDB constructor before initializing the database instance.fixVerify that `PouchDB.plugin(require('pouchdb-users'))` is called prior to `new PouchDB()` or `db = new PouchDB()`. -
Error: Document update conflict
cause Attempted to `put` a user document with an `_id` that already exists without specifying the `_rev` property of the existing document, or without handling the conflict programmatically.fixWhen updating an existing user, retrieve the document first to get its `_rev`, then include `_rev` in the updated document. Alternatively, handle the conflict in your `catch` block.
Warnings
- gotcha pouchdb-users explicitly does NOT implement any access restrictions or user context. It solely handles password hashing and document formatting for CouchDB's `_users` database. You will need to implement your own authorization logic on top of this plugin.
- deprecated This package is no longer actively maintained; the last release was in June 2017. This may lead to compatibility issues with newer Node.js versions, updated PouchDB APIs, or potential security vulnerabilities due to unaddressed bugs or outdated dependencies (e.g., in `pbkdf2`).
- gotcha The plugin is designed for PouchDB environments that primarily use CommonJS `require()`. While PouchDB itself has evolved, this plugin might not be directly compatible with pure ESM environments without transpilation or specific build configurations.
Install
-
npm install pouchdb-users -
yarn add pouchdb-users -
pnpm add pouchdb-users
Imports
- PouchDB
import PouchDB from 'pouchdb'
const PouchDB = require('pouchdb') - pouchdb-users
import PouchDBUsers from 'pouchdb-users'
const PouchDBUsers = require('pouchdb-users') - plugin installation
db.plugin(require('pouchdb-users'))PouchDB.plugin(require('pouchdb-users'))
Quickstart
const PouchDB = require('pouchdb')
PouchDB.plugin(require('pouchdb-users'))
const memdown = require('memdown') // For an in-memory database example
async function setupUsersDatabase() {
const db = new PouchDB('my-users', { db: memdown })
console.log('Installing users behavior...')
await db.installUsersBehavior()
console.log('Users behavior installed.')
console.log('Creating a new user...')
const newUserDoc = {
_id: 'org.couchdb.user:testuser',
type: 'user',
name: 'testuser',
password: 'supersecretpassword123'
}
const response = await db.put(newUserDoc)
console.log('User created/updated:', response)
console.log('Fetching the user document to see hashed password...')
const userDoc = await db.get('org.couchdb.user:testuser')
console.log('Retrieved user document:', userDoc)
// Example against a remote CouchDB _users database (requires admin credentials)
// const remoteDb = new PouchDB('http://localhost:5984/_users', {
// auth: {
// username: process.env.COUCHDB_ADMIN_USERNAME ?? 'admin',
// password: process.env.COUCHDB_ADMIN_PASSWORD ?? 'adminpassword'
// }
// })
// await remoteDb.installUsersBehavior()
// console.log('Remote users behavior installed.')
// const remoteResponse = await remoteDb.put({
// _id: 'org.couchdb.user:remoteuser',
// type: 'user',
// name: 'remoteuser',
// password: 'anothersecretpassword'
// })
// console.log('Remote user created:', remoteResponse)
}
setupUsersDatabase().catch(console.error)