-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
302 lines (275 loc) · 10 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
'use strict';
var _ = require('lodash'),
cls = require('continuation-local-storage');
module.exports = function (Sequelize, options) {
options = _.defaults({
virtualFiltering: true,
advancedCreateUpdate: false
}, options);
if (options.advancedCreateUpdate) {
if (_.isUndefined(Sequelize.cls) || _.isNull(Sequelize.cls)) {
Sequelize.cls = cls.createNamespace('sequelize-internal');
}
}
function doUpsert(dataItem, association, associationModel, mainEntity) {
if (!_.isUndefined(dataItem.Value)) {
return associationModel.find({
where: { Value: dataItem.Value }
})
.then(function (subEntity) {
if (subEntity) {
return mainEntity[association.associationType === 'HasMany' ? association.accessors.add : association.accessors.set].call(mainEntity, subEntity);
} else {
return mainEntity[association.accessors.create].call(mainEntity, dataItem, {});
}
});
} else {
if (!_.isUndefined(dataItem.isNewRecord)) {
return mainEntity[association.associationType === 'HasMany' ? association.accessors.add : association.accessors.set].call(mainEntity, dataItem);
} else {
if (_.isUndefined(dataItem.id)) {
if (_.isNumber(dataItem)) {
return mainEntity[association.accessors.set].bind(mainEntity)(dataItem);
} else {
return mainEntity[association.accessors.create].bind(mainEntity)(dataItem);
}
} else {
return mainEntity[association.accessors.set].bind(mainEntity)(dataItem.id);
}
}
}
}
function doDelete(dataItem, association, associationModel) {
dataItem.set(association.identifierField, null);
var stillAlive = _(associationModel.attributes)
.filter(function (attribute) {
return !_.isUndefined(attribute.references);
})
.pluck('field')
.any(function (key) {
return !_.isNull(dataItem[key]);
});
if (stillAlive) {
return dataItem.save();
} else {
return dataItem.destroy();
}
}
function upsertSubEntities(mainEntity, associations, data) {
if (associations.length === 0) {
return Sequelize.Promise.resolve(mainEntity);
} else {
var promises = _(associations).map(function (associationKey) {
var associationData = data[associationKey],
association = this.associations[associationKey],
associationModel = association.target;
if (_.isArray(associationData)) {
var difference = _.filter(mainEntity[associationKey], function (item) {
return associationData.length === 0 ? true : _.any(associationData, function (item2) {
return item.Value !== item2.Value;
});
});
return Sequelize.Promise.join(Sequelize.Promise.all(_.map(associationData, function (dataItem) {
if (!_.isNull(dataItem)) {
return doUpsert(dataItem, association, associationModel, mainEntity);
}
})), Sequelize.Promise.all(_.map(difference, function (dataItem) {
return doDelete(dataItem, association, associationModel);
})));
} else if (!_.isNull(associationData) && !_.isUndefined(associationData)) {
return doUpsert(associationData, association, associationModel, mainEntity);
}
}, this) // jshint ignore:line
.flatten()
.value();
return Sequelize.Promise.all(promises)
.return(mainEntity);
}
}
function sortWhere (where, includes, virtuals, operator) {
var model = this; // jshint ignore:line
operator = operator || 'AND';
_.each(where, function (value, key) {
if (_.isArray(value)) {
var newOperator = 'AND';
if (where instanceof Sequelize.Utils.or) {
newOperator = 'OR';
}
_.each(value, function (innerWhere) {
sortWhere.bind(model)(innerWhere, includes, virtuals, newOperator);
});
} else if (key.indexOf('.') >= 0) {
delete where[key];
var parts = key.split('.'),
modelName = parts[0],
propertyName = parts[1];
if (_.isUndefined(includes[modelName])) {
includes[modelName] = {
as: modelName,
model: model.associations[modelName].target,
'AND': [],
'OR': [],
where: {}
};
}
var obj = {};
obj[propertyName] = value;
includes[modelName][operator].push(obj);
} else if (where instanceof Sequelize.Utils.where) {
// We don't want to touch theses.
return;
} else {
if (_.isUndefined(model.tableAttributes[key])) {
delete where[key];
virtuals[key] = value;
}
}
});
}
Sequelize.hook('afterInit', function (sequelize) {
sequelize.helpers = {
transaction: function (callback) {
var isInTransaction = !_.isUndefined(Sequelize.cls.get('transaction'));
if (isInTransaction && sequelize.getDialect() === 'sqlite') { // SQLite doesn't support nested transactions.
return callback();
} else {
return sequelize.transaction(function () {
return callback();
});
}
},
createomatic: function (data) {
var model = this;
return sequelize.helpers.transaction(function () {
return model.constructor.prototype.create.call(model, data)
.bind(model)
.then(function (entity) {
var associations = this.associations;
var associationsKeysToUpdate = _.intersection(_.keys(associations), _.keys(data));
return upsertSubEntities.call(this, entity, associationsKeysToUpdate, data);
})
.then(function (entity) {
return this.getById(entity.id);
});
});
},
updateomatic: function (id, data) {
var model = this;
return sequelize.helpers.transaction(function () {
return model.getById(id)
.bind(model)
.then(function (entity) {
var updatable = _.pick(data, _.keys(this.schema().tableAttributes));
entity.set(updatable);
var associationsKeysToUpdate = _.intersection(_.keys(this.associations), _.keys(data));
return upsertSubEntities.call(this, entity, associationsKeysToUpdate, data);
})
.then(function (entity) {
return entity.save();
})
.then(function (entity) {
return this.getById(entity.id);
});
});
}
};
if (_.isUndefined(sequelize.options.define.classMethods)) {
sequelize.options.define.classMethods = {};
}
if (options.advancedCreateUpdate) {
sequelize.options.define.classMethods.create = function () {
if (arguments.length === 1) {
return sequelize.helpers.createomatic.call(this, arguments[0]);
} else {
return this.constructor.prototype.create.apply(this, arguments);
}
};
sequelize.options.define.classMethods.update = function (id, data) {
var numId = Number(id);
if(_.isNumber(numId)) {
return sequelize.helpers.updateomatic.call(this, numId, data);
} else {
return this.constructor.prototype.update.apply(this, arguments);
}
};
sequelize.options.define.classMethods.getById = function (id) {
return this.find({
where: { id: id },
include: [{ all: true }]
});
};
}
sequelize.hook('beforeFind', function (options) {
var model = this;
if (options.where) {
var includes = {};
var virtuals = {};
sortWhere.bind(model)(options.where, includes, virtuals);
_.each(includes, function (include) {
include.where = Sequelize.and(Sequelize.or.apply(null, include.OR), Sequelize.and.apply(null, include.AND));
delete include.OR;
delete include.AND;
});
if (options.include) {
options.include = options.include.concat(_.map(includes, _.identity));
} else {
options.include = _.map(includes, _.identity);
}
options.virtuals = virtuals;
}
if (options.order) {
for (var i = options.order.length -1; i >= 0 ; i--) {
var attribute = options.order[i][0];
if (!model.tableAttributes[attribute]) {
if (_.isUndefined(options.virtualOrder)) {
options.virtualOrder = [];
}
options.virtualOrder.push(options.order[i]);
options.order.splice(i, 1);
}
}
}
});
sequelize.hook('afterFind', function (instances, options) {
function matcher (test, propertyName) {
var propertyToTest = instance[propertyName];
if (_.isString(test)) {
return propertyToTest.toLowerCase() === test.toLowerCase();
} else if (_.isNumber(test)) {
return propertyToTest === test;
} else if (_.isObject(test) && _.isString(test.like)) {
// Remove the beginning and ending %.
return _.contains(propertyToTest.toLowerCase(), test.like.toLowerCase().substring(1, test.like.length - 2));
} else {
throw new Error('Cannot filter on virtual property using this method.');
}
}
if (options.virtuals) {
if (_.isArray(instances)) {
for (var i = instances.length -1; i >= 0 ; i--) {
var instance = instances[i];
var match = _.all(options.virtuals, matcher);
if (!match) {
instances.splice(i, 1);
}
}
}
}
if (options.virtualOrder && _.isArray(instances)) {
var newInstances = instances.splice(0, instances.length);
_(newInstances)
.sortByOrder(
_.map(options.virtualOrder, _.first),
_.map(options.virtualOrder,
function (dir) {
return _.last(dir) === 'ASC';
})
)
.each(function (item) {
instances.push(item);
})
.value();
}
});
});
};