|
| 1 | +let seed; |
| 2 | + |
| 3 | +if(process.env.NODE_ENV !== 'production') { |
| 4 | + require('dotenv-safe').config() // environment variables, used for hiding secrets |
| 5 | + seed = require('seedquelize') |
| 6 | +} |
| 7 | + |
| 8 | +const express = require('express') |
| 9 | +const bodyParser = require('body-parser') |
| 10 | +const Sequelize = require('sequelize') |
| 11 | + |
| 12 | +// Connect to a sql database |
| 13 | +const sequelize = new Sequelize(process.env.DATABASE_URL) |
| 14 | + |
| 15 | +const Artist = sequelize.define('artist', { |
| 16 | + name: { |
| 17 | + type: Sequelize.STRING, |
| 18 | + field: 'name' |
| 19 | + }, |
| 20 | + genre: { |
| 21 | + type: Sequelize.STRING, |
| 22 | + field: 'genre' |
| 23 | + } |
| 24 | +}); |
| 25 | + |
| 26 | +const User = sequelize.define('user', { |
| 27 | + username: { |
| 28 | + type: Sequelize.STRING, |
| 29 | + field: 'username' |
| 30 | + }, |
| 31 | + twitter: { |
| 32 | + type: Sequelize.STRING, |
| 33 | + field: 'twitter' |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +// This line saves your job by not deleting production data |
| 38 | +if(process.env.NODE_ENV !== 'production') { |
| 39 | + |
| 40 | + // Clear out the existing database |
| 41 | + sequelize.sync({force: true}).then(() => { |
| 42 | + |
| 43 | + // This shows the shape of the structure we are expecting. |
| 44 | + // data is just an array of whatever models you want to create, |
| 45 | + // model is of course the actual model you want created |
| 46 | + const artists = { |
| 47 | + data: [ |
| 48 | + { |
| 49 | + name: 'Andrea Bocelli', |
| 50 | + genre: 'Opera' |
| 51 | + }, |
| 52 | + { |
| 53 | + name: 'Britney Spears', |
| 54 | + genre: 'Pop' |
| 55 | + }, |
| 56 | + { |
| 57 | + name: 'Lee Morgan', |
| 58 | + genre: 'Jazz' |
| 59 | + } |
| 60 | + ], |
| 61 | + model: Artist |
| 62 | + } |
| 63 | + |
| 64 | + const users = { |
| 65 | + data: [ |
| 66 | + { |
| 67 | + username: 'jimthedev', |
| 68 | + twitter: '@jimthedev' |
| 69 | + }, |
| 70 | + { |
| 71 | + username: 'jnessview', |
| 72 | + twitter: 'JNessView' |
| 73 | + } |
| 74 | + ], |
| 75 | + model: User |
| 76 | + } |
| 77 | + |
| 78 | + // Actually seed the database using seedquelize |
| 79 | + seed([ |
| 80 | + artists, |
| 81 | + users |
| 82 | + ]).then(() =>{ |
| 83 | + // Only started after seeding in dev/test |
| 84 | + startExpress(); |
| 85 | + }) |
| 86 | + }) |
| 87 | +} else { |
| 88 | + // Started right away in prod |
| 89 | + startExpress(); |
| 90 | +} |
| 91 | + |
| 92 | +function startExpress() { |
| 93 | + // Create a new express app to server our api |
| 94 | + var app = express() |
| 95 | + |
| 96 | + // Teach express how to parse requests of type application/json |
| 97 | + // |
| 98 | + app.use(bodyParser.json()); |
| 99 | + |
| 100 | + // Teach express how to parse requests of type application/x-www-form-urlencoded |
| 101 | + // |
| 102 | + app.use(bodyParser.urlencoded({ extended: true })); |
| 103 | + |
| 104 | + app.get('/api/artists', (req, res) => { |
| 105 | + Artist.findAll().then((artists) => { |
| 106 | + res.json(artists); |
| 107 | + }) |
| 108 | + }) |
| 109 | + app.get('/api/users', (req, res) => { |
| 110 | + User.findAll().then((users) => { |
| 111 | + res.json(users); |
| 112 | + }) |
| 113 | + }) |
| 114 | + // Determine which port to listen on |
| 115 | + const port = process.env.PORT ? process.env.PORT : 3001 |
| 116 | + |
| 117 | + // Actually start the server |
| 118 | + app.listen(port, () => { |
| 119 | + console.log('Example app listening on port ' + port + '!') |
| 120 | + }) |
| 121 | +} |
0 commit comments