nano-repository
Working with CouchDB is a pretty pleasant experience, mostly because of the rather excellent nano library.
There are a couple of rough edges though, so what else could be better but an abstracton on top of an abstraction!
Examples
Create me a repository:
var Nano = Repository = ; // set up Nanovar connection = 'http://localhost:5984';var db = connectiondb; // set up repovar repository = db;
CRUD operations
var document = foo: 'bar'; // createrepository; // retrieverepository; // updaterepository; // deleterepository;
Attachments
Add files to your documents:
repository;
Retrieve attachments:
repository;
Attachment versions
The attachment version is dependent on the document version. E.g if you will pardon the pyramid of doom:
var attachmentName = 'myFile'; // add the first version of the attachmentrepository;
Streaming attachments
For the performance/memory conscious, stream files to your documents with the 4x argument version of Repository.addAttachment:
var fileStream = fs; fileStream;
...and retrieve them:
repository;
Views
Views are accessed by extra methods on the Repository. To create your views, first create a view template file, usally with the .json
extension.
The template file looks like this (n.b. feel free to include reduce
functions if you need them):
"views": "all": "map": "function(doc) {if(doc.name) emit(null, doc)}" "byName": "map": "function(doc) {if(doc.name) emit(doc.name, doc)}"
Update/create the views:
var repository = db;repository;
View methods are dynamically created:
repository;
Method names are chosen by capitalising the first letter of the view name and prepending find
to it.
So the view mapping file contained a view named all
- this was turned into findAll
, similarly the view named byName
was turned into a method named findByName
.
Arguments are also supported:
repository;
The code that makes up your view is hashed - the hash is stored along with the views. The next time you call Repository.updateViews the hashes are compared - it they've changed then the views are recreated automatically.
This means it's save to call Repository.updateViews every time you start your app - they'll only get altered if you change the code in your view file.
Get out of my console.log
You can pass an alternative logging implementation into the constructor and the repository will use that instead, as long as it supports .info
, .warn
, etc methods.
var Nano = Logger = Logger logger = ; //.. set up Nano as usual var repository = db logger;
Todo
- Retrieving specific document versions.
- Work out if I've missed the point somehow. Probably.