thug-couch

An add-on module to use couchdb as a document db for Thug models

thug-couch

Thug is a functional model system for nodejs, this module is an add-on module of common couchdb cmds that any model may use.

https://github.com/sintaxi/thug

Thug was created to minimize the complexity of validating and altering an object before writing it to a data store or performing an operation. Thug is not an ORM but is ment to be a replacment for one. Thug is very small and works on both the server or in a browser.

The thug-couch module was constructed to remove any persistent specific functionality from the actual thug model. ThugCouch implements the three primary thug constructors (read, write, remove) to a couchDb backend. Then it goes on to support a couple of api methods that make customizing your models easier.

npm install thug-couch --save

Thug Model

var Thug = require('thug');
var couch = require('thug-couch');
 
module.exports = function(config) {
  var post = new Thug({
    "methods": {
      all: function(cb) { db.all('posts', cb); }
    }
  });
 
  var db = couchDb(config.db);
 
  post.constructor.prototype.read = db.read;
  post.constructor.prototype.write = db.write;
  post.constructor.prototype.remove = db.remove;
 
  return post;
}

Implemented to Thug Specs.

Implemented to Thug Specs.

Implemented to Thug Specs.

The all method returns back a stream object as the first parameter in the callback

Example:

var db = couchDb('http://localhost:5984/db');
var post = new Thug({
  "methods": {
    allfunction(cb) { db.all('posts', cb); }
  }
});

Example:

var db = couchDb('http://localhost:5984/db');
var user = new Thug({
  "methods": {
    byEmailfunction(emailcb) { db.findOne('user', 'email', email, cb); }
  }
});

The findByView method returns back a stream object as the first parameter in the callback

Example:

var db = couchDb('http://localhost:5984/db');
var user = new Thug({
  "methods": {
    byRolesfunction(rolescb) { db.findByView('users', 'roles', roles, cb); }
  }
});

MIT

pull requests are welcome!