forked from alessioalex/ClientManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJakefile.js
executable file
·135 lines (115 loc) · 3.31 KB
/
Jakefile.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Jake is a JavaScript build tool for Node.js
* http://howtonode.org/intro-to-jake
* https://github.com/mde/jake
*
* To find out the available tasks for Jake run:
* jake -T
*
* To run a task do:
* jake db:reset
*
* To run a task with params do:
* jake db:populate[20]
*/
var mongoose = require('mongoose'),
colors = require('colors'),
faker = require('./vendor/faker'),
log = console.log,
ENV = process.env.NODE_ENV || 'development';
JK = {};
JK.abortIfProduction = function() {
if (ENV === 'production') {
throw new Error('Are you out of your mind? Drop the production db?!');
}
}
// desc('Initialize stuff.');
task('init', [], function() {
JK.utils = JK.utils || require('./lib/utils');
// make sure the configs are loaded only once
if (!JK.config) {
JK.utils.loadConfig(__dirname + '/config', function(config) {
JK.config = config;
complete();
});
}
}, { async: true });
namespace('db', function() {
// desc('Connect to database');
task('connect', ['init'], function() {
var _self = this;
log('- db:connect'.yellow);
if (!JK.mongoose) {
JK.mongoose = JK.utils.connectToDatabase(mongoose, JK.config.db[ENV].main, function(err) {
if (err) { throw err; }
log(' connected to database'.green);
complete.call(_self);
});
} else {
complete();
}
}, { async: true });
// desc('Load models');
task('loadModels', [], function(params) {
log('- db:loadModels'.yellow);
if (!JK.models) {
JK.models = {};
['client'].forEach(function (elem, index) {
JK.models[elem] = require('./app/models/' + elem)(JK.mongoose);
});
}
log(' loaded models'.green);
});
desc('Remove all items from the database.');
task('empty', ['db:connect', 'db:loadModels'], function(params) {
var Client, query;
log('- db:empty'.yellow);
Client = JK.models.client;
query = Client.find().remove(function(err) {
if (err) { throw err; }
log(' emptied db'.green);
complete();
});
}, { async: true });
// Run jake db:populate OR jake db:populate[<numberOfItems>]
desc('Populate db with phony data.');
task('populate', ['db:empty'], function(howMany) {
var Client;
log('- db:populate'.yellow);
Client = JK.models.client;
howMany = howMany || 50;
(function populate(nr) {
var client, _now, _pastDate;
if (nr === 0) {
log((' populated ' + ENV + ' database').green);
process.exit(0);
}
_now = JK.utils.getRandDate();
// client has to be older than 18 years -> 18 * 12 = 216
_pastDate = JK.utils.getRandDate('past', _now, {
timeUnit : 'months',
timeVal : (18 + parseInt(nr, 10)) * 12,
});
client = new Client({
name : faker.Name.findName(),
email : faker.Internet.email(),
born : _pastDate,
company : faker.Company.companyName()
});
client.save(function(err) {
if (err) {
// Faker sometimes generates bad emails
if (err.errors && err.errors.email) {
nr++;
} else {
throw err;
}
}
nr--;
process.nextTick(function() {
populate(nr);
});
});
}(howMany));
});
});