Skip to content

Commit

Permalink
Fix bug loopbackio#1795 Count issues related models
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Nov 30, 2019
1 parent 022029d commit bdf4d87
Show file tree
Hide file tree
Showing 2 changed files with 293 additions and 6 deletions.
39 changes: 33 additions & 6 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
where: where,
},
};
where = {};
scopeOnRelatedModel = true;
relatedModel = targetModel.relations[queryRelated.relation].modelTo;
IdKey = idName(relatedModel);
keyFrom = targetModel.relations[queryRelated.relation].keyFrom || IdKey;
fieldsRelated = [keyFrom];
if (where[IdKey]) {
where = {
[keyFrom]: where[IdKey],
};
} else {
where = {};
}
}
const scoped = (this._scope && this._scope.where) || {};
const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated});
Expand Down Expand Up @@ -534,12 +540,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
where: where,
},
};
where = {};
scopeOnRelatedModel = true;
relatedModel = targetModel.relations[queryRelated.relation].modelTo;
IdKey = idName(relatedModel);
keyFrom = targetModel.relations[queryRelated.relation].keyFrom || IdKey;
fieldsRelated = [keyFrom];
if (where[IdKey]) {
where = {
[keyFrom]: where[IdKey],
};
} else {
where = {};
}
}
const scoped = (this._scope && this._scope.where) || {};
const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated});
Expand Down Expand Up @@ -648,9 +660,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
relatedModel = targetModel.relations[queryRelated.relation].modelTo;
IdKey = idName(relatedModel);
keyFrom = targetModel.relations[queryRelated.relation].keyFrom || IdKey;
filter = {
fields: [keyFrom],
};
if (filter.where[IdKey]) {
filter = {
fields: [keyFrom],
where: {
[keyFrom]: filter.where [IdKey],
},
};
} else {
filter = {
fields: [keyFrom],
};
}
}
const scoped = (this._scope && this._scope.where) || {};
filter = mergeQuery({where: scoped}, filter || {});
Expand Down Expand Up @@ -727,12 +748,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
where: where,
},
};
where = {};
scopeOnRelatedModel = true;
relatedModel = targetModel.relations[queryRelated.relation].modelTo;
IdKey = idName(relatedModel);
keyFrom = targetModel.relations[queryRelated.relation].keyFrom || IdKey;
fieldsRelated = [keyFrom];
if (where[IdKey]) {
where = {
[keyFrom]: where[IdKey],
};
} else {
where = {};
}
}
const scoped = (this._scope && this._scope.where) || {};
const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated});
Expand Down
260 changes: 260 additions & 0 deletions test/relations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,64 @@ describe('relations', function() {
}
});

it('should count scoped record with promises based on related model id', function(done) {
let id;
Physician.create()
.then(function(physician) {
return physician.patients.create({name: 'a'})
.then(function(ch) {
id = ch.id;
return physician.patients.create({name: 'z'});
})
.then(function() {
return physician.patients.create({name: 'c'});
})
.then(function() {
return verify(physician);
});
}).catch(done);

function verify(physician) {
return physician.patients.count({
id: id,
}, function(err, count) {
if (err) return done(err);
count.should.equal(1);
done();
});
}
});

it('should count scoped record with promises based on related model id list', function(done) {
let id;
Physician.create()
.then(function(physician) {
return physician.patients.create({name: 'a'})
.then(function(ch) {
id = ch.id;
return physician.patients.create({name: 'z'});
})
.then(function() {
return physician.patients.create({name: 'c'});
})
.then(function() {
return verify(physician);
});
}).catch(done);

function verify(physician) {
return physician.patients.count({
id: {
inq: [id],
},
}, function(err, count) {
if (err) return done(err);
count.should.equal(1);
done();
});
}
});

it('should find one scoped record with promises based on related model properties', function(done) {
let id;
Physician.create()
Expand Down Expand Up @@ -664,6 +722,70 @@ describe('relations', function() {
}
});

it('should find one scoped record with promises based on related model id', function(done) {
let id;
Physician.create()
.then(function(physician) {
return physician.patients.create({name: 'a'})
.then(function(ch) {
id = ch.id;
return physician.patients.create({name: 'z'});
})
.then(function() {
return physician.patients.create({name: 'c'});
})
.then(function() {
return verify(physician);
});
}).catch(done);

function verify(physician) {
return physician.patients.findOne({
where: {
id,
},
}, function(err, patient) {
if (err) return done(err);
should.exist(patient);
patient.name.should.equal('a');
done();
});
}
});

it('should find one scoped record with promises based on related model id list', function(done) {
let id;
Physician.create()
.then(function(physician) {
return physician.patients.create({name: 'a'})
.then(function(ch) {
id = ch.id;
return physician.patients.create({name: 'z'});
})
.then(function() {
return physician.patients.create({name: 'c'});
})
.then(function() {
return verify(physician);
});
}).catch(done);

function verify(physician) {
return physician.patients.findOne({
where: {
id: {
inq: [id],
},
},
}, function(err, patient) {
if (err) return done(err);
should.exist(patient);
patient.name.should.equal('a');
done();
});
}
});

it('should update all scoped record with promises based on related model properties', function(done) {
let id;
Physician.create()
Expand Down Expand Up @@ -702,6 +824,84 @@ describe('relations', function() {
}
});

it('should update all scoped record with promises based on related model id', function(done) {
let id;
Physician.create()
.then(function(physician) {
return physician.patients.create({name: 'a'})
.then(function(ch) {
id = ch.id;
return physician.patients.create({name: 'z'});
})
.then(function() {
return physician.patients.create({name: 'c'});
})
.then(function() {
return verify(physician);
});
}).catch(done);

function verify(physician) {
return physician.patients.updateAll({
id,
}, {age: 5}, function(err, result) {
if (err) return done(err);
should.exist(result);
result.count.should.equal(1);
physician.patients.findOne({
where: {
name: 'a',
},
}, function(err, patient) {
if (err) return done(err);
should.exist(patient);
patient.age.should.equal(5);
done();
});
});
}
});

it('should update all scoped record with promises based on related model id list', function(done) {
let id;
Physician.create()
.then(function(physician) {
return physician.patients.create({name: 'a'})
.then(function(ch) {
id = ch.id;
return physician.patients.create({name: 'z'});
})
.then(function() {
return physician.patients.create({name: 'c'});
})
.then(function() {
return verify(physician);
});
}).catch(done);

function verify(physician) {
return physician.patients.updateAll({
id: {
inq: [id],
},
}, {age: 5}, function(err, result) {
if (err) return done(err);
should.exist(result);
result.count.should.equal(1);
physician.patients.findOne({
where: {
name: 'a',
},
}, function(err, patient) {
if (err) return done(err);
should.exist(patient);
patient.age.should.equal(5);
done();
});
});
}
});

it('should destroyAll all scoped record with promises based on related model properties', function(done) {
let id;
Physician.create()
Expand Down Expand Up @@ -731,6 +931,66 @@ describe('relations', function() {
}
});

it('should destroyAll all scoped record with promises based on related model id', function(done) {
let id;
Physician.create()
.then(function(physician) {
return physician.patients.create({name: 'a'})
.then(function(ch) {
id = ch.id;
return physician.patients.create({name: 'z'});
})
.then(function() {
return physician.patients.create({name: 'c'});
})
.then(function() {
return verify(physician);
});
}).catch(done);

function verify(physician) {
return physician.patients.destroyAll({
id,
}, function(err, result) {
if (err) return done(err);
should.exist(result);
result.count.should.equal(1);
done();
});
}
});

it('should destroyAll all scoped record with promises based on related model id list', function(done) {
let id;
Physician.create()
.then(function(physician) {
return physician.patients.create({name: 'a'})
.then(function(ch) {
id = ch.id;
return physician.patients.create({name: 'z'});
})
.then(function() {
return physician.patients.create({name: 'c'});
})
.then(function() {
return verify(physician);
});
}).catch(done);

function verify(physician) {
return physician.patients.destroyAll({
id: {
inq: [id],
},
}, function(err, result) {
if (err) return done(err);
should.exist(result);
result.count.should.equal(1);
done();
});
}
});

it('should build record on scope', function(done) {
Physician.create(function(err, physician) {
const patient = physician.patients.build();
Expand Down

0 comments on commit bdf4d87

Please sign in to comment.