查询和更新

    我们先在可测试示例中创建一个 Todo。 我们将以下代码添加到 来完成这项工作:

    执行 go test,它应该是能通过测试的。

    正如你看到的,我们的 Todo 只有一个 ID 字段,什么事也做不了。 让我们来改进它,在 todo/ent/schema/todo.go 中给它增加一些字段:

    1. func (Todo) Fields() []ent.Field {
    2. return []ent.Field{
    3. field.Text("text").
    4. NotEmpty(),
    5. field.Time("created_at").
    6. Default(time.Now).
    7. Immutable(),
    8. field.Enum("status").
    9. NamedValues(
    10. "InProgress", "IN_PROGRESS",
    11. "Completed", "COMPLETED",
    12. ).
    13. Default("IN_PROGRESS"),
    14. field.Int("priority").
    15. Default(0),
    16. }
    17. }

      你可能注意到了,除了 text 字段,其他字段在创建时都有一个默认值,唯独它必须由用户提供。 让我们修改 example_test.go 来适配刚刚的变动:

      很好! 我们在数据库中成功创建了一张表,包含了 5 列 (id, text, created_at, status, ),并且通过插入了两行数据到表中;即我们创建了两个任务到我们的 Todo list 中。

      Let’s change our schema again in todo/ent/schema/todo.go:

      1. func (Todo) Edges() []ent.Edge {
      2. return []ent.Edge{
      3. edge.To("parent", Todo.Type).
      4. Unique().
      5. From("children"),
      6. }
      7. }

      After adding these edges, we need to run the code-generation as before:

      1. go generate ./ent

      We continue our edges example, by updating the 2 todo items we just created. We define that item-2 (“Add Tracing Example”) depends on item-1 (“Add GraphQL Example”).

      After connecting item-2 to item-1, we’re ready to start querying our todo list.

      Query all todo items:

      1. func Example_Todo() {
      2. // ...
      3. // Query all todo items.
      4. items, err := client.Todo.Query().All(ctx)
      5. if err != nil {
      6. log.Fatalf("failed querying todos: %v", err)
      7. }
      8. for _, t := range items {
      9. fmt.Printf("%d: %q\n", t.ID, t.Text)
      10. }
      11. // Output:
      12. // 1: "Add GraphQL Example"
      13. // 2: "Add Tracing Example"
      14. }

      Query all todo items that depend on other items:

      1. func Example_Todo() {
      2. items, err := client.Todo.Query().Where(todo.HasParent()).All(ctx)
      3. if err != nil {
      4. log.Fatalf("failed querying todos: %v", err)
      5. }
      6. for _, t := range items {
      7. fmt.Printf("%d: %q\n", t.ID, t.Text)
      8. }
      9. // Output:
      10. // 2: "Add Tracing Example"
      11. }

      Query all todo items that don’t depend on other items and have items that depend on them:

      Query parent through its children:

      1. func Example_Todo() {
      2. // ...
      3. // Get a parent item through its children and expect the
      4. // query to return exactly one item.
      5. parent, err := client.Todo.Query(). // Query all todos.
      6. Where(todo.HasParent()). // Filter only those with parents.
      7. QueryParent(). // Continue traversals to the parents.
      8. Only(ctx) // Expect exactly one item.
      9. if err != nil {
      10. log.Fatalf("failed querying todos: %v", err)
      11. }
      12. fmt.Printf("%d: %q\n", parent.ID, parent.Text)
      13. // Output:
      14. // 1: "Add GraphQL Example"