func createProductsTable()

in src/products/src/products-service/localdev.go [81:153]


func createProductsTable() error {
	log.Println("Creating products table: ", ddbTableProducts)

	// Table definition mapped from /aws/cloudformation-templates/base/tables.yaml
	input := &dynamodb.CreateTableInput{
		AttributeDefinitions: []*dynamodb.AttributeDefinition{
			{
				AttributeName: aws.String("id"),
				AttributeType: aws.String("S"),
			},
			{
				AttributeName: aws.String("category"),
				AttributeType: aws.String("S"),
			},
			{
				AttributeName: aws.String("featured"),
				AttributeType: aws.String("S"),
			},
		},
		KeySchema: []*dynamodb.KeySchemaElement{
			{
				AttributeName: aws.String("id"),
				KeyType:       aws.String("HASH"),
			},
		},
		BillingMode: aws.String("PAY_PER_REQUEST"),
		GlobalSecondaryIndexes: []*dynamodb.GlobalSecondaryIndex{
			{
				IndexName: aws.String("category-index"),
				KeySchema: []*dynamodb.KeySchemaElement{
					{
						AttributeName: aws.String("category"),
						KeyType:       aws.String("HASH"),
					},
				},
				Projection: &dynamodb.Projection{
					ProjectionType: aws.String("ALL"),
				},
			},
			{
				IndexName: aws.String("featured-index"),
				KeySchema: []*dynamodb.KeySchemaElement{
					{
						AttributeName: aws.String("featured"),
						KeyType:       aws.String("HASH"),
					},
				},
				Projection: &dynamodb.Projection{
					ProjectionType: aws.String("ALL"),
				},
			},
		},
		TableName: aws.String(ddbTableProducts),
	}

	_, err := dynamoClient.CreateTable(input)
	if err != nil {
		log.Println("Error creating products table: ", ddbTableProducts)

		if aerr, ok := err.(awserr.Error); ok {
			if aerr.Code() == dynamodb.ErrCodeResourceInUseException {
				log.Println("Table already exists; continuing")
				err = nil
			} else {
				log.Println(err.Error())
			}
		} else {
			log.Println(err.Error())
		}
	}

	return err
}