-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (92 loc) · 3.99 KB
/
index.js
File metadata and controls
104 lines (92 loc) · 3.99 KB
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
var fs = require('fs'),
path = require('path'),
Sequelize = require('sequelize'),
config = {
associationFile: null,
database: 'myDatabase',
username: null,
password: null,
host: 'localhost',
port: 3306,
dialect: 'mysql',
models: './models',
logging: false,
native: false,
force: false,
dialectOptions: {}
};
exports.register = function (plugin, options, next) {
Object.keys(options).forEach(function(k) {
if (config[k] !== undefined) {
config[k] = options[k];
}
});
var models = {},
sequelize = new Sequelize(config.database, config.username, config.password, {
dialect: config.dialect,
port: config.port,
host: config.host,
logging: config.logging,
native: config.native,
dialectOptions: config.dialectOptions
});
sequelize
.authenticate()
.then(function() {
if (config.models) {
config.models = path.resolve(config.models);
fs.readdirSync(config.models).forEach(function(file) {
if (file !== config.associationFile) {
models[file.substr(0, file.indexOf('.'))] =
sequelize.import(path.join(config.models, file));
}
});
if (config.associationFile && fs.existsSync(path.join(config.models, config.associationFile))) {
var associations = require(path.join(config.models, config.associationFile)),
assoc = null;
for (var i = 0, length = associations.length; i < length; i ++) {
assoc = associations[i];
if (models[assoc.source] && models[assoc.target]) {
assoc.options = assoc.options || {};
if (assoc.options.through && assoc.options.through.model) {
assoc.options.through.model = models[assoc.options.through.model];
}
switch (assoc.type) {
case 'oneone':
models[assoc.source].hasOne(models[assoc.target], assoc.options);
models[assoc.target].belongsTo(models[assoc.source], assoc.options);
break;
case 'onemany':
models[assoc.source].hasMany(models[assoc.target], assoc.options);
models[assoc.target].belongsTo(models[assoc.source], assoc.options);
break;
case 'manymany':
models[assoc.source].hasMany(models[assoc.target], assoc.options);
models[assoc.target].hasMany(models[assoc.source], assoc.options);
break;
}
}
}
}
sequelize.sync({ force: config.force }).done(function() {
plugin.expose('db', sequelize);
plugin.expose('models', models);
plugin.log(['hapi-sequelize', 'info'], 'Sequelize connection created');
return next();
});
} else {
// use plugin defaults
plugin.expose('db', sequelize);
plugin.expose('models', models);
plugin.log(['hapi-sequelize', 'info'], 'Sequelize connection created');
return next();
}
})
.catch(function(err) {
plugin.log(['hapi-sequelize', 'error'], 'Error connecting to database. ' + err);
return next(err);
});
};
exports.register.attributes = {
pkg: require('./package.json')
};