entx
Extensions for ent such as polymorphic relations, cascade delete and per-API GraphQL schemas.
go get github.com/ubgo/entx/entpoly
- Language
- Go
- Go version
- –
- Core deps
- –
- Latest tag
- 6 streams
- License
- Apache-2.0
- Go modules
- 10
How it fits together
Code from the README
// One annotation on the schema
func (User) Annotations() []schema.Annotation {
return []schema.Annotation{
entcascade.Cascade(),
}
}// Schema annotation — expose Chatbot in dashboard API (full CRUD) and public API (read-only)
func (Chatbot) Annotations() []schema.Annotation {
return []schema.Annotation{
entgqlmulti.ApiConfig(map[string][]entgqlmulti.ApiTarget{
"apidash": {{Query: true, Mutations: true, Filters: true}},
"apipub": {{TypeName: "PublicBot", Fields: []string{"name", "avatar"}, Query: true}},
}),
}
}// Comment can attach to any AllowedTypes parent (Post, Video, Image, …)
func (Comment) Mixin() []ent.Mixin {
return []ent.Mixin{entpoly.MorphMixin("commentable", entpoly.MixinAllowed(Post.Type, Video.Type))}
}
func (Comment) Edges() []ent.Edge {
return []ent.Edge{
entpoly.MorphTo("commentable", Post.Type, Video.Type).
Required(). // hook rejects unset / cleared writes
Touch(). // bumps parent.updated_at on save
Cascade(). // deletes children when parent dies
SoftDelete(). // filters soft-deleted parents on read
GQL(), // emits union Commentable = Post | Video for gqlgen
}
}// app's projection of authmgr's auth_users — read it, never write it
func (User) Annotations() []schema.Annotation {
return []schema.Annotation{
entsql.Annotation{Table: "auth_users"},
entreadonly.ReadOnly(), // app.User.Create() etc. won't even exist
}
}From the entx README.
At a glance (from the README)
| Package | Description |
|---|---|
| entcascade | Generate cascade delete functions from schema annotations |
| entgqlmulti | Generate per-API GraphQL schemas from a single ent schema |
| entpoly | Laravel-style polymorphic relationships — MorphTo / MorphOne / MorphMany / MorphedByMany with compile-time + DB-level type safety + GraphQL union surface |
| entreadonly | Make a schema read-only at codegen — strips Create/Update/Delete builders so writes fail to compile, while edges + queries + GraphQL stay intact |
| entskiptable | Exclude externally-owned tables from ent auto-migration via a composable migration DiffHook (no DDL on tables you only read) |
| enttui | Schema-driven terminal UI / admin browser for ent — a k9s-style keyboard-driven data explorer generated straight from your schema |