An Introduction to ENT

An Introduction to ENT

ENT - The Facebook developed entity framework for Go. Learn all the features of the Ent framework and build a simple CRUD API that leverages the various functionalities of Ent.

Database systems are an integral part of software development. A software developer needs to be skilled in working with databases irrespective of the programming language of choice. Most programming languages have various tools/packages that make working with database management systems easy for developers. Some of these tools are native to the programming language, others are built/maintained by the community of developers around the language and made available for free use.

The lack of a graph-based ORM(Object-relational mapping) for the Go programming language led a team of developers at Facebook to create ent. Ent is an entity framework typically used for modeling data in a graph-based structure. The ent framework prides itself on its ability to model data as Go code unlike many other ORM’s that model data as struct tags. Due to the graph-based structure of the ent framework, querying data stored in the database can be done with ease and takes the form of graph traversal. ent comes with a command-line tool which we can use to automatically generate code schema and get a visual representation of the schema.

In this post, we will explore all the cool features of the ent framework and build a simple CRUD API that leverages the various functionalities of ent.

Prerequisites

To follow along while reading this article, you will need:

Getting started with ent

The first step in working with the ent framework is to install it into our project. To install ent, run the following command go get github.com/facebook/ent/cmd/entc. The command will install entc the command-line tool for the ent package.

Throughout this article, we will build a simple CRUD(Create, Read, Update, and Delete) API that leverages ent. The API will contain five endpoints, the purpose of building this API is to show how to perform common create, read, update, and delete operations on a database using ent.

To get started, create the needed files and folders to match the tree structure below:

├── handlers/
│ ├── handler.go
├── database/
│ ├── db.go
└── main.go
  • The main.go file will contain all of the logic related to creating the server for the API. We will be using fiber, the express style framework for Go to quickly wire up our API endpoints.
  • The db.go file in the database directory will contain code related to creating a database connection and a client
  • The handler.go file will house the API handlers

In the next section, we will start building the API and explore ent.

A deep dive into ent

To get started with the project, run go mod init in the root directory of the project. This will initialize a new project with Go modules. Next, we have to install fiber, the framework we will use in building the API, by running the following command in the root directory of the project github.com/gofiber/fiber/v2.

In building the API for an imaginary note-taking application, we will need the following endpoints:

  • /api/v1/createnote
  • /api/v1/readnote/
  • /api/v1/searchnote/:title
  • /api/v1/updatenote/:id
  • /api/v1/deletenote/:id

In the main.go file, add the following lines of code:

package main

import (
   "fmt"

   "github.com/gofiber/fiber/v2"
)

func Routes(app *fiber.App){
   api := app.Group("/api/v1")

   api.Get("/", func(c *fiber.Ctx) error {
      return c.SendString("Hello, World!")
   })
}

func main() {
   app := fiber.New()

   Routes(app)

   err := app.Listen(":3000")
   if err != nil {
      fmt.Println("Unable to start server")
   }
}

The above code creates a simple web server. At the moment only one endpoint is wired up, in the coming sections we will be working in the handler.go files to ensure that all the API endpoints are functional. For now, you can run the above file and visit localhost:3000/api/v1/ on your browser. If everything went well, you should see “hello world” printed out.

go programming api web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Build a Go Web API with the New Digital Ocean App Platform: A How-To Guide

Recently, Digital Ocean announced they're entering the PaaS market with their new application platform. They've hosted virtual machines (droplets) and Kubernetes based services for years, but now they're creating a platform that's a simple point and click to get an application up and running.

A Simple Guide to API Development Tools

APIs can be as simple as 1 endpoint for use by 100s of users or as complex as the AWS APIs with 1000s of endpoints and 100s of thousands of users. Building them can mean spending a couple of hours using a low-code platform or months of work using a multitude of tools. Hosting them can be as simple as using one platform that does everything we need or as complex as setting up and managing ingress control, security, caching, failover, metrics, scaling.

Tracking a Developer’s Journey From Documentation Visit

Measuring website activity provides only half the story. See how to best track the developer's journey and what funnel stages makes sense for API-first products

How to Market to Developers with Paid Marketing

Selling to developers is hard. How to market to developers efficiently using paid advertising leveraging inbound marketing techniques.

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...