This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #1 Sails-seed as 11 or 10 hook
- Loading branch information
Showing
9 changed files
with
313 additions
and
88 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
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,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: [] | ||
}); | ||
} | ||
}); | ||
} |
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,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; | ||
}; | ||
|
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,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; | ||
}; |
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,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; | ||
}; |
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
Oops, something went wrong.