Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
automatically set collection name, if missing
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemedes committed Feb 16, 2015
1 parent 35d9bc1 commit 29e4ab5
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Expand Up @@ -41,7 +41,7 @@ Mongorito.connect('localhost/blog');

// define model
class Post extends Model {
get collection () { return 'posts'; }
}


Expand Down
28 changes: 20 additions & 8 deletions build/mongorito.js
Expand Up @@ -12,6 +12,8 @@ var _classCallCheck = function (instance, Constructor) { if (!(instance instance

var ObjectID = require("monk/node_modules/mongoskin").ObjectID;
var Class = require("class-extend");

var pluralize = require("pluralize");
var compose = require("koa-compose");
var monk = require("monk");
var wrap = require("co-monk");
Expand Down Expand Up @@ -330,7 +332,11 @@ var Model = (function () {
return this;
};

Model.collection = function collection() {
Model._collection = function _collection() {
if (!this.prototype.collection) {
this.prototype.collection = pluralize(this.name).toLowerCase();
}

var name = this.prototype.collection;

// support for multiple connections
Expand All @@ -342,7 +348,7 @@ var Model = (function () {
};

Model.find = function* find(query) {
var collection = this.collection();
var collection = this._collection();
var model = this;

var q = new Query(collection, model).find(query);
Expand All @@ -351,7 +357,7 @@ var Model = (function () {
};

Model.count = function* count(query) {
var collection = this.collection();
var collection = this._collection();
var model = this;

var count = new Query(collection, model).count(query);
Expand All @@ -374,7 +380,7 @@ var Model = (function () {
};

Model.remove = function* remove(query) {
var collection = this.collection();
var collection = this._collection();
var model = this;

var query = new Query(collection, model).remove(query);
Expand All @@ -383,26 +389,32 @@ var Model = (function () {
};

Model.index = function* index() {
var collection = this.collection();
var collection = this._collection();

return yield collection.index.apply(collection, arguments);
};

Model.indexes = function* indexes() {
var collection = this.collection();
var collection = this._collection();

return yield collection.indexes();
};

Model.id = function id() {
var collection = this.collection();
var collection = this._collection();

return collection.id.apply(collection, arguments);
};

_prototypeProperties(Model, null, {
_collection: {
get: function () {
if (!this.collection) {
var _constructor = this.constructor;

this.collection = _constructor.prototype.collection = pluralize(_constructor.name).toLowerCase();
}

return Mongorito.collection(this._db, this.collection);
},
configurable: true
Expand All @@ -425,7 +437,7 @@ var methods = ["where", "limit", "skip", "sort", "exists", "lt", "lte", "gt", "g

methods.forEach(function (method) {
Model[method] = function () {
var collection = this.collection();
var collection = this._collection();
var model = this;

var query = new Query(collection, model);
Expand Down
28 changes: 20 additions & 8 deletions lib/mongorito.js
Expand Up @@ -4,6 +4,8 @@

var ObjectID = require('monk/node_modules/mongoskin').ObjectID;
var Class = require('class-extend');

var pluralize = require('pluralize');
var compose = require('koa-compose');
var monk = require('monk');
var wrap = require('co-monk');
Expand Down Expand Up @@ -100,6 +102,12 @@ class Model {
}

get _collection () {
if (!this.collection) {
let constructor = this.constructor;

this.collection = constructor.prototype.collection = pluralize(constructor.name).toLowerCase();
}

return Mongorito.collection(this._db, this.collection);
}

Expand Down Expand Up @@ -282,7 +290,11 @@ class Model {
return this;
}

static collection () {
static _collection () {
if (!this.prototype.collection) {
this.prototype.collection = pluralize(this.name).toLowerCase();
}

let name = this.prototype.collection;

// support for multiple connections
Expand All @@ -294,7 +306,7 @@ class Model {
}

static * find (query) {
let collection = this.collection();
let collection = this._collection();
let model = this;

let q = new Query(collection, model).find(query);
Expand All @@ -303,7 +315,7 @@ class Model {
}

static * count (query) {
let collection = this.collection();
let collection = this._collection();
let model = this;

let count = new Query(collection, model).count(query);
Expand All @@ -326,7 +338,7 @@ class Model {
}

static * remove (query) {
let collection = this.collection();
let collection = this._collection();
let model = this;

let query = new Query(collection, model).remove(query);
Expand All @@ -335,19 +347,19 @@ class Model {
}

static * index () {
let collection = this.collection();
let collection = this._collection();

return yield collection.index.apply(collection, arguments);
}

static * indexes () {
let collection = this.collection();
let collection = this._collection();

return yield collection.indexes();
}

static id () {
let collection = this.collection();
let collection = this._collection();

return collection.id.apply(collection, arguments);
}
Expand Down Expand Up @@ -377,7 +389,7 @@ var methods = [

methods.forEach(method => {
Model[method] = function () {
let collection = this.collection();
let collection = this._collection();
let model = this;

let query = new Query(collection, model);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -7,7 +7,8 @@
"class-extend": "^0.1.1",
"co-monk": "^1.0.0",
"koa-compose": "^2.3.0",
"monk": "^0.9.1"
"monk": "^0.9.1",
"pluralize": "^1.1.2"
},
"devDependencies": {
"babel": "^4.0.1",
Expand Down
30 changes: 30 additions & 0 deletions test.js
@@ -0,0 +1,30 @@
var pluralize = require('pluralize');

class Model {
_collection () {
if (!this.collection) {
this.collection = this.constructor.prototype.collection = pluralize(this.constructor.name).toLowerCase();
}

return this.collection;
}

find () {
this._collection();
}
}

class User extends Model {

}

class Account extends Model {

}

var account = new Account();
console.log(account.collection);
account.find();
console.log(account.collection);
account = new Account();
console.log(account.collection);
21 changes: 21 additions & 0 deletions test/models/account.js
@@ -0,0 +1,21 @@
/**
* Dependencies
*/

var Model = require('../../').Model;


/**
* Account model
*/

class Account extends Model {

}


/**
* Expose `Account`
*/

module.exports = Account;
16 changes: 14 additions & 2 deletions test/mongorito.test.js
Expand Up @@ -10,9 +10,10 @@ var chance = require('chance')();
var Mongorito = require('../');
var Model = Mongorito.Model;

var Account = require('./models/account');
var Comment = require('./models/comment');
var Post = require('./models/post');
var Task = require('./models/task');
var Comment = require('./models/comment');

/**
* Fixtures
Expand All @@ -32,9 +33,10 @@ describe ('Mongorito', function () {
});

beforeEach (function *() {
yield Account.remove();
yield Comment.remove();
yield Post.remove();
yield Task.remove();
yield Comment.remove();
});

describe ('Model', function () {
Expand Down Expand Up @@ -749,6 +751,16 @@ describe ('Mongorito', function () {
hooks[10].should.equal('afterSave');
});
});

it ('should automatically set collection name', function *() {
let account = new Account();
yield account.save();

account.collection.should.equal('accounts');

let accounts = yield Account.find();
accounts.length.should.equal(1);
});
});

it ('should be able to use multiple databases', function *() {
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Expand Up @@ -2,4 +2,4 @@ require('babel/register')({
blacklist: ['regenerator']
});

require('./mongorito.test');
require('./mongorito.test');

0 comments on commit 29e4ab5

Please sign in to comment.