Skip to content

Commit

Permalink
Add preloading of options to the repository
Browse files Browse the repository at this point in the history
  • Loading branch information
demdxx committed Aug 5, 2024
1 parent 4d6725f commit d671852
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions repository/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ type QueryPreparer interface {

// List select options
type listOptions[F, O QueryPreparer] struct {
Filter F
Order O
Page *Pagination
Filter F
Order O
Page *Pagination
Preloads []string
}

// Option for list query
Expand All @@ -38,8 +39,20 @@ func WithPagination[F, O QueryPreparer](page *Pagination) ListOption[F, O] {
}
}

// WithPreloads option for list query
func WithPreloads[F, O QueryPreparer](preloads []string) ListOption[F, O] {
return func(opts *listOptions[F, O]) {
opts.Preloads = preloads
}
}

// PrepareQuery prepare query with options
func (opts *listOptions[F, O]) PrepareQuery(query *gorm.DB) *gorm.DB {
if len(opts.Preloads) > 0 {
for _, preload := range opts.Preloads {
query = query.Preload(preload)
}
}
query = opts.Filter.PrepareQuery(query)
query = opts.Order.PrepareQuery(query)
query = opts.Page.PrepareQuery(query)
Expand Down Expand Up @@ -75,6 +88,10 @@ func (opts ListOptions[F, O]) WithPagination(page *Pagination) ListOptions[F, O]
return append(opts, WithPagination[F, O](page))
}

func (opts ListOptions[F, O]) WithPreloads(preloads []string) ListOptions[F, O] {
return append(opts, WithPreloads[F, O](preloads))
}

func (opts ListOptions[F, O]) PrepareQuery(query *gorm.DB) *gorm.DB {
return PrepareListOptions(query, opts...)
}

0 comments on commit d671852

Please sign in to comment.