forked from seanemmer/mongoose-seed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (165 loc) · 5.21 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict';
/**
* Module dependencies.
**/
var _ = require('lodash'),
async = require('async'),
mongoose = require('mongoose'),
chalk = require('chalk'),
path = require('path');
function Seeder() {
this.connected = false;
this.consoleLogEnabled = true;
}
function consoleLog(_this, message) {
if (_this.consoleLogEnabled !== undefined && _this.consoleLogEnabled === true) {
console.log(message);
}
}
Seeder.prototype.setLogOutput = function (logOutput) {
this.consoleLogEnabled = logOutput;
};
Seeder.prototype.connect = function (...params) {
var _this = this;
/*
switch (mongoose.connection.readyState) {
case 0 : Disconnected;
case 1 : Connected;
case 2 : Connecting;
case 3 : Disconnecting;
}
source http://mongoosejs.com/docs/api.html#connection_Connection-readyState
*/
var db, cb, opts = null;
if (params.length == 2) {
db = params[0];
cb = params[1];
} else if (params.length == 3) {
db = params[0];
opts = params[1];
cb = params[2];
} else {
console.error('Pass either 2 or 3 arguments to seeder.connect');
process.exit(1);
}
if (mongoose.connection.readyState === 1) {
_this.connected = true;
consoleLog(_this, 'Successfully initialized mongoose-seed');
cb();
} else {
mongoose.connect(db, opts, function (err) {
afterConnect(_this, err, cb);
});
}
};
function afterConnect(_this, err, cb) {
// Log Error
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
consoleLog(_this, err);
} else {
_this.connected = true;
consoleLog(_this, 'Successfully initialized mongoose-seed');
cb();
}
}
Seeder.prototype.loadModels = function (modelPaths) {
consoleLog(this, modelPaths);
modelPaths.forEach(function (modelPath) {
var model = require(path.resolve(modelPath));
if (model instanceof Function) {
model();
}
});
};
Seeder.prototype.invalidModelCheck = function (models, cb) {
var invalidModels = [];
models.forEach(function (model) {
if (_.indexOf(mongoose.modelNames(), model) === -1) {
invalidModels.push(model);
}
});
if (invalidModels.length) {
cb(new Error('Models not registered in Mongoose: ' + invalidModels));
} else {
cb();
}
};
Seeder.prototype.clearModels = function (models, cb) {
if (!this.connected) {
return new Error('Not connected to db, exiting function');
}
var modelNames = [];
var _this = this;
// Convert to array if not already
if (Array.isArray(models)) {
modelNames = models;
} else if (typeof (models) === 'string') {
modelNames.push(models);
} else {
console.error(chalk.red('Error: Invalid model type'));
return;
}
// Confirm that all Models have been registered in Mongoose
this.invalidModelCheck(modelNames, function (err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}
// Clear each model
async.each(modelNames, function (modelName, done) {
var Model = mongoose.model(modelName);
Model.deleteMany({}, function (err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}
consoleLog(_this, modelName + 's collection cleared');
done();
});
}, function (err) {
// Final async callback
if (err) {
return;
}
cb();
});
});
};
Seeder.prototype.populateModels = function (seedData, cb) {
if (!this.connected) {
return new Error('Not connected to db, exiting function');
}
var modelNames = _.uniq(_.map(seedData, 'model'));
var _this = this;
// Confirm that all Models have been registered in Mongoose
var invalidModels = this.invalidModelCheck(modelNames, function (err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}
// Populate each model
async.eachOf(seedData, function (entry, i, outerCallback) {
var Model = mongoose.model(entry.model);
async.eachOf(entry.documents, function (document, j, innerCallback) {
Model.create(document, function (err) {
if (err) {
console.error(chalk.red('Error creating document [' + j + '] of ' + entry.model + ' model'));
console.error(chalk.red('Error: ' + err.message));
} else {
consoleLog(_this, 'Successfully created document [' + j + '] of ' + entry.model + ' model');
}
innerCallback();
});
}, function (err) {
outerCallback();
});
}, function (err) {
cb();
});
});
};
Seeder.prototype.disconnect = function () {
mongoose.disconnect();
};
module.exports = new Seeder();