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

couchdeconflict

v0.2.0

Published

CouchDB conflict cleanup utility

Downloads

18

Readme

couchdeconflict

A command-line utility to remove conflicts from Apache CouchDB or IBM Cloudant documents. It works by fetching the document to be treated and then deleting all of the non-winning revisions with futher bulk API calls. You may choose the revision you wish to keep if the current winning revision isn't your chosen winner.

This tool is designed to "fix" a document that contains multiple conflicts using the brute force method of deleting all but one revision. This method results in the loss of data - you may wish to examine the bodies of the conflicting revisions before using this tool.

Installation

The package is installed using npm. Node.js required:

npm install -g couchdeconflict

Running

If you have a the URL of the document that needs deconflicting, simply pass that to couchdeconflict as the -u/--url parameter:

> couchdeconflict -u http://localhost:5984/mydb/mydoc
couchdeconflict
---------------
options: {"url":"http://localhost:5984/mydb/mydoc","keep":null,"batch":100}
Fetching document
217 conflicts
  [==================================================] 100% 217/217           
217 conflicts deleted

You may also supply a URL with credentials e.g. https://U:[email protected]/mydb/mydoc.

Alternatively, if you are using IAM authentication with IBM Cloudant, then supply an IAM_API_KEY environment variable containing your IAM API KEY. Your URK (without credentials) will be supplied as the -u/--url parameter.

If you wish to keep a revision other than the current winning revision, simply pass the revision token as a -k/--keep parameter:

> couchdeconflict --url http://localhost:5984/mydb/mydoc  --keep 1-111
couchdeconflict
---------------
options: {"url":"http://###:###@localhost:5984/mydb/mydoc","keep":"1-111","batch":100}
Fetching document
217 conflicts
Keeping revision 1-111
  [==================================================] 100% 217/217           
217 conflicts deleted

Deletions are processed in batches of 100, although this can be overridden with the -b/--batch parameter.

Parameter Reference

  • -u/--url (required) - the URL of the document to treat e.g. https://U:[email protected]/mydb/mydoc or http://localhost:5984/mydb/mydoc
  • -k/--keep - the revision token that is to be left as the winner at the end of the process. If not supplied, the current winning revision is chosen.
  • -b/--batch - the number of deletions processed per API call. Defaults to 100.
  • -d/--dryrun - output the revisions that would be deleted, but don't actually delete them.
  • -v/--verbose - display verbose output. Default to true
  • --help - display help
  • --version - display current version

Using programmatically

You may use this library in your own Node.js code:

var decon = require('couchdeconflict')
var opts = {
  url: 'http://localhost:5984/mydb/mydoc'
}
decon(opts).then((data) => {
  console.log('Done', data)
})

What are conflicts?

This is discussed in more detail in this three part blog series, but essentially they occur when the same document is written to in different ways in separate copies of the database. This can occur when there is:

  • a mobile app and a server-side replica.
  • two databases that are replicated together.
  • a single, multi-node database that has mulitple writes to the same document at the same time.

Conflicts are not an error condition - it's just the database's way of preventing you from losing data. It is your app's responsibility to resolve them.

This tool provides a simplistic solution to fix documents that have become conflicted and need all but one revision deleting. A document with two many conflicts can cause performance problems for a database, so it's good practice to resolve them.

Making conflicts

The easiest way to create a conflicted document is to write multiple revisions with new_edits: false using the POST /db/_bulk_docs method:

# Your CouchDB/Cloudant URL goes here
URL='http://localhost:5984/mydb'
# Create the database
curl -X PUT "$URL"
# JSON to create multiple revisons of the same document
JSON='{"new_edits":false,"docs":[{"_id":"mydoc","_rev":"1-1","a":0},{"_id":"mydoc","_rev":"1-2","a":1},{"_id":"mydoc","_rev":"1-3","a":2}]}'
# Make _bulk_docs request
curl -v -X POST -H 'Content-type:application/json' -d "$JSON" "$URL/_bulk_docs"

Further reading