Skip to content

Commit 07c3263

Browse files
committed
feat: add resource param to enforce query limit
1 parent 71a64f1 commit 07c3263

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/mongo/operation/query.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ export class QueryMongoOperation extends MongoOperation {
9696
if (typeof job.req.query.limit !== 'undefined') {
9797
job.opts.limit = job.req.query.limit;
9898
}
99+
if ((this.resource.info.queryLimit || 0) > 0 && !(job.opts.limit < this.resource.info.queryLimit!)) {
100+
job.opts.limit = this.resource.info.queryLimit;
101+
}
99102
if (typeof job.req.query.skip !== 'undefined') {
100103
job.opts.skip = job.req.query.skip;
101104
}

src/mongo/resource.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface MongoResourceDefinition extends ResourceDefinition {
3737
idIsObjectId?: boolean;
3838
createIndexes?: boolean;
3939
escapeProperties?: boolean;
40+
queryLimit?: number;
4041
}
4142

4243
export class MongoResource extends Resource {

test/ts/mongo.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ describe('mongo', function () {
215215
before(async function () {
216216
db = await MongoClient.connect('mongodb://localhost:27017/local').then((c) => c.db());
217217
r1 = new MongoResource(db, { name: 'Test', collection: collectionName });
218-
r2 = new MongoResource(db, { name: 'Other', collection: collectionName, id: 'myid', idIsObjectId: false });
218+
r2 = new MongoResource(db, { name: 'Other', collection: collectionName, id: 'myid', idIsObjectId: false, queryLimit: 5 });
219219
r3 = new MongoResource(db, { name: 'Fake', collection: collectionName }, { '/1': { get: FakeOp1 }, '/2': { get: FakeOp2 } });
220220
r4 = new MongoResource(db, { name: 'Oid', collection: collectionName + '_oid', id: 'myoid', idIsObjectId: true });
221221
r5 = new MongoResource(db, { name: 'Aggregation', collection: collectionName }, { '/': { get: Aggregation } });
@@ -350,7 +350,6 @@ describe('mongo', function () {
350350
});
351351

352352
it('should create a record with a date preserving its type', function () {
353-
debugger;
354353
return request
355354
.post('/dates')
356355
.send({ myid: 'ts', ts: new Date() })
@@ -716,6 +715,39 @@ describe('mongo', function () {
716715
});
717716
});
718717

718+
it('should return all objects in the collection (6), up to hardcoded limit', function () {
719+
return request
720+
.get('/others')
721+
.expect(200)
722+
.expect('Content-Type', /json/)
723+
.expect('Results-Matching', '7')
724+
.then(({ body: data }) => {
725+
data.length.should.equal(5);
726+
});
727+
});
728+
729+
it('should return all objects in the collection (7), up to the lower of requested and hardcoded limits', function () {
730+
return request
731+
.get('/others?limit=6')
732+
.expect(200)
733+
.expect('Content-Type', /json/)
734+
.expect('Results-Matching', '7')
735+
.then(({ body: data }) => {
736+
data.length.should.equal(5);
737+
});
738+
});
739+
740+
it('should return all objects in the collection (8), up to the lower of requested and hardcoded limits', function () {
741+
return request
742+
.get('/others?limit=4')
743+
.expect(200)
744+
.expect('Content-Type', /json/)
745+
.expect('Results-Matching', '7')
746+
.then(({ body: data }) => {
747+
data.length.should.equal(4);
748+
});
749+
});
750+
719751
it('should return a single attribute of all objects, in ascending order by id (1)', function () {
720752
return request
721753
.get('/tests?fields=y&sort=myid')

0 commit comments

Comments
 (0)