loaders

package
v0.0.0-...-d47f4a9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetByID

func GetByID[k comparable, t any](ctx context.Context, loader *dataloader.Loader[k, t], id k) (t, error)

GetByID returns the object from the loader

func GetMany

func GetMany[k comparable, t any](ctx context.Context, loader *dataloader.Loader[k, t], ids []k) ([]t, error)

GetMany retrieves multiple items from specified loader

func NewMemoryLoaderCache

func NewMemoryLoaderCache[K comparable, V any](ctx context.Context, cacheKey string, expiration time.Duration) dataloader.Cache[K, V]

NewMemoryLoaderCache returns a new memory cache

func WithOnDelete

func WithOnDelete(cb func()) any

WithOnDelete tells the collection to run this function when entry is deleted

Types

type BatchLoaders

type BatchLoaders struct {
	ApplicationLoader           *Loader[int, *common.Application]
	ApplicationIDFromCodeLoader *Loader[string, *int]
	ApplicationGroupLoader      *Loader[uuid.UUID, *common.ApplicationGroup]

	RedirectLoader                     *Loader[uuid.UUID, *common.Redirect]
	RedirectIDFromCodeLoader           *Loader[string, *uuid.UUID]
	PageLoader                         *Loader[int, *common.Page]
	PageIDFromCodeLoader               *Loader[string, *int]
	CollectionIDFromSlugLoader         *Loader[string, *int]
	SectionLoader                      *Loader[int, *common.Section]
	CollectionLoader                   *Loader[int, *common.Collection]
	CollectionItemLoader               *Loader[int, []*common.CollectionItem]
	StudyTopicLoader                   *Loader[uuid.UUID, *common.StudyTopic]
	StudyLessonLoader                  *Loader[uuid.UUID, *common.Lesson]
	StudyTaskLoader                    *Loader[uuid.UUID, *common.Task]
	StudyQuestionAlternativesLoader    *Loader[uuid.UUID, []*common.QuestionAlternative]
	ShowLoader                         *Loader[int, *common.Show]
	SeasonLoader                       *Loader[int, *common.Season]
	EpisodeLoader                      *Loader[int, *common.Episode]
	EpisodeIDFromLegacyIDLoader        *Loader[int, *int]
	EpisodeIDFromLegacyProgramIDLoader *Loader[int, *int]
	LinkLoader                         *Loader[int, *common.Link]
	PlaylistLoader                     *Loader[uuid.UUID, *common.Playlist]

	FilesLoader   *Loader[int, []*common.File]
	StreamsLoader *Loader[int, []*common.Stream]

	AssetFilesLoader   *Loader[int, []*common.File]
	AssetStreamsLoader *Loader[int, []*common.Stream]

	EventLoader        *Loader[int, *common.Event]
	EventEntriesLoader *Loader[int, []*int]

	FAQCategoryLoader    *Loader[uuid.UUID, *common.FAQCategory]
	QuestionLoader       *Loader[uuid.UUID, *common.Question]
	MessageGroupLoader   *Loader[int, *common.MessageGroup]
	SurveyLoader         *Loader[uuid.UUID, *common.Survey]
	SurveyQuestionLoader *Loader[uuid.UUID, *common.SurveyQuestion]
	GameLoader           *Loader[uuid.UUID, *common.Game]
	ShortLoader          *Loader[uuid.UUID, *common.Short]
	ShortsMediaIDLoader  *Loader[uuid.UUID, *uuid.UUID]

	MemberLoader       *Loader[int, *members.Member]
	OrganizationLoader *Loader[uuid.UUID, *members.Organization]

	EpisodeProgressLoader   *Loader[uuid.UUID, []*int]
	EpisodeIDFromUuidLoader *Loader[uuid.UUID, *int]
	ShowIDFromUuidLoader    *Loader[uuid.UUID, *int]
	// Permissions
	ShowPermissionLoader    *Loader[int, *common.Permissions[int]]
	SeasonPermissionLoader  *Loader[int, *common.Permissions[int]]
	EpisodePermissionLoader *Loader[int, *common.Permissions[int]]
	PagePermissionLoader    *Loader[int, *common.Permissions[int]]
	SectionPermissionLoader *Loader[int, *common.Permissions[int]]

	CompletedTopicsLoader         *Loader[uuid.UUID, []*uuid.UUID]
	CompletedLessonsLoader        *Loader[uuid.UUID, []*uuid.UUID]
	CompletedTasksLoader          *Loader[uuid.UUID, []*uuid.UUID]
	CompletedAndLockedTasksLoader *Loader[uuid.UUID, []*uuid.UUID]

	// Achievements
	AchievementLoader             *Loader[uuid.UUID, *common.Achievement]
	AchievementGroupLoader        *Loader[uuid.UUID, *common.AchievementGroup]
	AchievementsLoader            *Loader[uuid.UUID, []*uuid.UUID]
	UnconfirmedAchievementsLoader *Loader[uuid.UUID, []*uuid.UUID]

	AchievementGroupAchievementsLoader *Loader[uuid.UUID, []*uuid.UUID]

	ComputedDataLoader *Loader[uuid.UUID, []*common.ComputedData]

	UserLoader *Loader[string, *common.User]

	// UserCollections
	UserCollectionLoader         *Loader[uuid.UUID, *common.UserCollection]
	UserCollectionEntryLoader    *Loader[uuid.UUID, *common.UserCollectionEntry]
	UserCollectionEntryIDsLoader *Loader[uuid.UUID, []*uuid.UUID]

	ProfileUserCollectionIDsLoader *Loader[uuid.UUID, []*uuid.UUID]
	ProfileMyListCollectionID      *Loader[uuid.UUID, *uuid.UUID]

	PromptLoader *Loader[uuid.UUID, *common.Prompt]

	MediaItemPrimaryEpisodeIDLoader *Loader[uuid.UUID, *int]
	TimedMetadataLoader             *Loader[uuid.UUID, *common.TimedMetadata]
	ChaptersLoader                  *Loader[int, []*common.TimedMetadata]
	PersonLoader                    *Loader[uuid.UUID, *common.Person]
	SongLoader                      *Loader[uuid.UUID, *common.Song]
	PhraseLoader                    *Loader[string, *common.Phrase]
	ContributionsLoader             *Loader[int32, *common.Contribution]
}

