Skip to content

numero33/dataloaden

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dataloaden

GitHub Workflow Status Go Report Card Coverage Status GitHub go.mod Go version GitHub top language GitHub code size in bytes GitHub

This is an implementation of Facebook's DataLoader in Golang 1.18.

Heavily inspired by vektah/dataloaden

Install

go get -u github.com/numero33/dataloaden

Usage

type User struct {
	ID   string
	Name string
}

// NewLoader will collect user requests for 2 milliseconds and send them as a single batch to the fetch func
// normally fetch would be a database call.
func NewLoader() *dataloaden.Loader[string, *User] {
	return dataloaden.NewLoader(dataloaden.LoaderConfig[string, *User]{
		Wait:     2 * time.Millisecond,
		MaxBatch: 100,
		Fetch: func(keys []string) ([]*User, []error) {
			users := make([]*User, len(keys))
			errors := make([]error, len(keys))

			for i, key := range keys {
				users[i] = &User{ID: key, Name: "user " + key}
			}
			return users, errors
		},
	})
}

func main() {
    userLoader := NewLoader()
    u, _ := dl.Load("E1")
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages