forked from seanemmer/mongoose-seed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (141 loc) · 3.8 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
'use strict';
/**
* Module dependencies.
**/
var _ = require('lodash'),
async = require('async'),
mongoose = require('mongoose'),
chalk = require('chalk'),
path = require('path');
function Seeder() {
this.connected = false;
}
Seeder.prototype.connect = function(db, cb) {
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
*/
if (mongoose.connection.readyState == 1) {
_this.connected = true;
console.log('Successfully initialized mongoose-seed');
cb();
}
mongoose.connect(db, function(err) {
// Log Error
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(err);
} else {
_this.connected = true;
console.log('Successfully initialized mongoose-seed');
cb();
}
});
};
Seeder.prototype.loadModels = function(modelPaths) {
console.log(modelPaths);
modelPaths.forEach(function(modelPath) {
require(path.resolve(modelPath));
});
};
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 = [];
// 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.remove({}, function(err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}
console.log(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 = _.unique(_.pluck(seedData,'model'));
// 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.each(seedData, function (entry, callback) {
var Model = mongoose.model(entry.model);
async.forEachOf(entry.documents,
function(document, index, callback) {
Model.create(document, function(err) {
if (err) {
console.error(chalk.red('Error creating document [' + index + '] of ' + entry.model + ' model'));
console.error(chalk.red('Error: ' + err.message));
return;
}
console.log('Successfully created document [' + index + '] of ' + entry.model + ' model');
callback();
});
},
function(err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}
callback();
}
);
}, function(err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}
cb();
});
});
};
module.exports = new Seeder();