BatchLoaders contains loaders for the different items

func InitBatchLoaders

func InitBatchLoaders(queries *sqlc.Queries, membersClient *members.Client) *BatchLoaders

type Collection

type Collection[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Collection is a collection of loaders or other advanced structures

func NewCollection

func NewCollection[K comparable, V any](expiration time.Duration) *Collection[K, V]

NewCollection of loaders or other structures

func (Collection[K, V]) Delete

func (c Collection[K, V]) Delete(key K)

Delete the specified key (&entry)

func (Collection[K, V]) DeleteExpired

func (c Collection[K, V]) DeleteExpired()

DeleteExpired entries

func (Collection[K, V]) Get

func (c Collection[K, V]) Get(key K) (value V, ok bool)

Get a value by the specified key

func (Collection[K, V]) Keys

func (c Collection[K, V]) Keys() []K

Keys returns all keys

func (Collection[K, V]) Set

func (c Collection[K, V]) Set(key K, value V, opts ...any)

Set a key to specified value

type HasKey

type HasKey[k comparable] interface {
	GetKey() k
}

HasKey interface for items with keys

type Loader

type Loader[K comparable, V any] struct {
	*dataloader.Loader[K, V]
}

Loader contains loader and additional functions to retrieve data easily

func New

func New[K comparable, V any](
	ctx context.Context,
	factory func(ctx context.Context, ids []K) ([]V, error),
	opts ...Option,
) *Loader[K, *V]

New creates a new batch loader

func NewConversionLoader

func NewConversionLoader[o comparable, rt any](
	ctx context.Context,
	converter func(ctx context.Context, ids []o) ([]common.Conversion[o, rt], error),
	opts ...Option,
) *Loader[o, *rt]

NewConversionLoader creates a new batch loader that converts between two types. It's particularly useful for when the query returns data of a different type than the object we want out.

The function takes a `converter` function that knows how to convert a batch of input keys (type O) into a slice of Conversion[O, R] results. The loader handles batching, caching, and error propagation automatically.

Parameters:

  • ctx: Context for cancellation and timeouts
  • factory: Function that converts a batch of input keys to Conversion results
  • opts: Optional configuration (e.g., WithMemoryCache, WithName)

Returns:

  • *Loader[O, *R]: A loader that can be used to fetch individual or batched results

Example:

loader := NewConversionLoader(
	ctx,
	func(ctx context.Context, ids []uuid.UUID) ([]Conversion[uuid.UUID, string], error) {
		// Fetch and convert data here
		return conversions, nil
	},
	WithMemoryCache(5*time.Minute),
	WithName("my-loader"),
)

func NewCustomLoader

func NewCustomLoader[K comparable, V any](
	ctx context.Context,
	factory func(ctx context.Context, ids []K) ([]V, error),
	getKey func(V) K,
	opts ...Option,
) *Loader[K, *V]

NewCustomLoader returns a configured batch loader for items

func NewFilterLoader

func NewFilterLoader[K comparable](ctx context.Context, factory func(ctx context.Context, keys []K) ([]K, error), opts ...Option) *Loader[K, *K]

NewFilterLoader is just for filtering a list of keys or checking if user has access to a specific id

func NewListLoader

func NewListLoader[K comparable, V any](
	ctx context.Context,
	factory func(ctx context.Context, ids []K) ([]V, error),
	getKey func(item V) K,
	opts ...Option,
) *Loader[K, []*V]

NewListLoader returns a configured batch loader for Lists

func NewLoader

func NewLoader[K comparable, V HasKey[K]](
	ctx context.Context,
	factory func(ctx context.Context, ids []K) ([]V, error),
	opts ...Option,
) *Loader[K, *V]

NewLoader returns a configured batch loader for items

func NewRelationLoader

func NewRelationLoader[K comparable, R comparable](
	ctx context.Context,
	factory func(ctx context.Context, ids []R) ([]common.Relation[K, R], error),
	opts ...Option,
) *Loader[R, []*K]

NewRelationLoader returns a configured batch loader for Lists

func (*Loader[K, V]) Get

func (bl *Loader[K, V]) Get(ctx context.Context, key K) (V, error)

Get retrieves a specific entry from the loader

func (*Loader[K, V]) GetMany

func (bl *Loader[K, V]) GetMany(ctx context.Context, keys []K) ([]V, error)

GetMany retrieves the specified entries from the loader

type LoaderCache

type LoaderCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LoaderCache is a cache for batchloaders

func (*LoaderCache[K, V]) Clear

func (c *LoaderCache[K, V]) Clear()

Clear clears the entire cache

func (*LoaderCache[K, V]) Delete

func (c *LoaderCache[K, V]) Delete(_ context.Context, key K) bool

Delete deletes the specified key

func (*LoaderCache[K, V]) Get

func (c *LoaderCache[K, V]) Get(_ context.Context, key K) (dataloader.Thunk[V], bool)

Get retrieves an entry from the cache

func (*LoaderCache[K, V]) Set

func (c *LoaderCache[K, V]) Set(_ context.Context, key K, val dataloader.Thunk[V])

Set sets the specified key to value

type LoadersWithPermissions

type LoadersWithPermissions struct {
	Key string

	EpisodeFilterLoader     *Loader[int, *int]
	EpisodeUUIDFilterLoader *Loader[uuid.UUID, *uuid.UUID]
	EpisodesLoader          *Loader[int, []*int]
	SeasonFilterLoader      *Loader[int, *int]
	SeasonsLoader           *Loader[int, []*int]
	ShowFilterLoader        *Loader[int, *int]
	ShowUUIDFilterLoader    *Loader[uuid.UUID, *uuid.UUID]
	SectionsLoader          *Loader[int, []*int]
	CalendarEntryLoader     *Loader[int, *common.CalendarEntry]
	StudyTopicFilterLoader  *Loader[uuid.UUID, *uuid.UUID]
	StudyLessonsLoader      *Loader[uuid.UUID, []*uuid.UUID]
	StudyLessonFilterLoader *Loader[uuid.UUID, *uuid.UUID]
	StudyTasksLoader        *Loader[uuid.UUID, []*uuid.UUID]
	StudyTaskFilterLoader   *Loader[uuid.UUID, *uuid.UUID]
	SurveyQuestionsLoader   *Loader[uuid.UUID, []*uuid.UUID]

	FAQQuestionsLoader *Loader[uuid.UUID, []*uuid.UUID]

	//Relations
	StudyLessonEpisodesLoader *Loader[uuid.UUID, []*int]
	EpisodeStudyLessonsLoader *Loader[int, []*uuid.UUID]
	StudyLessonLinksLoader    *Loader[uuid.UUID, []*int]
	LinkStudyLessonsLoader    *Loader[int, []*uuid.UUID]

	// Lists
	PromptIDsLoader       func(ctx context.Context) ([]uuid.UUID, error)
	FAQCategoryIDsLoader  func(ctx context.Context) ([]uuid.UUID, error)
	ShortIDsLoader        func(ctx context.Context) ([][]uuid.UUID, error)
	ShortWithScoresLoader func(ctx context.Context) ([]common.ShortIDWithMeta, error)
}

LoadersWithPermissions contains loaders that will be filtered by permissions.

func GetLoadersForRoles

func GetLoadersForRoles(queries *sqlc.Queries, roles []string) *LoadersWithPermissions

type MemoryCache

type MemoryCache struct {
	// contains filtered or unexported fields
}

MemoryCache is a simple memory cache

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is the interface for all options

func WithKeyFunc

func WithKeyFunc[K comparable, V any](getKey func(V) K) Option

WithKeyFunc specifies that the key should be retrieved with this function.

func WithMemoryCache

func WithMemoryCache(expiration time.Duration) Option

WithMemoryCache defines how long a key should live in the cache

func WithName

func WithName(name string) Option

WithName assigns the specified name to the tracer

type PersonalizedLoaders

type PersonalizedLoaders struct {
	Key string

	CollectionItemsLoader        *Loader[int, []*common.CollectionItem]
	ContributionsForPersonLoader *Loader[uuid.UUID, []*common.Contribution]
	TagEpisodesLoader            *Loader[int, []*int]
}

PersonalizedLoaders contains loaders that are personalized to the user

This includes things like permissions, settings, langauges, etc

func GetPersonalizedLoaders

func GetPersonalizedLoaders(
	queries *sqlc.Queries,
	roles []string,
	languagePreferences common.LanguagePreferences,
) *PersonalizedLoaders

type ProfileLoaders

type ProfileLoaders struct {
	ProgressLoader                     *Loader[int, *common.Progress]
	TaskCompletedLoader                *Loader[uuid.UUID, *uuid.UUID]
	TaskAlternativesAnswersCountLoader *Loader[uuid.UUID, *common.AlternativesTasksProgress]
	AchievementAchievedAtLoader        *Loader[uuid.UUID, *common.Achieved]
	GetSelectedAlternativesLoader      *Loader[uuid.UUID, *common.SelectedAlternatives]

	SeasonDefaultEpisodeLoader *Loader[int, *int]
	ShowDefaultEpisodeLoader   *Loader[int, *int]

	MediaProgressLoader *Loader[uuid.UUID, *common.MediaProgress]

	TopicDefaultLessonLoader *Loader[uuid.UUID, *uuid.UUID]
}

ProfileLoaders contains loaders per profile

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL