Skip to content

Commit

Permalink
Fixed up the default findByType accessor and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanoehlman committed Jan 31, 2012
1 parent b8576ce commit f966f6c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
6 changes: 4 additions & 2 deletions flatpack.js
Expand Up @@ -4,6 +4,7 @@ var models = {},
views = require('./lib/views')
async = require('async'),
_ = require('underscore'),
_s = require('underscore.string'),
debug = require('debug')('flatpack');

/**
Expand All @@ -30,10 +31,11 @@ exports.define = function(couchAdminUrl, db, model, options, callback) {
if (attributes && attributes.length > 0) {
// Create the accessor, with args as params
} else {
var design = designName;
// Create the accessor
accessor['find' + viewName] = function(id, done) {
accessor['find' + _s.camelize('_' + viewName)] = function(done) {
var db = this._getDB();
db.get(designName + '/view/' + dbView, {key:id}, done);
this._getDB().get(designName + '/_view/' + dbView, null, done);
};

}
Expand Down
6 changes: 3 additions & 3 deletions lib/views.js
Expand Up @@ -48,7 +48,7 @@ exports.createIfNotExists = function(couchurl, db, model, attributes, options, c
// Check for the existence of the view
this.exists(couchurl, db, model, attributes, function(err, found) {
if (err) return callback(err);
if (found) return callback();
if (found) return callback(null, _getDesignName(model), _getViewName(model, attributes));
// Doesn't exist, so create
that.create(couchurl, db, model, attributes, options, callback);
});
Expand Down Expand Up @@ -139,6 +139,6 @@ exports._createMapFunction = createMapFunction = function(model, attributes) {
} else {
emit += 'doc.id';
}
emit += ", null);";
return "function(doc) { if (doc.Type == '" + model + "') { " + emit + " } }";
emit += ", doc);";
return "function(doc) { if (doc.type == '" + model + "') { " + emit + " } }";
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@
"description": "Simple mapping library for CouchDB",
"author": "Nathan Oehlman <nathan.oehlman@sidelab.com>",
"tags": ["flatpack", "sidelab", "couch", "db"],
"version": "0.0.1",
"version": "0.0.2",

"main": "flatpack.js",
"engines": { "node": ">= 0.4.x < 0.7.0" },
Expand Down
31 changes: 30 additions & 1 deletion test/flatpack_spec.js
@@ -1,7 +1,8 @@
var expect = require('chai').expect,
flatpack = require('../flatpack'),
views = require('../lib/views'),
config = require('config');
config = require('config'),
async = require('async');

describe('defining a flatpack model definition', function() {

Expand Down Expand Up @@ -49,5 +50,33 @@ describe('defining a flatpack model definition', function() {
});
});
});

it('should be able to get all instances of the object by type', function(done) {

var customers = [{firstName: 'Nathan', lastName: 'Oehlman', company: 'Sidelab'}, {firstName: 'Damon', lastName: 'Oehlman', company: 'Sidelab'}],
customerdb = flatpack.use('customer');

async.forEach(customers, function(customer, callback) {
customerdb.save(customer, callback);
}, function(err) {
if (err) return done(err);
var results = customerdb.findByType(function(err, results) {
if (err) return done(err);

// Check we have the correct results
expect(results.total_rows).to.equal(2);
expect(results.rows[0].value.firstName).to.equal('Nathan');
expect(results.rows[1].value.firstName).to.equal('Damon');

// Delete the entries
async.forEach(results.rows, function(row, callback) {
customerdb.delete(row.value, callback);
}, function (err) {
done(err);
});
});
});

});

});
4 changes: 2 additions & 2 deletions test/views_spec.js
Expand Up @@ -6,8 +6,8 @@ var expect = require('chai').expect,
describe('views should be named, detected and created, and destroyed appropriately', function() {

it('should name views correctly', function(done) {
expect(views._getViewName('client', ['type'])).to.equal('by_type');
expect(views._getViewName('client', ['firstName', 'lastName'])).to.equal('by_firstName_lastName');
expect(views._getViewName('client', ['type'])).to.equal('byType');
expect(views._getViewName('client', ['firstName', 'lastName'])).to.equal('byFirstNameLastName');
done();
});

Expand Down

0 comments on commit f966f6c

Please sign in to comment.