The project comes with a codegen tool called
ent. In order to install
ent run the following command:
In order to generate one or more schema templates, run
ent init as follows:
init will create the 2 schemas (
pet.go) under the
ent/schema directory. If the
ent directory does not exist, it will create it as well. The convention is to have an
ent directory under the root directory of the project.
generate command generates the following assets for the schemas:
Txobjects used for interacting with the graph.
- CRUD builders for each schema type. See CRUD for more info.
- Entity object (Go struct) for each of the schema types.
- Package containing constants and predicates used for interacting with the builders.
migratepackage for SQL dialects. See Migration for more info.
Version Compatibility Between
When working with
ent CLI in a project, you want to make sure the version being used by the CLI is identical to the
ent version used by your project.
One of the options for achieving this is asking
go generate to use the version mentioned in the
go.mod file when running
ent. If your project does not use Go modules, setup one as follows:
And then, re-run the following command in order to add
ent to your
generate.go file to your project under
Finally, you can run
go generate ./ent from the root directory of your project in order to run
ent code generation on your project schemas.
For more info about codegen options, run
ent generate -h:
ent can generate assets for both SQL and Gremlin dialect. The default dialect is SQL.
ent accepts external Go templates to execute. If the template name already defined by
ent, it will override the existing one. Otherwise, it will write the execution output to a file with the same name as the template. The flag format supports
glob as follows:
More information and examples can be found in the external templates doc.
entc As A Package#
Another option for running
ent CLI is to use it as a package as follows:
The full example exists in GitHub.
In order to get a description of your graph schema, run:
An example for the output is as follows:
entc package provides an option to add a list of hooks (middlewares) to the code-generation phase. This option is ideal for adding custom validators for the schema, or for generating additional assets using the graph schema.
entc package provides a collection of code-generation features that be added or removed using flags.
For more information, please see the features-flags page.