用于从 SQL 结构生成 Ent 结构的工具
entimport is a tool for creating Ent schemas from existing SQL databases. Currently, MySQL
and PostgreSQL are supported. The tool can import to ent schema any number of
tables, including relations between them.
If your project directory is outside GOPATH or you are not familiar with GOPATH, setup a Go module project as follows:
go mod init go install entgo.io/ent/cmd/entAfter installing ent codegen tool, you should have it in your PATH. If you don't find it your path, you can also
run: go run entgo.io/ent/cmd/ent
Go to the root directory of your project, and run:
ent initThe command above will create /ent/schema/ directory and the file inside /ent/generate.go
Installing and running entimport
go run ariga.io/entimport/cmd/entimportusers table:go run ariga.io/entimport/cmd/entimport -dsn "mysql://root:pass@tcp(localhost:3308)/test" -tables "users"The command above will write a valid ent schema into the directory specified (or the default ./ent/schema):
.
├── generate.go
└── schema
└── user.go
1 directory, 2 filesIn order to generate ent files from the produced schemas, run:
go run -mod=mod entgo.io/ent/cmd/ent generate ./schema
# OR `ent` init:
go generate ./entIf you are not yet familiar with ent, you can also follow
the quick start guide.
entimport -hUsage of ./entimport:
-dsn string
data source name (connection information), for example:
"mysql://user:pass@tcp(localhost:3306)/dbname"
"postgres://user:pass@host:port/dbname"
-schema-path string
output path for ent schema (default "./ent/schema")
-tables value
comma-separated list of tables to inspect (all if empty)Note: add search_path=foo if you use non
publicschema.
go run ariga.io/entimport/cmd/entimport -dsn "postgres://postgres:pass@localhost:5432/test?sslmode=disable" go run ariga.io/entimport/cmd/entimport -dsn "mysql://root:pass@tcp(localhost:3308)/test"Note: When importing specific tables:
if the table is a join table, you must also provide referenced tables.
If the table is only one part of a relation, the other part won't be imported unless specified.
If the-tablesflags is omitted all tables in currentdatabase schemawill be imported
go run ariga.io/entimport/cmd/entimport -dsn "..." -tables "users,user_friends" go run ariga.io/entimport/cmd/entimport -dsn "..." -schema-path "/some/path/here"uuid in Postgres).entimport.O2O Bidirectional and O2O Same Type - both will result in the same
ent schema.M2M Bidirectional and M2M Same Type - both will result in the same
ent schema.edge names will be prefixed with child_ & parent_.users with M2M relation to itself will result in:func (User) Edges() []ent.Edge {
return []ent.Edge{edge.To("child_users", User.Type), edge.From("parent_users", User.Type)}
}For discussion and support, open an issue or join our channel in the gophers Slack.
暂无开放 Issues,或尚未同步最近议题。