lolopinto / ent

MIT License
52 stars 6 forks source link

polymorphic edge support #503

Open lolopinto opened 3 years ago

lolopinto commented 3 years ago

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 commented 3 years ago

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

need full polymorphic edge support in the future

rvlasveld commented 2 years ago

+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:

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?

lolopinto commented 2 years ago

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:

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;
Swahvay commented 9 months ago

Is this fixed?