-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
167 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+$/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]>", | ||
"bugs": "https://github.com/jimthedev/seedquelize/issues", | ||
"config": { | ||
|
@@ -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", | ||
|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]) | ||
}) | ||
}) |