Permalink
Browse files

moved database inside server to simplify event model

  • Loading branch information...
rayui committed Feb 10, 2012
1 parent f7cefd4 commit 855726c6b78c7683fe0700b673b533e4b8bd7322
Showing with 57 additions and 41 deletions.
  1. +16 −4 README.md
  2. +6 −6 config/settings.js
  3. +11 −1 js/modules/db.js
  4. +11 −8 js/modules/model.js
  5. +2 −4 js/modules/models.js
  6. +11 −10 js/modules/server.js
  7. +0 −8 js/simple-cms.js
View
@@ -1,3 +1,7 @@
+# DISCLAIMER
+
+## This software is experimental and under development. In no way can it be considered a finished product.
+
# A simple CMS written in Node JS
## To install all the requirements:
@@ -11,9 +15,13 @@
* npm install optimist
* npm install docco
-Docco requires Pygments. On Debian based systems you can install this with the following command:
+Docco requires Pygments. On Debian based systems you can install these with the following command:
+
+* sudo aptitude install python-pygments
-sudo aptitude install python-pygments
+You will also require Foreman if you wish to start the process using the Procfile. Foreman comes as part of the Heroku toolbelt. Please see here for further information:
+
+* http://devcenter.heroku.com/articles/procfile
## To generate the documentation do this:
@@ -22,7 +30,11 @@ sudo aptitude install python-pygments
## To start the server:
-* node js/app.js
+* node js/simple-cms.js --env=dev --ac_config=config/settings.js
+
+### or with Foreman:
+
+* foreman start
## To access the app:
@@ -39,4 +51,4 @@ sudo aptitude install python-pygments
## Heroku ready!
* Set up a new Heroku app
-* git push heroku master
+* git push heroku master
View
@@ -105,12 +105,12 @@ var dev = {
}
}
],
- sessions:{}
- },
- database:{
- options:{
- host:'localhost',
- database:'test'
+ sessions:{},
+ database:{
+ options:{
+ host:'localhost',
+ database:'test'
+ }
}
}
};
View
@@ -28,6 +28,9 @@ var Database = function(_settings) {
this.db.connection.on('open', onOpen);
this.db.connection.on('error', onError);
+ _.bind(this.fetch, this);
+ _.bind(this.update, this);
+
events.EventEmitter.call(this);
return this;
@@ -41,10 +44,17 @@ Database.prototype = Object.create(events.EventEmitter.prototype, {
}
});
-Database.prototype.query = function(schemaName, query, fields, callback) {
+Database.prototype.fetch = function(schemaName, query, fields, callback) {
var schema = new mongoose.Schema(schemas[schemaName].definition);
var model = this.db.model(schemaName, schema);
var results = model.find(query, fields, callback);
};
+Database.prototype.update = function(schemaName, query, fields, callback) {
+ /*var schema = new mongoose.Schema(schemas[schemaName].definition);
+ var model = this.db.model(schemaName, schema);
+ var results = model.update(query, fields, callback);*/
+};
+
+
exports.Database = Database;
View
@@ -1,7 +1,8 @@
//page model
//- data model functionality
//- emits events
-//-- 'model:query' - needs data
+//-- 'db:fetch' - needs data
+//-- 'db:update' - sends data
//-- 'mode:ready' - ready to render
//processes requests from server
@@ -18,8 +19,6 @@ var Model = function(name) {
return this;
};
-var onReady =
-
Model.super = events.EventEmitter;
Model.prototype = Object.create(events.EventEmitter.prototype, {
constructor: {
@@ -29,20 +28,24 @@ Model.prototype = Object.create(events.EventEmitter.prototype, {
});
Model.prototype.onReady = function(callback) {
- this.removeAllListeners('model:ready');
- this.on('model:ready', function(data) {
+ this.once('model:ready', function(data) {
//send back an empty model if null result
callback.call(callback, data);
});
};
-Model.prototype.query = function(query, fields, callback) {
- this.emit('db:query', this.schema.name, query, fields, function(data) {
+Model.prototype.fetch = function(query, fields, callback) {
+ this.emit('db:fetch', this.schema.name, query, fields, function(data) {
data = data || {};
- callback.call(this, data);
+ //clone it! do not take a reference or all the sessions will reference the same object
+ callback.call(this, _.extend({},data));
});
};
+Model.prototype.update = function(conditions, update, options, callback) {
+
+};
+
Model.prototype.end = function(data) {
this.emit('model:ready', data);
};
View
@@ -28,10 +28,8 @@ models.User = {
get: function(body) {
var that = this;
- this.query({}, [], function(_data) {
- //clone it! do not take a reference
- that.data = _.extend({},_data);
- that.end(_data);
+ this.fetch({}, [], function(data) {
+ that.end(data);
});
},
View
@@ -5,8 +5,9 @@ var _ = require('underscore')._,
express = require('express'),
jade = require('jade'),
utilities = require('./shared/utilities'),
+ database = require('./db'),
sessions = require('./sessions');
-
+
//set up server model
var Server = function(_settings){
//default settings
@@ -87,10 +88,6 @@ var Server = function(_settings){
serveHTML(data, template, utilities.callback(sendResponse, {args:[headers,res]}));
};
- function queryDB(schemaName, query, fields, callback) {
- that.emit('db:query', schemaName, query, fields, callback);
- };
-
switch (route.type) {
case '302':
res.redirect(headers['Location']);
@@ -118,7 +115,8 @@ var Server = function(_settings){
if (!m) {
//get or create the model for this route
m = sessionHandler.createModel(sessionId, route.model);
- m.on('db:query', queryDB);
+ m.on('db:fetch', utilities.callback(db.fetch, {scope:db}));
+ m.on('db:update', utilities.callback(db.update, {scope:db}));
}
//bind response callbacks based on request type
@@ -135,8 +133,11 @@ var Server = function(_settings){
_.extend(settings, _settings);
//create express server and configure
- var app = express.createServer();
- var sessionHandler = new sessions.Handler(settings.sessions);
+ var app = express.createServer(),
+ db = new database.Database(settings.database),
+ sessionHandler = new sessions.Handler(settings.sessions),
+ appPort = process.env.PORT || parseInt(settings.options['port'], 10)
+
app.configure(function(){
app.use(express.bodyParser());
@@ -155,12 +156,12 @@ var Server = function(_settings){
}
//get app to listen to requests
- app.listen(process.env.PORT || parseInt(settings.options['port'], 10));
+ app.listen(appPort);
events.EventEmitter.call(this);
//confirm app is running
- console.log("Web server started at " + settings.options['port']);
+ console.log("Web server started at " + appPort);
return this;
View
@@ -1,13 +1,5 @@
//require web server module
var webserver = require('./modules/server');
-var database = require('./modules/db');
var appconfig = require('./modules/appconfig');
var ws = new webserver.Server(appconfig.get('webserver'));
-var db = new database.Database(appconfig.get('database'));
-
-//we only do this once because a response is an http one-time thing
-ws.on('db:query', function() {
- db.query.apply(db, arguments);
-});
-

0 comments on commit 855726c

Please sign in to comment.