不要怂,就是干,撸起袖子干!

Commit 3e85d250 by Jan Aagaard Meier

🔥 Remove hasSingle in hasmany and belongstomany. Aggregated in hasMultiple

1 parent d965119a
...@@ -360,20 +360,23 @@ module.exports = (function() { ...@@ -360,20 +360,23 @@ module.exports = (function() {
return model.findAll(options, queryOptions); return model.findAll(options, queryOptions);
}; };
obj[this.accessors.hasAll] = function(instances, options) { obj[this.accessors.hasSingle] = obj[this.accessors.hasAll] = function(instances, options) {
var instance = this var where = { $or: [] };
, where;
if (!Array.isArray(instances)) {
instances = [instances];
}
options = options || {}; options = options || {};
options.scope = false; options.scope = false;
instances.forEach(function(instance) { instances.forEach(function(instance) {
if (instance instanceof association.target.Instance) { if (instance instanceof association.target.Instance) {
where = { $or: [where, instance.primaryKeyValues]}; where.$or.push(instance.primaryKeyValues);
} else { } else {
var _where = {}; var _where = {};
_where[association.target.primaryKeyAttribute] = instance; _where[association.target.primaryKeyAttribute] = instance;
where = { $or: [where, _where]}; where.$or.push(_where);
} }
}); });
...@@ -384,7 +387,7 @@ module.exports = (function() { ...@@ -384,7 +387,7 @@ module.exports = (function() {
] ]
}; };
return instance[association.accessors.get]( return this[association.accessors.get](
options, options,
{ raw: true } { raw: true }
).then(function(associatedObjects) { ).then(function(associatedObjects) {
...@@ -392,34 +395,6 @@ module.exports = (function() { ...@@ -392,34 +395,6 @@ module.exports = (function() {
}); });
}; };
obj[this.accessors.hasSingle] = function(param, options) {
var instance = this
, where;
options = options || {};
options.scope = false;
if (param instanceof association.target.Instance) {
where = param.primaryKeyValues;
} else {
where = {};
where[association.target.primaryKeyAttribute] = param;
}
options.where = {
$and: [
where,
options.where
]
};
return instance[association.accessors.get](
options,
{ raw: true }
).then(function(associatedObjects) {
return associatedObjects.length !== 0;
});
};
return this; return this;
}; };
......
...@@ -340,20 +340,23 @@ module.exports = (function() { ...@@ -340,20 +340,23 @@ module.exports = (function() {
return new Class(association, this).injectGetter(options, queryOptions); return new Class(association, this).injectGetter(options, queryOptions);
}; };
obj[this.accessors.hasAll] = function(instances, options) { obj[this.accessors.hasSingle] = obj[this.accessors.hasAll] = function(instances, options) {
var instance = this var where = { $or: [] };
, where;
if (!Array.isArray(instances)) {
instances = [instances];
}
options = options || {}; options = options || {};
options.scope = false; options.scope = false;
instances.forEach(function(instance) { instances.forEach(function(instance) {
if (instance instanceof association.target.Instance) { if (instance instanceof association.target.Instance) {
where = { $or: [where, instance.primaryKeyValues]}; where.$or.push(instance.primaryKeyValues);
} else { } else {
var _where = {}; var _where = {};
_where[association.target.primaryKeyAttribute] = instance; _where[association.target.primaryKeyAttribute] = instance;
where = { $or: [where, _where]}; where.$or.push(_where);
} }
}); });
...@@ -364,7 +367,7 @@ module.exports = (function() { ...@@ -364,7 +367,7 @@ module.exports = (function() {
] ]
}; };
return instance[association.accessors.get]( return this[association.accessors.get](
options, options,
{ raw: true } { raw: true }
).then(function(associatedObjects) { ).then(function(associatedObjects) {
...@@ -372,34 +375,6 @@ module.exports = (function() { ...@@ -372,34 +375,6 @@ module.exports = (function() {
}); });
}; };
obj[this.accessors.hasSingle] = function(param, options) {
var instance = this
, where;
options = options || {};
options.scope = false;
if (param instanceof association.target.Instance) {
where = param.primaryKeyValues;
} else {
where = {};
where[association.target.primaryKeyAttribute] = param;
}
options.where = {
$and: [
where,
options.where
]
};
return instance[association.accessors.get](
options,
{ raw: true }
).then(function(associatedObjects) {
return associatedObjects.length !== 0;
});
};
return this; return this;
}; };
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!