All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dataspray.singletable.builder.DeleteBuilder Maven / Gradle / Ivy

Go to download

DynamoDB best practices encourages a single-table design that allows multiple record types to reside within the same table. The goal of this library is to improve the experience of Java developers and make it safer to define non-conflicting schema of each record, serializing and deserializing automatically and working with secondary indexes.

The newest version!
package io.dataspray.singletable.builder;

import io.dataspray.singletable.ExpressionBuilder;
import io.dataspray.singletable.Schema;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;

import java.util.Map;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkState;

public class DeleteBuilder extends ExpressionBuilder, DeleteItemRequest.Builder> implements ConditionExpressionBuilder> {

    public DeleteBuilder(Schema schema) {
        super(schema);
    }

    protected DeleteBuilder getParent() {
        return this;
    }

    private Optional> keyOpt = Optional.empty();

    public DeleteBuilder key(Map primaryKey) {
        this.keyOpt = Optional.of(schema.primaryKey(primaryKey));
        return getParent();
    }

    public DeleteItemRequest.Builder builder() {
        Expression expression = buildExpression();
        DeleteItemRequest.Builder builder = DeleteItemRequest.builder();
        builder.tableName(schema.tableName());
        checkState(expression.updateExpression().isEmpty(), "Delete does not support update expression");
        expression.conditionExpression().ifPresent(builder::conditionExpression);
        expression.expressionAttributeNames().ifPresent(builder::expressionAttributeNames);
        expression.expressionAttributeValues().ifPresent(builder::expressionAttributeValues);
        keyOpt.ifPresent(builder::key);
        expression.builderAdjustments().forEach(c -> c.accept(builder));
        return builder;
    }

    public DeleteItemRequest build() {
        return builder().build();
    }

    public DeleteItemResponse execute(DynamoDbClient dynamo) {
        return dynamo.deleteItem(build());
    }

    public Optional executeGetDeleted(DynamoDbClient dynamo) {
        return Optional.ofNullable(schema.fromAttrMap(dynamo.deleteItem(builder()
                .returnValues(ReturnValue.ALL_OLD)
                .build()).attributes()));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy