| 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 |
2×
2×
2×
136×
136×
136×
136×
136×
136×
136×
145×
145×
202×
202×
202×
202×
51×
105×
51×
51×
105×
51×
151×
88×
494×
88×
88×
88×
63×
63×
202×
136×
202×
202×
131×
131×
71×
71×
71×
71×
136×
139×
139×
139×
139×
68×
139×
136×
202×
139×
68×
68×
68×
68×
68×
68×
71×
71×
71×
63×
59×
59×
59×
59×
59×
4×
4×
4×
4×
4×
4×
136×
136×
| /**
* Module Dependencies
*/
var async = require('async');
var _ = require('lodash');
/**
* Queue up operations on a model instance for any nested association
* values in a .create() query.
*
* @param {Object} parentModel
* @param {Object} associations
* @param {Function} cb
* @api private
*/
module.exports = function(parentModel, collections, cb) {
var self = this;
var parentName = self.identity;
var parentSchema = self.offshore.schema[parentName];
var parentPk = _.findKey(parentSchema.attributes, { primaryKey: true });
var parentPkValue = parentModel[parentPk];
// Create operations
var operations = [];
_.forEach(collections, function(childs, associationName) {
childs = _.isArray(childs) ? childs : [childs];
_.forEach(childs, function(childValue) {
var association = parentSchema.attributes[associationName];
var joinSchema = self.offshore.schema[association.references];
var operation = {
associationName: associationName,
parentPkValue: parentPkValue
};
// With a junction table
if (joinSchema.junctionTable) {
var childAttribute = _.find(joinSchema.attributes, function(attribute) {
return (attribute.references && attribute.references !== parentName);
});
var childPk = _.findKey(self.offshore.schema[childAttribute.references].attributes, { primaryKey: true });
operation.junction = {
collection: self.offshore.collections[association.references],
parentKey: association.onKey,
childKey: _.findKey(joinSchema.attributes, function(attribute) {
return (attribute.references && attribute.references !== parentName);
})
};
operation.child = {
collection: self.offshore.collections[childAttribute.references],
value: childValue,
pk: childPk
};
// With a through table
} else if (joinSchema.throughTable) {
var childAttribute = _.find(joinSchema.attributes, function(attribute) {
return (attribute.references && attribute.references !== parentName);
});
var childPk = _.findKey(self.offshore.schema[childAttribute.references].attributes, { primaryKey: true });
operation.junction = {
collection: self.offshore.collections[association.references],
parentKey: association.onKey,
childKey: joinSchema.throughTable[parentName + '.' + associationName]
};
operation.child = {
collection: self.offshore.collections[childAttribute.references],
value: childValue,
pk: childPk
}
// Without junction table
} else {
var childPk = _.findKey(self.offshore.schema[association.references].attributes, { primaryKey: true });
operation.child = {
collection: self.offshore.collections[association.references],
value: childValue,
pk: childPk,
fk: association.onKey
}
}
operations.push(operation);
});
});
var populateChild = function(operation, cb) {
parentModel[operation.associationName] = parentModel[operation.associationName] || [];
if (_.isObject(operation.child.value)) {
parentModel[operation.associationName].push(operation.child.value);
cb();
} else {
operation.child.collection._loadQuery(self._query).findOne(operation.child.value, function(err, child) {
Iif(err) {
return cb(err);
}
parentModel[operation.associationName].push(child);
cb();
});
}
};
var createJunction = function(operation, cb) {
var junction = {};
junction[operation.junction.parentKey] = operation.parentPkValue;
junction[operation.junction.childKey] = operation.child.value;
if (_.isObject(operation.child.value)) {
junction[operation.junction.childKey] = operation.child.value[operation.child.pk];
}
operation.junction.collection._loadQuery(self._query).create(junction, cb);
}
// Execute all operations
async.eachLimit(operations, 10, function(operation, callback) {
// With a junction table
if (operation.junction) {
// With a child object
if(_.isObject(operation.child.value)) {
operation.child.collection._loadQuery(self._query).create(operation.child.value, function(err, child) {
Iif(err) {
return callback(err);
}
operation.child.value[operation.child.pk] = child[operation.child.pk];
createJunction(operation, function(err) {
Iif(err) {
return callback(err);
}
populateChild(operation, callback);
});
});
// With only a foreign key
} else {
createJunction(operation, function(err) {
Iif(err) {
return callback(err);
}
populateChild(operation, callback);
});
}
// Without a junction table
} else {
// With a child object
if(_.isObject(operation.child.value)) {
operation.child.value[operation.child.fk] = operation.parentPkValue;
operation.child.collection._loadQuery(self._query).create(operation.child.value, function(err, child) {
Iif(err) {
return callback(err);
}
operation.child.value[operation.child.pk] = child[operation.child.pk];
populateChild(operation, callback);
});
// With only a foreign key
} else {
// update child model
var update = {};
update[operation.child.fk] = operation.parentPkValue;
operation.child.collection._loadQuery(self._query).update(operation.child.value, update, function(err, child) {
Iif(err) {
return callback(err);
}
operation.child.value = child;
populateChild(operation, callback);
});
}
}
}, function(err) {
Iif (err) {
return cb(err);
}
cb(null, parentModel);
});
};
|