Setting Up

This guide is intended for first-time users who want instructions on how to set up an Ent project from scratch. Before we get started, make sure you have the following prerequisites installed on your machine.

Prerequisites

After installing these dependencies, create a directory for the project and initialize a Go module:

  1. mkdir todo
  2. cd $_
  3. go mod init todo

Installation

Run the following Go commands to install Ent, and tell it to initialize the project structure along with a Todo schema.

  1. go get -d entgo.io/ent/cmd/ent
  1. go run -mod=mod entgo.io/ent/cmd/ent new Todo

After installing Ent and running ent new, your project directory should look like this:

  1. .
  2. ├── ent
  3. ├── generate.go
  4. └── schema
  5. └── todo.go
  6. ├── go.mod
  7. └── go.sum

The ent directory holds the generated assets (see the next section), and the ent/schema directory contains your entity schemas.

Code Generation

When we ran ent new Todo above, a schema named Todo was created in the todo.go file under thetodo/ent/schema/ directory:

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

As you can see, initially, the schema has no fields or edges defined. Let’s run the command for generating assets to interact with the Todo entity:

  1. go generate ./ent

Create a Test Case

Running go generate ./ent invoked Ent’s automatic code generation tool, which uses the schemas we define in our schema package to generate the actual Go code which we will now use to interact with a database. At this stage, you can find under ./ent/client.go, client code that is capable of querying and mutating the Todo entities. Let’s create a testable example to use this. We’ll use SQLite in this test-case for testing Ent.

  1. go get github.com/mattn/go-sqlite3
  2. touch example_test.go

Paste the following code in example_test.go that instantiates an ent.Client and automatically creates all schema resources in the database (tables, columns, etc).

  1. package todo
  2. import (
  3. "context"
  4. "log"
  5. "todo/ent"
  6. "entgo.io/ent/dialect"
  7. _ "github.com/mattn/go-sqlite3"
  8. )
  9. func Example_Todo() {
  10. // Create an ent.Client with in-memory SQLite database.
  11. client, err := ent.Open(dialect.SQLite, "file:ent?mode=memory&cache=shared&_fk=1")
  12. if err != nil {
  13. log.Fatalf("failed opening connection to sqlite: %v", err)
  14. }
  15. defer client.Close()
  16. ctx := context.Background()
  17. // Run the automatic migration tool to create all schema resources.
  18. if err := client.Schema.Create(ctx); err != nil {
  19. log.Fatalf("failed creating schema resources: %v", err)
  20. }
  21. // Output:
  22. }

Then, run go test to verify that everything works as expected.

  1. go test

After setting up our project, we’re ready to create our Todo list.