Recently, Facebook’s open source Golang entity framework, Ent, completed support for TiDB databases.

Ent is an easy framework for building and maintaining applications and big data models. It has the following features.

  • Schema as code : the ability to model any database table as a Go object.
  • Easy to traverse any graph : can easily run queries, aggregation and traversal of any graph structure .
  • Static Types and Explicit APIs: Use code to generate static types and explicit APIs for easier querying of data.
  • Multi-storage drivers : support for MySQL, PostgreSQL, SQLite, Gremlin, and now also TiDB.
  • Extensible: easy to extend and customize with Go templates.

TiDB is an open source distributed relational database designed and developed by PingCAP, which is a converged distributed database product that supports both online transaction processing and online analytical processing (HTAP). It has important features such as horizontal scaling or scaling, financial grade high availability, real-time HTAP, cloud-native distributed database, compatibility with MySQL 5.7 protocol and MySQL ecology. The goal is to provide users with one-stop OLTP (Online Transactional Processing), OLAP (Online Analytical Processing) and HTAP solutions. TiDB is suitable for various application scenarios such as high availability, strong consistency requirements, and large data size.

The following is a Hello World application example to see how to quickly implement an Ent + TiDB based application.

Hello World Application Example

Start a TiDB Server locally with Docker

1
docker run -p 4000:4000 pingcap/tidb

You now have a running instance of TiDB, with port 4000 open for listening.

Copy the hello world example repo locally

1
git clone https://github.com/hedwigz/tidb-hello-world.git

In this example repo we have defined a simple User schema.

1
2
3
4
5
6
7
8
9
go title="ent/schema/user.go"
func (User) Fields() []ent.Field {
        return []ent.Field{
                field.Time("created_at").
                        Default(time.Now),
                field.String("name"),
                field.Int("age"),
        }
}

Then, to connect Ent and TiDB.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
go title="main.go"
client, err := ent.Open("mysql", "root@tcp(localhost:4000)/test?parseTime=true")
if err != nil {
        log.Fatalf("failed opening connection to TiDB: %v", err)
}
defer client.Close()
// Run the auto migration tool, with Atlas.
if err := client.Schema.Create(context.Background(), schema.WithAtlas(true)); err != nil {
        log.Fatalf("failed printing schema changes: %v", err)
}

As you can see, in the first line we connect to TiDB Server through a MySQL statement, since TiDB is MySQL compatible, no special driver is needed. Having said that, there are still many differences between TiDB and MySQL, especially operations related to Schema migration, such as SQL diagnosis and migration planning. So, Atlas can automatically monitor the connection to TiDB and do the migration process accordingly. In addition, in the seventh line we use schema.WithAtlas (true) to indicate that Ent is using “Atlas” as the migration engine, which is the migration engine that Ent just released, and thanks to the latest design of Atlas, it is easier than ever to support new databases. Atlas is Ent’s newly released migration engine, and thanks to Atlas’ latest design, supporting new databases has never been easier.

Finally, we create a new user data and save it to TiDB for later data reading and exporting.

1
2
3
4
5
6
7
go title="main.go"
client.User.Create().
                SetAge(30).
                SetName("hedwigz").
                SaveX(context.Background())
user := client.User.Query().FirstX(context.Background())
fmt.Printf("the user: %s is %d years old\n", user.Name, user.Age)

Run this sample program

1
2
$ go run main.go
the user: hedwigz is 30 years old

Summary

In this quick walkthrough, we successfully completed.

  • start a local instance of TiDB.
  • connecting Ent and TiDB databases.
  • migrating Ent Schema using Atlas.
  • inserting and reading data from TiDB using Ent.

Release Notes

This sample application currently works fine with Ent v0.10 and TiDB v5.4.0, and Ent plans to continue to expand support for TiDB in the future.

In addition to Ent, TiDB has previously added support for GORM and go-sql-driver/mysql, see docs for details.