-
Notifications
You must be signed in to change notification settings - Fork 1
/
seeder.js
144 lines (113 loc) · 3.06 KB
/
seeder.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
var fallbackFaker = {
forString: function (field) {
var str = faker.lorem.sentence();
return validatedData.get(field, str);
},
forNumber: function (field) {
var max = field.max || 1000,
min = field.min || 1;
return _.random(min, max);
},
forBoolean: function () {
return (_.random(1, 1000) % 2) == 0;
},
forDocument: function (field) {
var subSeeder = new Seeder({collection: field.seeder});
return _.first(subSeeder.results);
},
generate: function (field, type) {
// some fallbacks in case seeder is not set.
if (field.seeder instanceof Mongo.Collection) {
return this.forDocument(field);
}
if (_.isString(type)) {
return this.forString(field);
}
if (_.isNumber(type)) {
return this.forNumber(field);
}
if (_.isBoolean(type)) {
return this.forBoolean();
}
if (_.isDate(type)) {
return faker.date.past();
}
return null;
}
};
var validatedData = {
forString: function (field, str) {
if (field.max)
return str.substr(0, field.max-1);
return str;
},
get: function (field, data) {
if (_.isString(data))
return this.forString(field, data);
return data;
}
};
// From StackOverflow - http://stackoverflow.com/a/22129960/1079478
function getFaker (method, safe) {
// console.log(method);
return method.split('.').reduce(function(prev, curr) {
return !safe ? prev[curr] : (prev ? prev[curr] : undefined)
}, faker || self);
}
Seeder = function (config) {
this.results = [];
var self = this,
schema = null;
var options = _.extend({
collection: null,
total: 1
}, config);
if (!(options.collection instanceof Mongo.Collection) || !_.isFunction(options.collection.simpleSchema)) {
throw new Meteor.Error('Seeder config error', 'config Must contain a valid mongo collection');
}
schema = options.collection.simpleSchema();
if (!schema || !_.isFunction(schema.schema)) {
throw new Meteor.Error('Seeder config error', 'collection does not have a valid SimpleSchema');
}
schema = schema.schema();
function getData (name) {
var field = schema[name],
type = new field.type;
if (_.isArray(type)) {
var data = [],
realType = new schema[name +'.$'].type,
max = field.maxCount || _.random(2, 10),
min = field.minCount || 1;
if (min > max)
max = min + 1;
_(_.random(min, max)).times(function () {
data.push(getDataFromSeeder(field, realType));
});
return data;
}
return getDataFromSeeder(field, type);
}
function getDataFromSeeder (field, type) {
var data = null;
if (_.has(field, 'seeder') && _.isString(field.seeder)){
data = getFaker(field.seeder, true)();
} else {
data = fallbackFaker.generate(field, type)
}
return validatedData.get(field, data);
}
_(options.total).times(function (n) {
var data = {};
_.each(_.keys(schema), function (property) {
if (property.indexOf('.$') > 0)
return;
data[property] = getData(property);
});
// console.log(data);
self.results.push(options.collection.insert(data));
});
return this;
};
SimpleSchema.extendOptions({
seeder: Match.Optional(Match.OneOf(String, Mongo.Collection))
});