From 29e4ab55404142be3b3605f9b184e0b8636e5d9d Mon Sep 17 00:00:00 2001 From: vdemedes Date: Mon, 16 Feb 2015 18:45:58 +0200 Subject: [PATCH] automatically set collection name, if missing --- Readme.md | 2 +- build/mongorito.js | 28 ++++++++++++++++++++-------- lib/mongorito.js | 28 ++++++++++++++++++++-------- package.json | 3 ++- test.js | 30 ++++++++++++++++++++++++++++++ test/models/account.js | 21 +++++++++++++++++++++ test/mongorito.test.js | 16 ++++++++++++++-- test/test.js | 2 +- 8 files changed, 109 insertions(+), 21 deletions(-) create mode 100644 test.js create mode 100644 test/models/account.js diff --git a/Readme.md b/Readme.md index 0a5b00f..12dfc94 100644 --- a/Readme.md +++ b/Readme.md @@ -41,7 +41,7 @@ Mongorito.connect('localhost/blog'); // define model class Post extends Model { - get collection () { return 'posts'; } + } diff --git a/build/mongorito.js b/build/mongorito.js index 871b553..cb0c621 100644 --- a/build/mongorito.js +++ b/build/mongorito.js @@ -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"); @@ -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 @@ -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); @@ -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); @@ -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); @@ -383,19 +389,19 @@ 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); }; @@ -403,6 +409,12 @@ var Model = (function () { _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 @@ -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); diff --git a/lib/mongorito.js b/lib/mongorito.js index ba44276..80d43a7 100644 --- a/lib/mongorito.js +++ b/lib/mongorito.js @@ -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'); @@ -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); } @@ -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 @@ -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); @@ -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); @@ -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); @@ -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); } @@ -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); diff --git a/package.json b/package.json index eef419c..3ca5a5e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test.js b/test.js new file mode 100644 index 0000000..9c45e47 --- /dev/null +++ b/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); diff --git a/test/models/account.js b/test/models/account.js new file mode 100644 index 0000000..c710735 --- /dev/null +++ b/test/models/account.js @@ -0,0 +1,21 @@ +/** + * Dependencies + */ + +var Model = require('../../').Model; + + +/** + * Account model + */ + +class Account extends Model { + +} + + +/** + * Expose `Account` + */ + +module.exports = Account; diff --git a/test/mongorito.test.js b/test/mongorito.test.js index 3ed9fb7..53bfeb3 100644 --- a/test/mongorito.test.js +++ b/test/mongorito.test.js @@ -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 @@ -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 () { @@ -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 *() { diff --git a/test/test.js b/test/test.js index 7f59e99..3768792 100644 --- a/test/test.js +++ b/test/test.js @@ -2,4 +2,4 @@ require('babel/register')({ blacklist: ['regenerator'] }); -require('./mongorito.test'); \ No newline at end of file +require('./mongorito.test');