For back-end developers, an easy-to-use framework can greatly improve the development efficiency of applications. In order to lower the threshold for developers to use TiDB and facilitate developers to quickly connect to TiDB, we are also working with our partners to gradually improve the connection support for mainstream development languages and frameworks.
Recently, Facebook's open source Golang Entity Framework Ent has completed support for TiDB database.
Ent is an easy framework for building and maintaining applications and big data models. Has the following characteristics:
- Schema is code: any database table can be modeled as a Go object;
- Traverse any graph with ease: queries, aggregates, and traversal of any graph structure can be easily run;
- Static type and explicit API: Using code to generate static type and explicit API, it is more convenient to query data;
- Multiple storage drivers: support MySQL, PostgreSQL, SQLite, Gremlin, and now TiDB;
- Extensible: Easy to extend and customize with Go templates.
Let's see how to quickly implement an Ent + TiDB-based application through an application example of Hello World.
Hello World application example
1. Start a TiDB Server locally with Docker
docker run -p 4000:4000 pingcap/tidb
You should now have a running TiDB instance with port 4000 open for listening.
2. Copy the example repo of hello world locally
git clone https://github.com/hedwigz/tidb-hello-world.git
In this example repo we define a simple User schema
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, connect Ent and TiDB:
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 use a MySQL statement to connect to the TiDB Server. Because TiDB is compatible with MySQL, no other special driver is required.
Having said that, there are still many differences between TiDB and MySQL, especially the operations related to schema migration, such as SQL diagnosis and migration planning. Therefore, Atlas can automatically detect that it is connected to TiDB and perform corresponding migration processing.
Also, in the seventh line we use schema.WithAtlas(true)
, indicating that Ent is using "Atlas" as the migration engine. Atlas is Ent's just-released migration engine, and thanks to Atlas' latest design, support for new databases has never been easier.
Finally, we create a new user data and save it to TiDB for later data reading and output.
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)
3. Run this sample program:
$ go run main.go
the user: hedwigz is 30 years old
In this quick walkthrough, we successfully achieved:
- Start a local TiDB instance;
- Connect Ent and TiDB database;
- Use Atlas to migrate Ent Schema;
- Use Ent to insert and read data from TiDB.
Imprint
At present, this sample application can run normally in Ent v0.10 and TiDB v5.4.0, and Ent also plans to continue to expand the support for TiDB in the future. If you use other versions of TiDB or need help, please join asktug.com to communicate. If you also have projects that want to adapt to TiDB, please submit issue
In addition to Ent, TiDB has previously added support for GORM and go-sql-driver/mysql. For details, please refer to the document: https://docs.pingcap.com/appdev/dev
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。