Skip to content

Commit

Permalink
feat(seeding): add ability to seed data
Browse files Browse the repository at this point in the history
This adds functionality for seeding data. We avoid using sequelize direction by relying upon

bluebird for mapping. Included are also tests.
  • Loading branch information
jimthedev committed Jan 15, 2017
1 parent b41246d commit a284baf
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 4 deletions.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: node_js
cache:
directories:
- node_modules
notifications:
email: false
node_js:
- '7'
- '6'
- '4'
before_script:
- npm prune
after_success:
- npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "seedquelize",
"description": "A seeder for Sequelize",
"version": "1.0.0",
"version": "0.0.0-development",
"author": "Jim Cummins <jimthedev@gmail.com>",
"bugs": "https://github.com/jimthedev/seedquelize/issues",
"config": {
Expand Down Expand Up @@ -57,7 +57,8 @@
"secure": "nsp check",
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
"test": "npm run unit",
"unit": "mocha src/*-spec.js"
"unit": "mocha src/*-spec.js",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"devDependencies": {
"ban-sensitive-files": "1.9.0",
Expand All @@ -68,6 +69,10 @@
"mocha": "3.2.0",
"nsp": "2.6.2",
"pre-git": "3.12.0",
"semantic-release": "6.3.2",
"standard": "8.6.0"
},
"dependencies": {
"bluebird": "3.4.7"
}
}
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
'use strict'

const Promise = require('bluebird')

module.exports = function seed (seeds) {
return Promise.each(seeds, (seed) => {
return Promise.each(seed.data, (item) => {
return seed.model.create(item)
})
})
}
135 changes: 133 additions & 2 deletions src/seedquelize-spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,138 @@
'use strict'

const seed = require('./index.js')

/* global describe, it */
describe('empty test suite', () => {
it('write this test', () => {
describe('should create items', () => {
it('when given one model and one data item', (done) => {
// Mocking out a sequelize model's create function
// Users should use an actual sequelize model
const Artist = {
create: function () {
done()
}
}

// This shows the shape of the structure we are expecting
const artists = {
data: [
{
name: 'Andrea Bocelli',
genre: 'Opera'
}
],
model: Artist
}

// This shows how to call seedquelize
seed([artists])
})
it('when given one model and multiple data items', function (done) {
let currentDoneCount = 0
let finalDoneCount = 2

// Mocking out a sequelize model's create function
// Users should use an actual sequelize model
const Artist = {
create: function () {
checkIfDone()
}
}

// This shows the shape of the structure we are expecting
const artists = {
data: [
{
name: 'Andrea Bocelli',
genre: 'Opera'
},
{
name: 'Britney Spears',
genre: 'Pop'
},
{
name: 'Lee Morgan',
genre: 'Jazz'
}
],
model: Artist
}

// Only used in test to see if we've fired create enough times
const checkIfDone = () => {
if (currentDoneCount === finalDoneCount) {
done()
} else {
currentDoneCount++
}
}

// This shows how to call seedquelize
seed([artists])
})
it('when given multiple models and multiple data items', function (done) {
let currentDoneCount = 0
let finalDoneCount = 4

// Mocking out a sequelize model's create function
// Users should use an actual sequelize model
const Artist = {
create: function () {
checkIfDone()
}
}
const User = {
create: function () {
checkIfDone()
}
}

// This shows the shape of the structure we are expecting
const artists = {
data: [
{
name: 'Andrea Bocelli',
genre: 'Opera'
},
{
name: 'Britney Spears',
genre: 'Pop'
},
{
name: 'Lee Morgan',
genre: 'Jazz'
}
],
model: Artist
}

const users = {
data: [
{
username: 'jimthedev',
twitter: '@jimthedev'
},
{
username: 'jnessview',
twitter: 'JNessView'
}
],
model: User
}

// Only used in test to see if we've fired create enough times
const checkIfDone = () => {
if (currentDoneCount === finalDoneCount) {
done()
} else {
currentDoneCount++
}
}

// This shows how to call seedquelize
seed([
artists,
users
])
})
})

0 comments on commit a284baf

Please sign in to comment.