Quick Introduction

ent is a simple, yet powerful entity framework for Go, that makes it easy to build and maintain applications with large data-models and sticks with the following principles:

  • Easily model database schema as a graph structure.
  • Define schema as a programmatic Go code.
  • Static typing based on code generation.
  • Database queries and graph traversals are easy to write.
  • Simple to extend and customize using Go templates.

gopher-schema-as-code

Setup A Go Environment

If your project directory is outside GOPATH or you are not familiar with GOPATH, setup a Go module project as follows:

  1. go mod init <project>

Installation

  1. go get -d entgo.io/ent/cmd/ent

After 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 <command>

Create Your First Schema

Go to the root directory of your project, and run:

  1. go run entgo.io/ent/cmd/ent init User

The command above will generate the schema for User under <project>/ent/schema/ directory:

<project>/ent/schema/user.go

  1. package schema
  2. import "entgo.io/ent"
  3. // User holds the schema definition for the User entity.
  4. type User struct {
  5. ent.Schema
  6. }
  7. // Fields of the User.
  8. func (User) Fields() []ent.Field {
  9. return nil
  10. }
  11. // Edges of the User.
  12. func (User) Edges() []ent.Edge {
  13. return nil
  14. }

Add 2 fields to the User schema:

<project>/ent/schema/user.go

  1. package schema
  2. import (
  3. "entgo.io/ent"
  4. "entgo.io/ent/schema/field"
  5. )
  6. // Fields of the User.
  7. func (User) Fields() []ent.Field {
  8. return []ent.Field{
  9. field.Int("age").
  10. Positive(),
  11. field.String("name").
  12. Default("unknown"),
  13. }
  14. }

Run go generate from the root directory of the project as follows:

  1. go generate ./ent

This produces the following files:

  1. ent
  2. ├── client.go
  3. ├── config.go
  4. ├── context.go
  5. ├── ent.go
  6. ├── generate.go
  7. ├── mutation.go
  8. ... truncated
  9. ├── schema
  10. └── user.go
  11. ├── tx.go
  12. ├── user
  13. ├── user.go
  14. └── where.go
  15. ├── user.go
  16. ├── user_create.go
  17. ├── user_delete.go
  18. ├── user_query.go
  19. └── user_update.go

Create Your First Entity

To get started, create a new ent.Client. For this example, we will use SQLite3.

<project>/start/start.go

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "<project>/ent"
  6. _ "github.com/mattn/go-sqlite3"
  7. )
  8. func main() {
  9. client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
  10. if err != nil {
  11. log.Fatalf("failed opening connection to sqlite: %v", err)
  12. }
  13. defer client.Close()
  14. // Run the auto migration tool.
  15. if err := client.Schema.Create(context.Background()); err != nil {
  16. log.Fatalf("failed creating schema resources: %v", err)
  17. }
  18. }

Now, we’re ready to create our user. Let’s call this function CreateUser for the sake of example:

<project>/start/start.go

  1. func CreateUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
  2. u, err := client.User.
  3. Create().
  4. SetAge(30).
  5. SetName("a8m").
  6. Save(ctx)
  7. if err != nil {
  8. return nil, fmt.Errorf("failed creating user: %w", err)
  9. }
  10. log.Println("user was created: ", u)
  11. return u, nil
  12. }

Query Your Entities

ent generates a package for each entity schema that contains its predicates, default values, validators and additional information about storage elements (column names, primary keys, etc).

<project>/start/start.go

  1. package main
  2. import (
  3. "log"
  4. "<project>/ent"
  5. "<project>/ent/user"
  6. )
  7. func QueryUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
  8. u, err := client.User.
  9. Query().
  10. Where(user.Name("a8m")).
  11. // `Only` fails if no user found,
  12. // or more than 1 user returned.
  13. Only(ctx)
  14. if err != nil {
  15. return nil, fmt.Errorf("failed querying user: %w", err)
  16. }
  17. log.Println("user returned: ", u)
  18. return u, nil
  19. }

