Skip to content

Latest commit

 

History

History
53 lines (41 loc) · 1.69 KB

README.md

File metadata and controls

53 lines (41 loc) · 1.69 KB

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")
}