Merge github.com:1602/jugglingdb

This commit is contained in:
Sébastien Drouyer 2012-12-16 23:58:53 +01:00
commit 519d6cf7fb
4 changed files with 49 additions and 30 deletions

View File

@ -63,7 +63,7 @@ check following list of available adapters
<!-- PostgreSQL --> <!-- PostgreSQL -->
<tr> <tr>
<td><a href="http://www.postgresql.org/"><img src="http://www.postgresql.org/favicon.ico" style="vertical-align:middle"" alt="PostgreSQL" /></a> PostgreSQL</td> <td><a href="http://www.postgresql.org/"><img src="http://www.postgresql.org/favicon.ico" style="vertical-align:middle"" alt="PostgreSQL" /></a> PostgreSQL</td>
<td><a href="/1602/jugglingdb-nano">jugglingdb-postgres</a></td> <td><a href="/1602/jugglingdb-postgres">jugglingdb-postgres</a></td>
<td><a href="/anatoliychakkaev">Anatoliy Chakkaev</a></td> <td><a href="/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://travis-ci.org/1602/jugglingdb-postgres"><img src="https://travis-ci.org/1602/jugglingdb-postgres.png?branch=master" alt="Build Status" /></a></td> <td><a href="https://travis-ci.org/1602/jugglingdb-postgres"><img src="https://travis-ci.org/1602/jugglingdb-postgres.png?branch=master" alt="Build Status" /></a></td>
</tr> </tr>

View File

@ -7,7 +7,7 @@ exports.Validatable = require('./lib/validatable').Validatable;
exports.BaseSQL = require('./lib/sql'); exports.BaseSQL = require('./lib/sql');
exports.init = function (rw) { exports.init = function (rw) {
if (typeof rw === 'string') { if (global.railway) {
railway.orm = exports; railway.orm = exports;
} else { } else {
rw.orm = {Schema: exports.Schema, AbstractClass: exports.AbstractClass}; rw.orm = {Schema: exports.Schema, AbstractClass: exports.AbstractClass};

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,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;
@ -201,6 +209,15 @@ function prepareContext(models, railway, app, defSchema, done) {
if (cname) settings[cname].table = name; 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; ctx.Text = Schema.Text;
return ctx; return ctx;