Add Your First Edge (Relation)

In this part of the tutorial, we want to declare an edge (relation) to another entity in the schema.
Let’s create 2 additional entities named Car and Group with a few fields. We use ent CLI to generate the initial schemas:

  1. go run entgo.io/ent/cmd/ent init Car Group

And then we add the rest of the fields manually:

<project>/ent/schema/car.go

  1. // Fields of the Car.
  2. func (Car) Fields() []ent.Field {
  3. return []ent.Field{
  4. field.String("model"),
  5. field.Time("registered_at"),
  6. }
  7. }

<project>/ent/schema/group.go

  1. // Fields of the Group.
  2. func (Group) Fields() []ent.Field {
  3. return []ent.Field{
  4. field.String("name").
  5. // Regexp validation for group name.
  6. Match(regexp.MustCompile("[a-zA-Z_]+$")),
  7. }
  8. }

Let’s define our first relation. An edge from User to Car defining that a user can have 1 or more cars, but a car has only one owner (one-to-many relation).

er-user-cars

Let’s add the "cars" edge to the User schema, and run go generate ./ent:

<project>/ent/schema/user.go

  1. // Edges of the User.
  2. func (User) Edges() []ent.Edge {
  3. return []ent.Edge{
  4. edge.To("cars", Car.Type),
  5. }
  6. }

We continue our example by creating 2 cars and adding them to a user.

<project>/start/start.go

  1. import (
  2. "<project>/ent"
  3. "<project>/ent/car"
  4. "<project>/ent/user"
  5. )
  6. func CreateCars(ctx context.Context, client *ent.Client) (*ent.User, error) {
  7. // Create a new car with model "Tesla".
  8. tesla, err := client.Car.
  9. Create().
  10. SetModel("Tesla").
  11. SetRegisteredAt(time.Now()).
  12. Save(ctx)
  13. if err != nil {
  14. return nil, fmt.Errorf("failed creating car: %w", err)
  15. }
  16. log.Println("car was created: ", tesla)
  17. // Create a new car with model "Ford".
  18. ford, err := client.Car.
  19. Create().
  20. SetModel("Ford").
  21. SetRegisteredAt(time.Now()).
  22. Save(ctx)
  23. if err != nil {
  24. return nil, fmt.Errorf("failed creating car: %w", err)
  25. }
  26. log.Println("car was created: ", ford)
  27. // Create a new user, and add it the 2 cars.
  28. a8m, err := client.User.
  29. Create().
  30. SetAge(30).
  31. SetName("a8m").
  32. AddCars(tesla, ford).
  33. Save(ctx)
  34. if err != nil {
  35. return nil, fmt.Errorf("failed creating user: %w", err)
  36. }
  37. log.Println("user was created: ", a8m)
  38. return a8m, nil
  39. }

But what about querying the cars edge (relation)? Here’s how we do it:

<project>/start/start.go

  1. import (
  2. "log"
  3. "<project>/ent"
  4. "<project>/ent/car"
  5. )
  6. func QueryCars(ctx context.Context, a8m *ent.User) error {
  7. cars, err := a8m.QueryCars().All(ctx)
  8. if err != nil {
  9. return fmt.Errorf("failed querying user cars: %w", err)
  10. }
  11. log.Println("returned cars:", cars)
  12. // What about filtering specific cars.
  13. ford, err := a8m.QueryCars().
  14. Where(car.Model("Ford")).
  15. Only(ctx)
  16. if err != nil {
  17. return fmt.Errorf("failed querying user cars: %w", err)
  18. }
  19. log.Println(ford)
  20. return nil
  21. }

Add Your First Inverse Edge (BackRef)

Assume we have a Car object and we want to get its owner; the user that this car belongs to. For this, we have another type of edge called “inverse edge” that is defined using the edge.From function.

er-cars-owner

The new edge created in the diagram above is translucent, to emphasize that we don’t create another edge in the database. It’s just a back-reference to the real edge (relation).

