Skip to content

Commit

Permalink
Merge pull request #104 from SphericalElephant/94_support_pagination_…
Browse files Browse the repository at this point in the history
…for_relationship_get

added pagination to HasMany and BelongsToMany queries
  • Loading branch information
siyb committed Apr 21, 2021
2 parents f097828 + 78f1773 commit 5e8ab8f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Added proper model extension for Sequelize 6.x.x
- Added pagination support to hasMany and belongsToMany association queries

### Changed
- Restructured code, moved several methods to the model extension
Expand Down
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,16 +359,17 @@ module.exports = (models, opts) => {
};
// TODO: move into own file (maybe with update)
const relationshipGet = (postProcess) => {
return (req, res, next) => {
source.findByPk(req.params.id).then(sourceInstance => {
if (!sourceInstance) return createErrorPromise(404, 'source not found.');
return sourceInstance[association.accessors.get]().then(targetInstance => {
if (!targetInstance) return createErrorPromise(404, 'target not found.');
return res.replyHandler(next, 200, postProcess(req, targetInstance));
});
}).catch(err => {
return async (req, res, next) => {
try {
const sourceInstance = await source.findByPk(req.params.id);
if (!sourceInstance) throw createError(404, 'source not found.');
const query = queryBuilder.create(req.query).prepare().query;
const targetInstance = await sourceInstance[association.accessors.get](query);
if (!targetInstance) throw createError(404, 'target not found.');
return res.replyHandler(next, 200, postProcess(req, targetInstance));
} catch (err) {
return res.errorHandler(next, err);
});
}
};
};

Expand Down
38 changes: 34 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,13 @@ module.exports = (Sequelize) => {
fieldName: 'test'
});
const toTest = {};
middleware(toTest, {}, () => {});
middleware(toTest, {}, () => { });
expect(toTest['test']).to.not.be.null;
});
it('should use a default name if no fieldName was specified', () => {
const middleware = associationMiddleware([TestModel]);
const toTest = {};
middleware(toTest, {}, () => {});
middleware(toTest, {}, () => { });
expect(toTest['associationInformation']).to.not.be.null;
});
});
Expand Down Expand Up @@ -622,7 +622,7 @@ module.exports = (Sequelize) => {
exseqResult.forEach(routingInformation => {
const middleware = getAssocMiddleware(routingInformation)[0].handle;
const toCheck = {};
middleware(toCheck, null, () => {});
middleware(toCheck, null, () => { });
expect(toCheck['associationInformation']).to.exist;
});
});
Expand Down Expand Up @@ -1608,7 +1608,7 @@ module.exports = (Sequelize) => {
});
});
});
describe('/model/:id/hasOneRleation/ POST', () => {
describe('/model/:id/hasOneRelation/ POST', () => {
it('should create the hasOne relation of the resource', () => {
return TestModel.findOne({where: {value1: 'addrelationTestModel'}}).then(testModelInstance => {
return request(app)
Expand Down Expand Up @@ -1751,6 +1751,36 @@ module.exports = (Sequelize) => {
expect(response.body.result.sort(sortById)[0].name).to.equal(`${manyRelation.association.associationType}-child1`);
});
});
it(`${manyRelation.source.name} ${manyRelation.target.name} should return the paginated ${manyRelation.association.associationType} relations of the requested resource.`, async () => {
// test the first page
await request(app)
.get(`/${manyRelation.source.name}/1/${manyRelation.association.options.name.singular}/?i=2&p=0`)
.expect(200)
.then(response => {
expect(response.body.result).to.have.lengthOf(2);
expect(response.body.result.sort(sortById)[0].name).to.equal(`${manyRelation.association.associationType}-child1`);
expect(response.body.result.sort(sortById)[1].name).to.equal(`${manyRelation.association.associationType}-child2`);
});
// test the second page
await request(app)
.get(`/${manyRelation.source.name}/1/${manyRelation.association.options.name.singular}/?i=2&p=1`)
.expect(200)
.then(response => {
expect(response.body.result).to.have.lengthOf(1);
expect(response.body.result.sort(sortById)[0].name).to.equal(`${manyRelation.association.associationType}-child3`);
});
});
it(`${manyRelation.source.name} ${manyRelation.target.name} should return the paginated and sorted ${manyRelation.association.associationType} relations of the requested resource.`, async () => {
// test the first page
await request(app)
.get(`/${manyRelation.source.name}/1/${manyRelation.association.options.name.singular}/?i=2&p=0&f=name&o=DESC`)
.expect(200)
.then(response => {
expect(response.body.result).to.have.lengthOf(2);
expect(response.body.result[0].name).to.equal(`${manyRelation.association.associationType}-child3`);
expect(response.body.result[1].name).to.equal(`${manyRelation.association.associationType}-child2`);
});
});
it('should only show attributes that have been specified.', () => {
return request(app)
.get(`/${manyRelation.source.name}/1/${manyRelation.association.options.name.singular}/?a=value`)
Expand Down

0 comments on commit 5e8ab8f

Please sign in to comment.