Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
Close #1 Sails-seed as 11 or 10 hook
Browse files Browse the repository at this point in the history
  • Loading branch information
frostme committed May 2, 2015
1 parent 49aa729 commit ecc1543
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 88 deletions.
62 changes: 41 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,55 @@ SAILS-SEED [![Build Status](https://travis-ci.org/frostme/sails-seed.svg?branch=
```
npm install sails-seed
```
Depending on you version of sails, sails-seed is treated differently
On install, a config/seed.js is created. Here you will put your seeding data.

## Sails 0.11
For Sails 0.11 and greater there is nothing further to do. Sails-seed runs as an installable hook.

## Sails 0.10
For Sails 0.10 and previous, a file of api/hook/seed/index.js is created on installation.
No further configuration is required, but this file is necessary for the data to seed.

## Usage
```js
var seed = require('sails-seed');
```
1) In your config/models.js file, add the following lines
```js
seed: seed.seed,
seedArray: seed.seedArray,
seedObject: seed.seedObject
```
These are necessary functions to be loaded with waterline.
Place your seeding data in the config/seed.js file.
For exampe, for a Person model, your config/seed.js file might look like

2) In your config/bootstrap.js file, add the following line
```js
seed(cb);
module.exports.seed = {
person: [
{
firstName: 'Luke',
lastName: 'Skywalker'
},
{
firstName: 'Darth',
lastName: 'Vader'
}
]
}
```
This will run your seed on startup.

3) Add seed data to your models
In the models you wish to seed, add the following
By default, sails-seed will overwrite the data which is present. If you would not like your
seed to overwrite, your new config/seed.js file might look like

```js
seedData: []
module.exports.seed = {
person: {
data: [
{
firstName: 'Luke',
lastName: 'Skywalker'
},
{
firstName: 'Darth',
lastName: 'Vader'
}
],
overwrite: false
}
}
```
In your seed data add an array of objects, or a single object, that represent new model(s) to be seeded.

4) Configuration
If you would like to configure your seed, you can add the following options to you models,
- overwrite: if set to true, will overwrite you existing data

## Author