Let’s add an inverse edge named owner to the Car schema, reference it to the cars edge in the User schema, and run go generate ./ent.

<project>/ent/schema/car.go

  1. // Edges of the Car.
  2. func (Car) Edges() []ent.Edge {
  3. return []ent.Edge{
  4. // Create an inverse-edge called "owner" of type `User`
  5. // and reference it to the "cars" edge (in User schema)
  6. // explicitly using the `Ref` method.
  7. edge.From("owner", User.Type).
  8. Ref("cars").
  9. // setting the edge to unique, ensure
  10. // that a car can have only one owner.
  11. Unique(),
  12. }
  13. }

We’ll continue the user/cars example above by querying the inverse edge.

<project>/start/start.go

  1. import (
  2. "fmt"
  3. "log"
  4. "<project>/ent"
  5. "<project>/ent/user"
  6. )
  7. func QueryCarUsers(ctx context.Context, a8m *ent.User) error {
  8. cars, err := a8m.QueryCars().All(ctx)
  9. if err != nil {
  10. return fmt.Errorf("failed querying user cars: %w", err)
  11. }
  12. // Query the inverse edge.
  13. for _, ca := range cars {
  14. owner, err := ca.QueryOwner().Only(ctx)
  15. if err != nil {
  16. return fmt.Errorf("failed querying car %q owner: %w", ca.Model, err)
  17. }
  18. log.Printf("car %q owner: %q\n", ca.Model, owner.Name)
  19. }
  20. return nil
  21. }

Create Your Second Edge

We’ll continue our example by creating a M2M (many-to-many) relationship between users and groups.

er-group-users

As you can see, each group entity can have many users, and a user can be connected to many groups; a simple “many-to-many” relationship. In the above illustration, the Group schema is the owner of the users edge (relation), and the User entity has a back-reference/inverse edge to this relationship named groups. Let’s define this relationship in our schemas:

<project>/ent/schema/group.go

  1. // Edges of the Group.
  2. func (Group) Edges() []ent.Edge {
  3. return []ent.Edge{
  4. edge.To("users", User.Type),
  5. }
  6. }

<project>/ent/schema/user.go

  1. // Edges of the User.
  2. func (User) Edges() []ent.Edge {
  3. return []ent.Edge{
  4. edge.To("cars", Car.Type),
  5. // Create an inverse-edge called "groups" of type `Group`
  6. // and reference it to the "users" edge (in Group schema)
  7. // explicitly using the `Ref` method.
  8. edge.From("groups", Group.Type).
  9. Ref("users"),
  10. }
  11. }

We run ent on the schema directory to re-generate the assets.

  1. go generate ./ent

Run Your First Graph Traversal

In order to run our first graph traversal, we need to generate some data (nodes and edges, or in other words, entities and relations). Let’s create the following graph using the framework:

re-graph

<project>/start/start.go

  1. func CreateGraph(ctx context.Context, client *ent.Client) error {
  2. // First, create the users.
  3. a8m, err := client.User.
  4. Create().
  5. SetAge(30).
  6. SetName("Ariel").
  7. Save(ctx)
  8. if err != nil {
  9. return err
  10. }
  11. neta, err := client.User.
  12. Create().
  13. SetAge(28).
  14. SetName("Neta").
  15. Save(ctx)
  16. if err != nil {
  17. return err
  18. }
  19. // Then, create the cars, and attach them to the users in the creation.
  20. err = client.Car.
  21. Create().
  22. SetModel("Tesla").
  23. SetRegisteredAt(time.Now()). // ignore the time in the graph.
  24. SetOwner(a8m). // attach this graph to Ariel.
  25. Exec(ctx)
  26. if err != nil {
  27. return err
  28. }
  29. err = client.Car.
  30. Create().
  31. SetModel("Mazda").
  32. SetRegisteredAt(time.Now()). // ignore the time in the graph.
  33. SetOwner(a8m). // attach this graph to Ariel.
  34. Exec(ctx)
  35. if err != nil {
  36. return err
  37. }
  38. err = client.Car.
  39. Create().
  40. SetModel("Ford").
  41. SetRegisteredAt(time.Now()). // ignore the time in the graph.
  42. SetOwner(neta). // attach this graph to Neta.
  43. Exec(ctx)
  44. if err != nil {
  45. return err
  46. }
  47. // Create the groups, and add their users in the creation.
  48. err = client.Group.
  49. Create().
  50. SetName("GitLab").
  51. AddUsers(neta, a8m).
  52. Exec(ctx)
  53. if err != nil {
  54. return err
  55. }
  56. err = client.Group.
  57. Create().
  58. SetName("GitHub").
  59. AddUsers(a8m).
  60. Exec(ctx)
  61. if err != nil {
  62. return err
  63. }
  64. log.Println("The graph was created successfully")
  65. return nil
  66. }

