Skip to content

Commit

Permalink
add order_by query arg (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
imperfect-fourth authored Jun 17, 2024
1 parent 981a757 commit c8bd16f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
12 changes: 7 additions & 5 deletions cmd/eywagen/eywatest/eywa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import (
)

func TestSelectQuery(t *testing.T) {
age := 12
q := eywa.Get[testTable]().Limit(2).Offset(1).DistinctOn(testTable_Age).Where(
age := 10
q := eywa.Get[testTable]().Limit(2).Offset(1).DistinctOn(testTable_Name).OrderBy(
eywa.Desc[testTable](testTable_Name),
).Where(
eywa.Or(
eywa.Eq[testTable](testTable_NameField("abc")),
eywa.Eq[testTable](testTable_NameField("abcd")),
eywa.Eq[testTable](testTable_AgeField(&age)),
),
).Select(testTable_Name)

expected := `query get_test_table {
test_table(limit: 2, offset: 1, distinct_on: age, where: {_or: [{name: {_eq: "abc"}}, {age: {_eq: 12}}]}) {
test_table(limit: 2, offset: 1, distinct_on: name, where: {_or: [{name: {_eq: "abcd"}}, {age: {_eq: 10}}]}, order_by: {name: desc}) {
name
}
}`
Expand All @@ -33,7 +35,7 @@ name
resp, err := q.Exec(c)

assert.NoError(t, err)
assert.Equal(t, []testTable{{Name: "a"}}, resp)
assert.Equal(t, []testTable{{Name: "abcd"}, {Name: "abc"}}, resp)
}
}

Expand Down
6 changes: 6 additions & 0 deletions eywa.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ func (sq GetQueryBuilder[M, FN, F]) Limit(n int) GetQueryBuilder[M, FN, F] {
return sq
}

func (sq GetQueryBuilder[M, FN, F]) OrderBy(o ...OrderByExpr) GetQueryBuilder[M, FN, F] {
orderByArr := orderBy(o)
sq.orderBy = &orderByArr
return sq
}

func (sq GetQueryBuilder[M, FN, F]) Where(w *WhereExpr) GetQueryBuilder[M, FN, F] {
sq.where = &where{w}
return sq
Expand Down
50 changes: 50 additions & 0 deletions query_arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type queryArgs[M Model, FN FieldName[M], F Field[M]] struct {
offset *offset
distinctOn *distinctOn[M, FN]
where *where
orderBy *orderBy
set *set[M, F]
}

Expand All @@ -20,6 +21,7 @@ func (qa queryArgs[M, FN, F]) marshalGQL() string {
args = appendArg(args, qa.offset)
args = appendArg(args, qa.distinctOn)
args = appendArg(args, qa.where)
args = appendArg(args, qa.orderBy)
args = appendArg(args, qa.set)

return fmt.Sprintf("(%s)", strings.Join(args, ", "))
Expand Down Expand Up @@ -204,3 +206,51 @@ func (w *WhereExpr) marshalGQL() string {
expr := fmt.Sprintf("{%s}", strings.Join(stringArr, ", "))
return expr
}

type OrderByExpr struct {
order string
field string
}

func (ob OrderByExpr) marshalGQL() string {
return fmt.Sprintf("%s: %s", ob.field, ob.order)
}

func Asc[M Model, FN FieldName[M]](field FN) OrderByExpr {
return OrderByExpr{"asc", string(field)}
}
func AscNullsFirsst[M Model, FN FieldName[M]](field FN) OrderByExpr {
return OrderByExpr{"asc_nulls_first", string(field)}
}
func AscNullsLast[M Model, FN FieldName[M]](field FN) OrderByExpr {
return OrderByExpr{"asc_nulls_last", string(field)}
}
func Desc[M Model, FN FieldName[M]](field FN) OrderByExpr {
return OrderByExpr{"desc", string(field)}
}
func DescNullsFirst[M Model, FN FieldName[M]](field FN) OrderByExpr {
return OrderByExpr{"desc_nulls_first", string(field)}
}
func DescNullsLast[M Model, FN FieldName[M]](field FN) OrderByExpr {
return OrderByExpr{"desc_nulls_last", string(field)}
}

type orderBy []OrderByExpr

func (oba orderBy) queryArgName() string {
return "order_by"
}

func (oba orderBy) marshalGQL() string {
if len(oba) == 0 {
return ""
}
stringArr := make([]string, 0, len(oba))
for _, ob := range oba {
expr := ob.marshalGQL()
if expr != "" {
stringArr = append(stringArr, expr)
}
}
return fmt.Sprintf("%s: {%s}", oba.queryArgName(), strings.Join(stringArr, ", "))
}
10 changes: 5 additions & 5 deletions unsafe/eywa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ type jsonbcol struct {
}

func TestSelectQuery(t *testing.T) {
q := Get[testTable]().Limit(2).Offset(1).DistinctOn("age").Where(
q := Get[testTable]().Limit(2).Offset(1).DistinctOn("name").OrderBy(eywa.Desc[testTable]("name")).Where(
eywa.Or(
eywa.Eq[testTable](eywa.RawField{"name", "abc"}),
eywa.Eq[testTable](eywa.RawField{"age", 12}),
eywa.Eq[testTable](eywa.RawField{"name", "abcd"}),
eywa.Eq[testTable](eywa.RawField{"age", 10}),
),
).Select("name")

expected := `query get_test_table {
test_table(limit: 2, offset: 1, distinct_on: age, where: {_or: [{name: {_eq: "abc"}}, {age: {_eq: 12}}]}) {
test_table(limit: 2, offset: 1, distinct_on: name, where: {_or: [{name: {_eq: "abcd"}}, {age: {_eq: 10}}]}, order_by: {name: desc}) {
name
}
}`
Expand All @@ -52,7 +52,7 @@ name
resp, err := q.Exec(c)

assert.NoError(t, err)
assert.Equal(t, []testTable{{Name: "a"}}, resp)
assert.Equal(t, []testTable{{Name: "abcd"}, {Name: "abc"}}, resp)
}
}

Expand Down

0 comments on commit c8bd16f

Please sign in to comment.