diff --git a/.gitignore b/.gitignore index c0ded60..1863d67 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ yarn.lock !.idea/modules.xml !.idea/vcs.xml !.idea/jsonSchemas.xml +.aider* diff --git a/package.json b/package.json index c38e985..ee6a55d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.test.ts b/src/index.test.ts index d172485..795207e 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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'); + }); }); diff --git a/src/index.ts b/src/index.ts index dec72bb..89adb4c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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), }); } @@ -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), });