Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "root",
"password": null,
"database": "PauseOrdering_development",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "PauseOrdering_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "PauseOrdering_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
54 changes: 54 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Sequelize from 'sequelize'
import path from 'path'
import fs from 'fs'
import {dbUsername, dbPassword, dbHost, dbPort} from '../../env.js'

// Make a database connection
const sequelize = new Sequelize('PauseOrdering', dbUsername, dbPassword, {
host: dbHost,
dialect: 'mysql',
port: dbPort || 3306,
pool: {
max: 5,
min: 0,
idle: 10000
}
})

// Test DB connection
sequelize.authenticate()
.then(() => {
console.log('Connected to database')
})
.catch(err => {
console.error('Error connecting to database: ', err)
})


let db = {}
let modelNames = []

// Get all the database table files from the files in this directory
fs.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !=='index.js')
})
.forEach(file => {
let model = sequelize.import(path.join(__dirname, file))
db[model.name] = model
model.sync({force: false})
modelNames.push(model.name)
})


modelNames.forEach(modelName => {
if (db[modelName].associate) { // if this function exists
db[modelName].associate(db) // call it
db[modelName].sync({alter: true}) // and re-sync the table
}
})

db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db
Loading