Files
ent/doc/md/predicates.md
Ariel Mashraki 2c8d7fcc8b ent/doc: add custom predicate exmaple
Reviewed By: alexsn

Differential Revision: D17926144

fbshipit-source-id: 5390585bf634aecfad86a729db6e633c4a7e6f5e
2019-10-15 06:42:44 -07:00

93 lines
1.3 KiB
Markdown
Executable File

---
id: predicates
title: Predicates
---
## 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 `owner` 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)
```
## Custom Predicates
Custom predicates can be useful if you want to write your own dialect-specific logic.
```go
pets := client.Pet.
Query().
Where(predicate.Pet(func(s *sql.Selector) {
s.Where(sql.InInts(pet.OwnerColumn, 1, 2, 3))
})).
AllX(ctx)
```