Merge git://github.com/1602/jugglingdb
This commit is contained in:
commit
77ead6d123
9
index.js
9
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;
|
||||
exports.init = function (rw) {
|
||||
if (typeof rw === 'string') {
|
||||
railway.orm = exports;
|
||||
require('./lib/railway')(root);
|
||||
} else {
|
||||
rw.orm = {Schema: exports.Schema, AbstractClass: exports.AbstractClass};
|
||||
}
|
||||
require('./lib/railway')(rw);
|
||||
};
|
||||
|
||||
try {
|
||||
|
|
|
@ -168,10 +168,10 @@ AbstractClass.create = function (data, callback) {
|
|||
|
||||
var obj = null;
|
||||
// if we come from save
|
||||
if (data instanceof AbstractClass && !data.id) {
|
||||
if (data instanceof this && !data.id) {
|
||||
obj = data;
|
||||
data = obj.toObject(true);
|
||||
this.prototype._initProperties.call(obj, data, false);
|
||||
obj._initProperties(data, false);
|
||||
create();
|
||||
} else {
|
||||
obj = new this(data);
|
||||
|
@ -195,6 +195,8 @@ AbstractClass.create = function (data, callback) {
|
|||
|
||||
this._adapter().create(modelName, this.constructor._forDB(data), function (err, id) {
|
||||
if (id) {
|
||||
obj.__data.id = id;
|
||||
obj.__dataWas.id = id;
|
||||
defineReadonlyProp(obj, 'id', id);
|
||||
}
|
||||
done.call(this, function () {
|
||||
|
|
|
@ -1,36 +1,58 @@
|
|||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var Schema = railway.orm.Schema;
|
||||
var Schema = require('./schema').Schema;
|
||||
|
||||
var existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
if (global.railway) {
|
||||
railway.orm._schemas = [];
|
||||
}
|
||||
|
||||
module.exports = function init(root) {
|
||||
var railway, app, models;
|
||||
|
||||
var confFile = (root || app.root) + '/config/database.json';
|
||||
if (typeof root !== 'object' || root.constructor.name !== 'Railway') {
|
||||
railway = global.railway;
|
||||
app = global.app;
|
||||
models = app.models;
|
||||
} else {
|
||||
railway = root;
|
||||
app = railway.app;
|
||||
root = railway.root;
|
||||
models = railway.models;
|
||||
}
|
||||
|
||||
railway.orm._schemas = [];
|
||||
|
||||
var confFile = (root || app.root) + '/config/database';
|
||||
var config;
|
||||
|
||||
if (existsSync(confFile)) {
|
||||
if (existsSync(confFile + '.json')) {
|
||||
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);
|
||||
}
|
||||
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 {
|
||||
} 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);
|
||||
|
||||
context = prepareContext(schema);
|
||||
var context = prepareContext(models, railway, app, schema);
|
||||
|
||||
// run schema first
|
||||
var schemaFile = (root || app.root) + '/db/schema.';
|
||||
|
@ -43,7 +65,12 @@ if (existsSync(schemaFile + 'js')) {
|
|||
}
|
||||
|
||||
if (schemaFile) {
|
||||
railway.utils.runCode(schemaFile, context);
|
||||
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
|
||||
|
@ -54,8 +81,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(models).forEach(function (model) {
|
||||
var Model = models[model];
|
||||
if (Model._validations) {
|
||||
displayWarning = true;
|
||||
}
|
||||
|
@ -77,9 +104,9 @@ function log(str, startTime) {
|
|||
});
|
||||
}
|
||||
|
||||
function prepareContext(defSchema, done) {
|
||||
function prepareContext(models, railway, app, defSchema, done) {
|
||||
var ctx = {app: app},
|
||||
models = {},
|
||||
_models = {},
|
||||
settings = {},
|
||||
cname,
|
||||
schema,
|
||||
|
@ -137,15 +164,18 @@ function prepareContext(defSchema, done) {
|
|||
ctx.describe = ctx.define = function (className, callback) {
|
||||
var m;
|
||||
cname = className;
|
||||
models[cname] = {};
|
||||
_models[cname] = {};
|
||||
settings[cname] = {};
|
||||
if (nonJugglingSchema) {
|
||||
m = callback;
|
||||
} else {
|
||||
callback && callback();
|
||||
m = (schema || defSchema).define(className, models[cname], settings[cname]);
|
||||
m = (schema || defSchema).define(className, _models[cname], settings[cname]);
|
||||
}
|
||||
return global[cname] = app.models[cname] = ctx[cname] = m;
|
||||
if (global.railway) {
|
||||
global[cname] = m;
|
||||
}
|
||||
return models[cname] = ctx[cname] = m;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -158,7 +188,7 @@ function prepareContext(defSchema, done) {
|
|||
type = String;
|
||||
}
|
||||
params.type = type || String;
|
||||
models[cname][name] = params;
|
||||
_models[cname][name] = params;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "jugglingdb",
|
||||
"description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
|
||||
"version": "0.1.24-pre2",
|
||||
"version": "0.1.27",
|
||||
"author": "Anatoliy Chakkaev <rpm1602@gmail.com>",
|
||||
"contributors": [
|
||||
{ "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" },
|
||||
|
|
|
@ -1027,7 +1027,6 @@ function testOrm(schema) {
|
|||
User.find(user.id, function (err, user) {
|
||||
test.ok(user !== u);
|
||||
test.equal(user.passwd, 'qwertysalt');
|
||||
console.log(user.id);
|
||||
User.all({where: {passwd: 'qwertysalt'}}, function (err, users) {
|
||||
test.ok(users[0] !== user);
|
||||
test.equal(users[0].passwd, 'qwertysalt');
|
||||
|
|
Loading…
Reference in New Issue