1

I am currently trying to write to multiple existing databases with one call to BatchWriteCommand. All architecture is currently setup using the AWS CDK, and this function is being executed within a Typescript Lambda function. I have setup the input to my BatchWriteCommand with the same syntax as the following (anonymized):

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, GetCommand, BatchWriteCommand } from "@aws-sdk/lib-dynamodb"; const client = new DynamoDBClient({}); const ddbDocClient = DynamoDBDocumentClient.from(client); ... const writeToDatabases(body: Object) => { const Id = String(crypto.randomUUID()); const field1 = String(body.field1); const field2 = String(body.field2); const response = await ddbDocClient.send( new BatchWriteCommand({ RequestItems: [ process.env.TABLE_1_NAME: [{ PutRequest: { Item: { Id: Id, Field1: Field1 } } }], process.env.TABLE_2_NAME: [{ PutRequest: { Item: { Id: Id, Field2: Field2 } } }] ] }) ); } 

According to the BatchWriteCommand lib-dynamodb documentation, and the underlying BatchWriteItem client-dynamodb documentation, I have everything correctly formatted. However, I am getting an error on the lines where I specify the tables to add to (process.env.TABLE_1_NAME and process.env.TABLE_2_NAME):

Type 'string | undefined' is not assignable to type '(Omit<WriteRequest, "PutRequest" | "DeleteRequest"> & { PutRequest?: (Omit<PutRequest, "Item"> & { Item: Record<string, any> | undefined; }) | undefined; DeleteRequest?: (Omit<...> & { ...; }) | undefined; })[]'. Type 'undefined' is not assignable to type '(Omit<WriteRequest, "PutRequest" | "DeleteRequest"> & { PutRequest?: (Omit<PutRequest, "Item"> & { Item: Record<string, any> | undefined; }) | undefined; DeleteRequest?: (Omit<...> & { ...; }) | undefined; })[]'.ts(2322) 

Even when I hard-code these values, I get the same error about wrong type assignments.

I'd rather not have to execute multiple PutCommands in a row, so getting this working would be ideal. Thanks in advance for the help!

1
  • When you hardcode those values, the error cannot the same, since it's string, not string | undefined. Please post the error with hardcoded strings.
    – gshpychka
    CommentedJan 16 at 8:24

1 Answer 1

1

RequestItems is not a list of items, it's a map in which each table name is only defined once.

const response = await ddbDocClient.send( new BatchWriteCommand({ RequestItems: { process.env.TABLE_1_NAME: [{ PutRequest: { Item: { Id: Id, Field1: Field1 } } }], process.env.TABLE_2_NAME: [{ PutRequest: { Item: { Id: Id, Field2: Field2 } } }] } }) ); 
1
  • 1
    Thanks Leeroy! I knew I was missing something simple :^)CommentedJan 16 at 14:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.