Skip to content

Commit

Permalink
Specify the collection for a model [ref modella#7]
Browse files Browse the repository at this point in the history
  • Loading branch information
lvivier committed Apr 11, 2014
1 parent c079214 commit 17855a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ var debug = require('debug')('modella:mongo'),
module.exports = function(url) {
var mongo = require('mongoskin').db(url, {w: 1}, function() { });

// Use alternate collection name, defaults to modelName
return function(Model) {
var db = mongo.collection(Model.modelName);
return ('string' == typeof Model) ? plugin.bind(null, Model) : plugin(Model.modelName, Model);
};

function plugin(collection, Model) {
var db = mongo.collection(collection);
Model.db = db;

db.open(function(err, col) {
Expand Down Expand Up @@ -125,5 +130,5 @@ module.exports = function(url) {
if(typeof query == 'object' && query._id && typeof query._id == 'string')
query._id = db.id(query._id);
}
};
}
};
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ describe("Modella-Mongo", function() {
col.remove({}, done);
});

describe("collection", function() {
it("sets the collection name", function() {
var Foo = modella('Foo').use(mongo('bar'));
expect(Foo.db.collection.collectionName).to.be('bar');
});

it("sets a default collection name", function() {
var Baz = modella('Baz').use(mongo);
expect(Baz.db.collection.collectionName).to.be('Baz');
});
});

describe("sync layer operations", function() {
it("defines the required sync layer operations", function() {
expect(User.save).to.be.a('function');
Expand Down

0 comments on commit 17855a8

Please sign in to comment.