Permalink
Browse files

0.0.3, Not dependant on class.js anymore

  • Loading branch information...
1 parent 87c4b7c commit 47e4d8cde1fbe89151930b77598469ffdc710b5e @masylum masylum committed Nov 13, 2010
Showing with 104 additions and 115 deletions.
  1. +8 −5 example/advertiser.js
  2. +10 −12 example/user.js
  3. +59 −57 lib/model.js
  4. +4 −3 lib/validator.js
  5. +2 −2 package.json
  6. +21 −36 test/model.js
View
@@ -1,12 +1,15 @@
var User = require('./user');
-var Advertiser = User.extend({
- onCreate: function (element) {
+module.exports = function (db) {
+ var advertiser = User(db);
+
+ advertiser.onCreate = function (element) {
element.created_at = new Date();
element.is_deleted = false;
element.last_login_at = null;
element.updated_at = null;
- },
-});
+ };
+
+ return advertiser;
+};
-module.exports = Advertiser;
View
@@ -1,12 +1,10 @@
-var Model= require('./../model');
+var Model = require('./../model');
-var User = Model.extend({
- constructor: function (db) {
- Model.call(this, db, 'users');
- },
+module.exports = function (db) {
+ var user = Model(db, 'users');
- validate: function (user, data, callback) {
- var validator= $.model('validator', [user, data]);
+ user.validate = function (user, data, callback) {
+ var validator = $.model('validator', [user, data]);
validator.validateRegex({
name: [validator.regex.username, 'Incorrect name'],
@@ -15,13 +13,13 @@ var User = Model.extend({
description: [validator.regex.description, 'Incorrect description']
});
- if(validator.attrChanged('password')) {
+ if (validator.attrChanged('password')) {
validator.validateConfirmation({
'password': ['password_confirmation', 'Passwords must match']
});
}
- if(!data.tags || data.tags.length <= 0) {
+ if (!data.tags || data.tags.length <= 0) {
validator.addError('tags', 'Select at least one tag');
}
@@ -31,7 +29,7 @@ var User = Model.extend({
}, function () {
callback(null, validator);
});
- }
-});
+ };
-module.exports = User;
+ return user;
+};
View
@@ -1,60 +1,63 @@
-var Class = require('class').Class;
+module.exports = function (db, collection_name) {
-module.exports = new Class({
- constructor: function (db, collection_name) {
- this.db = db;
- this.collection_name = collection_name;
- },
+ if (!db) {
+ throw (new Error('You must specify a db'));
+ }
+
+ if (!collection_name) {
+ throw (new Error('You must specify a collection name'));
+ }
- getCollection: function (callback) {
- this.db.collection(this.collection_name, function (error, collection) {
+ var model = {};
+
+ model.getCollection = function (callback) {
+ db.collection(collection_name, function (error, collection) {
if (error) {
callback(error);
} else {
callback(null, collection);
}
});
- },
+ };
- mongoCall: function () {
- var self = this,
- args = Array.prototype.slice.call(arguments, 0),
+ model.mongoCall = function () {
+ var args = Array.prototype.slice.call(arguments, 0),
funk = args.shift(),
- callback = args.last;
+ callback = args[args.length - 1];
this.getCollection(function (error, collection) {
if (error) {
callback(error);
} else {
switch (funk) {
case 'findArray':
- args.last = function (error, cursor) {
+ args[args.length - 1] = function (error, cursor) {
cursor.toArray(callback);
};
funk = 'find';
break;
case 'insert':
if (Array.isArray(args[0])) {
args[0].forEach(function (element) {
- self.onCreate(element);
+ model.onCreate(element);
});
} else {
- self.onCreate(args[0]);
+ model.onCreate(args[0]);
}
- args.last = function (error, docs) {
+ args[args.length - 1] = function (error, docs) {
docs.forEach(function (element) {
- self.afterCreate(element);
+ model.afterCreate(element);
});
callback(error, docs);
};
break;
case 'update':
case 'findAndModify':
- self.onUpdate(args[1]);
+ model.onUpdate(args[1]);
break;
case 'mapReduceArray':
- args.last = function (error, collection) {
+ args[args.length - 1] = function (error, collection) {
collection.find(function (error, cursor) {
var results = [];
@@ -80,7 +83,7 @@ module.exports = new Class({
funk = 'mapReduce';
break;
case 'mapReduceCursor':
- args.last = function (error, collection) {
+ args[args.length - 1] = function (error, collection) {
collection.find(callback);
};
funk = 'mapReduce';
@@ -89,91 +92,88 @@ module.exports = new Class({
collection[funk].apply(collection, args);
}
});
- },
-
- createInstance: function (element, callback) {
- var self = this;
+ };
- self.validate({}, element, function (errors, validator) {
+ model.createInstance = function (element, callback) {
+ model.validate({}, element, function (error, validator) {
if (!validator.hasErrors()) {
- self.onCreateInstance(element, function (errors, element) {
- self.mongoCall('insert', element, function (errors, element) {
+ model.onCreateInstance(element, function (error, element) {
+ model.mongoCall('insert', element, function (error, element) {
validator.updated_model = element[0];
- callback(null, validator);
+ callback(error, validator);
});
});
} else {
- callback(null, validator);
+ callback(error, validator);
}
});
- },
+ };
- onCreate: function (element) {
+ model.onCreate = function (element) {
if (!element.created_at) {
element.created_at = new Date();
}
- },
+ };
- afterCreate: function (element) {
+ model.afterCreate = function (element) {
// to implement on your models
- },
+ };
- onCreateInstance: function (element, callback) {
+ model.onCreateInstance = function (element, callback) {
// to implement on your models
callback(null, element);
- },
+ };
- updateInstance: function (model, data, options, callback) {
- var self = this,
- update = {};
+ model.updateInstance = function (model, data, options, callback) {
+ var update = {};
if (typeof options === "function") {
callback = options;
options = {};
}
- self.validate(model, data, function (errors, validator) {
+ model.validate(model, data, function (errors, validator) {
if (!validator.hasErrors()) {
- self.onUpdateInstance(model, data, function (errors, new_data) {
+ model.onUpdateInstance(model, data, function (errors, new_data) {
update[options.verb || '$set'] = new_data;
- self.mongoCall('update', {'_id': model._id}, update, { upsert: true, multi: false}, function (error, element) {
+ model.mongoCall('update', {'_id': model._id}, update, { upsert: true, multi: false}, function (error, element) {
callback(null, validator);
});
});
} else {
callback(null, validator);
}
});
- },
+ };
- onUpdate: function (update) {
+ model.onUpdate = function (update) {
if (!update.$set) {
update.$set = {};
}
update.$set.updated_at = new Date();
- },
+ };
- onUpdateInstance: function (model, update, callback) {
+ model.onUpdateInstance = function (model, update, callback) {
// to implement
callback(null, update);
- },
+ };
- setEmbedObject: function (name, object) {
+ model.setEmbedObject = function (name, object) {
var result = {};
this.skeletons[name].forEach(function (attr) {
result[attr] = object[attr];
});
return result;
- },
+ };
- updateEmbedObject: function (model, data, embed, options, callback) {
+ model.updateEmbedObject = function (model, data, embed, options, callback) {
var new_data = {},
- i = null,
+ i,
query = {};
query[embed + '._id'] = model._id;
@@ -183,11 +183,11 @@ module.exports = new Class({
}
this.mongoCall('update', query, {'$set': new_data}, options || {upsert: true, multi: true}, callback);
- },
+ };
- pushEmbedObject: function (model, data, embed, options, callback) {
+ model.pushEmbedObject = function (model, data, embed, options, callback) {
var new_data = {},
- i = null,
+ i,
query = {};
query[embed + '._id'] = model._id;
@@ -197,5 +197,7 @@ module.exports = new Class({
}
this.mongoCall('update', query, {'$push': new_data}, options || {upsert: true, multi: true}, callback);
- }
-});
+ };
+
+ return model;
+};
View
@@ -18,7 +18,8 @@ module.exports = function (model, data) {
title: /^[A-Za-z0-9].{3,50}/,
description: /.{10,300}/,
email: /^\S+@\S+\.\S+$/,
- password: /.{6,20}/
+ password: /.{6,20}/,
+ url: /((http|https|ftp):\/\/(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)/i
};
validator.isUpdating = function () {
@@ -99,7 +100,7 @@ module.exports = function (model, data) {
getKeys(validations).forEach(function (key, i) {
validations[key][0].mongoCall('findOne', validations[key][1], funk.add(function (errors, doc) {
if ((validations[key][2] === true && !doc || validations[key][2] === false && doc)) {
- this.addError(key, validations[key][3]);
+ validator.addError(key, validations[key][3]);
}
}));
});
@@ -113,4 +114,4 @@ module.exports = function (model, data) {
};
return validator;
-}
+};
View
@@ -1,11 +1,11 @@
{
"name": "mongolia",
"description": "Mongodb driver wrapper. Acts as model.",
- "version": "0.0.2",
+ "version": "0.0.3",
"author": "Pau Ramon <masylum@gmail.com>",
"keywords": ["mongo", "mongodb", "orm", "database", "db"],
"main": "./lib/model",
- "dependencies": { "mongodb": ">=0.7.9", "funk": "0.0.1", "class": "0.3.0"},
+ "dependencies": { "mongodb": ">=0.7.9", "funk": ">=0.0.1"},
"repository" : {"type": "git" , "url": "http://github.com/masylum/mongolia.git" },
"engines": { "node": ">= 0.2.0" }
}
Oops, something went wrong.

0 comments on commit 47e4d8c

Please sign in to comment.