Golang Tutorial: Build a Go application with the Ent ORM


Golang is a language built by Google with the main focus on performance and concurrency. In recent years Go has become one of the most loved and wanted programming languages among developers. Golang is particularly suited for developing infrastructures like networked servers or even microservices. Even though Golang had many excellent features and tools built with it, there are only a few tools available in Go that handle the data layer properly, like an ORM.

What is an ORM

“Technique for converting data between incompatible type systems using object-oriented programming languages”

Object Relational Mapper(Mapping) is a method of writing SQL queries for a relational database using the object-oriented paradigm of your preferred programming language. An ORM will act as an interface that will wrap your tables or stored procedures in classes so you can access them using methods and properties of objects instead of writing SQL queries.

But in Golang, most ORM libraries cannot handle the features below.

  • Relationship support
  • Prefetching
  • Multicreate
  • Composable Queries

The most widely used ORM in Go lang, GORM, can handle the above tasks, but there are several shortcomings in GORM, like performance that will hurt your application. With these issues in mind, the Meta developers have developed an ORM that can easily define any data model or graph structure in Go code called Ent.

What is Ent?

Ent is an ORM(Object Relational Mapping) framework built by Meta Open Source, which provides an API for modeling any database schema as Go objects. With Ent, you can run queries and aggregations and traverse graph structures. Ent supports major databases MySQLMariaDBPostgreSQLSQLite, and Gremlin-based graph databases(Azure Cosmos DB). All you need to do with Ent is define the Schema for your application, and Ent will handle the rest for you. The Schema you specify will be validated by the Ent codegen(entc), which will generate a well-typed and idiomatic API.

Why ENT is a better ORM for Golang

There are many tools in Golang like go-pgsqlxsqlcsql-migrate, and sqlboiler which can generate type-safe code that will map the application’s primitives to the database tables with struct and methods. But these tools are not a complete solution, so you will have to depend on each tool to do its part, like generating code and handling migrations when building your application. With Ent, you can have a complete framework that enables all related tasks. Ent also provides

  • Statically typed and explicit API
  • Queries, aggregations, and graph traversals
  • Support for context.Context
  • Enables Caching through entcache
  • The OpenAPI Specification (OAS, formerly known as Swagger Specification) generation through entoas

You can see why Ent is better than other tools and ORM with these options or features. Next, let’s dive into the Concepts and API in Ent.

Concepts and API of Ent

Before working with Ent, you must grasp a few concepts/keywords.

  • Schema
    A schema defines one entity type in the graph
  • Fields
    represent its properties
  • Edges
    Edges represent relationships (one-to-one, one-to-many, many-to-many) in Ent.
  • Mixin
    Mixin is an interface that allows you to create reusable pieces of a schema that Mixin can inject into another schema. A Mixin can be a set of Feilds, Edges, or Hooks.
  • Annotations
    Annotations allow us to add additional metadata to our schema objects like Edges and Fields.
  • Privacy
    One of Ent’s best features is the Privacy option, which defines the privacy policy for queries and mutations of entities in the database. When you define a policy for a schema, it will always be evaluated whenever queries and mutations are performed on it.

To create policies, you will have to extend the class ent.Policy holds two methods, EvalQuery and EvalMutation, responsible for read-policy and write-policy. A policy can have any number of rules defined by the user, and rules will evaluate them in the same order declared in the Schema.

Building a Golang application with Ent

Let’s move on to building an application with Ent. Here we will be building a small pokemon application with Fiber, an Express-inspired web framework written in Go.

Prerequisites

Before building the application, you must have the go 1.17 or the latest version installed and configured. We will be using MySQL as the database for this example so make sure you have a running instance of MySQL (latest of 5.6 and above).