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
You now have a running instance of TiDB, with port 4000 open for listening.
Copy the hello world example repo locally
In this example repo we have defined a simple User schema.
Then, to connect Ent and TiDB.
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.
Run this sample program
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.
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.