Last active
November 8, 2022 20:36
-
-
Save marcusgadbem/ff1dd87004c26ca2e1a66f659dc394ba to your computer and use it in GitHub Desktop.
aws-sdk - dynamodb client + test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { DynamoDBClient, DynamoDBClientConfig } from '@aws-sdk/client-dynamodb' | |
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb' | |
// @aws-sdk (v3) has keepAlive enabled by default | |
export const DynamoClient = new DynamoDBClient({}); | |
const marshallOptions = { | |
convertEmptyValues: false, | |
removeUndefinedValues: false, | |
convertClassInstanceToMap: false, | |
}; | |
const unmarshallOptions = { | |
wrapNumbers: false, | |
}; | |
const translateConfig = { marshallOptions, unmarshallOptions }; | |
export const DynamoDocument = DynamoDBDocumentClient.from(DynamoClient, translateConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'aws-sdk-client-mock-jest' // provides `toHaveReceivedCommandWith` assertion | |
import { mockClient } from 'aws-sdk-client-mock' | |
import { DynamoDocument } from './adapter-dynamodb' | |
// mock either your dynamo client instance or `DynamoClient` directly from @aws-sdk/client-dynamodb | |
const ddbMock = mockClient(DynamoDocument) | |
beforeEach(() => { | |
ddbMock.reset() | |
} | |
it('expects PutCommand to be called', async () => { | |
const payload = { amount: 4382490 } | |
await createLoan(payload) | |
await expect(ddbMock).toHaveReceivedCommandWith(PutCommand, { | |
TableName: 'loans', | |
Item: expect.objectContaining(payload) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PutCommand } from '@aws-sdk/lib-dynamodb' | |
import { DynamoDocument } from './adapter-dynamodb' | |
export async function createLoan(payload): Promise<any> { | |
await DynamoDocument.send( | |
new PutCommand({ | |
TableName: 'loans', | |
Item: payload, | |
}) | |
) | |
return payload | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment