Skip to content

Commit a284baf

Browse files
committed
feat(seeding): add ability to seed data
This adds functionality for seeding data. We avoid using sequelize direction by relying upon bluebird for mapping. Included are also tests.
1 parent b41246d commit a284baf

File tree

4 files changed

+167
-4
lines changed

4 files changed

+167
-4
lines changed

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: node_js
2+
cache:
3+
directories:
4+
- node_modules
5+
notifications:
6+
email: false
7+
node_js:
8+
- '7'
9+
- '6'
10+
- '4'
11+
before_script:
12+
- npm prune
13+
after_success:
14+
- npm run semantic-release
15+
branches:
16+
except:
17+
- /^v\d+\.\d+\.\d+$/

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "seedquelize",
33
"description": "A seeder for Sequelize",
4-
"version": "1.0.0",
4+
"version": "0.0.0-development",
55
"author": "Jim Cummins <[email protected]>",
66
"bugs": "https://github.com/jimthedev/seedquelize/issues",
77
"config": {
@@ -57,7 +57,8 @@
5757
"secure": "nsp check",
5858
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
5959
"test": "npm run unit",
60-
"unit": "mocha src/*-spec.js"
60+
"unit": "mocha src/*-spec.js",
61+
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
6162
},
6263
"devDependencies": {
6364
"ban-sensitive-files": "1.9.0",
@@ -68,6 +69,10 @@
6869
"mocha": "3.2.0",
6970
"nsp": "2.6.2",
7071
"pre-git": "3.12.0",
72+
"semantic-release": "6.3.2",
7173
"standard": "8.6.0"
74+
},
75+
"dependencies": {
76+
"bluebird": "3.4.7"
7277
}
7378
}

src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
'use strict'
2+
3+
const Promise = require('bluebird')
4+
5+
module.exports = function seed (seeds) {
6+
return Promise.each(seeds, (seed) => {
7+
return Promise.each(seed.data, (item) => {
8+
return seed.model.create(item)
9+
})
10+
})
11+
}

src/seedquelize-spec.js

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,138 @@
11
'use strict'
22

3+
const seed = require('./index.js')
4+
35
/* global describe, it */
4-
describe('empty test suite', () => {
5-
it('write this test', () => {
6+
describe('should create items', () => {
7+
it('when given one model and one data item', (done) => {
8+
// Mocking out a sequelize model's create function
9+
// Users should use an actual sequelize model
10+
const Artist = {
11+
create: function () {
12+
done()
13+
}
14+
}
15+
16+
// This shows the shape of the structure we are expecting
17+
const artists = {
18+
data: [
19+
{
20+
name: 'Andrea Bocelli',
21+
genre: 'Opera'
22+
}
23+
],
24+
model: Artist
25+
}
26+
27+
// This shows how to call seedquelize
28+
seed([artists])
29+
})
30+
it('when given one model and multiple data items', function (done) {
31+
let currentDoneCount = 0
32+
let finalDoneCount = 2
33+
34+
// Mocking out a sequelize model's create function
35+
// Users should use an actual sequelize model
36+
const Artist = {
37+
create: function () {
38+
checkIfDone()
39+
}
40+
}
41+
42+
// This shows the shape of the structure we are expecting
43+
const artists = {
44+
data: [
45+
{
46+
name: 'Andrea Bocelli',
47+
genre: 'Opera'
48+
},
49+
{
50+
name: 'Britney Spears',
51+
genre: 'Pop'
52+
},
53+
{
54+
name: 'Lee Morgan',
55+
genre: 'Jazz'
56+
}
57+
],
58+
model: Artist
59+
}
60+
61+
// Only used in test to see if we've fired create enough times
62+
const checkIfDone = () => {
63+
if (currentDoneCount === finalDoneCount) {
64+
done()
65+
} else {
66+
currentDoneCount++
67+
}
68+
}
69+
70+
// This shows how to call seedquelize
71+
seed([artists])
72+
})
73+
it('when given multiple models and multiple data items', function (done) {
74+
let currentDoneCount = 0
75+
let finalDoneCount = 4
76+
77+
// Mocking out a sequelize model's create function
78+
// Users should use an actual sequelize model
79+
const Artist = {
80+
create: function () {
81+
checkIfDone()
82+
}
83+
}
84+
const User = {
85+
create: function () {
86+
checkIfDone()
87+
}
88+
}
89+
90+
// This shows the shape of the structure we are expecting
91+
const artists = {
92+
data: [
93+
{
94+
name: 'Andrea Bocelli',
95+
genre: 'Opera'
96+
},
97+
{
98+
name: 'Britney Spears',
99+
genre: 'Pop'
100+
},
101+
{
102+
name: 'Lee Morgan',
103+
genre: 'Jazz'
104+
}
105+
],
106+
model: Artist
107+
}
108+
109+
const users = {
110+
data: [
111+
{
112+
username: 'jimthedev',
113+
twitter: '@jimthedev'
114+
},
115+
{
116+
username: 'jnessview',
117+
twitter: 'JNessView'
118+
}
119+
],
120+
model: User
121+
}
122+
123+
// Only used in test to see if we've fired create enough times
124+
const checkIfDone = () => {
125+
if (currentDoneCount === finalDoneCount) {
126+
done()
127+
} else {
128+
currentDoneCount++
129+
}
130+
}
131+
132+
// This shows how to call seedquelize
133+
seed([
134+
artists,
135+
users
136+
])
6137
})
7138
})

0 commit comments

Comments
 (0)