DynamoDB DocumentClient do not return any data after put operation

Viewed 202

I'm testing a lambda using the serverless framework with the sls offline command, this lambda should connect to my local dynamoDB (initialized with a docker-compose image), and put a new data in Dynamo using aws-sdk, but I can never get the return of the put().promise() function, if I use the get function I don't get any return either .I checked and the data is being entered into dynamodb. Follow the code below

import ILocationData, { CreateLocationDTO } from '@domain/location/data/ILocationData';
import { LocationEntity } from '@domain/location/entities/LocationEntity';
import { uuid } from 'uuidv4';

import DynamoDBClient from './DynamoDBClient';

export default class LocationProvider extends DynamoDBClient implements ILocationData {
  private tableName = 'Locations';

  public async createLocation(data: CreateLocationDTO): Promise<LocationEntity> {
    const toCreateLocation: LocationEntity = {
      ...data,
      locationId: uuid(),
      hasOffers: false,
    };

    try {
      const location = await this.client
        .put({
          TableName: this.tableName,
          Item: toCreateLocation,
          ReturnValues: 'ALL_OLD',
        })
        .promise();

      console.log(location);

      return location.Attributes as LocationEntity;
    } catch (err) {
      console.log(err);
      return {} as LocationEntity;
    }
  }
}

DynamoDBClient.ts -> Class file

import * as AWS from 'aws-sdk';
import { DocumentClient } from 'aws-sdk/clients/dynamodb';

abstract class DynamoDBClient {
  public client: DocumentClient;
  private config = {};

  constructor() {
    if (process.env.IS_OFFLINE) {
      this.config = {
        region: process.env.DYNAMO_DB_REGION,
        accessKeyId: 'xxxx',
        secretAccessKey: 'xxxx',
        endpoint: process.env.DYNAMO_DB_ENDPOINT,
      };
    }

    this.client = new AWS.DynamoDB.DocumentClient(this.config);
  }
}

export default DynamoDBClient;
1 Answers

I assume locationId is your partition key and you assign it to uuid() which will be always unique so you will never update any existing items with your put operation. Put operation returns anything only if there is already existing item with the same partition key which will be overwritten by newly provided item.

Related