MCPcopy
hub / github.com/ent/ent / Select

Function Select

entc/integration/integration_test.go:554–832  ·  view source on GitHub ↗
(t *testing.T, client *ent.Client)

Source from the content-addressed store, hash-verified

552}
553
554func Select(t *testing.T, client *ent.Client) {
555 ctx := context.Background()
556 require := require.New(t)
557
558 t.Log("select one field")
559 u := client.User.Create().SetName("foo").SetAge(30).SaveX(ctx)
560 name := client.User.
561 Query().
562 Where(user.ID(u.ID)).
563 Select(user.FieldName).
564 StringX(ctx)
565 require.Equal("foo", name)
566 client.User.Create().SetName("bar").SetAge(30).AddFriends(u).SaveX(ctx)
567 t.Log("select one field with ordering")
568 names := client.User.
569 Query().
570 Order(ent.Asc(user.FieldName)).
571 Select(user.FieldName).
572 StringsX(ctx)
573 require.Equal([]string{"bar", "foo"}, names)
574 names = client.User.
575 Query().
576 Order(ent.Desc(user.FieldName)).
577 Select(user.FieldName).
578 StringsX(ctx)
579 require.Equal([]string{"foo", "bar"}, names)
580 client.User.Create().SetName("baz").SetAge(30).SaveX(ctx)
581 names = client.User.
582 Query().
583 Order(ent.Asc(user.FieldName)).
584 Select(user.FieldName).
585 StringsX(ctx)
586 require.Equal([]string{"bar", "baz", "foo"}, names)
587
588 t.Log("select 2 fields")
589 var v []struct {
590 Age int `json:"age"`
591 Name string `json:"name"`
592 }
593 client.User.
594 Query().
595 Order(ent.Asc(user.FieldName)).
596 Select(user.FieldAge, user.FieldName).
597 ScanX(ctx, &v)
598 require.Equal([]int{30, 30, 30}, []int{v[0].Age, v[1].Age, v[2].Age})
599 require.Equal([]string{"bar", "baz", "foo"}, []string{v[0].Name, v[1].Name, v[2].Name})
600
601 users := client.User.
602 Query().
603 Select(user.FieldAge).
604 Where(user.Name("foo")).
605 WithFriends(func(q *ent.UserQuery) {
606 q.Select(user.FieldName)
607 }).
608 AllX(ctx)
609 for i := range users {
610 require.Empty(users[i].Name)
611 require.NotZero(users[i].ID)

Callers

nothing calls this directly

Calls 15

IDFunction · 0.92
AscFunction · 0.92
DescFunction · 0.92
NameFunction · 0.92
TableFunction · 0.92
ExprFuncFunction · 0.92
AsFunction · 0.92
CountFunction · 0.92
SelectExprFunction · 0.92
RawFunction · 0.92
SelectFunction · 0.92
EQFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…