PouchDB Users Database Plugin

1.0.6 · abandoned · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates how to install the pouchdb-users plugin and create a new user document, showing how the password gets hashed and stored in a CouchDB-compatible format within a local PouchDB instance.

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)

view raw JSON →