npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pouchdb-undo

v0.1.3

Published

PouchDB undo plugin

Downloads

10

Readme

PouchDB Undo Plugin

Build Status

Adds undo functionality to all operations (put, post, remove and bulkDocs) on a database. It allows the reversal of creations, changes and deletions.

Each operation will return a undoId within the response. Use the undoId to revert the operation.

Undo creating a document:

pouch.put({ _id: 'cat' }).then(function (result) {
  return pouch.undo(result.undoId);
});

Undo changing a document:

pouch.put({ _id: 'dog', sound: 'bark' }).then(function (result) {
  return pouch.put({ _id: 'dog', _rev: result.rev, sound: 'woof'});
}).then(function (result) {
  return pouch.undo(result.undoId);
});

Undo deleting a document:

pouch.put({ _id: 'wolf' }).then(function (result) {
  return pouch.remove('wolf', result.rev);
}).then(function (result) {
  return pouch.undo(result.undoId);
});

Usage

To use this plugin, include it after pouchdb.js in your HTML page:

<script src="pouchdb.js"></script>
<script src="pouchdb.undo.js"></script>

Or to use it in Node.js, just npm install it:

npm install pouchdb-undo

Attach it to the PouchDB object and enable it per database:

var PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-undo'));

var animals = new PouchDB('animals');
animals.enableUndo();

API

###Undo put, post and remove

For put, post and remove, an undoId is returned within the result, which must be provided to the undo method:

pouch.put({ _id: 'dog', sound: 'bark' }).then(function (result) {
  return pouch.put({ _id: 'dog', _rev: result.rev, sound: 'woof'});
}).then(function (result) {
  // undo changing the dog's sound
  return pouch.undo(result.undoId);
}).then(function () {
  return pouch.get('dog')
}).then(function (dog) {
  // dog.sound === 'bark'
});

###Undo bulkDocs

For bulkDocs, the undoId is returned within the each row of the result (and they will have identical undoIds):

pouch.bulkDocs([
  { _id: 'dog', sound: 'woof' },
  { _id: 'cat', sound: 'miow' }
]).then(function (result) {
  // undo creating the cat and dog
  return pouch.undo(result[0].undoId); // result[0].undoId === result[1].undoId
}).then(function () {
  pouch.get('dog'); // --> 404 not_found
  pouch.get('cat'); // --> 404 not_found
});

Advanced usage

Undo history is stored in _local documents (so they will not show up in allDocs, and they are not synced). By default, up to 100 undo's are stored, which can be changed by providing options to enableUndo:

pouch.enableUndo({ limit: 500 });

You can clear the entire undo history manually:

pouch.clearUndo();

Known issues

  1. At the moment only the latest operation on a document can be reverted.
  2. Race condition: making multiple changes at once to a database will lead to only one being kept in the undo history.
  3. Undo will not work well if there are multiple leaves (unresolved conflicts) - it will try to apply the undo to the first leaf found.

Building

npm install
npm run build

Testing

This will run the tests in Node using LevelDB:

npm test