标签:ase func contain fatal size yaml 介绍 let ext
接上文,前边我们了解了关于基本代码生成以及schema 迁移的学习,下边我们看看基本的数据操作
参考代码: https://github.com/rongfengliang/ent-demo
使用docker-compose 运行
version: "3"
services:
mysql:
image: mysql:5.7.16
ports:
- 3306:3306
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment:
MYSQL_ROOT_PASSWORD: dalongrong
MYSQL_DATABASE: gogs
MYSQL_USER: gogs
MYSQL_PASSWORD: dalongrong
TZ: Asia/Shanghai
go mod github.com/rongfengliang/ent-demo
├── Makefile
├── README.md
├── build
│ ├── create
│ ├── migration
│ └── query
├── cmd
│ ├── create
│ │ └── main.go
│ ├── migration
│ │ └── main.go
│ └── query
│ └── main.go
├── docker-compose.yaml
├── ent
│ ├── client.go
│ ├── config.go
│ ├── context.go
│ ├── ent.go
│ ├── example_test.go
│ ├── migrate
│ │ ├── migrate.go
│ │ └── schema.go
│ ├── predicate
│ │ └── predicate.go
│ ├── schema
│ │ └── user.go
│ ├── tx.go
│ ├── user
│ │ ├── user.go
│ │ └── where.go
│ ├── user.go
│ ├── user_create.go
│ ├── user_delete.go
│ ├── user_query.go
│ └── user_update.go
├── go.mod
└── go.sum
说明
cmd 目录为操作,ent 为代码生成
Makefile
.PHONY : clean
all: clean cli
clean:
rm -rf build/*
cli:
go build -o build ./cmd/...
go run cmd/migration/main.go
package main
?
import (
"context"
"fmt"
"log"
?
_ "github.com/go-sql-driver/mysql"
"github.com/rongfengliang/ent-demo/ent"
)
?
func main() {
client, err := ent.Open("mysql", "root:dalongrong@tcp(127.0.0.1)/gogs")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
ctx := context.Background()
createUser(ctx, client)
?
}
?
func createUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
u, err := client.User.
Create().
SetAge(30).
SetName("a8m").
Save(ctx)
if err != nil {
return nil, fmt.Errorf("failed creating user: %v", err)
}
log.Println("user was created: ", u)
return u, nil
}
说明:
从上边可以看出,使用很简单
go run cmd/create/main.go
?
2019/10/14 13:53:20 user was created: User(id=5, age=30, name=a8m)
? first git:(master) ?
package main
?
import (
"context"
"fmt"
"log"
?
_ "github.com/go-sql-driver/mysql"
"github.com/rongfengliang/ent-demo/ent"
"github.com/rongfengliang/ent-demo/ent/user"
)
?
func main() {
?
client, err := ent.Open("mysql", "root:dalongrong@tcp(127.0.0.1)/gogs")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
ctx := context.Background()
queryUser(ctx, client)
}
?
func queryUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
u, err := client.User.
Query().
Where(user.ID(1)).
// `Only` fails if no user found,
// or more than 1 user returned.
Only(ctx)
if err != nil {
return nil, fmt.Errorf("failed querying user: %v", err)
}
log.Println("user returned: ", u)
return u, nil
}
go run cmd/query/main.go
2019/10/14 13:54:49 user returned: User(id=1, age=30, name=a8m)
以上是一个简单的insert 以及查询处理,后边会介绍下关于图查询的处理,实际上ent 还包含了数据校验的处理
https://entgo.io/docs/getting-started/
https://github.com/rongfengliang/ent-demo
标签:ase func contain fatal size yaml 介绍 let ext
原文地址:https://www.cnblogs.com/rongfengliang/p/11671164.html