ent/doc: traversal and aggregation and predicates

Reviewed By: alexsn

Differential Revision: D17113889

fbshipit-source-id: edf4d9af9660fe31e0d02f58a65bcbc4d549a695
This commit is contained in:
Ariel Mashraki
2019-08-29 09:00:12 -07:00
committed by Facebook Github Bot
parent b71ee6820b
commit 78c58190be
7 changed files with 171 additions and 16 deletions

View File

@@ -1,5 +1,52 @@
---
id: aggregate
title: Aggregations
title: Aggregation
---
Lorem ipsum.
## Group By
Group by `name` and `age` fields of all users, and sum their total age.
```go
package main
import (
"context"
"<project>/ent"
"<project>/ent/user"
)
func Do(ctx context.Context, client *ent.Client) {
var v []struct {
Name string `json:"name"`
Age int `json:"age"`
Sum int `json:"sum"`
Count int `json:"count"`
}
err := client.User.Query().
GroupBy(user.FieldName, user.FieldAge).
Aggregate(ent.Count(), ent.Sum(user.FieldAge)).
Scan(ctx, &v)
}
```
Group by one field.
```go
package main
import (
"context"
"<project>/ent"
"<project>/ent/user"
)
func Do(ctx context.Context, client *ent.Client) {
names, err := client.User.
Query().
GroupBy(user.FieldName).
Strings(ctx)
}
```

View File

@@ -1,6 +0,0 @@
---
id: doc4
title: Other Document
---
this is another document

View File

@@ -1,6 +0,0 @@
---
id: doc5
title: Fifth Document
---
Another one

37
doc/md/paging.md Executable file
View File

@@ -0,0 +1,37 @@
---
id: paging
title: Paging And Ordering
---
## Limit
`Limit` limits the query result to `n` entities.
```go
users, err := client.User.
Query().
Limit(n).
All(ctx)
```
## Offset
`Offset` sets the first vertex to return from the query.
```go
users, err := client.User.
Query().
Offset(10).
All(ctx)
```
## Ordering
`Order` returns the entities sorted by the values of one or more fields.
```go
users, err := client.User.Query().
Order(ent.Asc(user.FieldName)).
All(ctx)
```

View File

@@ -2,4 +2,79 @@
id: predicates
title: Predicates
---
Lorem ipsum.
## Field Predicates
- **Bool**:
- =, !=
- **Numeric**:
- =, !=, >, <, >=, <=,
- IN, NOT IN
- **Time**:
- =, !=, >, <, >=, <=
- IN, NOT IN
- **String**:
- =, !=, >, <, >=, <=
- IN, NOT IN
- Contains, HasPrefix, HasSuffix
- ContainsFold, EqualFold (**SQL** specific)
- **Optional** fields:
- IsNil, NotNil
## Edge Predicates
- **HasEdge**. For example, for edge named `owenr` of type `Pet`, use:
```go
client.Pet.
Query().
Where(user.HasOwner()).
All(ctx)
```
- **HasEdgeWith**. Add list of predicates for edge predicate.
```go
client.Pet.
Query().
Where(user.HasOwnerWith(user.Name("a8m"))).
All(ctx)
```
## Negation (NOT)
```go
client.Pet.
Query().
Where(user.Not(user.NameHasPrefix("Ari"))).
All(ctx)
```
## Disjunction (OR)
```go
client.Pet.
Query().
Where(
user.Or(
user.HasOwner(),
user.Not(user.HasFriends()),
)
).
All(ctx)
```
## Conjunction (AND)
```go
client.Pet.
Query().
Where(
user.And(
user.HasOwner(),
user.Not(user.HasFriends()),
)
).
All(ctx)
```

View File

@@ -2,4 +2,11 @@
id: transactions
title: Transactions
---
Lorem ipsum.
## Starting A Transaction
## Transactional Client
## Best Practices

View File

@@ -15,6 +15,7 @@
"traversals",
"aggregate",
"predicates",
"paging",
"transactions"
],
"Migration": [