forked from 1602/jugglingdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (94 loc) · 2.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var fs = require('fs');
var path = require('path');
var Schema = exports.Schema = require('./lib/schema').Schema;
exports.AbstractClass = require('./lib/model.js');
var baseSQL = './lib/sql';
exports.__defineGetter__('BaseSQL', function () {
return require(baseSQL);
});
exports.loadSchema = function(filename, settings, compound) {
var schema = [];
var definitions = require(filename);
Object.keys(definitions).forEach(function(k) {
var conf = settings[k];
if (!conf) {
console.log('No config found for ' + k + ' schema, using in-memory schema');
conf = {driver: 'memory'};
}
schema[k] = new Schema(conf.driver, conf);
schema[k].on('define', function(m, name, prop, sett) {
compound.models[name] = m;
if (conf.backyard) {
schema[k].backyard.define(name, prop, sett);
}
});
schema[k].name = k;
schema.push(schema[k]);
if (conf.backyard) {
schema[k].backyard = new Schema(conf.backyard.driver, conf.backyard);
}
if ('function' === typeof definitions[k]) {
define(schema[k], definitions[k]);
if (conf.backyard) {
define(schema[k].backyard, definitions[k]);
}
}
});
return schema;
function define(db, def) {
def(db, compound);
}
};
exports.init = function (compound) {
if (global.railway) {
global.railway.orm = exports;
} else {
compound.orm = {
Schema: exports.Schema,
AbstractClass: exports.AbstractClass
};
if (compound.app.enabled('noeval schema')) {
compound.orm.schema = exports.loadSchema(
compound.root + '/db/schema',
compound.app.get('database'),
compound
);
if (compound.app.enabled('autoupdate')) {
compound.on('ready', function() {
compound.orm.schema.forEach(function(s) {
s.autoupdate();
if (s.backyard) {
s.backyard.autoupdate();
s.backyard.log = s.log;
}
});
});
}
return;
}
}
// legacy stuff
if (compound.version > '1.1.5-15') {
compound.on('after routes', initialize);
} else {
initialize();
}
function initialize() {
var railway = './lib/railway';
try {
var init = require(railway);
} catch (e) {
console.log(e.stack);
}
if (init) {
init(compound);
}
}
};
exports.__defineGetter__('version', function () {
return JSON.parse(fs.readFileSync(__dirname + '/package.json')).version;
});
var commonTest = './test/common_test';
exports.__defineGetter__('test', function () {
return require(commonTest);
});