-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
203 lines (175 loc) · 6.48 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*jslint node: true */
'use strict';
/**
* Barrels: Simple fixtures for Sails.js
*/
/**
* Dependencies
*/
var fs = require('fs');
var path = require('path');
var async = require('async');
var _ = require('lodash');
module.exports = Barrels;
/**
* Barrels module
* @param {string} sourceFolder defaults to <project root>/test/fixtures
*/
function Barrels(sourceFolder) {
if (!(this instanceof Barrels))
return new Barrels(sourceFolder);
// Fixture objects loaded from the JSON files
this.data = {};
// Map fixture positions in JSON files to the real DB IDs
this.idMap = {};
// The list of associations by model
this.associations = {};
// Load the fixtures
sourceFolder = sourceFolder || process.cwd() + '/test/fixtures';
var files = fs.readdirSync(sourceFolder);
for (var i = 0; i < files.length; i++) {
if (['.json', '.js'].indexOf(path.extname(files[i]).toLowerCase()) !== -1) {
var modelName = path.basename(files[i]).split('.')[0].toLowerCase();
this.data[modelName] = require(path.join(sourceFolder, files[i]));
}
}
// The list of the fixtures model names
this.modelNames = Object.keys(this.data);
}
/**
* Add associations
* @param {function} done callback
*/
Barrels.prototype.associate = function(collections, done) {
if (!_.isArray(collections)) {
done = collections;
collections = this.modelNames;
}
var that = this;
// Add associations whenever needed
async.each(collections, function(modelName, nextModel) {
var Model = sails.models[modelName];
if (Model) {
var fixtureObjects = _.cloneDeep(that.data[modelName]);
async.each(fixtureObjects, function(item, nextItem) {
// Item position in the file
var itemIndex = fixtureObjects.indexOf(item);
// Find and associate
Model.findOne(that.idMap[modelName][itemIndex]).exec(function(err, model) {
if (err)
return nextItem(err);
// Pick associations only
item = _.pick(item, Object.keys(that.associations[modelName]));
async.each(Object.keys(item), function(attr, nextAttr) {
var association = that.associations[modelName][attr];
// Required associations should have beed added earlier
if (association.required)
return nextAttr();
var joined = association[association.type];
if (!_.isArray(item[attr]))
model[attr] = that.idMap[joined][item[attr]-1];
else {
for (var j = 0; j < item[attr].length; j++) {
model[attr].add(that.idMap[joined][item[attr][j]-1]);
}
}
model.save(function(err) {
if (err)
return nextAttr(err);
nextAttr();
});
}, nextItem);
});
}, nextModel);
} else {
nextModel();
}
}, done);
};
/**
* Put loaded fixtures in the database, associations excluded
* @param {array} collections optional list of collections to populate
* @param {function} done callback
* @param {boolean} autoAssociations automatically associate based on the order in the fixture files
*/
Barrels.prototype.populate = function(collections, done, autoAssociations) {
var preserveLoadOrder = true;
if (!_.isArray(collections)) {
autoAssociations = done;
done = collections;
collections = this.modelNames;
preserveLoadOrder = false;
}
else {
collections = _.map(collections, function(collection) {
return collection.toLowerCase();
});
}
autoAssociations = !(autoAssociations === false);
var that = this;
// Populate each table / collection
async[preserveLoadOrder ? 'eachSeries' : 'each'](collections, function(modelName, nextModel) {
var Model = sails.models[modelName];
if (Model) {
// Cleanup existing data in the table / collection
Model.destroy().exec(function(err) {
if (err)
return nextModel(err);
// Save model's association information
that.associations[modelName] = {};
for (var i = 0; i < Model.associations.length; i++) {
var alias = Model.associations[i].alias;
that.associations[modelName][alias] = Model.associations[i];
that.associations[modelName][alias].required = !!(Model._validator.validations[alias].required);
}
// Insert all the fixture items
that.idMap[modelName] = [];
var fixtureObjects = _.cloneDeep(that.data[modelName]);
async.each(fixtureObjects, function(item, nextItem) {
// Item position in the file
var itemIndex = fixtureObjects.indexOf(item);
for (var alias in that.associations[modelName]) {
if (that.associations[modelName][alias].required) {
// With required associations present, the associated fixtures
// must be already loaded, so we can map the ids
var collectionName = that.associations[modelName][alias].collection; // many-to-many
var associatedModelName = that.associations[modelName][alias].model; // one-to-many
if ((_.isArray(item[alias]))&&(collectionName)) {
if (!that.idMap[collectionName])
return nextItem(new Error('Please provide a loading order acceptable for required associations'));
for (var i = 0; i < item[alias].length; i++) {
item[alias][i] = that.idMap[collectionName][item[alias][i] - 1];
}
} else if (associatedModelName) {
if (!that.idMap[associatedModelName])
return nextItem(new Error('Please provide a loading order acceptable for required associations'));
item[alias] = that.idMap[associatedModelName][item[alias] - 1];
}
} else if (autoAssociations) {
// The order is not important, so we can strip
// associations data and associate later
item = _.omit(item, alias);
}
}
// Insert
Model.create(item).exec(function(err, model) {
if (err)
return nextItem(err);
// Primary key mapping
that.idMap[modelName][itemIndex] = model[Model.primaryKey];
nextItem();
});
}, nextModel);
});
} else {
nextModel();
}
}, function(err) {
if (err)
return done(err);
// Create associations if requested
if (autoAssociations)
return that.associate(collections, done);
done();
});
};