UNPKG

1.3 kBJavaScriptView Raw
1/**
2 * models/index.js
3 * */
4
5"use strict";
6
7var fs = require("fs"),
8 path = require("path"),
9 exists = require('fs-exists-sync'),
10 Sequelize = require("sequelize"),
11 middleware = require('./middleware/index.js');
12
13module.exports = class {
14
15 constructor(config, paths) {
16 if (!config) {
17 return null;
18 }
19
20 this.middleware = new middleware(paths);
21
22 return this.connect(config);
23 }
24
25 connect(config) {
26 if (exists(paths.app.models)) {
27 var sequelize = new Sequelize(config.name, config.username, config.password, config.config),
28 db = {};
29
30 fs.readdirSync(paths.app.models)
31 .filter((file) => {
32 return (file.indexOf(".") !== 0 && file.indexOf(".mongoose.js") == -1);
33 })
34 .forEach((file) => {
35 var model = sequelize.import(path.join(paths.app.models, file));
36 db[model.name] = model;
37 });
38
39 Object.keys(db).forEach((modelName) => {
40 if ("associate" in db[modelName]) {
41 db[modelName].associate(db);
42 }
43 });
44
45 db.sequelize = sequelize;
46
47 return db;
48 }
49 return null;
50 }
51
52}
\No newline at end of file