Skip to content

Commit

Permalink
feat: limit relations
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewPattell committed Mar 16, 2023
1 parent 96c700a commit 6a9a515
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions __tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,21 @@ describe('services/typeorm-json-query', () => {
expect(rel[0].alias).to.equal('myRelation');
});

it('should throw error: reached maximum relation', () => {
const instance = TypeormJsonQuery.init<TestEntity>(
{
queryBuilder,
// @ts-ignore
query: { relations: ['testRelation', { name: 'testRelation2' }] },
},
{ maxRelations: 1 },
);

expect(() => instance.getRelations()).to.throw(
'Invalid json query: reached maximum relations (1).',
);
});

it('should success return relations', () => {
const relations = commonInstance.getRelations();

Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ITypeormJsonQueryOptions {
defaultPageSize: number;
// 0 - disable (query all items), 200 - default value
maxPageSize: number;
maxRelations: number;
// level1.level2.level3.etc...
maxDeepRelation: number;
// { and: [{ and: [{ and: [] }]}] } deep condition level
Expand Down Expand Up @@ -102,6 +103,7 @@ class TypeormJsonQuery<TEntity = ObjectLiteral> {
private readonly options: ITypeormJsonQueryOptions = {
defaultPageSize: 25,
maxPageSize: 100,
maxRelations: 10,
maxDeepRelation: 4,
maxDeepWhere: 5,
defaultRelationPageSize: 50,
Expand Down Expand Up @@ -384,7 +386,11 @@ class TypeormJsonQuery<TEntity = ObjectLiteral> {
* Get query relations
*/
public getRelations(relations?: IJsonQuery<TEntity>['relations']): IJsonRelationResult[] {
const { maxDeepRelation, isDisableRelations, relationOptions } = this.options;
const { maxRelations, maxDeepRelation, isDisableRelations, relationOptions } = this.options;

if ((this.query.relations?.length ?? 0) > maxRelations) {
throw new Error(`Invalid json query: reached maximum relations (${maxRelations}).`);
}

const mapRelationOptions = relationOptions!.reduce((res, rel) => {
const {
Expand Down

0 comments on commit 6a9a515

Please sign in to comment.