Skip to main content

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

  1. Add fields to the schema
  2. Add edges to the schema for 1:1, 1:m, or m:m relationships with other objects
  3. 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")),
  4. Review and update the annotations on the templated schema
  5. Add hooks for use on mutations, these act as middleware that can happen before or after the request is executed
  6. 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
  7. 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:

  1. entc - ORM functions for the schema, for example your getters and setters for fields, creating, deleting or updating objects in the database, etc.
  2. gqlgen - graphapi resolvers and basic query generation
  3. 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