From ed8ff0ca360d1c473c12b28dabedcefda652dc77 Mon Sep 17 00:00:00 2001 From: Sascha Gehlich Date: Sun, 16 Dec 2012 14:08:51 +0100 Subject: [PATCH] let the adapter decide when to load the schema --- lib/adapters/cradle.js | 2 ++ lib/railway.js | 64 ++++++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/lib/adapters/cradle.js b/lib/adapters/cradle.js index cd1b74a7..9bfcccb5 100644 --- a/lib/adapters/cradle.js +++ b/lib/adapters/cradle.js @@ -145,6 +145,8 @@ function filtering(res, model, filter, instance) { exports.initialize = function(schema, callback) { if (!cradle) return; + // when using cradle if we dont wait for the schema to be connected, the models fails to load correctly. + schema.waitForConnect = true; if (!schema.settings.url) { var host = schema.settings.host || 'localhost'; var port = schema.settings.port || '5984'; diff --git a/lib/railway.js b/lib/railway.js index bc54a920..01a5abcd 100644 --- a/lib/railway.js +++ b/lib/railway.js @@ -49,36 +49,14 @@ module.exports = function init(root) { var schema = new Schema(config && config.driver || 'memory', config); schema.log = log; - // when using cradle if we dont wait for the schema to be connected, the models fails to load correctly. - schema.on('connected', function() { - railway.orm._schemas.push(schema); - var context = prepareContext(models, railway, app, schema); - - // run schema first - var schemaFile = (root || app.root) + '/db/schema.'; - if (existsSync(schemaFile + 'js')) { - schemaFile += 'js'; - } else if (existsSync(schemaFile + 'coffee')) { - schemaFile += 'coffee'; - } else { - schemaFile = false; - } - - if (schemaFile) { - var code = fs.readFileSync(schemaFile).toString(); - if (schemaFile.match(/\.coffee$/)) { - code = require('coffee-script').compile(code); - } - var fn = new Function('context', 'require', 'with(context){(function(){' + code + '})()}'); - fn(context, require); - } - - // and freeze schemas - railway.orm._schemas.forEach(function (schema) { - schema.freeze(); + if (schema.waitForConnect) { + schema.on('connected', function() { + loadSchema(schema, railway, app, models); }); - }); + } else { + loadSchema(schema, railway, app, models); + } // check validations and display warning @@ -95,6 +73,36 @@ if (displayWarning) { // require('util').puts($('WARNING:').bold.red + ' ' + $('I can see that you\'ve added validation to db/schema.js. However schema.js file is only used to describe database schema. Therefore validations configured in db/schema.js will be ignored.\nFor business logic (incl. validations) please create models as separate .js files here: app/models/*.js').yellow); } +function loadSchema(schema, railway, app, models) { + railway.orm._schemas.push(schema); + + var context = prepareContext(models, railway, app, schema); + + // run schema first + var schemaFile = (root || app.root) + '/db/schema.'; + if (existsSync(schemaFile + 'js')) { + schemaFile += 'js'; + } else if (existsSync(schemaFile + 'coffee')) { + schemaFile += 'coffee'; + } else { + schemaFile = false; + } + + if (schemaFile) { + var code = fs.readFileSync(schemaFile).toString(); + if (schemaFile.match(/\.coffee$/)) { + code = require('coffee-script').compile(code); + } + var fn = new Function('context', 'require', 'with(context){(function(){' + code + '})()}'); + fn(context, require); + } + + // and freeze schemas + railway.orm._schemas.forEach(function (schema) { + schema.freeze(); + }); +} + function log(str, startTime) { var $ = railway.utils.stylize.$; var m = Date.now() - startTime;