Skip to content

Commit

Permalink
0.0.3, Not dependant on class.js anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Nov 13, 2010
1 parent 87c4b7c commit 47e4d8c
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 115 deletions.
13 changes: 8 additions & 5 deletions 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;
22 changes: 10 additions & 12 deletions 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'],
Expand All @@ -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');
}

Expand All @@ -31,7 +29,7 @@ var User = Model.extend({
}, function () {
callback(null, validator);
});
}
});
};

module.exports = User;
return user;
};
116 changes: 59 additions & 57 deletions lib/model.js
@@ -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 = [];
Expand All @@ -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';
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -197,5 +197,7 @@ module.exports = new Class({
}

this.mongoCall('update', query, {'$push': new_data}, options || {upsert: true, multi: true}, callback);
}
});
};

return model;
};
7 changes: 4 additions & 3 deletions lib/validator.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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]);
}
}));
});
Expand All @@ -113,4 +114,4 @@ module.exports = function (model, data) {
};

return validator;
}
};
4 changes: 2 additions & 2 deletions 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" }
}

0 comments on commit 47e4d8c

Please sign in to comment.