Merge pull request #169 from saschagehlich/schema_wait_connect
let the adapter decide when to load the schema
This commit is contained in:
commit
bc250b1bb2
|
@ -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';
|
||||||
|
|
|
@ -49,36 +49,14 @@ 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.
|
|
||||||
schema.on('connected', function() {
|
|
||||||
railway.orm._schemas.push(schema);
|
|
||||||
|
|
||||||
var context = prepareContext(models, railway, app, schema);
|
if (schema.waitForConnect) {
|
||||||
|
schema.on('connected', function() {
|
||||||
// run schema first
|
loadSchema(schema, railway, app, models);
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
});
|
} else {
|
||||||
|
loadSchema(schema, railway, app, models);
|
||||||
|
}
|
||||||
|
|
||||||
// check validations and display warning
|
// 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);
|
// 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) {
|
function log(str, startTime) {
|
||||||
var $ = railway.utils.stylize.$;
|
var $ = railway.utils.stylize.$;
|
||||||
var m = Date.now() - startTime;
|
var m = Date.now() - startTime;
|
||||||
|
|
Loading…
Reference in New Issue