1.1.0 • Published 7 years ago

couch-recliner v1.1.0

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

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

methodfirst paramadditional params
destroyModelid, attname
readModelid, attname
writeModelid, attname, Attachment
destroyFixeddocattname
readFixeddocattname
writeFixeddocattname, Attachment

DbOperations

methodfirst paramadditional params
createModel
destroyModel"_DESTROY_"
headModel
resetModel"_RESET_"

DocOperations

methodfirst paramadditional params
createModelBody
destroyModelid
headModelid
readModelid
updateModelid, Body
updateOrWriteModelid, Body
writeModelid, Body
destroyFixeddoc
headFixeddoc
readFixeddoc
updateFixeddocBody
writeFixeddocBody

FindOperations

methodfirst paramadditional params
findModelFinder
findOneModelFinder

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

methodfirst paramadditional params
nextIdCouch

Shortcuts

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

methodadditional params
#attachmentid, attname
#createBody
#destroyid
#findFinder
#findOneFinder
#headid
#readid
#updateid, Body
#updateOrWriteid, Body
#writeid, Body
.attachmentattname
.destroy
.head
.read
.updateBody
.writeBody

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.

1.1.0

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago