Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

0.0.3, Not dependant on class.js anymore

  • Loading branch information...
commit 47e4d8cde1fbe89151930b77598469ffdc710b5e 1 parent 87c4b7c
@masylum masylum authored
View
13 example/advertiser.js
@@ -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
22 example/user.js
@@ -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
116 lib/model.js
@@ -1,26 +1,29 @@
-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) {
@@ -28,7 +31,7 @@ module.exports = new Class({
} else {
switch (funk) {
case 'findArray':
- args.last = function (error, cursor) {
+ args[args.length - 1] = function (error, cursor) {
cursor.toArray(callback);
};
funk = 'find';
@@ -36,25 +39,25 @@ module.exports = new Class({
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,43 +92,40 @@ 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;
@@ -133,13 +133,13 @@ module.exports = new Class({
}
- 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);
});
});
@@ -147,21 +147,21 @@ module.exports = new Class({
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) {
@@ -169,11 +169,11 @@ module.exports = new Class({
});
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
7 lib/validator.js
@@ -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
4 package.json
@@ -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" }
}
View
57 test/model.js
@@ -7,43 +7,28 @@ var Model = require('./../lib/model'),
Db = require('mongodb/db').Db,
db = new Db('mongolia_test', new Server('localhost', 27017, {auto_reconnect: true, native_parser: true}), {});
-var UserClass = Model.extend({
- constructor: function (db) {
- Model.call(this, db, 'users');
- }
-});
-var User = new UserClass(db);
-
-var inspect = require('eyes').inspector({
- styles: {
- all: 'yellow',
- label: 'underline',
- other: 'inverted',
- key: 'bold',
-
- special: 'grey',
- string: 'green',
- number: 'red',
- bool: 'blue',
- regexp: 'green'
- },
- maxLength: 9999999999
-});
+var UserClass = function (db) {
+ var user = Model(db, 'users');
+ return user;
+ },
+
+ User = UserClass(db),
+
+ load_user = function (callback) {
+ db.collection('users', function (error, collection) {
+ collection.insert({name: 'Pau', email: 'pau@mongolia.com', password: 'pau123'}, function (error, doc) {
+ callback(doc);
+ collection.remove({}, function (error, bla) {});
+ });
+ });
+ },
+
+ remove_users = function () {
+ db.collection('users', function (error, collection) {
+ collection.remove({}, function (error, bla) {});
+ });
+ };
-var load_user = function (callback) {
- db.collection('users', function (error, collection) {
- collection.insert({name: 'Pau', email: 'pau@mongolia.com', password: 'pau123'}, function (error, doc) {
- callback(doc);
- collection.remove({}, function (error, bla) {});
- });
- });
-}
-
-var remove_users = function () {
- db.collection('users', function (error, collection) {
- collection.remove({}, function (error, bla) {});
- });
-}
db.open(function () {
module.exports['test get collection'] = function (assert) {
Please sign in to comment.
Something went wrong with that request. Please try again.