| 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 |
3×
3×
3×
3×
3×
65×
65×
65×
65×
65×
65×
65×
65×
65×
65×
65×
7×
7×
7×
7×
65×
65×
21×
21×
21×
21×
21×
21×
14×
1×
13×
20×
20×
20×
7×
7×
7×
7×
13×
13×
13×
15×
15×
15×
12×
12×
14×
12×
4×
3×
6×
15×
15×
15×
15×
13×
13×
| /**
* Aggregate Queries Adapter Normalization
*/
var _ = require('lodash');
var async = require('async');
var normalize = require('../utils/normalize');
var hasOwnProperty = require('../utils/helpers').object.hasOwnProperty;
module.exports = {
// If an optimized createEach exists, use it, otherwise use an asynchronous loop with create()
createEach: function(valuesList, cb, metaContainer) {
var self = this;
var connName,
adapter;
// Normalize Arguments
cb = normalize.callback(cb);
// Build Default Error Message
var err = 'No createEach() or create() method defined in adapter!';
// Custom user adapter behavior
Iif (hasOwnProperty(this.dictionary, 'createEach')) {
connName = this.dictionary.createEach;
adapter = this.connections[connName]._adapter;
if (hasOwnProperty(adapter, 'createEach')) {
return adapter.createEach(connName, this.collection, valuesList, cb, metaContainer);
}
}
// Default behavior
// WARNING: Not transactional! (unless your data adapter is)
var results = [];
// Find the connection to run this on
Iif (!hasOwnProperty(this.dictionary, 'create')) return cb(new Error(err));
connName = this.dictionary.create;
adapter = this.connections[connName]._adapter;
Iif (!hasOwnProperty(adapter, 'create')) return cb(new Error(err));
async.eachSeries(valuesList, function(values, cb) {
adapter.create(connName, self.collection, values, function(err, row) {
Iif (err) return cb(err);
results.push(row);
cb();
}, metaContainer);
}, function(err) {
Iif (err) return cb(err);
cb(null, results);
});
},
// If an optimized findOrCreateEach exists, use it, otherwise use an asynchronous loop with create()
findOrCreateEach: function(attributesToCheck, valuesList, cb, metaContainer) {
var self = this;
var connName;
var adapter;
// Normalize Arguments
cb = normalize.callback(cb);
var isObjectArray = false;
if (_.isObject(attributesToCheck[0])) {
if (attributesToCheck.length > 1 &&
attributesToCheck.length !== valuesList.length) {
return cb(new Error('findOrCreateEach: The two passed arrays have to be of the same length.'));
}
isObjectArray = true;
}
// Clone sensitive data
attributesToCheck = _.clone(attributesToCheck);
valuesList = _.clone(valuesList);
// Custom user adapter behavior
if (hasOwnProperty(this.dictionary, 'findOrCreateEach')) {
connName = this.dictionary.findOrCreateEach;
adapter = this.connections[connName]._adapter;
Eif (hasOwnProperty(adapter, 'findOrCreateEach')) {
return adapter.findOrCreateEach(connName, this.collection, valuesList, cb, metaContainer);
}
}
// Build a list of models
var models = [];
var i = 0;
async.eachSeries(valuesList, function(values, cb) {
Iif (!_.isObject(values)) return cb(new Error('findOrCreateEach: Unexpected value in valuesList.'));
// Check that each of the criteria keys match:
// build a criteria query
var criteria = {};
if (isObjectArray) {
Eif (_.isObject(attributesToCheck[i])) {
Object.keys(attributesToCheck[i]).forEach(function(attrName) {
criteria[attrName] = values[attrName];
});
if (attributesToCheck.length > 1) {
i++;
}
} else {
return cb(new Error('findOrCreateEach: Element ' + i + ' in attributesToCheck is not an object.'));
}
} else {
attributesToCheck.forEach(function(attrName) {
criteria[attrName] = values[attrName];
});
}
return self.findOrCreate.call(self, criteria, values, function(err, model) {
Iif (err) return cb(err);
// Add model to list
Eif (model) models.push(model);
cb(null, model);
}, metaContainer);
}, function(err) {
Iif (err) return cb(err);
cb(null, models);
});
}
};
|