Now when we have a graph with data, we can run a few queries on it:

  1. Get all user’s cars within the group named “GitHub”:

    <project>/start/start.go

    1. import (
    2. "log"
    3. "<project>/ent"
    4. "<project>/ent/group"
    5. )
    6. func QueryGithub(ctx context.Context, client *ent.Client) error {
    7. cars, err := client.Group.
    8. Query().
    9. Where(group.Name("GitHub")). // (Group(Name=GitHub),)
    10. QueryUsers(). // (User(Name=Ariel, Age=30),)
    11. QueryCars(). // (Car(Model=Tesla, RegisteredAt=<Time>), Car(Model=Mazda, RegisteredAt=<Time>),)
    12. All(ctx)
    13. if err != nil {
    14. return fmt.Errorf("failed getting cars: %w", err)
    15. }
    16. log.Println("cars returned:", cars)
    17. // Output: (Car(Model=Tesla, RegisteredAt=<Time>), Car(Model=Mazda, RegisteredAt=<Time>),)
    18. return nil
    19. }
  2. Change the query above, so that the source of the traversal is the user Ariel:

    <project>/start/start.go

    1. import (
    2. "log"
    3. "<project>/ent"
    4. "<project>/ent/car"
    5. )
    6. func QueryArielCars(ctx context.Context, client *ent.Client) error {
    7. // Get "Ariel" from previous steps.
    8. a8m := client.User.
    9. Query().
    10. Where(
    11. user.HasCars(),
    12. user.Name("Ariel"),
    13. ).
    14. OnlyX(ctx)
    15. cars, err := a8m. // Get the groups, that a8m is connected to:
    16. QueryGroups(). // (Group(Name=GitHub), Group(Name=GitLab),)
    17. QueryUsers(). // (User(Name=Ariel, Age=30), User(Name=Neta, Age=28),)
    18. QueryCars(). //
    19. Where( //
    20. car.Not( // Get Neta and Ariel cars, but filter out
    21. car.Model("Mazda"), // those who named "Mazda"
    22. ), //
    23. ). //
    24. All(ctx)
    25. if err != nil {
    26. return fmt.Errorf("failed getting cars: %w", err)
    27. }
    28. log.Println("cars returned:", cars)
    29. // Output: (Car(Model=Tesla, RegisteredAt=<Time>), Car(Model=Ford, RegisteredAt=<Time>),)
    30. return nil
    31. }
  3. Get all groups that have users (query with a look-aside predicate):

    <project>/start/start.go

    1. import (
    2. "log"
    3. "<project>/ent"
    4. "<project>/ent/group"
    5. )
    6. func QueryGroupWithUsers(ctx context.Context, client *ent.Client) error {
    7. groups, err := client.Group.
    8. Query().
    9. Where(group.HasUsers()).
    10. All(ctx)
    11. if err != nil {
    12. return fmt.Errorf("failed getting groups: %w", err)
    13. }
    14. log.Println("groups returned:", groups)
    15. // Output: (Group(Name=GitHub), Group(Name=GitLab),)
    16. return nil
    17. }

Full Example

The full example exists in GitHub.