Created
February 25, 2023 15:06
-
-
Save benbpyle/4744318d476513e5452826d9a1430504 to your computer and use it in GitHub Desktop.
Using BatchGetItem to fetch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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