Skip to content
Browse files

refactored code

  • Loading branch information...
1 parent 88acf53 commit fbf7d54a7d7e91d0b9623e5f0280a9f310fd22ac @vdemedes committed May 30, 2012
View
3 Readme.md
@@ -24,6 +24,7 @@ Mongorito = require 'mongorito'
Mongorito.connect ['mongo://user:password@127.0.0.1:27017/databaseName']
class Post
+ constructor: -> super # do not change this line
keys: ['author', 'title']
scopes:
one: limit: 1
@@ -56,7 +57,7 @@ Check out **examples** folder, it has a lot of code, which describes all parts o
# Tests
-Tests made using Mocha. Run using:
+Tests made using Mocha. Start MongoDB and run them using:
```
mocha
View
2 examples/connection.js
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.3.1
+// Generated by CoffeeScript 1.3.3
var Mongorito;
Mongorito = require('../lib/mongorito');
View
3 examples/definition.coffee
@@ -3,5 +3,6 @@ Mongorito = require '../lib/mongorito'
Mongorito.connect ['mongo://127.0.0.1:27017/databaseName']
class Post
+ constructor: -> super # REQUIRED
-Post = Mongorito.bake Post # REQUIRED STEP
+Post = Mongorito.bake Post # REQUIRED, TOO
View
8 examples/definition.js
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.3.1
+// Generated by CoffeeScript 1.3.3
var Mongorito, Post;
Mongorito = require('../lib/mongorito');
@@ -7,9 +7,9 @@ Mongorito.connect(['mongo://127.0.0.1:27017/databaseName']);
Post = (function() {
- Post.name = 'Post';
-
- function Post() {}
+ function Post() {
+ Post.__super__.constructor.apply(this, arguments);
+ }
return Post;
View
1 examples/hooks.coffee
@@ -3,6 +3,7 @@ Mongorito = require '../lib/mongorito'
Mongorito.connect ['mongo://127.0.0.1:27017/databaseName']
class Post
+ constructor: -> super
beforeCreate: -> # before creating post
View
8 examples/hooks.js
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.3.1
+// Generated by CoffeeScript 1.3.3
var Mongorito, Post;
Mongorito = require('../lib/mongorito');
@@ -7,9 +7,9 @@ Mongorito.connect(['mongo://127.0.0.1:27017/databaseName']);
Post = (function() {
- Post.name = 'Post';
-
- function Post() {}
+ function Post() {
+ Post.__super__.constructor.apply(this, arguments);
+ }
Post.prototype.beforeCreate = function() {};
View
1 examples/operations.coffee
@@ -3,6 +3,7 @@ Mongorito = require '../lib/mongorito'
Mongorito.connect ['mongo://127.0.0.1:27017/databaseName']
class Post
+ constructor: -> super
keys: ['title', 'content']
Post = Mongorito.bake Post
View
8 examples/operations.js
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.3.1
+// Generated by CoffeeScript 1.3.3
var Mongorito, Post, post;
Mongorito = require('../lib/mongorito');
@@ -7,9 +7,9 @@ Mongorito.connect(['mongo://127.0.0.1:27017/databaseName']);
Post = (function() {
- Post.name = 'Post';
-
- function Post() {}
+ function Post() {
+ Post.__super__.constructor.apply(this, arguments);
+ }
Post.prototype.keys = ['title', 'content'];
View
1 examples/queries.coffee
@@ -3,6 +3,7 @@ Mongorito = require '../lib/mongorito'
Mongorito.connect ['mongo://127.0.0.1:27017/databaseName']
class Post
+ constructor: -> super
keys: ['title', 'author', 'content']
Post = Mongorito.bake Post
View
8 examples/queries.js
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.3.1
+// Generated by CoffeeScript 1.3.3
var Mongorito, Post;
Mongorito = require('../lib/mongorito');
@@ -7,9 +7,9 @@ Mongorito.connect(['mongo://127.0.0.1:27017/databaseName']);
Post = (function() {
- Post.name = 'Post';
-
- function Post() {}
+ function Post() {
+ Post.__super__.constructor.apply(this, arguments);
+ }
Post.prototype.keys = ['title', 'author', 'content'];
View
1 examples/scopes.coffee
@@ -3,6 +3,7 @@ Mongorito = require '../lib/mongorito'
Mongorito.connect ['mongo://127.0.0.1:27017/databaseName']
class Post
+ constructor: -> super
keys: ['author', 'content', 'title']
scopes:
byDrew: author: 'Drew'
View
8 examples/scopes.js
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.3.1
+// Generated by CoffeeScript 1.3.3
var Mongorito, Post;
Mongorito = require('../lib/mongorito');
@@ -7,9 +7,9 @@ Mongorito.connect(['mongo://127.0.0.1:27017/databaseName']);
Post = (function() {
- Post.name = 'Post';
-
- function Post() {}
+ function Post() {
+ Post.__super__.constructor.apply(this, arguments);
+ }
Post.prototype.keys = ['author', 'content', 'title'];
View
1 examples/validations.coffee
@@ -3,6 +3,7 @@ Mongorito = require '../lib/mongorito'
Mongorito.connect ['mongo://127.0.0.1:27017/databaseName']
class Tweet
+ constructor: -> super
keys: ['body']
validateBody: (callback) -> # you should pass false, if invalid and true, if valid
View
8 examples/validations.js
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.3.1
+// Generated by CoffeeScript 1.3.3
var Mongorito, Tweet, tweet;
Mongorito = require('../lib/mongorito');
@@ -7,9 +7,9 @@ Mongorito.connect(['mongo://127.0.0.1:27017/databaseName']);
Tweet = (function() {
- Tweet.name = 'Tweet';
-
- function Tweet() {}
+ function Tweet() {
+ Tweet.__super__.constructor.apply(this, arguments);
+ }
Tweet.prototype.keys = ['body'];
View
214 lib/mongorito.coffee
@@ -6,81 +6,99 @@ Client = undefined
String::replaceAt = (index, char) ->
@substr(0, index) + char + @substr(index + char.length)
-`var __hasProp = {}.hasOwnProperty,
- extendsClass = function(child, parent) {
- for (var key in parent) {
- if (__hasProp.call(parent, key)) child[key] = parent[key];
- }
- function ctor() {
- this.constructor = child;
- for(var key in child.prototype) {
- var newKey = inflect.camelize(key);
- this[newKey.replaceAt(0, String.fromCharCode(32 + newKey.charCodeAt(0)))] = child.prototype[key];
- }
- }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
-}`
+hasProp = {}.hasOwnProperty
+extendClass = (child, parent) ->
+ for key of parent
+ child[key] = parent[key] if hasProp.call parent, key
+
+ ctor = ->
+ @constructor = child
+ for key of child::
+ newKey = inflect.camelize key
+ @[newKey.replaceAt(0, String.fromCharCode(32 + newKey.charCodeAt(0)))] = child::[key]
+
+ undefined
+
+ ctor:: = parent::
+ child:: = new ctor
+ child.__super__ = parent::
+ child
class Mongorito
@disconnect: ->
- do Client.close
+ do Client.server.close
@connect: (servers = []) ->
- Client = new mongolian servers[0]
- Client.log=
+ Client = new mongolian servers[0], log:
debug: ->
info: ->
warn: ->
error: ->
@bake: (model) ->
- extendsClass model, Model
+ extendClass model, Model
model::collectionName = model.collectionName = model::collectionName or inflect.pluralize model.name.toLowerCase()
if model::scopes
- scopes = []
- for scope of model::scopes
- scopes.push scope
-
- async.forEach scopes, (scope, nextScope) ->
+ Object.keys(model::scopes).forEach (scope) ->
model[scope] = (callback) ->
model.find model::scopes[scope], callback
- do nextScope
- , ->
- model.model = model
+
+ model::keys.forEach (key) ->
+ model::__defineGetter__ key, ->
+ @get key
+
+ model::__defineSetter__ key, (value) ->
+ @set key, value
+
+ model::__defineGetter__ '_id', ->
+ @get '_id'
+
+ model::__defineSetter__ '_id', (value) ->
+ @set '_id', value.toString()
+
+ model::model = model.model = model
model
class Model
- constructor: ->
+ fields: {}
+ old: {}
- fields: ->
- notFields = ['constructor', 'save', 'collectionName', 'create', 'fields', 'update', 'remove', 'beforeCreate', 'aroundCreate', 'afterCreate', 'beforeUpdate', 'aroundUpdate', 'afterUpdate', 'old', 'callMethod', 'fill', 'scopes', 'keys', 'model']
- fields = {}
- for field of @
- fields[field] = @[field] if -1 is notFields.indexOf field
-
- fields
+ get: (key) ->
+ @fields[key]
+
+ set: (key, value) ->
+ @old[key] = @fields[key] if @fields[key]
+ @fields[key] = value
+
+ constructor: ->
+ @fields = @old = {}
toJSON: ->
- @fields()
+ @fields
- @bakeModelsFromItems: (items, _model) ->
+ @bakeModelsFromItems: (items) ->
models = []
for item in items
item._id = item._id.toString()
- model = new _model
- model.collectionName = _model.collectionName
+ model = new @model
+ model.collectionName = @model.collectionName
model.old = {}
- model.model = _model
+ model.model = @model
for field of item
- model.old[field] = model[field] = item[field]
+ model[field] = item[field]
models.push model
models
+ @extractFields: (options) ->
+ fields = {}
+ notFields = ['limit', 'skip', 'sort']
+ for property of options
+ fields[property] = options[property] if options.hasOwnProperty(property) and notFields.indexOf(property) is -1
+
+ fields
+
@find: (options, callback) ->
if typeof options is 'function'
callback = options
@@ -90,102 +108,86 @@ class Model
callback = options.callback
delete options.callback
- that = @
+ return @findOne(options, callback) if options._id
- query = (done) ->
- fields = {}
- notFields = ['limit', 'skip', 'sort']
- for property of options
- fields[property] = options[property] if options.hasOwnProperty(property) and notFields.indexOf(property) is -1
-
- request = Client.collection(that.collectionName).find(fields)
- request = request.limit options.limit if options.limit
- request = request.skip options.skip if options.skip
- request = request.sort options.sort if options.sort
-
- request.toArray (err, items) ->
- for item in items
- item._id = item._id.toString()
- done err, items
+ query = Client.collection(@collectionName).find @extractFields(options)
+ query = query.sort options.sort if options.sort
+ query = query.skip options.skip if options.skip
+ query = query.limit options.limit if options.limit
+
+ query.toArray (err, items) =>
+ callback err, @bakeModelsFromItems items
+
+ @findOne: (options, callback) ->
+ _id = new mongolian.ObjectId options._id
- query (err, items) ->
- models = that.bakeModelsFromItems items, that.model
- callback err, models
+ Client.collection(@collectionName).find(_id: _id).toArray (err, items) =>
+ callback err, @bakeModelsFromItems(items)[0]
- fill: (fields = {}) ->
+ updateAttributes: (fields = {}) ->
for key of fields
@[key] = fields[key] if -1 < @keys.indexOf key
+ update_attributes: -> @updateAttributes.apply @, arguments
+
callMethod: (method) ->
method = @[method] or @[inflect.underscore(method)]
method.call @ if method
save: (callback) ->
- that = @
- @old = fields = @fields()
- keys = []
- for field of fields
- keys.push field
- async.filter keys, (key, nextKey) ->
- validationMethod = that["validate#{ inflect.camelize key }"] or that["validate_#{ inflect.underscore key }"]
+ async.filter @keys, (key, nextKey) =>
+ validationMethod = @["validate#{ inflect.camelize key }"] or @["validate_#{ inflect.underscore key }"]
if validationMethod
- validationMethod.call that, (valid) ->
+ validationMethod.call @, (valid) ->
nextKey not valid
else
nextKey false
- , (results) ->
- return callback yes, results if results.length > 0
-
- performOperation = ->
- if fields._id
- that.update callback, yes
- else
- that.create callback, yes
+ , (results) =>
+ return callback(yes, results) if results.length > 0
- do performOperation
-
- create: (callback, fromSave = no) ->
- object = @fields()
+ if @fields._id then @update(callback) else @create(callback)
+ create: (callback) ->
@callMethod 'beforeCreate'
@callMethod 'aroundCreate'
- that = @
- Client.collection(@collectionName).insert object, (err, result) ->
+ Client.collection(@collectionName).insert @fields, (err, result) =>
result._id = result._id.toString()
- that._id = result._id
- that.callMethod 'aroundCreate'
- that.callMethod 'afterCreate'
+ @fields._id = result._id
+ @callMethod 'aroundCreate'
+ @callMethod 'afterCreate'
callback err, result if callback
- update: (callback, fromSave = no) ->
- object = @fields()
- _id = new mongolian.ObjectId object._id
- delete object._id
+ update: (callback) ->
+ fields = {}
+ for key of @fields
+ fields[key] = @fields[key]
+
+ _id = new mongolian.ObjectId fields._id
+ delete fields._id
@callMethod 'beforeUpdate'
@callMethod 'aroundUpdate'
- that = @
- Client.collection(@collectionName).update { _id: _id }, object, (err, rowsUpdated) ->
- that.callMethod 'aroundUpdate'
- that.callMethod 'afterUpdate'
+ Client.collection(@collectionName).update { _id: _id }, fields, (err, rowsUpdated) =>
+ @callMethod 'aroundUpdate'
+ @callMethod 'afterUpdate'
callback err, rowsUpdated if callback
remove: (callback) ->
- object = @fields()
-
- _id = new mongolian.ObjectId object._id
+ _id = new mongolian.ObjectId @fields._id
@callMethod 'beforeRemove'
@callMethod 'aroundRemove'
- that = @
- query = ->
- Client.collection(that.collectionName).remove _id: _id, (err) ->
- that.callMethod 'aroundRemove'
- that.callMethod 'afterRemove'
- callback err if callback
- do query
+
+ Client.collection(@collectionName).remove _id: _id, (err) =>
+ @callMethod 'aroundRemove'
+ @callMethod 'afterRemove'
+ callback err if callback
+
+ @remove: (callback) ->
+ Client.collection(@collectionName).remove {}, (err) ->
+ callback err if callback
module.exports=
connect: Mongorito.connect
View
312 lib/mongorito.js
@@ -1,5 +1,5 @@
-// Generated by CoffeeScript 1.3.1
-var Client, Model, Mongorito, async, inflect, mongolian;
+// Generated by CoffeeScript 1.3.3
+var Client, Model, Mongorito, async, extendClass, hasProp, inflect, mongolian;
mongolian = require('mongolian');
@@ -13,65 +13,77 @@ String.prototype.replaceAt = function(index, char) {
return this.substr(0, index) + char + this.substr(index + char.length);
};
-var __hasProp = {}.hasOwnProperty,
- extendsClass = function(child, parent) {
- for (var key in parent) {
- if (__hasProp.call(parent, key)) child[key] = parent[key];
- }
- function ctor() {
- this.constructor = child;
- for(var key in child.prototype) {
- var newKey = inflect.camelize(key);
- this[newKey.replaceAt(0, String.fromCharCode(32 + newKey.charCodeAt(0)))] = child.prototype[key];
- }
- }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
-};
+hasProp = {}.hasOwnProperty;
+extendClass = function(child, parent) {
+ var ctor, key;
+ for (key in parent) {
+ if (hasProp.call(parent, key)) {
+ child[key] = parent[key];
+ }
+ }
+ ctor = function() {
+ var newKey;
+ this.constructor = child;
+ for (key in child.prototype) {
+ newKey = inflect.camelize(key);
+ this[newKey.replaceAt(0, String.fromCharCode(32 + newKey.charCodeAt(0)))] = child.prototype[key];
+ }
+ return void 0;
+ };
+ ctor.prototype = parent.prototype;
+ child.prototype = new ctor;
+ child.__super__ = parent.prototype;
+ return child;
+};
Mongorito = (function() {
- Mongorito.name = 'Mongorito';
-
function Mongorito() {}
Mongorito.disconnect = function() {
- return Client.close();
+ return Client.server.close();
};
Mongorito.connect = function(servers) {
if (servers == null) {
servers = [];
}
- Client = new mongolian(servers[0]);
- return Client.log = {
- debug: function() {},
- info: function() {},
- warn: function() {},
- error: function() {}
- };
+ return Client = new mongolian(servers[0], {
+ log: {
+ debug: function() {},
+ info: function() {},
+ warn: function() {},
+ error: function() {}
+ }
+ });
};
Mongorito.bake = function(model) {
- var scope, scopes;
- extendsClass(model, Model);
+ extendClass(model, Model);
model.prototype.collectionName = model.collectionName = model.prototype.collectionName || inflect.pluralize(model.name.toLowerCase());
if (model.prototype.scopes) {
- scopes = [];
- for (scope in model.prototype.scopes) {
- scopes.push(scope);
- }
- async.forEach(scopes, function(scope, nextScope) {
- model[scope] = function(callback) {
+ Object.keys(model.prototype.scopes).forEach(function(scope) {
+ return model[scope] = function(callback) {
return model.find(model.prototype.scopes[scope], callback);
};
- return nextScope();
- }, function() {});
+ });
}
- model.model = model;
+ model.prototype.keys.forEach(function(key) {
+ model.prototype.__defineGetter__(key, function() {
+ return this.get(key);
+ });
+ return model.prototype.__defineSetter__(key, function(value) {
+ return this.set(key, value);
+ });
+ });
+ model.prototype.__defineGetter__('_id', function() {
+ return this.get('_id');
+ });
+ model.prototype.__defineSetter__('_id', function(value) {
+ return this.set('_id', value.toString());
+ });
+ model.prototype.model = model.model = model;
return model;
};
@@ -81,46 +93,62 @@ Mongorito = (function() {
Model = (function() {
- Model.name = 'Model';
+ Model.prototype.fields = {};
- function Model() {}
+ Model.prototype.old = {};
- Model.prototype.fields = function() {
- var field, fields, notFields;
- notFields = ['constructor', 'save', 'collectionName', 'create', 'fields', 'update', 'remove', 'beforeCreate', 'aroundCreate', 'afterCreate', 'beforeUpdate', 'aroundUpdate', 'afterUpdate', 'old', 'callMethod', 'fill', 'scopes', 'keys', 'model'];
- fields = {};
- for (field in this) {
- if (-1 === notFields.indexOf(field)) {
- fields[field] = this[field];
- }
+ Model.prototype.get = function(key) {
+ return this.fields[key];
+ };
+
+ Model.prototype.set = function(key, value) {
+ if (this.fields[key]) {
+ this.old[key] = this.fields[key];
}
- return fields;
+ return this.fields[key] = value;
};
+ function Model() {
+ this.fields = this.old = {};
+ }
+
Model.prototype.toJSON = function() {
- return this.fields();
+ return this.fields;
};
- Model.bakeModelsFromItems = function(items, _model) {
+ Model.bakeModelsFromItems = function(items) {
var field, item, model, models, _i, _len;
models = [];
for (_i = 0, _len = items.length; _i < _len; _i++) {
item = items[_i];
item._id = item._id.toString();
- model = new _model;
- model.collectionName = _model.collectionName;
+ model = new this.model;
+ model.collectionName = this.model.collectionName;
model.old = {};
- model.model = _model;
+ model.model = this.model;
for (field in item) {
- model.old[field] = model[field] = item[field];
+ model[field] = item[field];
}
models.push(model);
}
return models;
};
+ Model.extractFields = function(options) {
+ var fields, notFields, property;
+ fields = {};
+ notFields = ['limit', 'skip', 'sort'];
+ for (property in options) {
+ if (options.hasOwnProperty(property) && notFields.indexOf(property) === -1) {
+ fields[property] = options[property];
+ }
+ }
+ return fields;
+ };
+
Model.find = function(options, callback) {
- var query, that;
+ var query,
+ _this = this;
if (typeof options === 'function') {
callback = options;
options = {};
@@ -130,43 +158,36 @@ Model = (function() {
delete options.callback;
}
}
- that = this;
- query = function(done) {
- var fields, notFields, property, request;
- fields = {};
- notFields = ['limit', 'skip', 'sort'];
- for (property in options) {
- if (options.hasOwnProperty(property) && notFields.indexOf(property) === -1) {
- fields[property] = options[property];
- }
- }
- request = Client.collection(that.collectionName).find(fields);
- if (options.limit) {
- request = request.limit(options.limit);
- }
- if (options.skip) {
- request = request.skip(options.skip);
- }
- if (options.sort) {
- request = request.sort(options.sort);
- }
- return request.toArray(function(err, items) {
- var item, _i, _len;
- for (_i = 0, _len = items.length; _i < _len; _i++) {
- item = items[_i];
- item._id = item._id.toString();
- }
- return done(err, items);
- });
- };
- return query(function(err, items) {
- var models;
- models = that.bakeModelsFromItems(items, that.model);
- return callback(err, models);
+ if (options._id) {
+ return this.findOne(options, callback);
+ }
+ query = Client.collection(this.collectionName).find(this.extractFields(options));
+ if (options.sort) {
+ query = query.sort(options.sort);
+ }
+ if (options.skip) {
+ query = query.skip(options.skip);
+ }
+ if (options.limit) {
+ query = query.limit(options.limit);
+ }
+ return query.toArray(function(err, items) {
+ return callback(err, _this.bakeModelsFromItems(items));
});
};
- Model.prototype.fill = function(fields) {
+ Model.findOne = function(options, callback) {
+ var _id,
+ _this = this;
+ _id = new mongolian.ObjectId(options._id);
+ return Client.collection(this.collectionName).find({
+ _id: _id
+ }).toArray(function(err, items) {
+ return callback(err, _this.bakeModelsFromItems(items)[0]);
+ });
+ };
+
+ Model.prototype.updateAttributes = function(fields) {
var key, _results;
if (fields == null) {
fields = {};
@@ -182,6 +203,10 @@ Model = (function() {
return _results;
};
+ Model.prototype.update_attributes = function() {
+ return this.updateAttributes.apply(this, arguments);
+ };
+
Model.prototype.callMethod = function(method) {
method = this[method] || this[inflect.underscore(method)];
if (method) {
@@ -190,100 +215,89 @@ Model = (function() {
};
Model.prototype.save = function(callback) {
- var field, fields, keys, that;
- that = this;
- this.old = fields = this.fields();
- keys = [];
- for (field in fields) {
- keys.push(field);
- }
- return async.filter(keys, function(key, nextKey) {
+ var _this = this;
+ return async.filter(this.keys, function(key, nextKey) {
var validationMethod;
- validationMethod = that["validate" + (inflect.camelize(key))] || that["validate_" + (inflect.underscore(key))];
+ validationMethod = _this["validate" + (inflect.camelize(key))] || _this["validate_" + (inflect.underscore(key))];
if (validationMethod) {
- return validationMethod.call(that, function(valid) {
+ return validationMethod.call(_this, function(valid) {
return nextKey(!valid);
});
} else {
return nextKey(false);
}
}, function(results) {
- var performOperation;
if (results.length > 0) {
return callback(true, results);
}
- performOperation = function() {
- if (fields._id) {
- return that.update(callback, true);
- } else {
- return that.create(callback, true);
- }
- };
- return performOperation();
+ if (_this.fields._id) {
+ return _this.update(callback);
+ } else {
+ return _this.create(callback);
+ }
});
};
- Model.prototype.create = function(callback, fromSave) {
- var object, that;
- if (fromSave == null) {
- fromSave = false;
- }
- object = this.fields();
+ Model.prototype.create = function(callback) {
+ var _this = this;
this.callMethod('beforeCreate');
this.callMethod('aroundCreate');
- that = this;
- return Client.collection(this.collectionName).insert(object, function(err, result) {
+ return Client.collection(this.collectionName).insert(this.fields, function(err, result) {
result._id = result._id.toString();
- that._id = result._id;
- that.callMethod('aroundCreate');
- that.callMethod('afterCreate');
+ _this.fields._id = result._id;
+ _this.callMethod('aroundCreate');
+ _this.callMethod('afterCreate');
if (callback) {
return callback(err, result);
}
});
};
- Model.prototype.update = function(callback, fromSave) {
- var object, that, _id;
- if (fromSave == null) {
- fromSave = false;
+ Model.prototype.update = function(callback) {
+ var fields, key, _id,
+ _this = this;
+ fields = {};
+ for (key in this.fields) {
+ fields[key] = this.fields[key];
}
- object = this.fields();
- _id = new mongolian.ObjectId(object._id);
- delete object._id;
+ _id = new mongolian.ObjectId(fields._id);
+ delete fields._id;
this.callMethod('beforeUpdate');
this.callMethod('aroundUpdate');
- that = this;
return Client.collection(this.collectionName).update({
_id: _id
- }, object, function(err, rowsUpdated) {
- that.callMethod('aroundUpdate');
- that.callMethod('afterUpdate');
+ }, fields, function(err, rowsUpdated) {
+ _this.callMethod('aroundUpdate');
+ _this.callMethod('afterUpdate');
if (callback) {
return callback(err, rowsUpdated);
}
});
};
Model.prototype.remove = function(callback) {
- var object, query, that, _id;
- object = this.fields();
- _id = new mongolian.ObjectId(object._id);
+ var _id,
+ _this = this;
+ _id = new mongolian.ObjectId(this.fields._id);
this.callMethod('beforeRemove');
this.callMethod('aroundRemove');
- that = this;
- query = function() {
- return Client.collection(that.collectionName).remove({
- _id: _id
- }, function(err) {
- that.callMethod('aroundRemove');
- that.callMethod('afterRemove');
- if (callback) {
- return callback(err);
- }
- });
- };
- return query();
+ return Client.collection(this.collectionName).remove({
+ _id: _id
+ }, function(err) {
+ _this.callMethod('aroundRemove');
+ _this.callMethod('afterRemove');
+ if (callback) {
+ return callback(err);
+ }
+ });
+ };
+
+ Model.remove = function(callback) {
+ return Client.collection(this.collectionName).remove({}, function(err) {
+ if (callback) {
+ return callback(err);
+ }
+ });
};
return Model;
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "mongorito",
- "version": "0.3.4",
+ "version": "0.3.5",
"description": "ODM for MongoDB.",
"keywords": ["mongo", "mongodb"],
"author": "Vadim Demedes <sbioko@gmail.com>",
View
19 test/mongorito.test.coffee
@@ -6,12 +6,14 @@ require 'should'
async = require 'async'
class Post
- keys: ['author', 'title']
+ constructor: -> super
+ keys: ['author', 'title', 'body']
scopes:
one: limit: 1
latest: title: 'Just created'
Post = Mongorito.bake Post
+PostId = 0
describe 'Mongorito', ->
describe 'creating new record', ->
@@ -20,13 +22,14 @@ describe 'Mongorito', ->
post.title = 'Very nice post!'
post.author = 'Vadim'
post.save ->
+ PostId = post._id
Post.find (err, posts) ->
posts.length.should.equal 1
do done
it 'should mass-asign post info', (done) ->
post = new Post
- post.fill title: 'Very nice post!', author: 'Vadim', admin: yes
+ post.updateAttributes title: 'Very nice post!', author: 'Vadim', admin: yes
post.title.should.equal('Very nice post!') and post.author.should.equal('Vadim') and not post.admin
do done
@@ -46,6 +49,11 @@ describe 'Mongorito', ->
do done
describe 'getting records', ->
+ it 'should fetch post by _id', (done) ->
+ Post.find _id: PostId, (err, post) ->
+ post.title.should.equal 'Edited title!'
+ do done
+
it 'should fetch just edited post', (done) ->
Post.find (err, posts) ->
posts[0].title.should.equal 'Edited title!'
@@ -100,4 +108,9 @@ describe 'Mongorito', ->
, ->
Post.find (err, posts) ->
posts.length.should.equal 0
- do done
+ do done
+
+ after (done) ->
+ Post.remove ->
+ Mongorito.disconnect()
+ do done

0 comments on commit fbf7d54

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