forked from exi/rsyncCopyManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
47 lines (42 loc) · 1.3 KB
/
database.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
var config = require('./config.js');
var sequelize = new (require('sequelize'))(config.db.name, config.db.user, config.db.password, {
host: config.db.host
});
var Server = sequelize.import(__dirname + '/activeRecord/Server');
var User = sequelize.import(__dirname + '/activeRecord/User');
var Download = sequelize.import(__dirname + '/activeRecord/Download');
var FSEntry = sequelize.import(__dirname + '/activeRecord/FSEntry');
User.hasMany(Server, { as: 'Servers' });
User.hasMany(Download, { as: 'Downloads' });
Server.hasMany(FSEntry, { as: 'FSEntries' });
Server.belongsTo(User);
FSEntry.belongsTo(Server);
Download.belongsTo(User);
var synced = false;
var syncing = false;
var models = {
Server: Server,
User: User,
FSEntry: FSEntry,
Download: Download
};
var cbs = [];
module.exports = function(cb) {
if (!synced) {
cbs.push(cb);
if (!syncing) {
syncing = true;
sequelize.sync().success(function() {
synced = true;
cbs.forEach(function(cb) {
cb(null, models, sequelize);
});
}).error(function(err) {
cb(err);
});
}
} else {
cb(null, models, sequelize);
}
};
module.exports.chain = require('sequelize').Utils.QueryChainer;