From 67e5845369161f02c655cbd4207ef92470ffe321 Mon Sep 17 00:00:00 2001 From: Sascha Gehlich Date: Sun, 16 Dec 2012 13:50:30 +0100 Subject: [PATCH 1/4] fixed railway integration --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d5e38509..8efe4592 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ exports.Validatable = require('./lib/validatable').Validatable; exports.BaseSQL = require('./lib/sql'); exports.init = function (rw) { - if (typeof rw === 'string') { + if (global.railway) { railway.orm = exports; } else { rw.orm = {Schema: exports.Schema, AbstractClass: exports.AbstractClass}; From ed8ff0ca360d1c473c12b28dabedcefda652dc77 Mon Sep 17 00:00:00 2001 From: Sascha Gehlich Date: Sun, 16 Dec 2012 14:08:51 +0100 Subject: [PATCH 2/4] 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; From 53e2dcf30bd7dd1f0bfd5bdcbaabd1646e0f102e Mon Sep 17 00:00:00 2001 From: Sascha Gehlich Date: Sun, 16 Dec 2012 14:29:53 +0100 Subject: [PATCH 3/4] add additional types stored in Schema.types to schema context --- lib/railway.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/railway.js b/lib/railway.js index bc54a920..d8ac0203 100644 --- a/lib/railway.js +++ b/lib/railway.js @@ -201,6 +201,15 @@ function prepareContext(models, railway, app, defSchema, done) { if (cname) settings[cname].table = name; }; + /** + * If the Schema has additional types, add them to the context + * e.g. MySQL has an additional Point type + */ + if (Schema.types && Object.keys(Schema.types).length) { + for (var typeName in Schema.types) { + ctx[typeName] = Schema.types[typeName]; + } + } ctx.Text = Schema.Text; return ctx; From d413fe46c537e3ebac23c1530435bd6537b44261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Drouyer?= Date: Sun, 16 Dec 2012 19:06:30 +0100 Subject: [PATCH 4/4] Small copy-paste error :) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0aa1fd48..1dd5f71c 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ check following list of available adapters PostgreSQL PostgreSQL - jugglingdb-postgres + jugglingdb-postgres Anatoliy Chakkaev Build Status