dip-dobit-shared-model 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/models/index.js +47 -0
- package/package.json +2 -2
package/models/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const Sequelize = require("sequelize");
|
|
6
|
+
const process = require("process");
|
|
7
|
+
const basename = path.basename(__filename);
|
|
8
|
+
const env = process.env.NODE_ENV || "development";
|
|
9
|
+
const config = require(__dirname + "/../config/config.js")[env];
|
|
10
|
+
const db = {};
|
|
11
|
+
|
|
12
|
+
let sequelize;
|
|
13
|
+
if (config.use_env_variable) {
|
|
14
|
+
sequelize = new Sequelize(process.env[config.use_env_variable], config);
|
|
15
|
+
} else {
|
|
16
|
+
sequelize = new Sequelize(
|
|
17
|
+
config.database,
|
|
18
|
+
config.username,
|
|
19
|
+
config.password,
|
|
20
|
+
config
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
fs.readdirSync(__dirname)
|
|
25
|
+
.filter((file) => {
|
|
26
|
+
return (
|
|
27
|
+
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
|
|
28
|
+
);
|
|
29
|
+
})
|
|
30
|
+
.forEach((file) => {
|
|
31
|
+
const model = require(path.join(__dirname, file))(
|
|
32
|
+
sequelize,
|
|
33
|
+
Sequelize.DataTypes
|
|
34
|
+
);
|
|
35
|
+
db[model.name] = model;
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
Object.keys(db).forEach((modelName) => {
|
|
39
|
+
if (db[modelName].associate) {
|
|
40
|
+
db[modelName].associate(db);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
db.sequelize = sequelize;
|
|
45
|
+
db.Sequelize = Sequelize;
|
|
46
|
+
|
|
47
|
+
module.exports = db;
|
package/package.json
CHANGED