Book a Demo Start Instantly
Ent announces preview support for TiDB

Author: Amit Shani (Ent Engineer)

Editors: Mila Wu, Calvin Weng, Tom Dewan, Yajing Wang

Ent‘s latest version now has preview support for TiDB with a new migration engine Atlas enabled.

Ent

Ent is an entity framework for Go that makes developing large applications a breeze. It was open-sourced in late 2019 and officially joined the Linux Foundation in September 2021. After users define the data model of their applications, Ent provides the tools to:

  • Generate a type-safe, explicit API for accessing your database.
  • Automatically generate feature-rich, performant, and clean GraphQL, REST, and gRPC servers.
  • Traverse your data using graph semantics.
  • Perform safe database migrations.

Ent can be used to access data in many types of databases, both graph-oriented and relational. Most commonly, users have been using standard open-source relational databases such as MySQL, MariaDB, and PostgreSQL. As teams building Ent-based applications become more successful and need to deal with larger traffic on larger scales, these single-node databases often become the bottleneck for scaling out. For this reason, many members of the Ent community have requested support for NewSQL databases such as TiDB.

TiDB

TiDB is an open-source NewSQL database. It provides many features that traditional databases don’t, such as:

  • Horizontal scaling. For many years software architects needed to choose between the familiarity and guarantees that relational databases provide and the scale-out capability of NoSQL databases (such as MongoDB or Cassandra). TiDB supports horizontal scaling while maintaining good compatibility with MySQL features.
  • Hybrid transactional and analytical processing (HTAP) – Databases are traditionally divided into analytical (OLAP) and transactional (OLTP) databases. TiDB breaks this dichotomy by enabling both analytical and transactional workloads on the same database.
  • Prepacked monitoring with Prometheus + Grafana. TiDB is built on cloud-native paradigms from the ground up and natively supports the standard CNCF observability stack.

To learn more about TiDB, check out the official TiDB Introduction.

Hello World with TiDB

For a quick “Hello World” application with Ent + TiDB, follow these steps:

  1. Use Docker to spin up a local TiDB server:
    docker run -p 4000:4000 pingcap/tidb

    When this command finishes, you should have a running instance of TiDB listening on port 4000.

  2. Clone the example hello world repository:
    git clone https://github.com/hedwigz/tidb-hello-world.git

    In this example repository, we defined a simple schema User:

    func (User) Fields() []ent.Field {
         return []ent.Field{
           field.Time("created_at").
           Default(time.Now),
         field.String("name"),
         field.Int("age"),
     }
    }

    Then, we connected Ent with TiDB:

    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)
    }

    Note that in line 1 of the code above, we connect to the TiDB server using a mysql dialect. This is possible because TiDB is MySQL compatible, and it does not require any special driver.

    Having said that, there are some differences between TiDB and MySQL, especially regarding schema migrations, such as information schema inspection and migration planning. For this reason, Atlas automatically detects if it is connected to TiDB and handles the migration accordingly.

    In addition, note that in line 7 of the code above we used schema.WithAtlas(true), which flags Ent to use Atlas as its migration engine.

    Finally, we create a user and save the record to TiDB to later be queried and printed.

    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)

    You can see the full code here.

  3. Run the example program:
    $ go run main.go
    the user: hedwigz is 30 years old

Woohoo! In this quick walk-through we managed to:

  • Spin up a local instance of TiDB.
  • Connect Ent with TiDB.
  • Use Atlas to migrate our Ent schema.
  • Use Ent to insert and query from TiDB.

Preview support

The integration of Atlas with TiDB is well tested with TiDB v5.4.0 (the latest version at the time of this writing), and is expected to be extended in the future. If you use other versions of TiDB or are looking for help, don’t hesitate to file an issue on GitHub or join this Discord channel.

If you use Ent and are also interested in TiDB, you can join TiDB’s slack discussions or request a demo now. 


In addition to Ent, you can also use GORM or go-sql-driver/mysql to develop your application based on TiDB. For more details, please check TiDB Docs. Or if your project is already compatible with TiDB, please file an issue to contact us.

This article was first published on the Ent community blog


Book a Demo


Have questions? Let us know how we can help.

Contact Us
TiDB Dedicated

TiDB Dedicated

A fully-managed cloud DBaaS for predictable workloads

TiDB Dedicated

TiDB Serverless

A fully-managed cloud DBaaS for auto-scaling workloads