liber
Liber
makes using CouchDB not only easier to build but also shemaless. It is built on nano and inspired by AngularJS-Resource.
Principles:
- Minimalistic: Keep functions you would be using(view generation etc.) once out of this one. CouchDB's api is simple - do them using nano.
- Shemaless: NoSQL is shemaless why should a driver differ.
- Expectedness: Don't try to squeeze features of a RDBMS out of CouchDB, ergo don't generate anything automatically in the background. Be in control and know whats happening.
Installation
Liber
is nothing but a extended api for nano so for using it you also need the latter - it's good to have anyways working with CouchDB:
npm install liber nano
Getting started
to set liber up
var nano = 'http://localhost:5984/mydatabase';var Liber = db; //alias for document
now... live a simpler life
Liber;
Inheritance
{thissuperClass; // to give more flexibility// to construction processthiskind = 'user';thisfullName = thislastName + ', ' + thisfirstName;}User = Liber;User;/*get by idNote: Liber doesn't validate the kind or type so you couldfetch an User from any other Liber class.*/User;// get user from an generated view getter. Result is an array.User;// same as the last one but result is User object.
API: Class methods
Liber.inherit(constructor, prototype, options)
Liber.parse(data)
Liber.get(params, callback)
Liber.getOne(id, [params, callback])
Liber.getFromView(design, view, params, callback)
Liber.getOneFromView(design, view, params, callback)
API: Instance methods
Doc.save(callback)
Doc.insert(callback)
Doc.destroy(callback)
API: Options
viewShortcuts
View shortcuts: Generates methods to access views. Needs array of parameters to concat to the beginning of parameters of method calls. Let's say User class declaration has a options of:
'viewShortcuts':'fromMyDesign': 'myDesignDoc''byLastName': 'myDesignDoc' 'usersByLastName''admins': 'myDesignDoc' 'usersByStatus' 'admin'
then
User;// == User.getOneFromView('myDesignDoc', 'usersByAddress', 'broad way');User;// == User.getFromView('myDesignDoc', 'usersByLastName', 'smith');User;// == User.getFromView('myDesignDoc', 'usersByStatus', 'admin');
Although linguistically a bit off, the getOneAdmins
would limit latter to first
match. The shortcuts are very cheap to make so you could fix that by duplicating
'admins' with an 'admin' so you'll have both get(One)Admin
and get(One)Admins
.