Comparing version 0.0.2 to 0.0.3
@@ -42,3 +42,2 @@ var Utils = require('./Utils.js'); | ||
Liber.inherit = function (Child, childPrototype, opts) { | ||
console.log('inheriting', Child); | ||
Child = Child || function () {}; | ||
@@ -48,3 +47,2 @@ childPrototype = childPrototype || {}; | ||
console.log('opts', opts.viewShortcuts); | ||
if (opts.viewShortcuts) { | ||
@@ -148,15 +146,18 @@ var me = this; | ||
db.get(id, params, function (err, body) { | ||
cb && cb(err, me.parse(body)); | ||
cb && cb(err, me.parse.call(me, body)); | ||
}); | ||
}; | ||
Liber.parse = function (data) { | ||
var constr = this.name ? this : this.constructor; | ||
return data ? (new constr(data)) : undefined; | ||
}; | ||
var addViewGetter = Liber.addViewGetter = function (Class, name, attrs) { | ||
console.log('add view getter in Liber constructor', name, attrs); | ||
if (isArray(attrs) && attrs.length == 2) { | ||
console.log('capitaliseFirstLetter', capitaliseFirstLetter); | ||
Class[['get', capitaliseFirstLetter(name)].join('')] = function () { | ||
console.log('get from', attrs); | ||
Class[['get', capitaliseFirstLetter(name)].join('')] = function (params, cb) { | ||
this.getFromView.call(this, attrs[0], attrs[1], params, cb); | ||
}; | ||
Class[['getOne', capitaliseFirstLetter(name)].join('')] = function () { | ||
console.log('get from', attrs); | ||
Class[['getOne', capitaliseFirstLetter(name)].join('')] = function (params, cb) { | ||
this.getOneFromView.call(this, attrs[0], attrs[1], params, cb); | ||
}; | ||
@@ -168,14 +169,7 @@ } else { | ||
Liber.parse = function (data) { | ||
var constr = this.name ? this : this.constructor; | ||
return data ? (new constr(data)) : undefined; | ||
}; | ||
Liber.prototype = { | ||
_: { | ||
image: '' | ||
}, | ||
constructor: Liber, | ||
parse: Liber.parse, | ||
// constructor: Liber, | ||
insert: function (cb) { | ||
@@ -190,19 +184,11 @@ delete this._id; | ||
if (this) { | ||
db.insert(this, function (err, body) { | ||
if (err) return cb && cb(err); | ||
me['_id'] = body['id']; | ||
me['_rev'] = body['rev']; | ||
cb && cb(err, me, body); | ||
}); | ||
} else { | ||
cb(new Error('Object has no properties to save'), undefined); | ||
} | ||
db.insert(this, function (err, body) { | ||
if (err) return cb && cb(err); | ||
me['_id'] = body['id']; | ||
me['_rev'] = body['rev']; | ||
cb && cb(err, me, body); | ||
}); | ||
}, | ||
destroy: function (cb) { | ||
console.log('destroying', this); | ||
return db.destroy(this._id, this._rev, cb); | ||
}, | ||
toJSON: function () { | ||
return Utils.copyOwn(this); | ||
} | ||
@@ -209,0 +195,0 @@ }; |
@@ -60,3 +60,2 @@ | ||
} else if (obj.forEach && obj.forEach !== forEach) { | ||
console.log('problem'); | ||
obj.forEach(iterator, context); | ||
@@ -63,0 +62,0 @@ } else if (isObject(obj) && isNumber(obj.length)) { |
{ | ||
"name": "liber", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Shemaless ORMlike library for CouchDB", | ||
@@ -17,2 +17,5 @@ "main": "lib/Liber.js", | ||
"couchdb", | ||
"couch", | ||
"odm", | ||
"database", | ||
"orm", | ||
@@ -19,0 +22,0 @@ "shemaless", |
liber | ||
===== | ||
===== | ||
`Liber` makes using CouchDB not only easier to build but also shemaless. It is built on [nano][nano] and inspired by [AngularJS-Resource][angular]. | ||
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][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][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 libre up | ||
``` js | ||
var nano = require('nano')('http://localhost:5984/mydatabase'); | ||
var Liber = require('liber')(db); //alias for document | ||
``` | ||
now... live a simpler life | ||
``` js | ||
Liber.getOne('bobs_id', function (err, bob) { | ||
bob.age++; | ||
bob.save(function (err, newBob) { | ||
//newBob == bob; | ||
}); | ||
}); | ||
``` | ||
##Inheritance | ||
``` js | ||
<!-- //Liber.inherit(Constructor, prototype, options); where Liber already has a pointer to database. --> | ||
function User(data) { | ||
this.superClass.call(this, data); // needed to give more flexibility | ||
// to construction process | ||
this.kind = 'user'; | ||
this.fullName = this.lastName + ', ' + this.firstName; | ||
} | ||
User = Liber.inherit(User, { | ||
changePassword: function () { ... } | ||
}, { | ||
'byLastName': ['designDoc', 'viewName'] | ||
}); | ||
User.get('anId', cb); | ||
/* | ||
get by id | ||
Note: Liber doesn't validate the kind or type so you could | ||
fetch an User from any other Liber class. | ||
*/ | ||
User.getByLastName('Smith', cb); | ||
// get user from an generated view getter. Result is an array. | ||
User.getOneByLastName('Smith', cb); | ||
// same as the last one but result is User object. | ||
``` | ||
##API: Class methods | ||
###Libre.inherit | ||
###Libre.parse | ||
###Libre.get | ||
###Libre.getOne | ||
###Libre.getFromView | ||
###Libre.getOneFromView | ||
##API: Instance methods | ||
###Doc.save | ||
###Doc.insert | ||
###Doc.destroy | ||
[npm]: http://npmjs.org | ||
[nano]: http://github.com/dscape/nano | ||
[angular]: http://angularjs.org |
var should = require('should'); | ||
var shouldBeLibre = require('./testSingleResultMethod'); | ||
var data = require('../fixtures').data; | ||
var TEST_ON_ID = 'audi'; | ||
var TEST_ON_PROP = 'year'; | ||
var find = function (arr, prop, val) { | ||
if (arguments.length == 1) { | ||
val = arr; | ||
prop = '_id'; | ||
arr = data; | ||
} | ||
return arr.filter(function (el) { return el[prop] == val; }).pop(); | ||
}; | ||
exports = module.exports = function (Doc) { | ||
describe('#getOne', function () { | ||
describe('#getOne(id, {}, cb)', function () { | ||
before(function (done) { | ||
var me = this; | ||
Doc.getOne('audi', {}, function (err, res) { | ||
Doc.getOne(TEST_ON_ID, {}, function (err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
should.exist(res.year); | ||
me.res = res; | ||
@@ -18,5 +29,72 @@ done(); | ||
shouldBeLibre(Doc); | ||
it('result should get the right data', function () { | ||
var thing = find(TEST_ON_ID); | ||
this.res.should.have.property(TEST_ON_PROP, thing[TEST_ON_PROP]); | ||
}); | ||
it('result should be of correct class', function () { | ||
this.res.should.be.an.instanceOf(Doc); | ||
}); | ||
after(function () { | ||
delete this.res; | ||
}); | ||
}); | ||
describe('#getOne(id, cb)', function () { | ||
before(function (done) { | ||
var me = this; | ||
Doc.getOne(TEST_ON_ID, function (err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
me.res = res; | ||
done(); | ||
}); | ||
}); | ||
it('result should get the right data', function () { | ||
var thing = find(TEST_ON_ID); | ||
this.res.should.have.property(TEST_ON_PROP, thing[TEST_ON_PROP]); | ||
}); | ||
it('result should be of correct class', function () { | ||
this.res.should.be.an.instanceOf(Doc); | ||
}); | ||
after(function () { | ||
delete this.res; | ||
}); | ||
}); | ||
// describe('should save itself ...', function () { | ||
// before(function (done) { | ||
// var me = this; | ||
// this.oldRev = this.res._rev; | ||
// this.res.save(function (err, res) { | ||
// me.saveRes = res; | ||
// done(); | ||
// }); | ||
// }); | ||
// it('... and still have the same _id', function () { | ||
// this.saveRes.should.have.property('_id', this.res._id); | ||
// }); | ||
// it('... and have new _rev', function () { | ||
// this.saveRes.should.have.property('_rev'); | ||
// (this.oldRev == this.saveRes._rev).should.be.false; | ||
// }); | ||
// it('... have the saved property', function () { | ||
// this.saveRes.should.have.property('someUndefinedProperty', 42); | ||
// }); | ||
// after(function () { | ||
// this.res = this.saveRes; | ||
// }); | ||
// }); | ||
}; | ||
@@ -13,3 +13,3 @@ | ||
it('should have promised api', function () { | ||
var methods = [ | ||
var classMethods = [ | ||
'get', | ||
@@ -24,12 +24,30 @@ 'getOne', | ||
methods.forEach(function (classMethod) { | ||
Liber.should.have.property(classMethod); | ||
var instanceMethods = [ | ||
'save', | ||
'insert', | ||
'destroy', | ||
'parse' | ||
]; | ||
var instance = new Liber(); | ||
instanceMethods.forEach(function (method) { | ||
instance.should.have.property(method); | ||
}); | ||
}); | ||
classMethods.forEach(function (method) { | ||
Liber.should.have.property(method); | ||
}); | ||
describe('#getOne', function () { | ||
require('./testSingleResultMethod')(Liber, 'getOne'); | ||
}); | ||
require('./getOne')(Liber); | ||
// describe('#getOne', function () { | ||
// require('./testSingleResultMethod')(Liber, 'getOne'); | ||
// }); | ||
// describe('#getOneFromView', function () { | ||
// require('./testSingleResultMethod')(Liber, 'getOneFromView'); | ||
// }); | ||
// require('./parse')(Liber); | ||
@@ -36,0 +54,0 @@ |
@@ -29,3 +29,3 @@ var should = require('should'); | ||
var thing = find(TEST_ON_ID); | ||
res.should.have.property(TEST_ON_PROP, thing[TEST_ON_PROP]); | ||
this.res.should.have.property(TEST_ON_PROP, thing[TEST_ON_PROP]); | ||
}); | ||
@@ -32,0 +32,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27701
22
771
82