8000 proposal: eager loading support · Issue #91 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
proposal: eager loading support #91
Closed
@a8m

Description

@a8m

We open this task in order to get feedback for the generated-code API of eager loading.
The 3 requirements (maybe there are more) we thought about are:

  • Allow filter edges/relations (using .Where)
  • Get nested edges/relations
  • Limit/offset capabilities

A few examples here for what we thought about:

Add a With<EdgeName> method to the builder:

client.User.
  Query().
  Where(...).
  WithPets().                   // populate all user pets.
  WithGroups(group.HasAdmin()). // filter out groups without admin.
  All(ctx)

This example gets all users under a condition + their pets + their groups (with an admin).


What about getting all users + their groups (limit to X) + their admins (eager-loading with depth 2)?
I'm not sure how common this scenario, so I think either disable this, or add a With<Edge>Fn option for more flexible loading:

client.User.
  Query().
  Where(...).
  WithGroupsFn(func(q *ent.GroupQuery) {
    q.Where(...).Limit(X).WithAdmin()
  }).
  All(ctx)

Maybe we can change With<Edge> to behave like With<Edge>Fn and make the function to be an optional parameter.


The return type for eager loading:

We were thinking about the 2 options:

  • Add the schema edges as public fields in the generated struct (e.g. ent.User), and then populate the result into these fields.

  • Add another struct type (struct) for holding the schema edges. For example:

      type UserEdges struct {
         Pets []*Pet
         // ...
      }

    I'm not sure how this is supposed to handle nested eager-loading (User->Pets->Friends)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0