2019-06-06 23:54:32 +02:00
|
|
|
// check config.js existance
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
2019-06-07 17:02:33 +02:00
|
|
|
const argv = require('yargs').argv
|
2019-06-06 23:54:32 +02:00
|
|
|
|
2019-06-07 17:02:33 +02:00
|
|
|
const config_path = path.resolve(argv.config || './config.js')
|
2019-06-06 23:54:32 +02:00
|
|
|
|
|
|
|
if (!fs.existsSync(config_path)) {
|
|
|
|
console.error(`Configuration file not found at '${config_path}. Please copy 'config.example.js' and modify it.`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2019-06-10 00:40:37 +02:00
|
|
|
const config = require(config_path)
|
|
|
|
if (!config.secret) {
|
2019-06-06 23:54:32 +02:00
|
|
|
console.error(`Please specify a random 'secret' in '${config_path}'!`)
|
2019-06-07 17:02:33 +02:00
|
|
|
process.exit(1)
|
2019-06-06 23:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Sequelize = require('sequelize')
|
|
|
|
let db
|
|
|
|
try {
|
2019-06-10 00:40:37 +02:00
|
|
|
db = new Sequelize(config.db)
|
2019-06-07 17:02:33 +02:00
|
|
|
} catch (e) {
|
2019-06-10 00:40:37 +02:00
|
|
|
console.error(`DB Error: check '${config.env}' configuration.\n (sequelize error -> ${e})`)
|
2019-06-06 23:54:32 +02:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// return db existence
|
|
|
|
module.exports = db.authenticate()
|
2019-06-07 17:02:33 +02:00
|
|
|
.then(() => {
|
|
|
|
require('./api/models')
|
2019-06-10 00:40:37 +02:00
|
|
|
if (config.env === 'development') {
|
2019-06-07 17:02:33 +02:00
|
|
|
console.error('DB Force sync')
|
|
|
|
return db.sync({ force: true })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
console.error(e)
|
2019-06-10 00:40:37 +02:00
|
|
|
console.error(`DB Error: check '${config.env}' configuration\n (sequelize error -> ${e})`)
|
2019-06-07 17:02:33 +02:00
|
|
|
process.exit(1)
|
|
|
|
})
|