ent
ent copied to clipboard
polymorphic edge support
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
https://github.com/lolopinto/ent/pull/506 and https://github.com/lolopinto/ent/pull/508
need full polymorphic edge support in the future
+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
andBot
Schemas - Both can own one-or-many
Account
objects. - An
Account
must be owned by exactly oneUser
orBot
(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
?
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;
Is this fixed?