diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..b503d20 --- /dev/null +++ b/config/config.json @@ -0,0 +1,26 @@ +{ + "development": { + "username": "root", + "password": null, + "database": "database_development", + "host": "127.0.0.1", + "dialect": "mysql", + "operatorsAliases": false + }, + "test": { + "username": "root", + "password": null, + "database": "database_test", + "host": "127.0.0.1", + "dialect": "mysql", + "operatorsAliases": false + }, + "production": { + "username": "root", + "password": null, + "database": "database_production", + "host": "127.0.0.1", + "dialect": "mysql", + "operatorsAliases": false + } +} diff --git a/models/index.js b/models/index.js new file mode 100644 index 0000000..d5d8cb0 --- /dev/null +++ b/models/index.js @@ -0,0 +1,43 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const Sequelize = require('sequelize'); +const basename = path.basename(__filename); +const env = process.env.NODE_ENV || 'development'; +const config = require(__dirname + '/../config/config.json')[env]; +const db = {}; + +let sequelize; +if (config.use_env_variable) { + sequelize = new Sequelize(process.env[config.use_env_variable], config); +} else { + sequelize = new Sequelize( + config.database, + config.username, + config.password, + config + ); +} + +fs.readdirSync(__dirname) + .filter(file => { + return ( + file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js' + ); + }) + .forEach(file => { + const model = sequelize['import'](path.join(__dirname, file)); + db[model.name] = model; + }); + +Object.keys(db).forEach(modelName => { + if (db[modelName].associate) { + db[modelName].associate(db); + } +}); + +db.sequelize = sequelize; +db.Sequelize = Sequelize; + +module.exports = db; diff --git a/package.json b/package.json index ec03569..7c771b3 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "postinstall": "electron-builder install-app-deps package.json && yarn build-dll && opencollective-postinstall", "postlint-fix": "prettier --ignore-path .eslintignore --single-quote --write '**/*.{*{js,jsx,json},babelrc,eslintrc,prettierrc,stylelintrc}'", "postlint-styles-fix": "prettier --ignore-path .eslintignore --single-quote --write '**/*.{css,scss}'", - "precommit": "lint-staged", "prestart": "yarn build", "start": "cross-env NODE_ENV=production electron ./app/main.prod.js", "start-main-dev": "cross-env HOT=1 NODE_ENV=development electron ./app/main.dev.babel.js",