Skip to content
Browse files

1.0.0

  • Loading branch information...
1 parent 28927fe commit 2d38d657fe3b838f813218ba1e6997032512778a @masylum committed May 9, 2011
View
145 README.md
@@ -9,41 +9,33 @@
"Y88888P'
-Flexible wrapper for the nodejs mongoDB driver.
-Its not a ORM, but it can be used to handle the logic of your models.
-No magic, no pain.
+Mongolia is a layer that sits on top of the mongo driver and helps you dealing with your data logic.
+Mongolia is not an ORM. Models contains no state, just logic.
+Mongolia contains no magic.
## Install
``` bash
-$ npm install mongolia
+npm install mongolia
```
Mongolia contains two independent modules:
- * `model`: An object representing a collection with some hooks of mongoDB calls.
+ * `model`: An object representing a collection with some hooks of mongo calls.
* `validator`: An object that validates mongoDB documents and returns errors if found.
# Model
-Each model has a collection name and a reference to the database.
-
-Models don't map data from mongoDB, they are just a layer to centralize all the logic.
+Models are attached to collections.
+Models don't map data from the db, they just define the logic.
``` javascript
-module.exports = function (db) {
- // our user model will do mongoDB calls using 'users' collection
- var USER = require('mongolia').model(db, 'users');
-
- // implement some user logic
-
- return USER;
-};
+var USER = require('mongolia').model(db, 'users');
```
-## mongoDB commands
+## mongo commands
-Calls to the database are done using the function `mongo`.
+Calls to the db are done using the function `mongo`.
Mongolia proxies all the `collection` methods defiend on the driver plus some custom methods.
``` javascript
@@ -52,84 +44,88 @@ var Db = require('mongodb/lib/mongodb/db').Db,
db = new Db('blog', new Server('localhost', 27017, {auto_reconnect: true, native_parser: true}));
db.open(function () {
+ var User = require('./user.js')(db);
- var User = require('./user.js')(db);
- User.mongo('findOne', {name: 'foo'}, function (error, user) {
- console.log(user);
- });
-
+ User.mongo('findOne', {name: 'foo'}, function (error, user) {
+ console.log(user);
+ });
});
```
All the `collection` methods from the driver are supported.
If you need more information visit the [driver](http://github.com/christkv/node-mongodb-native) documentation
-## Custom mongoDB commands
+## Custom mongo collection commands
Mongolia provides some useful commands that are not available using the driver.
* `findArray`: find that returns an array instead of a cursor.
* `mapReduceArray`: mapReduce that returns an array with the results.
* `mapReduceCursor`: mapReduce that returns a cursor.
+You can add your own custom methods by adding functions to the `collection_proxy` attribute in your models.
+
+``` javascript
+// Implement a custom query => Post.mongo('findByAuthor', author_id, callback)
+Post.collection_proxy.findByAuthor = function (model, collection, args, callback) {
+ args[0] = {'author._id': args[0]};
+ args[args.length - 1] = function (error, cursor) {
+ cursor.sort(['created_at', 1]).toArray(callback);
+ };
+ collection[fn].apply(collection, args);
+}
+```
+
## Hooks
Mongolia let you define some hooks on your models that will be triggered after a mongoDB command.
- * `beforeInsert(documents, callback)`: triggered *before* an `insert`.
- * `afterInsert(documents, callback)`: triggered *after* an `insert.
+ * `beforeInsert(documents, callback)`: triggered *before* an `insert`.
+ * `afterInsert(documents, callback)`: triggered *after* an `insert.
- * `beforeUpdate(document, update, callback)`: triggered *before* an `update` or `findAndModify` command.
- * `afterUpdate(document, update, callback)`: triggered *after* an `update` or `findAndModify` command.
+ * `beforeUpdate(query, update, callback)`: triggered *before* an `update` or `findAndModify` command.
+ * `afterUpdate(query, update, callback)`: triggered *after* an `update` or `findAndModify` command.
- * `beforeRemove(query, callback)`: triggered *before* a `remove` command.
- * `afterRemove(query, callback)`: triggered *after* a `remove` command.
+ * `beforeRemove(query, callback)`: triggered *before* a `remove` command.
+ * `afterRemove(query, callback)`: triggered *after* a `remove` command.
Example:
``` javascript
-module.exports = function (db) {
- var COMMENT = require('mongolia').model(db, 'comments'),
- Post = require('./post');
-
- COMMENT.beforeInsert = function (documents, callback) {
- documents.forEach(function (doc) {
- doc.created_at = new Date();
- });
- callback(null, documents);
- };
-
- COMMENT.atferInsert = function (documents, callback) {
- Post(db).mongo('update', {_id: documents[0].post._id}, {'$inc': {num_posts: 1}}, callback);
- };
+var COMMENT = require('mongolia').model(db, 'comments'),
+ Post = require('./post');
+
+COMMENT.beforeInsert = function (documents, callback) {
+ documents.forEach(function (doc) {
+ doc.created_at = new Date();
+ });
+ callback(null, documents);
+};
- return COMMENT;
+COMMENT.atferInsert = function (documents, callback) {
+ Post(db).mongo('update', {_id: documents[0].post._id}, {'$inc': {num_posts: 1}}, callback);
};
```
## Embedded documents
-Mongolia helps you to _denormalize_ your mongoDB database.
+Mongolia helps you to _denormalize_ your mongo database.
### getEmbeddedDocument
-Filters document following the `skeletons` directive.
+Filters document following the `skeletons` attribute.
getEmbeddedDocument(name, object, scope [, dot_notation]);
Example:
``` javascript
-module.exports = function (db) {
- var POST = require('mongolia').model(db, 'posts');
-
- // only embed the comment's _id, and title
- POST.skeletons = {
- comment: ['_id', 'title']
- };
+var POST = require('mongolia').model(db, 'posts');
- return POST;
+// only embed the comment's _id, and title
+POST.skeletons = {
+ comment: ['_id', 'title']
};
var comment = {'_id': 1, title: 'foo', body: 'Lorem ipsum'}
@@ -191,7 +187,7 @@ module.exports = function (db) {
## Create and update using validations
-Mongolia provides with two methods that allow you to create and update using the `validator`.
+Mongolia provides two methods that allow you to create and update using the `validator`.
``` javascript
Model.validateAndInsert(document, callback(error, validator));
@@ -201,7 +197,7 @@ Model.validateAndUpdate(document, update, options, callback(error, validator));
In order to validate an insertion/update, the model have to implement a `validate` function on your model.
``` javascript
-validate(document, update, callback);
+validate(query, update, callback);
```
Example:
@@ -211,8 +207,8 @@ Example:
module.exports = function (db) {
var POST = require('mongolia').model(db, 'posts');
- POST.validate = function (document, update, callback) {
- var validator = require('mongolia').validator(document, update);
+ POST.validate = function (query, update, callback) {
+ var validator = require('mongolia').validator(query, update);
validator.validateRegex({
title: [validator.regex.title, 'Incorrect title'],
@@ -347,3 +343,34 @@ var User = function (db) {
return USER;
};
```
+## Contributors
+
+In no specific order.
+
+ * Josep M. Bach ([txus](http://github.com/txus))
+ * Pau Ramon ([masylum](http://github.com/masylum))
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2010-2011 Pau Ramon Revilla <masylum@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
44 example/app.js
@@ -1,28 +1,26 @@
require('colors');
-var mongodb = require('mongodb'),
- Db = mongodb.Db,
- Server = mongodb.Server,
- express = require('express'),
-
- model = require('./../lib/model'),
- validator = require('./../lib/validator'),
-
- db = new Db('blog', new Server('localhost', 27017, {auto_reconnect: true, native_parser: true}), {}),
- ObjectID = db.bson_serializer.ObjectID,
-
- app = express.createServer(),
-
- APP = {
- app: app,
- db: db,
- ObjectID: ObjectID,
- options: {
- port: 3000
- },
- model: model,
- validator: validator
- };
+var mongodb = require('mongodb')
+ , Db = mongodb.Db
+ , Server = mongodb.Server
+ , express = require('express')
+
+ , model = require('./../lib/model')
+ , validator = require('./../lib/validator')
+
+ , db = new Db('blog', new Server('localhost', 27017, {auto_reconnect: true, native_parser: true}), {})
+ , ObjectID = db.bson_serializer.ObjectID
+
+ , app = express.createServer()
+
+ , APP = {
+ app: app
+ , db: db
+ , ObjectID: ObjectID
+ , options: {port: 3000}
+ , model: model
+ , validator: validator
+ };
APP.loadModel = function (str) {
return function () {
View
84 example/controllers/posts.js
@@ -1,14 +1,12 @@
module.exports = function (APP) {
- var app = APP.app,
- User = APP.loadModel('user'),
- Post = APP.loadModel('post'),
- ObjectID = APP.ObjectID,
+ var app = APP.app
+ , User = APP.loadModel('user')
+ , Post = APP.loadModel('post')
+ , ObjectID = APP.ObjectID
- embedAuthor = function (document, callback) {
+ , embedAuthor = function (document, callback) {
User().mongo('findOne', {_id: new ObjectID(document.author._id)}, function (error, user) {
- if (error) {
- callback(error);
- }
+ if (error) callback(error);
document.author = Post().getEmbeddedDocument('author', user);
callback(null, document);
@@ -17,62 +15,55 @@ module.exports = function (APP) {
// show post
app.get('/posts/:id', function (req, res) {
- var id = req.param('id'),
- funk = require('funk')();
+ var id = req.param('id')
+ , funk = require('funk')();
User().mongo('findArray', {}, funk.result('users'));
Post().mongo('findOne', {_id: new ObjectID(id)}, funk.result('post'));
-
+
funk.parallel(function () {
res.render('posts/show', {
- title: this.post.title,
- post: this.post,
- users: this.users
+ title: this.post.title
+ , post: this.post
+ , users: this.users
});
});
});
// create post
app.post('/posts', function (req, res) {
- var document = req.param('post');
+ var document = req.param('post')
+ , funk = require('funk')();
- Post().getCollection(function (error, collection) {
+ Post().getCollection(funk.add(function (error, collection) {
document._id = collection.pkFactory.createPk();
+ }));
+ embedAuthor(document, funk.nothing());
- embedAuthor(document, function (error, document) {
- Post().validateAndInsert(document, function (error, validator) {
- if (error) {
- throw error;
- }
+ funk.parallel(function () {
+ Post().validateAndInsert(document, function (error, validator) {
+ if (error) throw error;
- if (validator.hasErrors()) {
- // Show flash error
- console.log(validator.errors);
- res.redirect('/');
- } else {
- res.redirect('/posts/' + document._id + '/');
- }
- });
+ if (validator.hasErrors()) {
+ // Show flash error
+ console.log(validator.errors);
+ res.redirect('/');
+ } else {
+ res.redirect('/posts/' + document._id + '/');
+ }
});
-
});
});
// update post
app.post('/posts/:id', function (req, res) {
- var id = req.param('id'),
- update = req.param('post');
+ var id = req.param('id')
+ , update = req.param('post');
embedAuthor(update, function (error, update) {
Post().validateAndUpdate({_id: new ObjectID(id)}, {'$set': update}, function (error, validator) {
- if (error) {
- throw error;
- }
-
- if (validator.hasErrors()) {
- // Show flash error
- console.log(validator.errors);
- }
+ if (error) throw error;
+ if (validator.hasErrors()) console.log(validator.errors);
res.redirect('/posts/' + id + '/');
});
@@ -81,18 +72,13 @@ module.exports = function (APP) {
// create comment
app.post('/posts/:id/comments', function (req, res) {
- var id = req.param('id'),
- comment = req.param('comment');
+ var id = req.param('id')
+ , comment = req.param('comment');
Post().mongo('findOne', {_id: new ObjectID(id)}, function (error, post) {
- if (error) {
- throw error;
- }
-
+ if (error) throw error;
Post().addComment(post, comment, function (error, document) {
- if (error) {
- throw error;
- }
+ if (error) throw error;
res.redirect('/posts/' + id + '/');
});
});
View
21 example/controllers/public.js
@@ -1,8 +1,8 @@
module.exports = function (APP) {
- var app = APP.app,
- User = APP.loadModel('user'),
- Post = APP.loadModel('post'),
- Comment = APP.loadModel('comment');
+ var app = APP.app
+ , User = APP.loadModel('user')
+ , Post = APP.loadModel('post')
+ , Comment = APP.loadModel('comment');
app.get('/', function (req, res) {
var funk = require('funk')();
@@ -12,14 +12,13 @@ module.exports = function (APP) {
Comment().mongo('findArray', {}, funk.result('comments'));
funk.parallel(function () {
- if (this.errors) {
- throw Error(this.errors[0]);
- }
+ if (this.errors) throw Error(this.errors[0]);
+
res.render('public/index', {
- title: 'home',
- users: this.users,
- posts: this.posts,
- comments: this.comments
+ title: 'home'
+ , users: this.users
+ , posts: this.posts
+ , comments: this.comments
});
});
});
View
35 example/controllers/users.js
@@ -1,21 +1,19 @@
module.exports = function (APP) {
- var app = APP.app,
- User = APP.loadModel('user'),
- ObjectID = APP.ObjectID;
+ var app = APP.app
+ , User = APP.loadModel('user')
+ , ObjectID = APP.ObjectID;
// show user
app.get('/users/:id', function (req, res) {
var id = req.param('id');
User().mongo('findOne', {_id: new ObjectID(id)}, function (error, user) {
- if (error) {
- throw error;
- }
+ if (error) throw error;
res.render('users/show', {
- title: 'Paco',
- user: user,
- posts: user.posts
+ title: 'Paco'
+ , user: user
+ , posts: user.posts
});
});
});
@@ -28,9 +26,7 @@ module.exports = function (APP) {
document._id = collection.pkFactory.createPk();
User().validateAndInsert(document, function (error, validator) {
- if (error) {
- throw error;
- }
+ if (error) throw error;
if (validator.hasErrors()) {
// Show flash error
@@ -45,21 +41,14 @@ module.exports = function (APP) {
// update user
app.post('/users/:id', function (req, res) {
- var id = req.param('id'),
- update = req.param('user');
+ var id = req.param('id')
+ , update = req.param('user');
User().validateAndUpdate({_id: new ObjectID(id)}, {'$set': update}, function (error, validator) {
- if (error) {
- throw error;
- }
-
- if (validator.hasErrors()) {
- // Show flash error
- console.log(validator.errors);
- }
+ if (error) throw error;
+ if (validator.hasErrors()) console.log(validator.errors);
res.redirect('/users/' + id + '/');
});
});
};
-
View
4 example/models/comment.js
@@ -5,7 +5,7 @@ module.exports = function (APP) {
COMMENT.skeletons = {
post: ['_id', 'title']
};
-
+
COMMENT.validate = function (document, update, callback) {
var validator = APP.validator(document, update);
@@ -19,9 +19,11 @@ module.exports = function (APP) {
COMMENT.afterInsert = function (documents, callback) {
var funk = require('funk')();
+
documents.forEach(function (document) {
Post().pushEmbeddedDocument({_id: document.post._id}, 'comments', document, funk.nothing());
});
+
funk.parallel(callback);
};
View
8 example/models/post.js
@@ -7,7 +7,7 @@ module.exports = function (APP) {
author: ['_id', 'name'],
comments: ['_id', 'body']
};
-
+
POST.validate = function (document, update, callback) {
var validator = APP.validator(document, update);
@@ -20,9 +20,9 @@ module.exports = function (APP) {
callback(null, validator);
};
- POST.addComment = function (post, document, callback) {
- document.post = Comment().getEmbeddedDocument('post', post);
- Comment().validateAndInsert(document, callback);
+ POST.addComment = function (post, comment, callback) {
+ comment.post = Comment().getEmbeddedDocument('post', post);
+ Comment().validateAndInsert(comment, callback);
};
POST.afterUpdate = function (query, update, callback) {
View
10 example/public/css/global.css
@@ -201,6 +201,16 @@ form .operations {
padding: 10px;
width: 562px;
}
+
+#forms {
+ float: left;
+ margin-right: 40px;
+}
+
+#lists {
+ float: left;
+}
+
.button {
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
View
9 example/views/layout.jade
@@ -4,7 +4,6 @@ html
title= title || ''
link(rel: 'stylesheet', href: '/css/reset.css' )
link(rel: 'stylesheet', href: '/css/global.css' )
- link(rel: 'shortcut icon', href: '/favicon.ico')
-var css
-if(css)
@@ -20,11 +19,3 @@ html
#content
!= body
-
- -var js
- -if(js)
- script(src: '/javascripts/vendor/jquery.js' )
- script(src: '/javascripts/global.js' )
-
- -each file in js
- script(src: '/javascripts/' + file + '.js' )
View
1 example/views/posts/_comment_form.jade
@@ -1,6 +1,7 @@
-var post = post || {};
form#comment_post(method: 'POST', action: '/posts/' + post._id + '/comments')
+ h2 Comment
fieldset
.field
label(for: 'comment_body') comment body
View
1 example/views/posts/_form.jade
@@ -2,6 +2,7 @@
-var action = action || '/posts/';
form#create_post(method: 'POST', action: action)
+ h2 Post
fieldset
.field
label(for: 'post_title) Post title
View
23 example/views/posts/show.jade
@@ -1,13 +1,14 @@
-h2 Post details
-!= partial('form', {post: post, action: '/posts/' + post._id + '/'})
-!= partial('comment_form', {post: post})
+#forms
+ != partial('form', {post: post, action: '/posts/' + post._id + '/'})
+ != partial('comment_form', {post: post})
-h2 Author
--if (post.author)
- a(href: '/users/' + post.author._id + '/')= post.author.name
+#lists
+ h2 Author
+ -if (post.author)
+ a(href: '/users/' + post.author._id + '/')= post.author.name
-h2 Comments
--if (post.comments)
- ul.list
- -each comment in post.comments
- li= comment.body
+ h2 Comments
+ -if (post.comments)
+ ul.list
+ -each comment in post.comments
+ li= comment.body
View
49 example/views/public/index.jade
@@ -1,28 +1,27 @@
-h2 Create user
-!= partial('users/form')
+#forms
+ != partial('users/form')
+ != partial('posts/form')
-h2 List users
--if(users)
- ul.list
- -each user in users
- li
- a(href: '/users/' + user._id + '/')= user.name
+#lists
+ h2 List users
+ -if(users)
+ ul.list
+ -each user in users
+ li
+ a(href: '/users/' + user._id + '/')= user.name
-h2 Create posts
-!= partial('posts/form')
+ h2 List posts
+ -if(posts)
+ ul.list
+ -each post in posts
+ li
+ a(href: '/posts/' + post._id + '/')= post.title
-h2 List posts
--if(posts)
- ul.list
- -each post in posts
- li
- a(href: '/posts/' + post._id + '/')= post.title
-
-h2 List comments
--if(comments)
- ul.list
- -each comment in comments
- li
- span= comment.body
- |
- a(href: '/posts/' + comment.post._id + '/')= comment.post.title
+ h2 List comments
+ -if(comments)
+ ul.list
+ -each comment in comments
+ li
+ span= comment.body
+ |
+ a(href: '/posts/' + comment.post._id + '/')= comment.post.title
View
1 example/views/users/_form.jade
@@ -2,6 +2,7 @@
-var action = action || '/users/';
form#create_user(method: 'POST', action: action)
+ h2 User
fieldset
.field
label(for: 'user_name') User name
View
17 example/views/users/show.jade
@@ -1,9 +1,10 @@
-h2 User details
-!= partial('form', {user: user, action: '/users/' + user._id + '/'})
+#forms
+ != partial('form', {user: user, action: '/users/' + user._id + '/'})
-h2 User Posts
--if (posts)
- ul.list
- -each post in posts
- li
- a(href: '/posts/' + post._id + '/')= post.title
+#lists
+ h2 User Posts
+ -if (posts)
+ ul.list
+ -each post in posts
+ li
+ a(href: '/posts/' + post._id + '/')= post.title
View
24 lib/helpers/collection_proxy.js
@@ -1,16 +1,16 @@
-var COLLECTION = {},
+var PROXY = {},
_apply = function (collection, fn, args) {
return collection[fn].apply(collection, args);
};
-COLLECTION.proxy = function (model, fn, collection, args, callback) {
+PROXY.proxy = function (model, fn, collection, args, callback) {
if (arguments.length < 5) {
callback = function () {};
}
// overwritten method
- if (COLLECTION[fn] !== undefined) {
- return COLLECTION[fn](model, collection, args, callback);
+ if (PROXY[fn] !== undefined) {
+ return PROXY[fn](model, collection, args, callback);
// driver method
} else {
@@ -27,7 +27,7 @@ COLLECTION.proxy = function (model, fn, collection, args, callback) {
* @param {Array} args
* @param {Function} callback
*/
-COLLECTION.findArray = function (model, collection, args, callback) {
+PROXY.findArray = function (model, collection, args, callback) {
args[args.length - 1] = function (error, cursor) {
cursor.toArray(callback);
};
@@ -43,7 +43,7 @@ COLLECTION.findArray = function (model, collection, args, callback) {
* @param {Array} args
* @param {Function} callback
*/
-COLLECTION.insert = function (model, collection, args, callback) {
+PROXY.insert = function (model, collection, args, callback) {
args[args.length - 1] = function (error, ret) {
if (error) {
return callback(error, null);
@@ -78,7 +78,7 @@ COLLECTION.insert = function (model, collection, args, callback) {
* @param {Function} callback
*/
['findAndModify', 'update'].forEach(function (method) {
- COLLECTION[method] = function (model, collection, args, callback) {
+ PROXY[method] = function (model, collection, args, callback) {
var update_index = method === 'update' ? 1 : 2;
args[args.length - 1] = function (error, ret) {
@@ -116,7 +116,7 @@ COLLECTION.insert = function (model, collection, args, callback) {
* @param {Array} args
* @param {Function} callback
*/
-COLLECTION.remove = function (model, collection, args, callback) {
+PROXY.remove = function (model, collection, args, callback) {
args[args.length - 1] = function (error, ret) {
if (error) {
return callback(error, null);
@@ -146,7 +146,7 @@ COLLECTION.remove = function (model, collection, args, callback) {
* @param {Array} args
* @param {Function} callback
*/
-COLLECTION.mapReduceCursor = function (model, collection, args, callback) {
+PROXY.mapReduceCursor = function (model, collection, args, callback) {
args[args.length - 1] = function (error, collection) {
if (error) {
callback(error, null);
@@ -165,8 +165,8 @@ COLLECTION.mapReduceCursor = function (model, collection, args, callback) {
* @param {Array} args
* @param {Function} callback
*/
-COLLECTION.mapReduceArray = function (model, collection, args, callback) {
- COLLECTION.mapReduceCursor(collection, args, 'mapReduce', function (error, cursor) {
+PROXY.mapReduceArray = function (model, collection, args, callback) {
+ PROXY.mapReduceCursor(collection, args, 'mapReduce', function (error, cursor) {
if (error) {
callback(error, null);
} else {
@@ -175,4 +175,4 @@ COLLECTION.mapReduceArray = function (model, collection, args, callback) {
});
};
-module.exports = COLLECTION;
+module.exports = PROXY;
View
14 lib/model.js
@@ -14,10 +14,13 @@ module.exports = function (db, collection_name) {
throw (Error('You must specify a collection name'));
}
- var MODEL = {db: db};
+ var MODEL = {
+ db: db,
+ collection_proxy: require('./helpers/collection_proxy')
+ };
/**
- * Gets and caches the mongo collection
+ * Gets the mongo collection
*
* @param {Function} callback
* @returns itself
@@ -39,8 +42,7 @@ module.exports = function (db, collection_name) {
* @returns itself
*/
MODEL.mongo = function (fn, options, callback) {
- var args = Array.prototype.slice.call(arguments, 0),
- collection_proxy = require('./helpers/collection_proxy');
+ var args = Array.prototype.slice.call(arguments, 0);
fn = args.shift();
@@ -53,7 +55,7 @@ module.exports = function (db, collection_name) {
if (error) {
callback(error, null);
} else {
- collection_proxy.proxy(MODEL, fn, collection, args, callback);
+ MODEL.collection_proxy.proxy(MODEL, fn, collection, args, callback);
}
});
@@ -104,7 +106,7 @@ module.exports = function (db, collection_name) {
*/
MODEL.beforeInsert = function (documents, callback) {
documents.forEach(function (document) {
- if (!document.created_at) {
+ if (!document.created_at) {
document.created_at = new Date();
}
});
View
13 package.json
@@ -1,16 +1,19 @@
{
"name": "mongolia",
- "description": "Mongodb driver wrapper. Acts as model.",
- "version": "0.0.10",
+ "description": "Layer on top of the mongodb driver to implement data logic.",
+ "version": "1.0.0",
"author": "Pau Ramon <masylum@gmail.com>",
- "keywords": ["mongo", "mongodb", "orm", "database", "db"],
+ "contributors": [
+ { "name": "Pau Ramon", "email": "masylum@gmail.com" },
+ { "name": "Josep M. Bach", "email": "josep.m.bach@gmail.com" }
+ ],
+ "keywords": ["mongo", "mongodb", "models", "database", "db"],
"main": "./index",
"dependencies": { "mongodb": ">=0.9.3", "funk": ">=1.0.1"},
"devDependencies": {
"testosterone": ">= 1.2.0",
"gently": ">= 0.9.0"
},
-
"repository" : {"type": "git" , "url": "http://github.com/masylum/mongolia.git" },
- "engines": { "node": ">= 0.2.0" }
+ "engines": { "node": ">= 0.4.0" }
}
View
5 test/model_test.js
@@ -3,7 +3,6 @@ var testosterone = require('testosterone')({sync: true, title: 'mongolia/model.j
gently = global.GENTLY = new (require('gently')),
Model = require('./../lib/model'),
- CollectionProxy = require('./../lib/helpers/collection_proxy'),
_db = {},
_mock_validator = function (ret) {
@@ -53,7 +52,7 @@ testosterone
_callback(null, collection);
});
- gently.expect(CollectionProxy, 'proxy', function (_model, _fn, _collection, _args, _callback) {
+ gently.expect(User.collection_proxy, 'proxy', function (_model, _fn, _collection, _args, _callback) {
assert.equal(_fn, 'findArray');
assert.deepEqual(_collection, collection);
assert.deepEqual(_args[0], query);
@@ -71,7 +70,7 @@ testosterone
_callback(null, collection);
});
- gently.expect(CollectionProxy, 'proxy', function (_model, _fn, _collection, _args, _callback) {
+ gently.expect(User.collection_proxy, 'proxy', function (_model, _fn, _collection, _args, _callback) {
assert.equal(_fn, 'findArray');
assert.deepEqual(_collection, collection);
assert.deepEqual(_args[0], query);

0 comments on commit 2d38d65

Please sign in to comment.
Something went wrong with that request. Please try again.