forked from lykmapipo/sails-hook-seed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (54 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
//dependencies
var path = require('path');
var loadSeeds = require(path.join(__dirname, 'lib', 'load'));
/**
* @description DRY data seeding for sails.
* @param {Object} sails a sails application
* @return {Object} sails-hook-seed which follow installable sails-hook spec
*/
module.exports = function(sails) {
//return hook
return {
//Defaults configurations
defaults: {
//set seeding to be active by default
active: true,
//directory where migration resides
//relative to `sails.appPath`
path: 'seeds'
},
//Runs automatically when the hook initializes
initialize: function(done) {
//reference this hook
var hook = this;
//extend defaults configuration
//with provided configuration from sails
//config
var config =
_.extend(hook.defaults, sails.config.seed);
//if seeding is disabled back-off
if (!config.active) {
done();
}
//continue with seeding
else {
// Lets wait on some of the sails core hooks to
// finish loading before
// load `sails-hook-seed`
var eventsToWaitFor = [];
if (sails.hooks.orm) {
eventsToWaitFor.push('hook:orm:loaded');
}
if (sails.hooks.pubsub) {
eventsToWaitFor.push('hook:pubsub:loaded');
}
sails
.after(eventsToWaitFor, function() {
//load seeds
loadSeeds(config, done);
});
}
}
};
};