Skip to content

Commit

Permalink
Merge pull request #91 from shelfio/feature/force-region
Browse files Browse the repository at this point in the history
Add force region property
  • Loading branch information
randomhash authored Sep 30, 2024
2 parents 041c0ca + cb898af commit 3ed2d8b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ yarn.lock
!.idea/modules.xml
!.idea/vcs.xml
!.idea/jsonSchemas.xml
.aider*
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shelf/aws-ddb-with-xray",
"version": "2.2.4",
"version": "2.2.5",
"description": "AWS DynamoDB Document Client initialized with X-Ray",
"keywords": [
"aws sdk",
Expand Down
44 changes: 32 additions & 12 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,42 @@ import {GetCommand, PutCommand} from '@aws-sdk/lib-dynamodb';
import {getDocumentClient} from '.';

process.env.AWS_XRAY_DAEMON_ADDRESS = 'localhost:2000';
const ddb = getDocumentClient();

it('should work as a ddb client', async () => {
const ddbPutCommand = new PutCommand({
TableName: 'example_table',
Item: {hash_key: 'foo', range_key: 'bar', some: 'key'},
describe('getDocumentClient', () => {
let originalEnv: NodeJS.ProcessEnv;

beforeEach(() => {
originalEnv = process.env;
process.env = {...originalEnv};
});
const ddbGetCommand = new GetCommand({
TableName: 'example_table',
Key: {hash_key: 'foo', range_key: 'bar'},

afterEach(() => {
process.env = originalEnv;
});

await ddb.send(ddbPutCommand);
it('should work as a ddb client', async () => {
const ddb = getDocumentClient();
const ddbPutCommand = new PutCommand({
TableName: 'example_table',
Item: {hash_key: 'foo', range_key: 'bar', some: 'key'},
});
const ddbGetCommand = new GetCommand({
TableName: 'example_table',
Key: {hash_key: 'foo', range_key: 'bar'},
});

const {Item} = await ddb.send(ddbGetCommand);
await ddb.send(ddbPutCommand);

expect(Item).toEqual({hash_key: 'foo', range_key: 'bar', some: 'key'});
expect(captureAWSv3Client).toHaveBeenCalledWith(ddb);
const {Item} = await ddb.send(ddbGetCommand);

expect(Item).toEqual({hash_key: 'foo', range_key: 'bar', some: 'key'});
expect(captureAWSv3Client).toHaveBeenCalledWith(ddb);
});

it('should respect FORCE_REGION environment variable', async () => {
process.env.FORCE_REGION = 'us-west-2';
const ddb = getDocumentClient();

expect(await ddb.config.region()).toBe('us-west-2');
});
});
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const getDDBClient = (params?: GetClientParams) => {
region: region ?? 'local-env',
}),
...(params?.clientConfig && params.clientConfig),
...(process.env.FORCE_REGION && {region: process.env.FORCE_REGION}),
...getCredentials(params?.credentials),
});
}
Expand All @@ -57,6 +58,7 @@ const getDDBClient = (params?: GetClientParams) => {
region: region ?? 'local-env',
}),
...(params?.clientConfig && params.clientConfig),
...(process.env.FORCE_REGION && {region: process.env.FORCE_REGION}),
...getCredentials(params?.credentials),
});

Expand Down

0 comments on commit 3ed2d8b

Please sign in to comment.