原文作者:Amit Shani,Ent Engineer
对于后端开发者来说,一款好用的框架能够大大提升应用的开发效率。为了降低开发者使用 TiDB 的门槛,方便开发者快速连接到 TiDB,我们也在和合作伙伴一起,逐步完善面向主流开发语言和框架的连接支持。
Hello World 应用示例
docker run -p 4000:4000 pingcap/tidb
git clone https://github.com/hedwigz/tidb-hello-world.git
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"),
}
}
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)
}
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)
$ go run main.go
the user: hedwigz is 30 years old
版本说明
文章评论