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-recliner

v1.1.0

Published

Utility modules for interacting with CouchDB2.0/Cloudant using Nodejs

Downloads

21

Readme

Couch Recliner

Utility modules for interacting with CouchDB2.0/Cloudant using Nodejs. It retries requests and generally tries to keep things clean.

npm

Download it

Install the library from npm.

npm i couch-recliner --save

Usage

Build a model that describes your database. You must set a dbName on it.

const { Model } = require('couch-recliner');

class Account extends Model {
}

Account.dbName = 'accounts';

Add a document.

Account.create({ name: 'Smith' }, (err, doc) => {
    if (err) return;
    console.log(doc.id);
    console.log(doc.body.name);
});
b77509102b4dc0a1389ae3b6d248ef18
Smith

A new account has been created in the database, and an instance of Account returned. In this case we are using a provided Model shortcut which runs DocOperations.create.

Modules

AttachmentOperations

| method | first param | additional params | | - | - | - | | destroy | Model | id, attname | | read | Model | id, attname | | write | Model | id, attname, Attachment | | destroyFixed | doc | attname | | readFixed | doc | attname | | writeFixed | doc | attname, Attachment |

DbOperations

| method | first param | additional params | | - | - | - | | create | Model | | | destroy | Model | "_DESTROY_" | | head | Model | | | reset | Model | "_RESET_" |

DocOperations

| method | first param | additional params | | - | - | - | | create | Model | Body | | destroy | Model | id | | head | Model | id | | read | Model | id | | update | Model | id, Body | | updateOrWrite | Model | id, Body | | write | Model | id, Body | | destroyFixed | doc | | | headFixed | doc | | | readFixed | doc | | | updateFixed | doc | Body | | writeFixed | doc | Body |

FindOperations

| method | first param | additional params | | - | - | - | | find | Model | Finder | | findOne | Model | Finder |

You can incorporate your own directly into your model, as an example this performs a lookup of Message instances.

const { Model, FindOperations } = require('couch-recliner');

class Message extends Model {
    static findByAccountId(accountId, callback) {
        const finder = {
            selector: { accountId },
            limit: 50
        };
        FindOperations.find(this, finder, callback);
    }
}

Message.dbName = 'messages';

Message.findByAccountId(myAccountId, (err, docs) => {
    if (err) return;
    console.log(docs.length);
});
4

Databases

It is likely you want to maintain a few databases, for testing, or development and keep them separate. Couch Recliner automatically postfixes your provided dbName with the current environment. If you ran through the examples on this page you will likely see databases named accounts-development and messages-development created for you. Possibly you'll see accounts-test and messages-test too.

But what about database location?

Set the couch attribute on your model. It should be a url indicating where you want Couch Recliner to communicate with your database.

const couch = 'http://localhost:1000';

Account.couch = couch;
Message.couch = couch;

For more uniform reuse of shared database instance information, create a Couch instance yourself and pass it as above instead. With a couch instance you can access more methods.

CouchOperations

| method | first param | additional params | | - | - | - | | nextId | Couch | |

Shortcuts

Your Model comes with a set of static and instance shortcuts included by default.

| method | additional params | | - | - | | #attachment | id, attname | | #create | Body | | #destroy | id | | #find | Finder | | #findOne | Finder | | #head | id | | #read | id | | #update | id, Body | | #updateOrWrite | id, Body | | #write | id, Body | | .attachment | attname | | .destroy | | | .head | | | .read | | | .update | Body | | .write | Body |

You can use these, override them, or always choose the longer version.

Attachments

Buffers and strings can be uploaded to your database individually by means of the specialised AttachmentOperations module, or it can be done in bulk within your document.

const body = {
    age: 22,
    _attachments: {
        'avatar.png': {
            contentType: 'image/png',
            body: myBuffer
        }
    }
};

Account.update(myId, body, (err, doc) => {
    if (err) return;
    console.log(doc.body._attachments);
});
{
    'avatar.png': {
        stub: true,
        content_type: 'image/png',
        length: 511
    }
}

Manipulating the _attachments object in your document in this way will trigger a multipart request. You can add attachments, overwrite them, or remove them entirely by setting undefined. All attachments can be unilaterally deleted by setting _attachments: undefined.

What this library doesn't do

The library is in it's early stages, it should be easily extendible. More help is welcomed warmly in the form of pull requests, questions, comments, feature requests, in the contributions section on github.

Coming soon

  • Show operations
  • View operations
  • Bulk document operations
  • Auth

More documentation

Please see the ./docs directory for more detailed information about available modules.

Contribute

If you like what you see please feel encouraged to get involved report problems and submit pull requests! As of the time of this writing the project has one maintainer.