Skip to content

with relation queries should be able to manage in the model definition #572

@ozziest

Description

@ozziest

Problem

Currently, developers can define a hasMany() relations like this:

class Post extends Model {
  views() {
    return this.hasMany("PostView", "id", "post_id");
  }
}

We can fetch the views? with the following query:

GET /api/v1/posts?with=views

But, we are not able to add any condition that relationship queries. Axe API returns all data even though there are one million views. That's not useful.

Suggestion

Developers should be able to define a onBeforeQuery options for hasMany relationship definitions. By using that function, we can limit, or add more conditions as we wish.

Example:

const onBeforeViewQuery = async (req: AxeRequest, query: Knex.QueryBuilder) => {
  query.orderBy("id", "desc").limit(2);
}

class Post extends Model {
  views() {
    return this.hasMany("PostView", "id", "post_id", {
      onBeforeQuery: onBeforeViewQuery,
    });
  }
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestnext-releaseThe feature will be released in the next releaseversion-minorMinor version changes

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions