|
1 | 1 | # seedquelize
|
2 | 2 |
|
3 |
| -> A seeder for Sequelize |
| 3 | +> Seed your database with [Sequelize](sequelizejs.com). |
| 4 | +> |
| 5 | +> Add initial data to your database so that you don't need to create dummy data each time you boot up your app in development. |
| 6 | +> |
| 7 | +> You can also use this when running end-to-end tests since you'll need consistent data to test against. |
4 | 8 |
|
5 | 9 | [![NPM][npm-icon] ][npm-url]
|
6 | 10 |
|
7 | 11 | [![Build status][ci-image] ][ci-url]
|
8 | 12 | [![semantic-release][semantic-image] ][semantic-url]
|
9 | 13 | [![js-standard-style][standard-image]][standard-url]
|
10 | 14 |
|
| 15 | +## Getting Started |
| 16 | + |
| 17 | +### Background |
| 18 | + |
| 19 | +Typically you want to seed your database before your express or other app begins. This means it needs to sit in between the spot where you define your sequelize models and where you serve your app. |
| 20 | + |
| 21 | +**NOTE: You also will want to make sure that you don't run this in production since it will remove all the data. :)** |
| 22 | + |
| 23 | +### Installation |
| 24 | + |
| 25 | +``` |
| 26 | +npm install seedquelize --save-dev |
| 27 | +``` |
| 28 | + |
| 29 | +### Usage |
| 30 | + |
| 31 | +This example uses the following npm modules which you will have to install on your own: `dotenv-safe`, `express`, `body-parser`, `sequelize` and `seedquelize` to start up an express app with seed data when not in production. Your database config info should be in a .env file. I've commented this file as much as possible to show how to use seeding in an actual app. Pull requests are welcome in order to make this more clear. |
| 32 | + |
| 33 | +```js |
| 34 | +if(process.env.NODE_ENV !== 'production') { |
| 35 | + require('dotenv-safe').config() // environment variables, used for hiding secrets |
| 36 | + const seed = require('seedquelize') |
| 37 | +} |
| 38 | + |
| 39 | +const express = require('express') |
| 40 | +const bodyParser = require('body-parser') |
| 41 | +const Sequelize = require('sequelize') |
| 42 | + |
| 43 | +// Connect to a sql database |
| 44 | +const sequelize = new Sequelize(process.env.DATABASE_URL) |
| 45 | + |
| 46 | +const Artist = sequelize.define('artist', { |
| 47 | + name: { |
| 48 | + type: Sequelize.STRING, |
| 49 | + field: 'name' |
| 50 | + }, |
| 51 | + genre: { |
| 52 | + type: Sequelize.STRING, |
| 53 | + field: 'genre' |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +const User = sequelize.define('user', { |
| 58 | + username: { |
| 59 | + type: Sequelize.STRING, |
| 60 | + field: 'username' |
| 61 | + }, |
| 62 | + twitter: { |
| 63 | + type: Sequelize.STRING, |
| 64 | + field: 'twitter' |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// This line saves your job by not deleting production data |
| 69 | +if(process.env.NODE_ENV !== 'production') { |
| 70 | + |
| 71 | + // Clear out the existing database |
| 72 | + sequelize.sync({force: true}).then(() => { |
| 73 | + |
| 74 | + // This shows the shape of the structure we are expecting. |
| 75 | + // data is just an array of whatever models you want to create, |
| 76 | + // model is of course the actual model you want created |
| 77 | + const artists = { |
| 78 | + data: [ |
| 79 | + { |
| 80 | + name: 'Andrea Bocelli', |
| 81 | + genre: 'Opera' |
| 82 | + }, |
| 83 | + { |
| 84 | + name: 'Britney Spears', |
| 85 | + genre: 'Pop' |
| 86 | + }, |
| 87 | + { |
| 88 | + name: 'Lee Morgan', |
| 89 | + genre: 'Jazz' |
| 90 | + } |
| 91 | + ], |
| 92 | + model: Artist |
| 93 | + } |
| 94 | + |
| 95 | + const users = { |
| 96 | + data: [ |
| 97 | + { |
| 98 | + username: 'jimthedev', |
| 99 | + twitter: '@jimthedev' |
| 100 | + }, |
| 101 | + { |
| 102 | + username: 'jnessview', |
| 103 | + twitter: 'JNessView' |
| 104 | + } |
| 105 | + ], |
| 106 | + model: User |
| 107 | + } |
| 108 | + |
| 109 | + // Actually seed the database using seedquelize |
| 110 | + seed([ |
| 111 | + artists, |
| 112 | + users |
| 113 | + ]).then(() =>{ |
| 114 | + // Only started after seeding in dev/test |
| 115 | + startExpress(); |
| 116 | + }) |
| 117 | + }) |
| 118 | +} else { |
| 119 | + // Started right away in prod |
| 120 | + startExpress(); |
| 121 | +} |
| 122 | + |
| 123 | +function startExpress() { |
| 124 | + // Create a new express app to server our api |
| 125 | + var app = express() |
| 126 | + |
| 127 | + // Teach express how to parse requests of type application/json |
| 128 | + // |
| 129 | + app.use(bodyParser.json()); |
| 130 | + |
| 131 | + // Teach express how to parse requests of type application/x-www-form-urlencoded |
| 132 | + // |
| 133 | + app.use(bodyParser.urlencoded({ extended: true })); |
| 134 | + |
| 135 | + app.get('/api/artists', (req, res) => { |
| 136 | + Artist.findAll().then((artists) => { |
| 137 | + res.json(artists); |
| 138 | + }) |
| 139 | + }) |
| 140 | + app.get('/api/users', (req, res) => { |
| 141 | + User.findAll().then((users) => { |
| 142 | + res.json(users); |
| 143 | + }) |
| 144 | + }) |
| 145 | + // Determine which port to listen on |
| 146 | + const port = process.env.PORT ? process.env.PORT : 3001 |
| 147 | + |
| 148 | + // Actually start the server |
| 149 | + app.listen(port, () => { |
| 150 | + console.log('Example app listening on port ' + port + '!') |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | +``` |
| 156 | +
|
11 | 157 | ### Small print
|
12 | 158 |
|
13 | 159 | Author: Jim Cummins <jimthedev@gmail.com> © 2017
|
|
0 commit comments