Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: embed adapter to local ent package #25

Closed
wants to merge 5 commits into from

Conversation

MuZhou233
Copy link
Contributor

@MuZhou233 MuZhou233 commented Jul 30, 2024

BREAKING
DRAFT: This provides a basic idea of fixing #20

What changed

  • Update ent version (update go version as a side-effect)
  • Refactor ent/schema/casbinrule.go to mixin.go
  • Refactor adapter.go to casbin.tmpl
  • Move ent to examples/ent
  • Re-run generate

How does it work

This idea is based on two ent features: Schema Mixin and Template

Mixin allows us embed adapter schema definition to ent package

import "github.com/casbin/ent-adapter"

type CasbinRule struct {
	ent.Schema
}

func (CasbinRule) Mixin() []ent.Mixin {
	return []ent.Mixin{
		template.CasbinRuleMixin{},
	}
}

Template allows us embed adapter logic to ent package

import "github.com/casbin/ent-adapter"

entc.Generate("./schema",
	&gen.Config{},
	entc.Extensions(&template.CasbinExtension{}),
)

By doing this, the adapter is embedded/injected to local ent client. So that we can have fully control of it including implement custom logic in a single transaction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants