dataloader

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: Apache-2.0 Imports: 4 Imported by: 1

README

Dataloader

DataLoader struct to avoiding N+1 queries when resolving graphql objects.

N+1 queries are a performance problem in which the application makes database queries in a loop, instead of making a single query that returns or modifies all the information at once. Each database connection takes some amount of time, so querying the database in a loop can be many times slower than doing it just once.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchLoader

type BatchLoader[K string | int, T any] interface {
	// Batch function that is called with a list of keys. This function should return a map of values for each passed key.
	// Should be implemented by the user.
	BatchLoad(ctx context.Context, keys *[]K) (map[K]*T, error)
}

type DataLoader

type DataLoader[K string, T any] struct {
	// contains filtered or unexported fields
}

func NewDataLoader

func NewDataLoader[K string, T any](ctx context.Context, batchLoader BatchLoader[K, T], maxBatchSize int32, maxBatchTimeMs int32) *DataLoader[K, T]

func (*DataLoader[K, T]) Load

func (d *DataLoader[K, T]) Load(key K) (*T, error)

Load retrieves a value for a given key. If the value is not already cached, the loader will call the batch function with a list of keys.

func (*DataLoader[K, T]) LoadMany

func (d *DataLoader[K, T]) LoadMany(keys *[]K) ([]*T, error)

LoadMany retrieves multiple values for a given list of keys. For each key that is not already cached, a single loader call will be scheduled.

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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