Merge pull request #169 from saschagehlich/schema_wait_connect

let the adapter decide when to load the schema
This commit is contained in:
Anatoliy Chakkaev 2012-12-16 10:10:04 -08:00
commit bc250b1bb2
2 changed files with 38 additions and 28 deletions

View File

@ -145,6 +145,8 @@ function filtering(res, model, filter, instance) {
exports.initialize = function(schema, callback) { exports.initialize = function(schema, callback) {
if (!cradle) return; 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) { if (!schema.settings.url) {
var host = schema.settings.host || 'localhost'; var host = schema.settings.host || 'localhost';
var port = schema.settings.port || '5984'; var port = schema.settings.port || '5984';

View File

@ -49,8 +49,31 @@ module.exports = function init(root) {
var schema = new Schema(config && config.driver || 'memory', config); var schema = new Schema(config && config.driver || 'memory', config);
schema.log = log; schema.log = log;
// when using cradle if we dont wait for the schema to be connected, the models fails to load correctly.
if (schema.waitForConnect) {
schema.on('connected', function() { schema.on('connected', function() {
loadSchema(schema, railway, app, models);
});
} else {
loadSchema(schema, railway, app, models);
}
// check validations and display warning
var displayWarning = false;
Object.keys(models).forEach(function (model) {
var Model = models[model];
if (Model._validations) {
displayWarning = true;
}
});
if (displayWarning) {
var $ = railway.utils.stylize.$;
// 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); railway.orm._schemas.push(schema);
var context = prepareContext(models, railway, app, schema); var context = prepareContext(models, railway, app, schema);
@ -78,21 +101,6 @@ module.exports = function init(root) {
railway.orm._schemas.forEach(function (schema) { railway.orm._schemas.forEach(function (schema) {
schema.freeze(); schema.freeze();
}); });
});
// check validations and display warning
var displayWarning = false;
Object.keys(models).forEach(function (model) {
var Model = models[model];
if (Model._validations) {
displayWarning = true;
}
});
if (displayWarning) {
var $ = railway.utils.stylize.$;
// 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 log(str, startTime) { function log(str, startTime) {