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

couch-promised

v1.0.1

Published

A promise-based wrapper around CouchDB. CouchPromised does not aim to be a full- featured CouchDB client and instead aims to provide a minimal set of methods for some of the most common interactions.

Downloads

9

Readme

couch-promised

A promise-based wrapper around CouchDB. CouchPromised does not aim to be a full- featured CouchDB client and instead aims to provide a minimal set of methods for some of the most common interactions.

Installation

The package is available through npm and can be installed as usual:

npm install --save couch-promised

Once installed you can import the package into your script as usual:

var CouchPromised = require('couch-promised');

Usage

The CouchPromised variable above refers to a constructor function. You must create instance before you can interact with a database:

var couch = new CouchPromised({
  host: 'example.com',   // Your CouchDB server. Defaults to 127.0.0.1.
  port: 5984,            // Port of CouchDB server. Defaults to 5984.
  path: '/my-database',  // Path to a database stored on the CouchDB instance.
});

API

As mentioned previously, CouchPromised does not attempt to cover all possible CouchDB functionality. At its heart is the request method, to which all other methods delegate. If you need more fine-grained control over a request you may find that method useful.

get( id )

Find an individual document by unique identifier:

couch.get('my-doc-id').then(function ( doc ) {
  // 'doc' will be an object corresponding to the latest revision of the stored
  // document.
});

fetch( ...ids )

Find many documents by unique identifiers. Takes an array or alternatively any number of separate parameters:

couch.fetch('my-doc-id-1', 'my-doc-id-2').then(function ( docs ) {
  // 'docs' will be an array of objects corresponding the the latest revisions
  // of the stored documents.
});

insert( doc )

Create a new document. If the provided object has an _id property that does not already exist in the database the new document will be created with that identifier. If a document already exists and the provided object doesn't have a valid _ref property an error will be thrown. The update method is better suited to that situation.

couch.insert({ something: 1, another: 'x' }).then(function ( res ) {
  // 'res' will be an object with '_id' and '_rev' properties corresponding to
  // the newly stored document.
});

update( doc )

Update an existing document. The provided object must have an _id property that corresponds to a stored document and a _rev property that matches the latest revision of that stored document.

couch.update({ _id: 'my-doc-id', _rev: 1 }).then(function ( res ) {
  // 'res' will be an object with '_id' and '_rev' properties corresponding to
  // the updated document.
});

destroy( doc )

Delete an existing document. The provided object must have an _id property that corresponds to a stored document and a _rev property that matches the latest revision of that stored document.

couch.destroy({ _id: 'my-doc-id', _rev: 1 }).then(function ( res ) {
  // 'res' will be an object with '_id' and '_rev' properties corresponding to
  // the deleted document.
});

view( designDocument , viewName [, viewParameters ] )

Query a database view. Requires the name of a design document and the name of a view within that document. Optionally also takes a map of view parameters to their values. The parameters object can contain any of the parameters a CouchDB view normally accepts. It is converted to a querystring and appended to the request URL.

couch.view('a-design', 'a-view', { key: 'some-key' }).then(function ( rows ) {
  // 'rows' will be an array of rows returned by the view. Note that the rows
  // are not documents themselves but may have a 'doc' property if e.g. the
  // 'include_docs' parameter was included.
});

viewDocs( designDocument , viewName [, viewParameters ] )

Query a database view and return the document associated with each result. This is effectively sugar around a call to view with the include_docs parameter set.

couch.viewDocs('a-design', 'a-view').then(function ( docs ) {
  // 'docs' will be an array of documents associated with the rows returned by
  // the view.
});

bulk( docs )

Perform a bulk insert or update of documents. Requires an array of documents. Any element of that array with an _id property that corresponds to a document already stored in the database must also have the correct _rev property.

couch.bulk([ { x: 1 }, { y: 2 } ]).then(function ( res ) {
  // 'res' will be an array of objects with '_id' and '_rev' properties
  // corresponding to the inserted or updated document.
});

request( method , path [, body ] )

Send an HTTP request to the CouchDB database. Requires an HTTP method and a URL path. Optionally also takes an object representing the request body.

couch.request('GET', '/my-doc-id').then(function ( res ) {
  // 'res' will be an object representing the raw HTTP response from CouchDB.
  // The other CouchPromised methods take that response and resolve with the
  // relevant data from within it.
});