entc/gen: add fluent-api for order options (#3449)

This commit is contained in:
Ariel Mashraki
2023-04-09 10:37:42 +03:00
committed by GitHub
parent 6165bdea32
commit 064c9118b7
436 changed files with 8086 additions and 1652 deletions

View File

@@ -65,6 +65,7 @@ func NewTxContext(parent context.Context, tx *Tx) context.Context {
}
// OrderFunc applies an ordering on the sql selector.
// Deprecated: Use Asc/Desc functions or the package builders instead.
type OrderFunc func(*sql.Selector)
var (
@@ -83,7 +84,7 @@ func checkColumn(table, column string) error {
}
// Asc applies the given fields in ASC order.
func Asc(fields ...string) OrderFunc {
func Asc(fields ...string) func(*sql.Selector) {
return func(s *sql.Selector) {
for _, f := range fields {
if err := checkColumn(s.TableName(), f); err != nil {
@@ -95,7 +96,7 @@ func Asc(fields ...string) OrderFunc {
}
// Desc applies the given fields in DESC order.
func Desc(fields ...string) OrderFunc {
func Desc(fields ...string) func(*sql.Selector) {
return func(s *sql.Selector) {
for _, f := range fields {
if err := checkColumn(s.TableName(), f); err != nil {

View File

@@ -6,6 +6,11 @@
package user
import (
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
)
const (
// Label holds the string label denoting the user type in the database.
Label = "user"
@@ -45,3 +50,42 @@ func ValidColumn(column string) bool {
}
return false
}
// Order defines the ordering method for the User queries.
type Order func(*sql.Selector)
// ByID orders the results by the id field.
func ByID(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldID, opts...).ToFunc()
}
// ByAge orders the results by the age field.
func ByAge(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldAge, opts...).ToFunc()
}
// ByName orders the results by the name field.
func ByName(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldName, opts...).ToFunc()
}
// ByFriendsCount orders the results by friends count.
func ByFriendsCount(opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborsCount(s, newFriendsStep(), opts...)
}
}
// ByFriends orders the results by friends terms.
func ByFriends(term sql.OrderTerm, terms ...sql.OrderTerm) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newFriendsStep(), append([]sql.OrderTerm{term}, terms...)...)
}
}
func newFriendsStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(Table, FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, FriendsTable, FriendsPrimaryKey...),
)
}

View File

@@ -186,11 +186,7 @@ func HasFriends() predicate.User {
// HasFriendsWith applies the HasEdge predicate on the "friends" edge with a given conditions (other predicates).
func HasFriendsWith(preds ...predicate.User) predicate.User {
return predicate.User(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(Table, FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, FriendsTable, FriendsPrimaryKey...),
)
step := newFriendsStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)

View File

@@ -23,7 +23,7 @@ import (
type UserQuery struct {
config
ctx *QueryContext
order []OrderFunc
order []user.Order
inters []Interceptor
predicates []predicate.User
withFriends *UserQuery
@@ -58,7 +58,7 @@ func (uq *UserQuery) Unique(unique bool) *UserQuery {
}
// Order specifies how the records should be ordered.
func (uq *UserQuery) Order(o ...OrderFunc) *UserQuery {
func (uq *UserQuery) Order(o ...user.Order) *UserQuery {
uq.order = append(uq.order, o...)
return uq
}
@@ -274,7 +274,7 @@ func (uq *UserQuery) Clone() *UserQuery {
return &UserQuery{
config: uq.config,
ctx: uq.ctx.Clone(),
order: append([]OrderFunc{}, uq.order...),
order: append([]user.Order{}, uq.order...),
inters: append([]Interceptor{}, uq.inters...),
predicates: append([]predicate.User{}, uq.predicates...),
withFriends: uq.withFriends.Clone(),