Move pagination_query.tmpl and time_mixin.go to starter
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ersteller_ent "git.gorlug.de/code/ersteller/schema/ent"
|
|
||||||
|
|
||||||
"entgo.io/ent"
|
"entgo.io/ent"
|
||||||
"entgo.io/ent/schema/edge"
|
"entgo.io/ent/schema/edge"
|
||||||
"entgo.io/ent/schema/field"
|
"entgo.io/ent/schema/field"
|
||||||
@@ -14,7 +12,7 @@ type GoogleAuth struct {
|
|||||||
|
|
||||||
func (GoogleAuth) Mixin() []ent.Mixin {
|
func (GoogleAuth) Mixin() []ent.Mixin {
|
||||||
return []ent.Mixin{
|
return []ent.Mixin{
|
||||||
ersteller_ent.TimeMixin{},
|
TimeMixin{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
{{ define "pagination_query" }}
|
||||||
|
// Code generated by entc template, DO NOT EDIT.
|
||||||
|
|
||||||
|
{{/* A template that adds the functionality for running each client <T> in debug mode */}}
|
||||||
|
|
||||||
|
{{/* Add the base header for the generated file */}}
|
||||||
|
{{ $pkg := base $.Config.Package }}
|
||||||
|
{{ template "header" $ }}
|
||||||
|
|
||||||
|
{{ range $n := $.Nodes }}
|
||||||
|
|
||||||
|
// PaginateAfterID paginates {{ $n.Name }} by monotonically increasing "id".
|
||||||
|
// It preserves any existing filters/joins applied to the query, fetches up to "limit" items,
|
||||||
|
// and returns the next cursor (last ID of this page) and whether a next page exists.
|
||||||
|
func (q *{{ $n.Name }}Query) PaginateAfterID(ctx context.Context, afterID, limit int) ([]*{{ $n.Name }}, int, bool, error) {
|
||||||
|
if limit <= 0 {
|
||||||
|
limit = 20
|
||||||
|
}
|
||||||
|
|
||||||
|
qq := q.Clone().
|
||||||
|
Order(func(s *sql.Selector) {
|
||||||
|
s.OrderBy(s.C("id"))
|
||||||
|
}).
|
||||||
|
Limit(limit + 1) // fetch one extra to detect "has next"
|
||||||
|
|
||||||
|
if afterID > 0 {
|
||||||
|
qq = qq.Where(func(s *sql.Selector) {
|
||||||
|
s.Where(sql.GT(s.C("id"), afterID))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := qq.All(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
hasNext := len(rows) > limit
|
||||||
|
if hasNext {
|
||||||
|
rows = rows[:limit]
|
||||||
|
}
|
||||||
|
|
||||||
|
next := 0
|
||||||
|
if len(rows) > 0 {
|
||||||
|
next = rows[len(rows)-1].ID
|
||||||
|
}
|
||||||
|
|
||||||
|
return rows, next, hasNext, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ end }}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package schema
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"entgo.io/ent"
|
||||||
|
"entgo.io/ent/schema/field"
|
||||||
|
"entgo.io/ent/schema/mixin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TimeMixin implements the ent.Mixin for sharing
|
||||||
|
// time fields with package schemas.
|
||||||
|
type TimeMixin struct {
|
||||||
|
mixin.Schema
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fields of the TimeMixin.
|
||||||
|
func (TimeMixin) Fields() []ent.Field {
|
||||||
|
return []ent.Field{
|
||||||
|
field.Time("created_at").
|
||||||
|
Default(time.Now).
|
||||||
|
Immutable(),
|
||||||
|
field.Time("updated_at").
|
||||||
|
Default(time.Now).
|
||||||
|
UpdateDefault(time.Now),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.gorlug.de/code/ersteller/schema/ent"
|
|
||||||
|
|
||||||
"entgo.io/ent"
|
"entgo.io/ent"
|
||||||
"entgo.io/ent/schema/edge"
|
"entgo.io/ent/schema/edge"
|
||||||
"entgo.io/ent/schema/field"
|
"entgo.io/ent/schema/field"
|
||||||
@@ -14,7 +12,7 @@ type Todo struct {
|
|||||||
|
|
||||||
func (Todo) Mixin() []ent.Mixin {
|
func (Todo) Mixin() []ent.Mixin {
|
||||||
return []ent.Mixin{
|
return []ent.Mixin{
|
||||||
ersteller_ent.TimeMixin{},
|
TimeMixin{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.gorlug.de/code/ersteller/schema/ent"
|
|
||||||
|
|
||||||
"entgo.io/ent"
|
"entgo.io/ent"
|
||||||
"entgo.io/ent/schema/field"
|
"entgo.io/ent/schema/field"
|
||||||
)
|
)
|
||||||
@@ -14,7 +12,7 @@ type User struct {
|
|||||||
|
|
||||||
func (User) Mixin() []ent.Mixin {
|
func (User) Mixin() []ent.Mixin {
|
||||||
return []ent.Mixin{
|
return []ent.Mixin{
|
||||||
ersteller_ent.TimeMixin{},
|
TimeMixin{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.gorlug.de/code/ersteller/starter/create"
|
|
||||||
"log"
|
"log"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"git.gorlug.de/code/ersteller/starter/create"
|
||||||
|
|
||||||
"entgo.io/ent/entc"
|
"entgo.io/ent/entc"
|
||||||
"entgo.io/ent/entc/gen"
|
"entgo.io/ent/entc/gen"
|
||||||
)
|
)
|
||||||
@@ -12,7 +13,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
log.Println("starter generate schema")
|
log.Println("starter generate schema")
|
||||||
thisDir := create.GetPathToThisDir()
|
thisDir := create.GetPathToThisDir()
|
||||||
paginationPath := path.Join(thisDir, "../../schema/ent/pagination_query.tmpl")
|
paginationPath := path.Join(thisDir, "./ent/schema/pagination_query.tmpl")
|
||||||
|
|
||||||
must(entc.Generate(
|
must(entc.Generate(
|
||||||
"./ent/schema",
|
"./ent/schema",
|
||||||
|
|||||||
Reference in New Issue
Block a user