Expand Down
104 changes: 94 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,96 @@
var async = require('async');
module.exports = function(cb){
async.series(_(sails.models).toArray().filter(function (it){
return !it.junctionTable;
}).value().map(function(model){
return model.seed;
}), cb);
'use strict';

//dependencies
var async = require('async'),
path = require('path'),
libPath = path.join(__dirname, 'lib');

// model extras
var seed = require(path.join(libPath, 'seed')),
seedArray = require(path.join(libPath, 'seedArray')),
seedObject = require(path.join(libPath, 'seedObject'));

module.exports = function(sails){
return {
initialize: function(done){
// first
//
// patch model to acquire seed data attribute
sails
.after(['hook:moduleloader:loaded'], function(){
patchAttributes();
});

//later on wait for this/these event(s)
//to apply methods to models
var eventsToWaitFor = [];

//wait for orm
//and pub sub hooks
//to be loaded
//for methods to
//be attached to models
if(sails.hooks.orm) eventsToWaitFor.push('hook:orm:loaded');
if(sails.hooks.pubsub) eventsToWaitFor.push('hook:pubsub:loaded');

sails
.after(eventsToWaitFor, function(){
//bind additional methods
//to models
//then seed models
//and let sails continue

patch();
seeds();
done();
});
}
};
};

function seeds(){
async.eachSeries(Object.keys(sails.models), function(model, cb){
if(sails.models[model].seed){
sails.models[model].seed(cb);
} else {
cb();
}
}, function(err){
if(err) sails.log.error('Your seeds were not planted correctly');
else sails.log.info('Your seeds are ready to grow!');
});
};
function patch(){
_(sails.models)
.forEach(function(model){
if(model.globalId){
seed(model);
seedArray(model);
seedObject(model);
}
});
}

module.exports.seed = require('./lib/seed');
module.exports.seedArray = require('./lib/seedArray');
module.exports.seedObject = require('./lib/seedObject');
function patchAttributes(){
_(sails.models)
.forEach(function(model){
var data = sails.config.seeds[model.identity];
if(data){
if(data.overwrite == false){
_extend(model, {
seedData: data.data,
overwrite: false
});
} else {
_.extend(model, {
seedData: data,
overwrite: true
});
}
} else {
_.extend(model, {
seedData: []
});
}
});
}
64 changes: 34 additions & 30 deletions lib/seed.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
module.exports = function (callback) {
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
if (!self.seedData) {
sails.log.debug('No data avaliable to seed ' + modelName);
callback();
return;
}
self.count().exec(function (err, count) {
if (!err && count === 0) {
sails.log.debug('Seeding ' + modelName + '...');
if (self.seedData instanceof Array) {
self.seedArray(callback);
}else{
self.seedObject(callback);
}
} else {
if (self.overwrite){
sails.log.debug(modelName + ' had models, overwriting data now');
self.destroy({}).exec(function(err){
if (self.seedData instanceof Array) {
self.seedArray(callback);
}else{
self.seedObject(callback);
}
});
module.exports = function(model){
function seed(callback) {
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
if (!self.seedData) {
sails.log.debug('No data avaliable to seed ' + modelName);
callback();
return;
}
self.count().exec(function (err, count) {
if (!err && count === 0) {
sails.log.debug('Seeding ' + modelName + '...');
if (self.seedData instanceof Array) {
self.seedArray(callback);
}else{
self.seedObject(callback);
}
} else {
sails.log.debug(modelName + ' had models, so no seed needed');
callback();
if (self.overwrite){
sails.log.debug(modelName + ' had models, overwriting data now');
self.destroy({}).exec(function(err){
if (self.seedData instanceof Array) {
self.seedArray(callback);
}else{
self.seedObject(callback);
}
});
} else {
sails.log.debug(modelName + ' had models, so no seed needed');
callback();
}
}
}
});
});
}

model.seed = seed;
};

28 changes: 16 additions & 12 deletions lib/seedArray.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
module.exports = function (callback) {
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
self.createEach(self.seedData).exec(function (err, results) {
if (err) {
sails.log.debug(err);
callback();
} else {
sails.log.debug(modelName + ' seed planted');
callback();
}
});
module.exports = function(model){
function seedArray(callback) {
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
self.createEach(self.seedData).exec(function (err, results) {
if (err) {
sails.log.error(err);
callback();
} else {
sails.log.debug(modelName + ' seed planted');
callback();
}
});
};

model.seedArray = seedArray;
};
28 changes: 16 additions & 12 deletions lib/seedObject.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
module.exports = function (callback) {
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
self.create(self.seedData).exec(function (err, results) {
if (err) {
sails.log.debug(err);
callback();
} else {
sails.log.debug(modelName + ' seed planted');
callback();
}
});
module.exports = function(model){
function seedObject(callback) {
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
self.create(self.seedData).exec(function (err, results) {
if (err) {
sails.log.error(err);
callback();
} else {
sails.log.debug(modelName + ' seed planted');
callback();
}
});

}

model.seedObject = seedObject;
};
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "sails-seed",
"version": "0.1.1",
"version": "0.2.2",
"description": "Simple data seeding for sails.js projects.",
"main": "index.js",
"scripts": {
"test": "mocha"
"test": "mocha",
"postinstall": "node scripts/postinstall"
},
"repository": {
"type": "git",
Expand All @@ -17,10 +18,14 @@
],
"author": "frostme",
"license": "ISC",
"sails": {
"isHook": true
},
"dependencies": {
"async": "^0.9.0",
"extend": "^2.0.0",
"lodash": "^3.7.0"
"lodash": "^3.7.0",
"semver": "^4.3.3"
},
"devDependencies": {
"chai": "^2.2.0",
Expand Down
Loading

0 comments on commit ecc1543

Please sign in to comment.