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

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