@middy/dynamodb 5.1.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +77 -57
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,62 +1,82 @@
1
- import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache } from '@middy/util';
2
- import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb';
3
- import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
1
+ import {
2
+ canPrefetch,
3
+ createPrefetchClient,
4
+ createClient,
5
+ getCache,
6
+ getInternal,
7
+ processCache,
8
+ modifyCache
9
+ } from '@middy/util'
10
+ import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb'
11
+ import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'
12
+
4
13
  const defaults = {
5
- AwsClient: DynamoDBClient,
6
- awsClientOptions: {},
7
- awsClientAssumeRole: undefined,
8
- awsClientCapture: undefined,
9
- fetchData: {},
10
- disablePrefetch: false,
11
- cacheKey: 'dynamodb',
12
- cacheKeyExpiry: {},
13
- cacheExpiry: -1,
14
- setToContext: false
15
- };
16
- const dynamodbMiddleware = (opts = {})=>{
17
- const options = {
18
- ...defaults,
19
- ...opts
20
- };
21
- for(const internalKey in options.fetchData){
22
- options.fetchData[internalKey].Key = marshall(options.fetchData[internalKey].Key);
14
+ AwsClient: DynamoDBClient,
15
+ awsClientOptions: {},
16
+ awsClientAssumeRole: undefined,
17
+ awsClientCapture: undefined,
18
+ fetchData: {},
19
+ disablePrefetch: false,
20
+ cacheKey: 'dynamodb',
21
+ cacheKeyExpiry: {},
22
+ cacheExpiry: -1,
23
+ setToContext: false
24
+ }
25
+ const dynamodbMiddleware = (opts = {}) => {
26
+ const options = {
27
+ ...defaults,
28
+ ...opts
29
+ }
30
+
31
+ // force marshall of Key during cold start
32
+ for (const internalKey in options.fetchData) {
33
+ options.fetchData[internalKey].Key = marshall(
34
+ options.fetchData[internalKey].Key
35
+ )
36
+ }
37
+
38
+ const fetch = (request, cachedValues = {}) => {
39
+ const values = {}
40
+ for (const internalKey in options.fetchData) {
41
+ if (cachedValues[internalKey]) continue
42
+ const inputParameters = options.fetchData[internalKey]
43
+ values[internalKey] = client
44
+ .send(new GetItemCommand(inputParameters))
45
+ .then((resp) => unmarshall(resp.Item))
46
+ .catch((e) => {
47
+ const value = getCache(options.cacheKey).value ?? {}
48
+ value[internalKey] = undefined
49
+ modifyCache(options.cacheKey, value)
50
+ throw e
51
+ })
23
52
  }
24
- const fetch = (request, cachedValues = {})=>{
25
- const values = {};
26
- for(const internalKey in options.fetchData){
27
- if (cachedValues[internalKey]) continue;
28
- const inputParameters = options.fetchData[internalKey];
29
- values[internalKey] = client.send(new GetItemCommand(inputParameters)).then((resp)=>unmarshall(resp.Item)).catch((e)=>{
30
- const value = getCache(options.cacheKey).value ?? {};
31
- value[internalKey] = undefined;
32
- modifyCache(options.cacheKey, value);
33
- throw e;
34
- });
35
- }
36
- return values;
37
- };
38
- let client;
39
- if (canPrefetch(options)) {
40
- client = createPrefetchClient(options);
41
- processCache(options, fetch);
53
+ return values
54
+ }
55
+
56
+ let client
57
+ if (canPrefetch(options)) {
58
+ client = createPrefetchClient(options)
59
+ processCache(options, fetch)
60
+ }
61
+ const dynamodbMiddlewareBefore = async (request) => {
62
+ if (!client) {
63
+ client = await createClient(options, request)
64
+ }
65
+ const { value } = processCache(options, fetch, request)
66
+ Object.assign(request.internal, value)
67
+ if (options.setToContext) {
68
+ const data = await getInternal(Object.keys(options.fetchData), request)
69
+ Object.assign(request.context, data)
42
70
  }
43
- const dynamodbMiddlewareBefore = async (request)=>{
44
- if (!client) {
45
- client = await createClient(options, request);
46
- }
47
- const { value } = processCache(options, fetch, request);
48
- Object.assign(request.internal, value);
49
- if (options.setToContext) {
50
- const data = await getInternal(Object.keys(options.fetchData), request);
51
- Object.assign(request.context, data);
52
- }
53
- };
54
- return {
55
- before: dynamodbMiddlewareBefore
56
- };
57
- };
58
- export function dynamoDbReq(req) {
59
- return req;
71
+ }
72
+ return {
73
+ before: dynamodbMiddlewareBefore
74
+ }
75
+ }
76
+
77
+ // used for TS type inference (see index.d.ts)
78
+ export function dynamoDbReq (req) {
79
+ return req
60
80
  }
61
- export default dynamodbMiddleware;
62
81
 
82
+ export default dynamodbMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/dynamodb",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "DynamoDB middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -58,14 +58,14 @@
58
58
  "url": "https://github.com/sponsors/willfarrell"
59
59
  },
60
60
  "dependencies": {
61
- "@middy/util": "5.1.0"
61
+ "@middy/util": "5.2.0"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@aws-sdk/client-dynamodb": "^3.245.0",
65
65
  "@aws-sdk/util-dynamodb": "^3.245.0",
66
- "@middy/core": "5.1.0",
66
+ "@middy/core": "5.2.0",
67
67
  "@types/aws-lambda": "^8.10.101",
68
68
  "aws-xray-sdk": "^3.3.3"
69
69
  },
70
- "gitHead": "bbdaf5843914921804ba085dd58117273febe6b5"
70
+ "gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
71
71
  }