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

com.limemojito.test.dynamodb.DynamoDbSupport Maven / Gradle / Ivy

Go to download

Test utilities for various development work. Json, reflection, getter/setter testing, DTO, Canonical form, etc. AWS support for Dynamo DB, SQS, SNS, S3. Prometheus metrics reader and asserter. Synthetic S3 Event generation.

There is a newer version: 14.3.1
Show newest version
/*
 * Copyright 2011-2024 Lime Mojito Pty Ltd
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 */

package com.limemojito.test.dynamodb;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.model.Page;

import java.util.List;

/**
 * DynamoDbSupport is a utility class that provides simplified access to DynamoDB operations using the DynamoDbEnhancedClient.
 * It can be used to delete all items from a DynamoDB table and save a list of objects to a DynamoDB table.
 * 

* To use DynamoDbSupport, it needs to be instantiated as a Spring bean and injected with a DynamoDbEnhancedClient. * It can then be used to perform deleteAll and saveAll operations on DynamoDB tables. *

* Example usage: * ``` * DynamoDbSupport support = new DynamoDbSupport(dynamoDbEnhancedClient); * support.deleteAll("myTable", MyObject.class); * support.saveAll("myTable", objectList, MyObject.class); * ``` * * @see DynamoDB Enhanced Client Documentation */ @Service @Slf4j @RequiredArgsConstructor public class DynamoDbSupport { private final DynamoDbEnhancedClient dbMapper; /** * Deletes all items from a DynamoDB table. * * @param tableName the name of the DynamoDB table from which to delete items * @param type the class representing the type of items stored in the table * @param the type of items stored in the table */ public void deleteAll(String tableName, Class type) { log.info("Deleting all {}", type.getSimpleName()); DynamoDbTable table = tableFor(tableName, type); int count = 0; for (Page t : table.scan()) { List items = t.items(); items.forEach(table::deleteItem); count += items.size(); } log.debug("Delete complete. {} items", count); } /** * Saves a list of objects to a DynamoDB table. * * @param tableName the name of the DynamoDB table to save the objects to * @param objectList the list of objects to save * @param type the class representing the type of objects in the list * @param the type of objects in the list */ public void saveAll(String tableName, List objectList, Class type) { if (!objectList.isEmpty()) { log.info("Saving {} objects", objectList.size()); DynamoDbTable dynamoDbTable = tableFor(tableName, type); objectList.forEach(item -> dynamoDbTable.putItem(r -> r.item(item))); log.debug("Saved"); } else { log.warn("Empty list provided"); } } private DynamoDbTable tableFor(String tableName, Class type) { TableSchema schema = TableSchema.fromBean(type); return dbMapper.table(tableName, schema); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy