From ecc1543c136805dcf987c06e94966efb739cb46a Mon Sep 17 00:00:00 2001 From: "M. Elliot Frost" Date: Sat, 2 May 2015 10:46:00 -0400 Subject: [PATCH] Close #1 Sails-seed as 11 or 10 hook --- README.md | 62 ++++++++++++++++-------- index.js | 104 ++++++++++++++++++++++++++++++++++++---- lib/seed.js | 64 +++++++++++++------------ lib/seedArray.js | 28 ++++++----- lib/seedObject.js | 28 ++++++----- package.json | 11 +++-- scripts/postinstall.js | 91 +++++++++++++++++++++++++++++++++++ templates/configSeed.js | 12 +++++ templates/userHook.js | 1 + 9 files changed, 313 insertions(+), 88 deletions(-) create mode 100755 scripts/postinstall.js create mode 100644 templates/configSeed.js create mode 100644 templates/userHook.js diff --git a/README.md b/README.md index 0402332..af5a7b1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 879de6e..656c44d 100644 --- a/index.js +++ b/index.js @@ -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: [] + }); + } + }); +} diff --git a/lib/seed.js b/lib/seed.js index cc9f5b6..cfe8018 100644 --- a/lib/seed.js +++ b/lib/seed.js @@ -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; }; diff --git a/lib/seedArray.js b/lib/seedArray.js index 90519df..eaba0c1 100644 --- a/lib/seedArray.js +++ b/lib/seedArray.js @@ -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; }; diff --git a/lib/seedObject.js b/lib/seedObject.js index 3b97fbf..6ce65e0 100644 --- a/lib/seedObject.js +++ b/lib/seedObject.js @@ -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; }; diff --git a/package.json b/package.json index 5376876..9e9fe03 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/scripts/postinstall.js b/scripts/postinstall.js new file mode 100755 index 0000000..0a98531 --- /dev/null +++ b/scripts/postinstall.js @@ -0,0 +1,91 @@ +var semver = require('semver'); +var path = require('path'); +var moduleDir = '../'; +var homeDir = '../../' +var requireDir= homeDir + moduleDir +var fs = require('fs'); +var util = require('util'); + +fs.exists(homeDir + 'node_modules/sails', function(exists){ + if(exists){ + var sailsVersion = require(requireDir + 'node_modules/sails/package.json').version; + createSeed(function(){ + if(semver.lt(sailsVersion, '0.11.0')) createHook(function(){}); + }); + } else { + util.debug('You are not using a sails project. Please use a sails project.'); + } +}) + +function createSeed(cb){ + fs.exists(homeDir + 'config/seeds.js', function(exists){ + if(exists) { + console.log('Seed file already exists.'); + cb(); + } else { + generateSeed(cb); + }; + + }); +} + +function createHook(cb){ + fs.exists(homeDir + 'api/hooks', function(exists){ + if(exists){ + fs.exists(homeDir + 'api/hooks/seed', function(exists){ + if(exists){ + generateHook(cb); + } else { + generateHookDir(cb); + } + }); + } else { + generateHooksDir(cb); + } + }); +} + +function generateSeed(cb){ + fs.readFile('./templates/configSeed.js', function(err, data){ + fs.writeFile(homeDir + 'config/seeds.js', data, function(err){ + console.log('Seed file was generated'); + cb(); + }); + }); +} + +function generateHook(cb){ + fs.exists(homeDir + 'api/hooks/seed/index.js', function(exists){ + if(exists){ + console.log('Hook file already exists'); + cb(); + } else { + fs.readFile('./templates/userHook.js', function(err, data){ + fs.writeFile(homeDir + 'api/hooks/seed/index.js', data, function(err){ + console.log('Hook file was generated'); + cb(); + }); + }); + } + }); +} + +function generateHookDir(cb){ + fs.mkdir(homeDir + '/api/hooks/seed', function(err){ + console.log('Seed Hook Dir File Generated'); + generateHook(cb); + }); +} + +function generateHooksDir(cb){ + fs.mkdir(homeDir + '/api/hooks', function(err){ + console.log('Hooks dir generated'); + fs.exists(homeDir + 'api/hooks/seed', function(exists){ + if(exists){ + generateHook(cb); + } else { + generateHookDir(cb); + } + }); + }); +} diff --git a/templates/configSeed.js b/templates/configSeed.js new file mode 100644 index 0000000..8c64ee3 --- /dev/null +++ b/templates/configSeed.js @@ -0,0 +1,12 @@ +/** + * Sails Seed Settings + * (sails.config.seed) + * + * Configuration for the data seeding in Sails. + * + * For more information on configuration, check out: + * http://github.com/frostme/sails-seed + */ +module.exports = { + +} diff --git a/templates/userHook.js b/templates/userHook.js new file mode 100644 index 0000000..98a35b6 --- /dev/null +++ b/templates/userHook.js @@ -0,0 +1 @@ +module.exports = require('sails-seed');