Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add deleteitem function to write interface #16

Merged
merged 11 commits into from
Sep 10, 2024
12 changes: 6 additions & 6 deletions delete_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ import (
* @param sk the sort key of the record
* @return true if the record was deleted, false otherwise
*/
func (t *Client) DeleteItem(ctx context.Context, pk string, sk string) error {
func (t *Client) DeleteItem(ctx context.Context, pk Attribute, sk Attribute) error {

//delete item from dynamodb
vankongv marked this conversation as resolved.
Show resolved Hide resolved
input := &dynamodb.DeleteItemInput{
TableName: &t.TableName,
Key: map[string]types.AttributeValue{
"pk": &types.AttributeValueMemberS{Value: pk},
"sk": &types.AttributeValueMemberS{Value: sk},
"pk": pk,
"sk": sk,
},
}
resp, err := t.client.DeleteItem(ctx, input)
_, err := t.client.DeleteItem(ctx, input)

vankongv marked this conversation as resolved.
Show resolved Hide resolved
if err != nil && resp == nil {
log.Println("failed to delete record into database. Error:" + err.Error())
if err != nil {
log.Println("failed to delete record from database. Error:" + err.Error())
return err
}

Expand Down
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type WriteAPI interface {
// Create or update given item in DynamoDB. Must implemenmt DynamoRecord interface.
// DynamoRecord.GetKeys will be called to get values for parition and sort keys.
PutItem(ctx context.Context, pk Attribute, sk Attribute, item interface{}) error
DeleteItem(ctx context.Context, pk Attribute, sk Attribute) error
}

type TransactionAPI interface {
Expand Down
60 changes: 60 additions & 0 deletions tests/delete_item_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package tests
vankongv marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"testing"

"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/oolio-group/dynago"
)

type user struct {
Pk string `dynamodbav:"pk"`
Sk string `dynamodbav:"sk"`
}

func TestDeleteItem(t *testing.T) {
table := prepareTable(t, dynamoEndpoint, "delete_test")

ctx := context.TODO()

// Insert a single item
item := user{Pk: "users#org1", Sk: "user#1"}
err := table.PutItem(ctx, dynago.StringValue(item.Pk), dynago.StringValue(item.Sk), item)
if err != nil {
t.Fatalf("failed to insert item; got %s", err)
}

// Query the item to ensure it exists
var output []user
_, err = table.Query(ctx, "pk = :pk and sk = :sk", map[string]types.AttributeValue{
":pk": &types.AttributeValueMemberS{Value: item.Pk},
":sk": &types.AttributeValueMemberS{Value: item.Sk},
}, &output)
if err != nil {
t.Fatalf("expected query to succeed; got %s", err)
}
if len(output) == 0 {
t.Fatalf("expected item to be found, but found none")
}

// Delete the item
err = table.DeleteItem(ctx, dynago.StringValue(item.Pk), dynago.StringValue(item.Sk))
if err != nil {
t.Fatalf("expected delete to succeed; got %s", err)
}

// Query the item again to ensure it no longer exists
var deleteOutput []user
_, err = table.Query(ctx, "pk = :pk and sk = :sk", map[string]types.AttributeValue{
":pk": &types.AttributeValueMemberS{Value: item.Pk},
":sk": &types.AttributeValueMemberS{Value: item.Sk},
}, &deleteOutput)

if err != nil {
t.Fatalf("expected query to succeed; got %s", err)
}
if len(deleteOutput) != 0 {
t.Fatalf("expected item to be deleted; found %v", deleteOutput)
}
}
Loading