Remove globals, read yaml
This commit is contained in:
parent
c27a624d02
commit
8b4da9e5e9
11
index.js
11
index.js
|
@ -5,10 +5,13 @@ exports.Schema = require('./lib/schema').Schema;
|
|||
exports.AbstractClass = require('./lib/abstract-class').AbstractClass;
|
||||
exports.Validatable = require('./lib/validatable').Validatable;
|
||||
|
||||
exports.init = function (root) {
|
||||
if (!global.railway) return;
|
||||
railway.orm = exports;
|
||||
require('./lib/railway')(root);
|
||||
exports.init = function (rw) {
|
||||
if (global.railway) {
|
||||
railway.orm = exports;
|
||||
} else {
|
||||
rw.orm = {Schema: exports.Schema, AbstractClass: exports.AbstractClass};
|
||||
}
|
||||
require('./lib/railway')(rw);
|
||||
};
|
||||
|
||||
try {
|
||||
|
|
|
@ -1,36 +1,57 @@
|
|||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var Schema = railway.orm.Schema;
|
||||
var Schema = require('./schema').Schema;
|
||||
|
||||
var existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
railway.orm._schemas = [];
|
||||
|
||||
module.exports = function init(root) {
|
||||
|
||||
var confFile = (root || app.root) + '/config/database.json';
|
||||
var config;
|
||||
|
||||
if (existsSync(confFile)) {
|
||||
try {
|
||||
config = JSON.parse(fs.readFileSync(confFile, 'utf-8'))[app.set('env')];
|
||||
// when driver name started with point - look for driver in app root (relative path)
|
||||
if (config.driver && config.driver.match(/^\./)) {
|
||||
config.driver = path.join(app.root, config.driver);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Could not parse config/database.json');
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
config = {};
|
||||
if (global.railway) {
|
||||
railway.orm._schemas = [];
|
||||
}
|
||||
|
||||
var schema = new Schema(config && config.driver || 'memory', config);
|
||||
schema.log = log;
|
||||
railway.orm._schemas.push(schema);
|
||||
module.exports = function init(root) {
|
||||
var railway, app;
|
||||
|
||||
context = prepareContext(schema);
|
||||
if (typeof root !== 'object' || root.constructor.name !== 'Railway') {
|
||||
railway = global.railway;
|
||||
app = global.app;
|
||||
railway.models = app.models;
|
||||
} else {
|
||||
railway = root;
|
||||
app = railway.app;
|
||||
root = railway.root;
|
||||
}
|
||||
|
||||
railway.orm._schemas = [];
|
||||
|
||||
var confFile = (root || app.root) + '/config/database';
|
||||
var config;
|
||||
|
||||
if (existsSync(confFile + '.json')) {
|
||||
try {
|
||||
config = JSON.parse(fs.readFileSync(confFile + '.json', 'utf-8'))[app.set('env')];
|
||||
} catch (e) {
|
||||
console.log('Could not parse config/database.json');
|
||||
throw e;
|
||||
}
|
||||
} else if (existsSync(confFile + '.yml')) {
|
||||
try {
|
||||
config = railway.utils.readYaml(confFile + '.yml')[app.set('env')];
|
||||
} catch (e) {
|
||||
console.log('Could not parse config/database.yml');
|
||||
throw e;
|
||||
}
|
||||
config = {};
|
||||
}
|
||||
// when driver name started with point - look for driver in app root (relative path)
|
||||
if (config.driver && config.driver.match(/^\./)) {
|
||||
config.driver = path.join(app.root, config.driver);
|
||||
}
|
||||
|
||||
var schema = new Schema(config && config.driver || 'memory', config);
|
||||
schema.log = log;
|
||||
railway.orm._schemas.push(schema);
|
||||
|
||||
var context = prepareContext(railway, app, schema);
|
||||
|
||||
// run schema first
|
||||
var schemaFile = (root || app.root) + '/db/schema.';
|
||||
|
@ -43,7 +64,12 @@ if (existsSync(schemaFile + 'js')) {
|
|||
}
|
||||
|
||||
if (schemaFile) {
|
||||
railway.utils.runCode(schemaFile, context);
|
||||
var code = fs.readFileSync(schemaFile);
|
||||
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
|
||||
|
@ -54,8 +80,8 @@ railway.orm._schemas.forEach(function (schema) {
|
|||
// check validations and display warning
|
||||
|
||||
var displayWarning = false;
|
||||
Object.keys(app.models).forEach(function (model) {
|
||||
var Model = app.models[model];
|
||||
Object.keys(railway.models).forEach(function (model) {
|
||||
var Model = railway.models[model];
|
||||
if (Model._validations) {
|
||||
displayWarning = true;
|
||||
}
|
||||
|
@ -77,7 +103,7 @@ function log(str, startTime) {
|
|||
});
|
||||
}
|
||||
|
||||
function prepareContext(defSchema, done) {
|
||||
function prepareContext(railway, app, defSchema, done) {
|
||||
var ctx = {app: app},
|
||||
models = {},
|
||||
settings = {},
|
||||
|
@ -145,7 +171,7 @@ function prepareContext(defSchema, done) {
|
|||
callback && callback();
|
||||
m = (schema || defSchema).define(className, models[cname], settings[cname]);
|
||||
}
|
||||
return global[cname] = app.models[cname] = ctx[cname] = m;
|
||||
return railway.models[cname] = ctx[cname] = m;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue