Creating a New Schema
Workflow of generating a new schema and related CRUD resolvers
Create a new schema file
task db:newschema -- MyNewSchema
Update new templated schema
The file will be located in internal/ent/schema/, the file name matching the schema name
- Add fields to the schema
- Add edges to the schema for 1:1, 1:m, or m:m relationships with other objects
- Add indexes to the schema
Example
A common use in our codebase is to add an index to allow soft-deleted names to be reused even when the name is required to be unique.
index.Fields("name").
Unique().Annotations(entsql.IndexWhere("deleted_at is NULL")), - Review and update the annotations on the templated schema
- Add hooks for use on mutations, these act as middleware that can happen before or after the request is executed
- Add interceptors for use on queries, these act as middleware that can happen before or after the request is executed, commonly used to filter data
- Review and update the privacy policy on the templated schema
Generate the CRUD resolvers
task generate
This will take a few minutes to run the first time, the main generation that will happen:
- entc - ORM functions for the schema, for example your getters and setters for fields, creating, deleting or updating objects in the database, etc.
- gqlgen - graphapi resolvers and basic query generation
- gqlgenc - golang api client based on queries
Generate the Migrations
This will generate the goose and atlas migrations based on the ent schema changes
task db:create
Stub out the CRUD cli commands
task cli:generate
info
The cli flags for create and update, along with field output (in root.go) will need to be manually updated