Permalink
Browse files

Protos 0.0.9

Refactoring of protos internals. Closes #127, #128

* Changes:
- app/config/databases.js is now app/config/drivers.js
- app/config/storage.js is now app/config/storages.js
- boot.js stayUp variable defaults to 'production' instead of false
- Added Protos::production
- Removed Protos::async, Protos::done
  • Loading branch information...
1 parent 15ad030 commit b6a78a82ad574a7e60ceef7e73f7f9c456ae73c7 @mendezcode mendezcode committed Jun 26, 2012
View
@@ -11,7 +11,8 @@ var _ = require('underscore'),
Db = mongodb.Db,
Server = mongodb.Server,
ObjectID = mongodb.ObjectID,
- Collection = mongodb.Collection;
+ Collection = mongodb.Collection,
+ EventEmitter = require('events').EventEmitter;
/**
MongoDB Driver class
@@ -26,19 +27,25 @@ var _ = require('underscore'),
function MongoDB(app, config) {
var self = this;
+
+ this.events = new EventEmitter();
config = protos.extend({
host: 'localhost',
port: 27017,
database: 'default',
- storage: null
+ storage: null,
+ username: '',
+ password: ''
}, config || {});
if (typeof config.port != 'number') config.port = parseInt(config.port, 10);
this.className = this.constructor.name;
this.app = app;
+ app.debug(util.format('Initializing MongoDB for %s:%s', config.host, config.port));
+
/**
Driver configuration
@@ -54,12 +61,9 @@ function MongoDB(app, config) {
*/
this.config = config;
- protos.async(app); // Register async queue
-
var reportError = function(err) {
- app.log(util.format("MongoDB [%s:%s] %s", config.host, config.port, err.code));
- self.client = err;
- protos.done(app); // Flush async queue
+ app.log(util.format("MongoDB [%s:%s] %s", config.host, config.port, err));
+ self.client = null;
}
// Set db
@@ -86,14 +90,27 @@ function MongoDB(app, config) {
if (config.username && config.password) {
self.db.authenticate(config.username, config.password, function(err, success) {
- if (err) app.log('MongoDB: ' + err.toSring());
- else if (!success) throw new Error(util.format('MongoDB: Unable to authenticate to %s:%s', config.host, config.port));
+
+ if (err) {
+
+ var msg = util.format('MongoDB: unable to authenticate %s@%s', config.username, config.host);
+ app.log(new Error(msg));
+ throw err;
+
+ } else {
+
+ // Emit initialization event
+ self.events.emit('init', self.db, self.client);
+
+ }
+
});
+ } else {
+ // Emit initialization event
+ self.events.emit('init', self.db, self.client);
}
- protos.done(app); // Flush async queue
-
}
});
View
@@ -66,14 +66,6 @@ function Application(domain, path) {
*/
this.className = this.constructor.name;
-/**
- Asynchronous callback queue
-
- @private
- @property asyncQueue
- */
- this.asyncQueue = [];
-
/**
Boolean value indicating whether or not the application has initialized
@@ -395,20 +387,16 @@ function Application(domain, path) {
this.emit('helpers_init');
- // Create storages
- createStorages.call(this);
-
- this.emit('storages_init', this.storages);
-
// Create drivers
- createDrivers.call(this);
-
- this.emit('drivers_init', this.drivers);
+ parseDriverConfig.call(this);
+ // Create storages
+ parseStorageConfig.call(this);
+
// Get models/
var model, name,
modelCtor = protos.lib.model,
- dbConfigAvailable = Object.keys(this.config.database).length > 0;
+ dbConfigAvailable = Object.keys(this.config.drivers).length > 0;
protos.util.requireAllTo(this.mvcpath + "models", this.models, function(Ctor, file) {
if (Ctor instanceof Function) {
@@ -570,12 +558,8 @@ function Application(domain, path) {
this.emit('partials_init', this.views.partials, partialPaths);
- // console.exit(this.views.partials);
+ this.initialize();
- // Initialize if no drivers or storages are present (no config set)
- if (isEmpty(this.config.database) && isEmpty(this.config.storage)) this.initialize();
-
- // console.exit(this);
}
util.inherits(Application, EventEmitter);
@@ -587,8 +571,6 @@ util.inherits(Application, EventEmitter);
*/
Application.prototype.initialize = function() {
- delete this.asyncQueue;
-
// Set initialized state
this.initialized = true;
@@ -597,7 +579,6 @@ Application.prototype.initialize = function() {
// Run initialization code from init.js
this.require('init').call(null, this);
-
}
/**
@@ -779,6 +760,16 @@ Application.prototype.use = function(middleware, options) {
}
}
+/**
+ Alias of Protos::production
+
+ @method production
+ @param {mixed} arg Argument to return
+ @returns {mixed}
+ */
+
+Application.prototype.production = protos.production;
+
/**
Validates request data (both for GET & POST), against the validation rules
provided when defining the route.
@@ -1410,7 +1401,8 @@ Application.prototype._storage = function(storage, config) {
*/
Application.prototype._getResource = function(schemeStr, callback) {
- var db, section, source, out,
+
+ var db, section, source, generator, out,
self = this,
scheme = schemeStr.split('/');
@@ -1425,27 +1417,53 @@ Application.prototype._getResource = function(schemeStr, callback) {
source = scheme[0];
scheme = scheme[1];
-
+
+ // Define resource generator
+ switch (source) {
+ case 'drivers':
+ generator = this._driver;
+ break;
+ case 'storages':
+ generator = this._storage;
+ break;
+ }
+
if (scheme.indexOf(':') > 0) {
scheme = scheme.split(':');
db = scheme[0].trim();
section = scheme[1].trim();
out = this[source][db][section];
+ if (out instanceof Array) {
+ out = generator.apply(this, out);
+ this[source][db][section] = out;
+ this.debug("Loading resource: " + schemeStr);
+ }
} else {
out = this[source][scheme];
+ if (out instanceof Array) {
+ out = generator.apply(this, out);
+ this[source][scheme] = out;
+ this.debug("Loading resource: " + schemeStr);
+ }
}
-
+
if (out == null) {
throw new Error(util.format("Unable to find resource: '%s'", schemeStr));
} else if (callback) {
if (out.client == null) {
- // Wait until client is initialized
- this.on('init', function() {
+
+ // If the client is attached asynchronously, the driver must implement
+ // an events interface, that emits the 'init' event when the client is ready
+
+ out.events.once('init', function() {
callback.call(self, out);
- });
+ })
+
} else {
- // Client already initialized
+
+ // Client is already available
callback.call(self, out);
+
}
} else {
return out;
@@ -1725,11 +1743,11 @@ function parseConfig() {
return config;
}
-// Creates database drivers from config
+// Parses database drivers from config
-function createDrivers() {
+function parseDriverConfig() {
var cfg, def, x, y, z,
- config = this.config.database,
+ config = this.config.drivers,
drivers = this.drivers;
if (Object.keys(config).length === 0) return;
@@ -1740,27 +1758,21 @@ function createDrivers() {
for (y in cfg) {
if (typeof cfg[y] == 'object') {
if (typeof drivers[x] == 'undefined') drivers[x] = {};
- drivers[x][y] = this._driver(x, cfg[y]);
+ drivers[x][y] = [x, cfg[y]];
} else {
- drivers[x] = this._driver(x, cfg);
+ drivers[x] = [x, cfg];
break;
}
}
}
- if (def) drivers.default = this._getResource('drivers/' + def);
- else throw new Error('No default database set. Please check your config/database.');
-
- if (typeof drivers.default == 'undefined') {
- throw new Error(util.format("No driver available for '%s'.", def));
- }
}
-// Creates storages from config
+// Parses storages from config
-function createStorages() {
+function parseStorageConfig() {
var cfg, x, y, z,
- config = this.config.storage,
+ config = this.config.storages,
storages = this.storages;
if (Object.keys(config).length === 0) return;
@@ -1770,9 +1782,9 @@ function createStorages() {
for (y in cfg) {
if (typeof cfg[y] == 'object') {
if (typeof storages[x] == 'undefined') storages[x] = {};
- storages[x][y] = this._storage(x, cfg[y]);
+ storages[x][y] = [x, cfg[y]];
} else {
- storages[x] = this._storage(x, cfg);
+ storages[x] = [x, cfg];
break;
}
}
View
@@ -60,9 +60,6 @@ Model.prototype.validation = {
Model.prototype.prepare = function(app) {
- // Exit if drivers are not ready yet
- if (typeof app == 'undefined' || typeof app.drivers.default == 'undefined') return;
-
var name, validation, self = this;
Object.defineProperty(this, 'app', {
@@ -72,8 +69,8 @@ Model.prototype.prepare = function(app) {
configurable: false
});
- name = this.driver = (this.driver || app.config.database.default);
-
+ name = this.driver = (this.driver || app.config.drivers.default);
+
// Get driver
if (typeof this.driver == 'string') {
this.driver = app._getResource('drivers/' + this.driver);
@@ -119,8 +116,9 @@ Model.prototype.prepare = function(app) {
app.once('models_init', function(models) {
setRelationships.call(self);
});
-
+
protos.util.onlySetEnumerable(this, ['className']);
+
}
/**
Oops, something went wrong.

0 comments on commit b6a78a8

Please sign in to comment.