-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (99 loc) · 3.12 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
const _ = require('lodash');
const faker = require('faker');
const mongoose = require('mongoose');
const fns = {}
fns.create = function(modelName, count, customData) {
const data = _forCountFixtures(count, i => {
return _createFixture(modelName)
});
_customizeFixtureData(data, customData);
console.log('Fixtures generated');
return count === 1 ? data[0] : data;
}
function _createFixture(modelName) {
const model = mongoose.models[modelName];
return _buildFixtureFromModel(model);
}
function _buildFixtureFromModel(model) {
// let fixture = { _id: mongoose.Types.ObjectId() };
let fixture = { };
_buildFixtureForModelProps(model.schema, 'obj', fixture);
return fixture;
}
function _buildFixtureForModelProps(schema, propPath, fixture) {
Object.keys(_.get(schema, propPath)).forEach((name) => {
const nestedPropPath = `${propPath}.${name}`;
if (_isNestedSchemaProp(schema, nestedPropPath)) {
return _buildFixtureForModelProps(schema, nestedPropPath, fixture);
}
const fakeVal = _getFakeVal(schema, nestedPropPath);
let fakedVal;
if (_isFakerCode(fakeVal)) {
const fakerCode = _extractFakerCode(fakeVal);
fakedVal = _createFake(fakerCode);
} else {
fakedVal = fakeVal;
}
if (_isNestedPropPathArray(schema, nestedPropPath) &&
fakedVal !== undefined) {
fakedVal = [fakedVal];
}
_.set(fixture, _convertToFixturePropPath(nestedPropPath), fakedVal);
});
}
function _getFakeVal(schema, nestedPropPath) {
return _isNestedPropPathArray(schema, nestedPropPath) ?
_.get(schema, nestedPropPath)[0].fake :
_.get(schema, nestedPropPath).fake;
}
function _isNestedPropPathArray(schema, nestedPropPath) {
return Array.isArray(_.get(schema, nestedPropPath));
}
function _convertToFixturePropPath(propPath) {
return propPath.substring(4);
}
function _isNestedSchemaProp(schema, propName) {
return !Array.isArray(_.get(schema, propName))
&& _.get(schema, propName).type === undefined
&& _.get(schema, propName).fake === undefined
}
function _forCountFixtures(count, modelCreatorFn) {
return _.range(count).map(modelCreatorFn);
}
function _isFakerCode(str) {
return str && typeof str === 'string' && str.startsWith('fake:');
}
function _extractFakerCode(str) {
return str.substring(5);
}
function _createFake(fakerCode) {
if (fakerCode === 'random.mongoId') {
return mongoose.Types.ObjectId();
}
return faker.fake('{{' + fakerCode + '}}')
}
function _customizeFixtureData(data, customData) {
if (customData) {
Object.keys(customData).forEach(key => {
data.forEach(d => {
_.set(d, key, customData[key]);
});
});
}
}
fns.createAndSave = function(modelName, count, customData) {
const data = this.create(modelName, count, customData);
return mongoose.models[modelName].insertMany(data)
.then((docs) => {
console.log('Fixtures saved');
return count === 1 ? docs[0] : docs;
})
}
fns.purge = function(modelName) {
return mongoose.models[modelName].remove({})
.then(() => {
console.log('Fixtures purged');
return;
});
}
module.exports = fns;