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

@@ -67,6 +67,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 (
@@ -87,7 +88,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 {
@@ -99,7 +100,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

@@ -8,6 +8,8 @@ package group
import (
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
)
const (
@@ -73,3 +75,56 @@ var (
// DefaultName holds the default value on creation for the "name" field.
DefaultName string
)
// Order defines the ordering method for the Group 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()
}
// ByTenantID orders the results by the tenant_id field.
func ByTenantID(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldTenantID, opts...).ToFunc()
}
// ByName orders the results by the name field.
func ByName(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldName, opts...).ToFunc()
}
// ByTenantField orders the results by tenant field.
func ByTenantField(field string, opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newTenantStep(), sql.OrderByField(field, opts...))
}
}
// ByUsersCount orders the results by users count.
func ByUsersCount(opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborsCount(s, newUsersStep(), opts...)
}
}
// ByUsers orders the results by users terms.
func ByUsers(term sql.OrderTerm, terms ...sql.OrderTerm) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newUsersStep(), append([]sql.OrderTerm{term}, terms...)...)
}
}
func newTenantStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(TenantInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2O, false, TenantTable, TenantColumn),
)
}
func newUsersStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(UsersInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2M, true, UsersTable, UsersPrimaryKey...),
)
}

View File

@@ -166,11 +166,7 @@ func HasTenant() predicate.Group {
// HasTenantWith applies the HasEdge predicate on the "tenant" edge with a given conditions (other predicates).
func HasTenantWith(preds ...predicate.Tenant) predicate.Group {
return predicate.Group(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(TenantInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2O, false, TenantTable, TenantColumn),
)
step := newTenantStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)
@@ -193,11 +189,7 @@ func HasUsers() predicate.Group {
// HasUsersWith applies the HasEdge predicate on the "users" edge with a given conditions (other predicates).
func HasUsersWith(preds ...predicate.User) predicate.Group {
return predicate.Group(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(UsersInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2M, true, UsersTable, UsersPrimaryKey...),
)
step := newUsersStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)

View File

@@ -26,7 +26,7 @@ import (
type GroupQuery struct {
config
ctx *QueryContext
order []OrderFunc
order []group.Order
inters []Interceptor
predicates []predicate.Group
withTenant *TenantQuery
@@ -62,7 +62,7 @@ func (gq *GroupQuery) Unique(unique bool) *GroupQuery {
}
// Order specifies how the records should be ordered.
func (gq *GroupQuery) Order(o ...OrderFunc) *GroupQuery {
func (gq *GroupQuery) Order(o ...group.Order) *GroupQuery {
gq.order = append(gq.order, o...)
return gq
}
@@ -300,7 +300,7 @@ func (gq *GroupQuery) Clone() *GroupQuery {
return &GroupQuery{
config: gq.config,
ctx: gq.ctx.Clone(),
order: append([]OrderFunc{}, gq.order...),
order: append([]group.Order{}, gq.order...),
inters: append([]Interceptor{}, gq.inters...),
predicates: append([]predicate.Group{}, gq.predicates...),
withTenant: gq.withTenant.Clone(),

View File

@@ -8,6 +8,7 @@ package tenant
import (
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
)
const (
@@ -48,3 +49,16 @@ var (
// NameValidator is a validator for the "name" field. It is called by the builders before save.
NameValidator func(string) error
)
// Order defines the ordering method for the Tenant 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()
}
// ByName orders the results by the name field.
func ByName(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldName, opts...).ToFunc()
}

View File

@@ -23,7 +23,7 @@ import (
type TenantQuery struct {
config
ctx *QueryContext
order []OrderFunc
order []tenant.Order
inters []Interceptor
predicates []predicate.Tenant
// intermediate query (i.e. traversal path).
@@ -57,7 +57,7 @@ func (tq *TenantQuery) Unique(unique bool) *TenantQuery {
}
// Order specifies how the records should be ordered.
func (tq *TenantQuery) Order(o ...OrderFunc) *TenantQuery {
func (tq *TenantQuery) Order(o ...tenant.Order) *TenantQuery {
tq.order = append(tq.order, o...)
return tq
}
@@ -251,7 +251,7 @@ func (tq *TenantQuery) Clone() *TenantQuery {
return &TenantQuery{
config: tq.config,
ctx: tq.ctx.Clone(),
order: append([]OrderFunc{}, tq.order...),
order: append([]tenant.Order{}, tq.order...),
inters: append([]Interceptor{}, tq.inters...),
predicates: append([]predicate.Tenant{}, tq.predicates...),
// clone intermediate query.

View File

@@ -8,6 +8,8 @@ package user
import (
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
)
const (
@@ -76,3 +78,56 @@ var (
// DefaultName holds the default value on creation for the "name" field.
DefaultName string
)
// 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()
}
// ByTenantID orders the results by the tenant_id field.
func ByTenantID(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldTenantID, opts...).ToFunc()
}
// ByName orders the results by the name field.
func ByName(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldName, opts...).ToFunc()
}
// ByTenantField orders the results by tenant field.
func ByTenantField(field string, opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newTenantStep(), sql.OrderByField(field, opts...))
}
}
// ByGroupsCount orders the results by groups count.
func ByGroupsCount(opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborsCount(s, newGroupsStep(), opts...)
}
}
// ByGroups orders the results by groups terms.
func ByGroups(term sql.OrderTerm, terms ...sql.OrderTerm) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newGroupsStep(), append([]sql.OrderTerm{term}, terms...)...)
}
}
func newTenantStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(TenantInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2O, false, TenantTable, TenantColumn),
)
}
func newGroupsStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(GroupsInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, GroupsTable, GroupsPrimaryKey...),
)
}

View File

@@ -176,11 +176,7 @@ func HasTenant() predicate.User {
// HasTenantWith applies the HasEdge predicate on the "tenant" edge with a given conditions (other predicates).
func HasTenantWith(preds ...predicate.Tenant) predicate.User {
return predicate.User(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(TenantInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2O, false, TenantTable, TenantColumn),
)
step := newTenantStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)
@@ -203,11 +199,7 @@ func HasGroups() predicate.User {
// HasGroupsWith applies the HasEdge predicate on the "groups" edge with a given conditions (other predicates).
func HasGroupsWith(preds ...predicate.Group) predicate.User {
return predicate.User(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(GroupsInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, GroupsTable, GroupsPrimaryKey...),
)
step := newGroupsStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)

View File

@@ -26,7 +26,7 @@ import (
type UserQuery struct {
config
ctx *QueryContext
order []OrderFunc
order []user.Order
inters []Interceptor
predicates []predicate.User
withTenant *TenantQuery
@@ -62,7 +62,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
}
@@ -300,7 +300,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...),
withTenant: uq.withTenant.Clone(),