Skip to content
Browse files

Get example app working with external model lib

  • Loading branch information...
1 parent 0815536 commit d278a7e8f74e7d1596dd6cf72112e0e61eebdce2 mde committed
Showing with 140 additions and 58 deletions.
  1. +5 −1 bin/cli.js
  2. +8 −8 lib/app.js
  3. +2 −45 lib/cluster/master.js
  4. +65 −0 lib/config.js
  5. +13 −3 lib/init/model.js
  6. +47 −1 templates/Jakefile
View
6 bin/cli.js
@@ -123,7 +123,7 @@ if(cmds.length) {
cmd = 'jake -t -f ' + filepath + ' ';
// If command isn't secret and has no other argument
- if (cmds[0] !== 'secret' && !cmds[1]) {
+ if ((cmds[0] != 'secret' && cmds[0] != 'db:init') && !cmds[1]) {
throw new Error(cmds[0] + ' command requires another argument.');
}
@@ -143,6 +143,10 @@ if(cmds.length) {
// Add Jake argument based on commands
switch(cmds[0]) {
+ case 'db:init':
+ // Generating application
+ cmd += '"db:init"';
+ break;
case 'app':
// Generating application
cmd += '"gen:app[' + cmds[1] + engineCmd + ']"';
View
16 lib/app.js
@@ -82,6 +82,12 @@ var App = function () {
return ret;
}
+ // Locales and models
+ // ==================
+ , _baseInit = function (next) {
+ init.init(next);
+ }
+
// Load controller ctors
// ==================
, _registerControllers = function (next) {
@@ -136,12 +142,6 @@ var App = function () {
}
}
- // Load any locales, set the default
- // ==================
- , _loadLocales = function (next) {
- init.init(next);
- }
-
// Load metrics if they're turned on
// ==================
, _loadMetrics = function (next) {
@@ -294,10 +294,10 @@ var App = function () {
, chain;
items = [
- _registerControllers
+ _baseInit
+ , _registerControllers
, _loadRouter
, _loadSessionStore
- , _loadLocales
, _loadMetrics
, _registerTemplatePaths
, _loadHelpers
View
47 lib/cluster/master.js
@@ -4,6 +4,7 @@ var Master
, path = require('path')
, Log = require('../../deps/log')
, dispatch = require('./master_dispatch')
+ , config = require('../config')
, utils = require('utilities')
, file = utils.file
, date = utils.date;
@@ -32,54 +33,10 @@ Master = function () {
Master.prototype = new (function () {
var _readConfig = function (next) {
- var opts = this.opts
- , dir = process.cwd()
- , dirList = fs.readdirSync(dir + '/config')
- , fileName
- , fileExt
- , fileBaseName
- , usingCoffee
- , appBaseConfig
- , appEnvConfig;
-
- this.config = require('../base_config');
- this.config.environment = opts.environment || this.config.environment;
-
- // Base config for workers-count should be 1 in dev-mode
- // Cycle based on filesystem changes, not keep-alive
- // Process-rotation not possible in this mode
+ this.config = config.readConfig(this.ops);
if (this.config.environment == 'development') {
- this.config.workers = 1;
this.processMode = processModes.WATCH_FILES;
- this.config.rotateWorkers = false;
- }
-
- // App configs
- for (var i = 0; i < dirList.length; i++) {
- fileName = dirList[i];
- fileExt = path.extname(fileName);
- fileBaseName = path.basename(fileName, fileExt);
- // Require the environment configuration and the base configuration file
- if(fileBaseName === this.config.environment || fileBaseName === 'environment') {
- if(fileExt === '.coffee') {
- // fileName is a CoffeeScript file so try to require it
- usingCoffee = usingCoffee || file.requireLocal('coffee-script');
- }
- appBaseConfig = require(dir + '/config/environment');
- appEnvConfig = require(dir + '/config/' + this.config.environment);
- }
}
-
- // Start with a blank slate, mix everything in
- geddy.mixin(this.config, appBaseConfig);
- geddy.mixin(this.config, appEnvConfig);
- geddy.mixin(this.config, opts);
-
- // Obvious, don't rotate with only one worker
- if (this.config.workers < 2) {
- this.config.rotateWorkers = false;
- }
-
next();
}
View
65 lib/config.js
@@ -0,0 +1,65 @@
+var config
+ , path = require('path')
+ , fs = require('fs')
+ , utils = require('utilities');
+
+config = new (function () {
+
+ this.readConfig = function (options) {
+ var opts = options || {}
+ , ret = {}
+ , baseConfig
+ , env
+ , dir = process.cwd()
+ , dirList = fs.readdirSync(path.join(dir, 'config'))
+ , fileName
+ , fileExt
+ , fileBaseName
+ , useCoffee
+ , appBaseConfig
+ , appEnvConfig;
+
+ baseConfig = utils.mixin({}, require('./base_config'), true);
+ env = opts.environment || baseConfig.environment;
+
+ // Base config for workers-count should be 1 in dev-mode
+ // Cycle based on filesystem changes, not keep-alive
+ // Process-rotation not possible in this mode
+ if (env == 'development') {
+ baseConfig.workers = 1;
+ baseConfig.rotateWorkers = false;
+ }
+
+ // App configs
+ for (var i = 0; i < dirList.length; i++) {
+ fileName = dirList[i];
+ fileExt = path.extname(fileName);
+ fileBaseName = path.basename(fileName, fileExt);
+ // Require the environment configuration and the base configuration file
+ if (fileBaseName === env || fileBaseName === 'environment') {
+ if (fileExt === '.coffee') {
+ // fileName is a CoffeeScript file so try to require it
+ useCoffee = useCoffee || file.requireLocal('coffee-script');
+ }
+ appBaseConfig = require(dir + '/config/environment');
+ appEnvConfig = require(dir + '/config/' + baseConfig.environment);
+ }
+ }
+
+ // Start with a blank slate, mix everything in
+ utils.mixin(ret, baseConfig, true);
+ utils.mixin(ret, appBaseConfig, true);
+ utils.mixin(ret, appEnvConfig, true);
+ utils.mixin(ret, opts, true);
+
+ // Obvious, don't rotate with only one worker
+ if (ret.workers < 2) {
+ ret.rotateWorkers = false;
+ }
+
+ return ret;
+ };
+
+})();
+
+module.exports = config;
View
16 lib/init/model.js
@@ -32,6 +32,9 @@ module.exports = new (function () {
, appAdaptersPath = path.join(cwd, modelDir, 'adapters')
, ctors;
+ // FIXME: Do this in app.js
+ utils.log.registerLogger(geddy.log);
+
// May be running totally model-less
if (!utils.file.existsSync(path.join(cwd, modelDir))) {
return callback();
@@ -47,7 +50,8 @@ module.exports = new (function () {
, adapterInfo
, adapterPath
, adapterName
- , config;
+ , adapterCtor
+ , adapter;
require(filePath);
@@ -65,8 +69,14 @@ module.exports = new (function () {
}
// Try again, see if there's a built-in adapter
else if ((adapterInfo = model.getAdapterInfo(adapterName))) {
- adapterPath = _getAdapterPath(builtinAdaptersPath, adapterInfo.filePath);
- model.adapters[name] = require(adapterPath)[adapterInfo.name];
+ adapter = model.adapters[name];
+ if (!adapter) {
+ adapterPath = _getAdapterPath(builtinAdaptersPath, adapterInfo.filePath);
+ adapterCtor = require(adapterPath).Adapter;
+ adapter = new adapterCtor(geddy.config.db);
+ model.loadedAdapters[adapterInfo.name] = adapter;
+ }
+ model.adapters[name] = adapter;
}
else {
// Maybe it's a default/builtin
View
48 templates/Jakefile
@@ -4,10 +4,56 @@ require('../lib/geddy')
// Dependencies
var fs = require('fs')
, path = require('path')
- , utils = require('../node_modules/utilities')
+ , utils = require('../lib/utils')
, adapter = require('../lib/template/adapters').Adapter
, templato = new (require('../deps/templato'));
+namespace('env', function () {
+ task('init', function () {
+ geddy.model = require('model');
+ geddy.config = require('../lib/config').readConfig();
+
+ require('../lib/init').init(complete);
+
+ jake.addListener('complete', function (e) {
+ jake.Task['env:cleanup'].invoke();
+ });
+ });
+
+ task('cleanup', function () {
+ // Disconnect all the adapters
+ adapters = geddy.model.loadedAdapters;
+ for (var p in adapters) {
+ adapters[p].disconnect();
+ }
+ });
+
+}, {async: true});
+
+namespace('db', function () {
+ task('retrofit', ['env:init'], function () {
+ var createTable
+ , adapters
+ , adapter
+ , models = Object.keys(geddy.model.descriptionRegistry);
+ createTable = function () {
+ if ((m = models.shift())) {
+ console.log('Creating table for ' + m);
+ adapter = geddy.model.adapters[m];
+ adapter.createTable(m, function (err, data) {
+ if (err) { throw err }
+ createTable();
+ });
+ }
+ else {
+ complete();
+ }
+ };
+ createTable();
+ }, {async: true});
+
+});
+
namespace('gen', function () {
var _writeTemplate = function (name, filename, dirname, options) {

0 comments on commit d278a7e

Please sign in to comment.
Something went wrong with that request. Please try again.