| 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 |
3×
3×
3×
3×
3×
3×
3×
3×
1142×
1142×
154×
154×
1142×
1142×
2294×
1142×
1142×
1142×
26×
1142×
154×
988×
16×
972×
972×
972×
972×
972×
968×
2×
972×
8528×
700×
700×
972×
972×
972×
972×
972×
2×
972×
972×
164×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
4×
5×
9×
9×
9×
9×
9×
9×
2×
972×
972×
972×
968×
2×
968×
968×
968×
857×
857×
857×
968×
857×
857×
857×
968×
968×
968×
968×
11×
11×
957×
957×
42×
42×
42×
42×
2×
957×
957×
957×
957×
| /**
* Module Dependencies
*/
var async = require('async');
var _ = require('lodash');
var utils = require('../../utils/helpers');
var Deferred = require('../deferred');
var callbacks = require('../../utils/callbacksRunner');
var nestedOperations = require('../../utils/nestedOperations');
var hop = utils.object.hasOwnProperty;
/**
* Create a new record
*
* @param {Object || Array} values for single model or array of multiple values
* @param {Function} callback
* @return Deferred object if no callback
*/
module.exports = function(values, cb, metaContainer) {
var self = this;
// Handle Deferred where it passes criteria first
if(_.isPlainObject(arguments[0]) && (_.isPlainObject(arguments[1]) || _.isArray(arguments[1]))) {
values = arguments[1];
cb = arguments[2];
}
// Loop through values and pull out any buffers before cloning
var bufferValues = {};
_.each(_.keys(values), function(key) {
Iif (Buffer.isBuffer(values[key])) {
bufferValues[key] = values[key];
}
});
values = _.cloneDeep(values) || {};
// Replace clone keys with the buffer values
_.each(_.keys(bufferValues), function(key) {
values[key] = bufferValues[key];
});
// Remove all undefined values
if (_.isArray(values)) {
values = _.remove(values, undefined);
}
// Return Deferred or pass to adapter
if (typeof cb !== 'function') {
return new Deferred(this, this.create, {}, values);
}
// Handle Array of values
if (Array.isArray(values)) {
return this.createEach(values, cb, metaContainer);
}
// Process Values
var valuesObject = processValues.call(this, values);
// Create any of the belongsTo associations and set the foreign key values
createBelongsTo.call(this, valuesObject, function(err) {
Iif (err) return cb(err);
beforeCallbacks.call(self, valuesObject, function(err) {
if (err) return cb(err);
createValues.call(self, valuesObject, cb, metaContainer);
}, metaContainer);
});
};
/**
* Process Values
*
* @param {Object} values
* @return {Object}
*/
function processValues(values) {
// Set Default Values if available
for (var key in this.attributes) {
if ((!hop(values, key) || values[key] === undefined) && hop(this.attributes[key], 'defaultsTo')) {
var defaultsTo = this.attributes[key].defaultsTo;
values[key] = typeof defaultsTo === 'function' ? defaultsTo.call(values) : _.clone(defaultsTo);
}
}
// Pull out any associations in the values
var _values = _.cloneDeep(values);
var associations = nestedOperations.valuesParser.call(this, this.identity, this.waterline.schema, values);
// Replace associated models with their foreign key values if available.
// Unless the association has a custom primary key (we want to create the object)
values = nestedOperations.reduceAssociations.call(this, this.identity, this.waterline.schema, values, 'create');
// Cast values to proper types (handle numbers as strings)
values = this._cast.run(values);
return { values: values, originalValues: _values, associations: associations };
}
/**
* Create BelongsTo Records
*
*/
function createBelongsTo(valuesObject, cb, metaContainer) {
var self = this;
async.each(valuesObject.associations.models, function(item, next) {
// Check if value is an object. If not don't try and create it.
if (!_.isPlainObject(valuesObject.values[item])) return next();
// Check for any transformations
var attrName = hop(self._transformer._transformations, item) ? self._transformer._transformations[item] : item;
var attribute = self._schema.schema[attrName];
var modelName;
Iif (hop(attribute, 'collection')) modelName = attribute.collection;
Eif (hop(attribute, 'model')) modelName = attribute.model;
Iif (!modelName) return next();
var model = self.waterline.collections[modelName];
var pkValue = valuesObject.originalValues[item][model.primaryKey];
var criteria = {};
criteria[model.primaryKey] = pkValue;
// If a pkValue if found, do a findOrCreate and look for a record matching the pk.
var query;
if (pkValue) {
query = model.findOrCreate(criteria, valuesObject.values[item]);
} else {
query = model.create(valuesObject.values[item]);
}
Iif(metaContainer) {
query.meta(metaContainer);
}
query.exec(function(err, val) {
Iif (err) return next(err);
// attach the new model's pk value to the original value's key
var pk = val[model.primaryKey];
valuesObject.values[item] = pk;
next();
});
}, cb);
}
/**
* Run Before* Lifecycle Callbacks
*
* @param {Object} valuesObject
* @param {Function} cb
*/
function beforeCallbacks(valuesObject, cb) {
var self = this;
async.series([
// Run Validation with Validation LifeCycle Callbacks
function(cb) {
callbacks.validate(self, valuesObject.values, false, cb);
},
// Before Create Lifecycle Callback
function(cb) {
callbacks.beforeCreate(self, valuesObject.values, cb);
}
], cb);
}
/**
* Create Parent Record and any associated values
*
* @param {Object} valuesObject
* @param {Function} cb
*/
function createValues(valuesObject, cb, metaContainer) {
var self = this;
var date;
// Automatically add updatedAt and createdAt (if enabled)
if (self.autoCreatedAt) {
Eif (!valuesObject.values[self.autoCreatedAt]) {
date = date || new Date();
valuesObject.values[self.autoCreatedAt] = date;
}
}
if (self.autoUpdatedAt) {
Eif (!valuesObject.values[self.autoUpdatedAt]) {
date = date || new Date();
valuesObject.values[self.autoUpdatedAt] = date;
}
}
// Transform Values
valuesObject.values = self._transformer.serialize(valuesObject.values);
// Clean attributes
valuesObject.values = self._schema.cleanValues(valuesObject.values);
// Pass to adapter here
self.adapter.create(valuesObject.values, function(err, values) {
if (err) {
Eif (typeof err === 'object') { err.model = self._model.globalId; }
return cb(err);
}
// Unserialize values
values = self._transformer.unserialize(values);
// If no associations were used, run after
if (valuesObject.associations.collections.length === 0) return after(values);
var parentModel = new self._model(values);
nestedOperations.create.call(self, parentModel, valuesObject.originalValues, valuesObject.associations.collections, function(err) {
Iif (err) return cb(err);
return after(parentModel.toObject());
});
function after(values) {
// Run After Create Callbacks
callbacks.afterCreate(self, values, function(err) {
Iif (err) return cb(err);
// Return an instance of Model
var model = new self._model(values);
cb(null, model);
});
}
}, metaContainer);
}
|