-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
34 lines (30 loc) · 1.12 KB
/
db.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
"use strict";
/**
* A module that allows for running a Sequelize sync.
*/
module.exports = function ($injector, sequelize, log, glob) {
// Resolve each model file to make sure they are registered with Sequelize.
// Normally these would just be resolved as needed, but we need them to all be loaded for the sync to work.
var files = glob.sync("models/**/*.js");
files.forEach(function (file) {
// This assumes the module name matches the file name, which is the default behavior.
var name = file.substring(7, file.length - 3);
$injector.resolve(name);
});
return {
connect: function (sync) {
log.info("Connecting to database...");
// If sync is true or "force" run a sync, otherwise run authenticate.
var promise;
if (sync) {
var opts = { force: sync === "force" };
promise = sequelize.sync(opts);
} else {
promise = sequelize.authenticate();
}
return promise.then(function () {
log.info("Connected");
});
}
};
};