Skip to content

Commit eac4aa4

Browse files
committed
fix(usage): updade syntax usage to be dev only
1 parent e6190ab commit eac4aa4

File tree

6 files changed

+203
-5
lines changed

6 files changed

+203
-5
lines changed

README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ npm install seedquelize --save-dev
2828

2929
### Usage
3030

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.
31+
This example shows how to use seedquelize with express sequelize and postgres. It uses the following npm modules which you will have to install on your own: `express`, `body-parser`, `sequelize` and `pg` as dependencies and `seedquelize` and `dotenv-safe` as dev dependencies. You will need to npm install all of those dependencies for this example to work. You can also check out the examples directory.
32+
33+
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.
3234

3335
```js
36+
let seed;
37+
3438
if(process.env.NODE_ENV !== 'production') {
3539
require('dotenv-safe').config() // environment variables, used for hiding secrets
36-
const seed = require('seedquelize')
40+
seed = require('seedquelize')
3741
}
3842

3943
const express = require('express')
@@ -52,7 +56,7 @@ const Artist = sequelize.define('artist', {
5256
type: Sequelize.STRING,
5357
field: 'genre'
5458
}
55-
}
59+
})
5660

5761
const User = sequelize.define('user', {
5862
username: {
@@ -63,7 +67,7 @@ const User = sequelize.define('user', {
6367
type: Sequelize.STRING,
6468
field: 'twitter'
6569
}
66-
}
70+
})
6771

6872
// This line saves your job by not deleting production data
6973
if(process.env.NODE_ENV !== 'production') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL=
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
*.pid.lock
11+
12+
# Directory for instrumented libs generated by jscoverage/JSCover
13+
lib-cov
14+
15+
# Coverage directory used by tools like istanbul
16+
coverage
17+
18+
# nyc test coverage
19+
.nyc_output
20+
21+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
22+
.grunt
23+
24+
# node-waf configuration
25+
.lock-wscript
26+
27+
# Compiled binary addons (http://nodejs.org/api/addons.html)
28+
build/Release
29+
30+
# Dependency directories
31+
node_modules
32+
jspm_packages
33+
34+
# Optional npm cache directory
35+
.npm
36+
37+
# Optional eslint cache
38+
.eslintcache
39+
40+
# Optional REPL history
41+
.node_repl_history
42+
43+
# Output of 'npm pack'
44+
*.tgz
45+
46+
# Yarn Integrity file
47+
.yarn-integrity
48+
49+
.env

examples/seedquelize-express/index.js

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "seedquelize-express",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "MIT",
12+
"dependencies": {
13+
"body-parser": "^1.15.2",
14+
"express": "^4.14.0",
15+
"pg": "^6.1.2",
16+
"sequelize": "^3.29.0"
17+
},
18+
"devDependencies": {
19+
"dotenv-safe": "^4.0.3",
20+
"seedquelize": "^1.0.0"
21+
}
22+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
},
2929
"files": [
3030
"src/*.js",
31-
"!src/*-spec.js"
31+
"!src/*-spec.js",
32+
"!examples/*"
3233
],
3334
"homepage": "https://github.com/jimthedev/seedquelize#readme",
3435
"keywords": [

0 commit comments

Comments
 (0)