Since GSI auto updated from the base table, how do I make sure that the GSI partition and sort key values have prefix? For instance Email
field is not part of primary key but I need email#
prefix on partition key of GSI.
This is how I put item first time.
func (d DynamoDB) CreateUser(ctx context.Context, user store.User) error {
model.PrimaryKey = store.PrimaryKey{
PK: "user#" + user.ID,
SK: "user#" + user.ID,
}
item, err := attributevalue.MarshalMap(user)
if err != nil {
return fmt.Errorf("marshal map: %w", err)
}
_, err = d.Client.PutItem(ctx, &dynamodb.PutItemInput{
TableName: aws.String(d.Table),
Item: item,
})
if err != nil {
return fmt.Errorf("put item: %w", err)
}
return nil
}
{
"TableName": "BlogTable",
"AttributeDefinitions": [
{
"AttributeName": "PK",
"AttributeType": "S"
},
{
"AttributeName": "SK",
"AttributeType": "S"
},
{
"AttributeName": "Email",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "PK",
"KeyType": "HASH"
},
{
"AttributeName": "SK",
"KeyType": "RANGE"
}
],
"GlobalSecondaryIndexes": [
{
"IndexName": "BlogGlobalIndex1",
"KeySchema": [
{
"AttributeName": "Email",
"KeyType": "HASH"
},
{
"AttributeName": "SK",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
}
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
}
}
aws --profile localstack --endpoint-url http://localhost:4566 dynamodb scan --table-name BlogTable
{
"Items": [
{
"SK": {
"S": "user#1722201431023993"
},
"PK": {
"S": "user#1722201431023993"
},
"ID": {
"S": "1722201431023993"
},
"Email": {
"S": "[email protected]"
},
"Name": {
"S": "Roberto Medhurst"
},
"Password": {
"S": "dentv9kkkjT7PDD"
}
}
],
"Count": 1,
"ScannedCount": 1,
"ConsumedCapacity": null
}
aws --profile localstack --endpoint-url http://localhost:4566 dynamodb query
--table-name BlogTable
--index-name BlogGlobalIndex1
--key-condition-expression "Email = :email"
--expression-attribute-values '{":email":{"S":"[email protected]"}}'
{
"Items": [
{
"SK": {
"S": "user#1722201431023993"
},
"PK": {
"S": "user#1722201431023993"
},
"ID": {
"S": "1722201431023993"
},
"Email": {
"S": "[email protected]"
},
"Name": {
"S": "Roberto Medhurst"
},
"Password": {
"S": "dentv9kkkjT7PDD"
}
}
],
"Count": 1,
"ScannedCount": 1,
"ConsumedCapacity": null
}