Skip to content

Instantly share code, notes, and snippets.

@benbpyle
Created February 25, 2023 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbpyle/4744318d476513e5452826d9a1430504 to your computer and use it in GitHub Desktop.
Save benbpyle/4744318d476513e5452826d9a1430504 to your computer and use it in GitHub Desktop.
Using BatchGetItem to fetch
func (d *DynamoDBCompanyRepository) GetCompanies(ctx context.Context, companyIds []string) ([]models.Company, error) {
var keys []map[string]*dynamodb.AttributeValue
for _, c := range companyIds {
key := models.GetCompanyKey(c)
m := map[string]*dynamodb.AttributeValue{
"PK": {
S: aws.String(key),
},
"SK": {
S: aws.String(key),
},
}
keys = append(keys, m)
}
input := &dynamodb.BatchGetItemInput{
RequestItems: map[string]*dynamodb.KeysAndAttributes{
d.tableName: {
Keys: keys,
},
},
}
log.WithFields(log.Fields{
"input": input,
}).Debug("The query input")
var companies []models.Company
err := d.db.BatchGetItemPagesWithContext(
ctx, input,
func(page *dynamodb.BatchGetItemOutput, lastPage bool) bool {
for _, v := range page.Responses {
for _, v2 := range v {
var c models.Company
_ = dynamodbattribute.UnmarshalMap(v2, &c)
companies = append(companies, c)
}
}
return lastPage
})
if err != nil {
return nil, err
}
return companies, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment