ent icon indicating copy to clipboard operation
ent copied to clipboard

polymorphic edge support

Open lolopinto opened this issue 3 years ago • 4 comments

e.g. an edge that's shared across different types

probably implemented with pattern support in edges and a way to reuse the same edge across things

lolopinto avatar Sep 16 '21 18:09 lolopinto

https://github.com/lolopinto/ent/pull/506 and https://github.com/lolopinto/ent/pull/508

need full polymorphic edge support in the future

lolopinto avatar Sep 22 '21 17:09 lolopinto

+1 on this issue.

I would love to see this implemented! It seems that this is required to model the following - or is there an alternative?

What I would like to model:

  • We have an User and Bot Schemas
  • Both can own one-or-many Account objects.
  • An Account must be owned by exactly one User or Bot (making the "owner" polymorphic)

Is it possible to model this with the current capabilities?

Alternatively, should the PolymorphicOptions.types perhaps allow to reference a Pattern, such as AccountOwner, instead of a (list of) Schema?

rvlasveld avatar Oct 18 '22 11:10 rvlasveld

you can kinda do this with patterns

I just checked in an example:

Pattern: https://github.com/lolopinto/ent/blob/c02c679fdab2d4f56967add1c81bd5b35b84b30e/examples/todo-sqlite/src/schema/patterns/todo_pattern.ts#L3-L17

used by Account/Workspace: https://github.com/lolopinto/ent/blob/c02c679fdab2d4f56967add1c81bd5b35b84b30e/examples/todo-sqlite/src/schema/account_schema.ts#L9

https://github.com/lolopinto/ent/blob/c02c679fdab2d4f56967add1c81bd5b35b84b30e/examples/todo-sqlite/src/schema/workspace_schema.ts#L7

and in a trigger, set the edge https://github.com/lolopinto/ent/blob/c02c679fdab2d4f56967add1c81bd5b35b84b30e/examples/todo-sqlite/src/ent/todo/actions/create_todo_action.ts#L20-L25

downsides:

  • the roundabout pattern and all the extra codegen
  • can technically have more than 1-edge but we don't currently support this anyways on an inverse edge and don't support it well on a 1-way edge
    • want to have 1-many, many-many used to support this in the future but not there yet

in your case:

export class AccountContainer implements Pattern {
  name = "account_container";
  fields: {};
  edges: Edge[] = [
    {
      name: "accountsOwned",
      schemaName: "Account",
      inverseEdge: { name: "accountOwner" },
    },
  ];
}
const UserSchema = new EntSchema({
  patterns: [new AccountContainer()],
//  ...
});
export default UserSchema;
const BotSchema = new EntSchema({
  patterns: [new AccountContainer()],
// ...
});
export default BotSchema;

lolopinto avatar Oct 18 '22 21:10 lolopinto

Is this fixed?

Swahvay avatar Feb 01 '24 20:02 Swahvay