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
7 changes: 7 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"development": {
"database": "database_development",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
7 changes: 5 additions & 2 deletions controllers/pokemon.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const express = require('express');
const router = express.Router();

const db = require("./models")
// GET /pokemon - return a page with favorited Pokemon
router.get('/', (req, res) => {
router.get('/', async (req, res) => {
// TODO: Get all records from the DB and render to view
let allFavePokemon = await db.pokemon.findAll()
console.log(allFavePokemon)
res.send('Render a page of favorites here');
});


// POST /pokemon - receive the name of a pokemon and add it to the database
router.post('/', (req, res) => {
// TODO: Get form data and add a new record to DB
Expand Down
43 changes: 43 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const process = require('process');
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' &&
file.indexOf('.test.js') === -1
);
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
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;
Loading