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

nano-doc-updater

v1.0.1

Published

Performs insert-updates of a couchdb document with configurable conflict resolution.

Downloads

11

Readme

Build Status

IBM Cloud DevOps Services - node-nano-doc-updater

Click here for more information on IBM Cloud DevOps Services.

This is one of hundreds of IBM Open Source projects at GitHub.

Synopsis

var db = require("nano")("http://db").db.use("db");
var updater = require("node-nano-doc-updater");

// Migrates a design document.  Will only perform an *upgrade* 
// of an existing design document with a lower version.
//
updater
.db(db)
.existingDoc(null)
.newDoc({ language: "javascript", version: 1 })
.id("_design/foo")
.shouldUpdate((existing, newVer) => {
	return !existing.version || existing.version < newVer.version;
})
.merge((existing, newVer) => {
	return newVer; 
})
.update((err) => { /* handle errors */  });

Description

A module designed to allow updates of CouchDB documents with configurable conflict resolution.

Because of the way CouchbDB handles concurrency, updating a document is non-trivial. Instead of asking CouchDB to kindly serialize the updates, the process involves determining the current revision of a document and submitting a request to update that particular revision. In the event other updates have occurred during this process, the request is rejected.

This module emits an object that can perform this tedious and error-prone flow. Using it involves priming it with information and then asking it to perform one or more updates.

Creation

nanoDocUpdater()

Returns a Nano Doc Updater object. This object holds the parameters of a particular update and can be reused or stored for a later update.

Setters

The following methods set parameters of an update

updater.db(db)

Specifies the database to use for the update. This should be result of nano.db.use() and is required.

updater.newDoc(doc)

The document to attempt to insert. This field is required.

updater.id(id)

The _id of the doc to be updated. This field is required.

updater.shouldCreate(boolean)

If true ensures that during an updater.update(), the newDoc is inserted even if it doesn't already exist in the DB. If not specified, true is assumed.

updater.shouldUpdate((publishedDoc, newDoc) => { ... })

Defines a function that is called to determine whether a document should be updated where one already exists in the DB. This function is given both the newDoc and already published documents and should return a boolean indicating wheter an update should be attempted. Skipping an update in this way results in updater.update() ending without error.

If not specified, an update will always be attempted.

updater.merge((publishedDoc, newDoc) => { ... })

Defines a function that is used to produce a new document, given an existing document and newDoc. Note that updater will overwrite the _rev field of the resulting document with the revision of the already published document. Also note that if this function returns an Error, no insert will occur. Instead, update.update() will fail with the returned value. This can be useful for bubbling unrecoverable merge conflicts.

If not specified, no sophisticated merging will be attempted and newDoc will be used, as is.

Actions

updater.update((err) => {})

Performs the update and calls the provided callback.

updater.updateJob()

Returns a function that can be used to perform an update at a later time. This function will behave exactly like updater.update(), however the parameters for the update are immutable and independent of the updater object.

Examples

Loading the module and binding it to to a database handle:

var updater = require("nano-doc-updater");
var db = require("nano")("https://mydatabase.com").use("mydatabase");
updater.db(db);
...
...

Updating a design document:

var designDocument = {
    language: "javascript",
    version: 1
};

updater
.newDoc(designDocument)
.id("_design/foo")
.shouldUpdate(function (existing, newVer) {
	/* Only publish a new document when it's below the version
	   required by this app */
	return !existing.version || existing.version < newVer.version;
})
.merge(function (existing, newDoc) {
	return newDoc; 
	/* To ensure this process will eventually terminate, 
	   nano-doc-updater will copy `_rev` from the 
       existing document to the newDoc one, just before
       publishing it to CouchDB. */
})
.update(function (err) {
    // handle errors
});

Performing another update with the same updater:

var newerDesignDocument = {
    language: "javascript",
    version: 2,
    views: {
        count: {
            map: function (d) {
                emit(null, 1);
            },
            reduce: function (k, v, r) {
                sum(v);
            }
        }
    }
};

/*
 * shouldUpdate() is unchanged, so updates will still only
 * occur if the published version is below 2.
 */
updater
.newDoc(newerDesignDocument)
.update((err) => {
    // handle errors
})
;

Publish that design document to another document, foo:

updater
.id("foo")
.shouldUpdate(null) // always update
.merge(null) // overwrite
.update((err) => {
    // handle errors
});

Delete that new document:

updater
.merge((existing) => {
    existing._deleted = true;
    return existing;
})
.update((err) => {
    // handle errors
});

Delete that design document from way back:

updater.id("_design/foo").update((err) => {
    // handle errors
});

Use async to sequence the flow above:

var async = require("async");

async.series([
    // Add a design document.  Only publish this if the in-db version is
    // lower.
    updater.db(db).id("_design/foo").newDoc({
        language: "javascript",
        version: 1
    })
    .shouldUpdate((existing, newDoc) => {
        return !existing.version || existing.version < newDoc.version;
    })
    .updateJob(),
    
    
    // Update the design document with an even newer version,
    // but, again, only if it's out of date.
    updater.db(db).id("_design/foo").newDoc({
		language: "javascript",
		version: 2,
		views: {
			count: {
				map: function (d) {
					emit(null, 1);
				},
				reduce: function (k, v, r) {
					sum(v);
				}
			}
		}
    })
    .updateJob(),
    
    
    // Place that same document somewhere else in the db:
    updater.shouldUpdate(null).merge(null).id("foo").updateJob(),
    
    // Delete that document:
    updater
    .merge((existing) => {
      existing._deleted = true;
      return existing;
    })
    .updateJob(),
    
    // Delete the design document:
    updater
    .id("_design/foo")
    .updateJob()
], (err) => {
    if (err) {
        // handle errors in any of the above.
    }
    console.log("All done");
});

License

The MIT License (MIT)

Contributing

Contributions are welcome via Pull Requests. Please submit your very first Pull Request against the Developer's Certificate of Origin, adding a line like the following to the end of the file... using your name and email address of course!

Signed-off-by: John Doe <[email protected]>