Skip to content

Commit

Permalink
organize gormrelay package
Browse files Browse the repository at this point in the history
  • Loading branch information
molon committed Sep 25, 2024
1 parent 24f6ab8 commit d18e4ba
Show file tree
Hide file tree
Showing 4 changed files with 455 additions and 414 deletions.
20 changes: 0 additions & 20 deletions gormrelay/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,23 +260,3 @@ func (a *KeysetCounter[T]) Count(ctx context.Context) (int, error) {
func NewKeysetAdapter[T any](db *gorm.DB) relay.ApplyCursorsFunc[T] {
return cursor.NewKeysetAdapter(NewKeysetCounter[T](db))
}

func parseSchema(db *gorm.DB, v any) (*schema.Schema, error) {
stmt := &gorm.Statement{DB: db}
if err := stmt.Parse(v); err != nil {
return nil, errors.Wrap(err, "parse schema with db")
}
return stmt.Schema, nil
}

// If T is not a struct or struct pointer, we need to use db.Statement.Model to find or count
func shouldBasedOnModel[T any](db *gorm.DB) (bool, error) {
tType := reflect.TypeOf((*T)(nil)).Elem()
if tType.Kind() != reflect.Struct && (tType.Kind() != reflect.Ptr || tType.Elem().Kind() != reflect.Struct) {
if db.Statement.Model == nil {
return true, errors.New("db.Statement.Model is nil and T is not a struct or struct pointer")
}
return true, nil
}
return false, nil
}
Loading

0 comments on commit d18e4ba

Please sign in to comment.