特性开关

特性开关可以通过 CLI 标志或作为参数提供给 包。

CLI

Go

  1. // +build ignore
  2. package main
  3. import (
  4. "log"
  5. "text/template"
  6. "entgo.io/ent/entc"
  7. "entgo.io/ent/entc/gen"
  8. )
  9. func main() {
  10. err := entc.Generate("./schema", &gen.Config{
  11. Features: []gen.Feature{
  12. gen.FeaturePrivacy,
  13. gen.FeatureEntQL,
  14. },
  15. Templates: []*gen.Template{
  16. gen.MustParse(gen.NewTemplate("static").
  17. Funcs(template.FuncMap{"title": strings.ToTitle}).
  18. ParseFiles("template/static.tmpl")),
  19. },
  20. })
  21. if err != nil {
  22. log.Fatalf("running ent codegen: %v", err)
  23. }

特性列表

隐私层

隐私层允许为数据库中实体的查询和更变操作配置隐私政策。

This option can added to a project using the --feature privacy flag, and its full documentation exists in the privacy page.

EntQL过滤器

entql配置项在运行时为不同的查询构造器提供了通用的动态筛选功能。

This option can be added to a project using the --feature entql flag, and more information about it exists in the privacy page.

自动解决合并冲突

schema/snapshot配置项告诉entc (ent 代码生成) ,对最新结构 (schema) 生成一个快照,当用户的 结构 (schema) 不能构建时,自动使用生成的快照解决合并冲突。

This option can be added to a project using the --feature schema/snapshot flag, but please see ent/ent/issues/852 to get more context about it.

Schema配置

This option can be added to a project using the --feature sql/schemaconfig flag. 在生成代码之后,你就可以使用新的配置项,比如:

  1. c, err := ent.Open(dialect, conn, ent.AlternateSchema(ent.SchemaConfig{
  2. User: "usersdb",
  3. Car: "carsdb",
  4. c.User.Query().All(ctx) // SELECT * FROM `usersdb`.`users`
  5. c.Car.Query().All(ctx) // SELECT * FROM `carsdb`.`cars`

Row-level Locks

The sql/lock option lets configure row-level locking using the SQL SELECT ... FOR {UPDATE | SHARE} syntax.

This option can be added to a project using the --feature sql/lock flag.

  1. tx, err := client.Tx(ctx)
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. tx.Pet.Query().
  6. Where(pet.Name(name)).
  7. ForUpdate().
  8. Only(ctx)
  9. tx.Pet.Query().
  10. Where(pet.ID(id)).
  11. ForShare(
  12. sql.WithLockTables(pet.Table),
  13. sql.WithLockAction(sql.NoWait),
  14. ).
  15. Only(ctx)

Custom SQL Modifiers

The sql/modifier option lets add custom SQL modifiers to the builders and mutate the statements before they are executed.

This option can be added to a project using the --feature sql/modifier flag.

Example 1

The above code will produce the following SQL query:

  1. SELECT SUM(LENGTH(name)) FROM `pet`
  1. var p1 []struct {
  2. ent.Pet
  3. NameLength int `sql:"length"`
  4. }
  5. client.Pet.Query().
  6. Order(ent.Asc(pet.FieldID)).
  7. Modify(func(s *sql.Selector) {
  8. s.AppendSelect("LENGTH(name)")
  9. }).
  10. ScanX(ctx, &p1)

The above code will produce the following SQL query:

  1. SELECT `pet`.*, LENGTH(name) FROM `pet` ORDER BY `pet`.`id` ASC

Example 3

The above code will produce the following SQL query:

  1. SELECT
  2. SUM(`price`) AS `price`,
  3. FROM
  4. `users`
  5. WHERE
  6. `created_at` > x AND `created_at` < y
  7. GROUP BY
  8. DATE(created_at)
  9. ORDER BY
  10. DATE(created_at) DESC

Example 4

  1. var gs []struct {
  2. ent.Group
  3. UsersCount int `sql:"users_count"`
  4. }
  5. client.Group.Query().
  6. Order(ent.Asc(group.FieldID)).
  7. Modify(func(s *sql.Selector) {
  8. t := sql.Table(group.UsersTable)
  9. s.LeftJoin(t).
  10. On(
  11. s.C(group.FieldID),
  12. t.C(group.UsersPrimaryKey[1]),
  13. ).
  14. // Append the "users_count" column to the selected columns.
  15. AppendSelect(
  16. sql.As(sql.Count(t.C(group.UsersPrimaryKey[1])), "users_count"),
  17. ).
  18. GroupBy(s.C(group.FieldID))
  19. }).
  20. ScanX(ctx, &gs)

The above code will produce the following SQL query:

  1. SELECT
  2. `groups`.*,
  3. COUNT(`t1`.`group_id`) AS `users_count`
  4. FROM
  5. `groups` LEFT JOIN `user_groups` AS `t1`
  6. ON
  7. `groups`.`id` = `t1`.`group_id`
  8. GROUP BY
  9. `groups`.`id`
  10. `groups`.`id` ASC

Upsert

The sql/upsert option lets configure upsert and bulk-upsert logic using the SQL ON CONFLICT / syntax. For full documentation, go to the .

This option can be added to a project using the --feature sql/upsert flag.