@middy/dynamodb 5.0.0-alpha.1 → 5.0.0-alpha.2

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 +58 -76
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -1,82 +1,64 @@
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
-
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';
13
4
  const defaults = {
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
- })
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
+ // force marshall of Key during cold start
22
+ for(const internalKey in options.fetchData){
23
+ options.fetchData[internalKey].Key = marshall(options.fetchData[internalKey].Key);
52
24
  }
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)
25
+ const fetch = (request, cachedValues = {})=>{
26
+ const values = {};
27
+ for(const internalKey in options.fetchData){
28
+ if (cachedValues[internalKey]) continue;
29
+ const inputParameters = options.fetchData[internalKey];
30
+ values[internalKey] = client.send(new GetItemCommand(inputParameters)).then((resp)=>unmarshall(resp.Item)).catch((e)=>{
31
+ const value = getCache(options.cacheKey).value ?? {};
32
+ value[internalKey] = undefined;
33
+ modifyCache(options.cacheKey, value);
34
+ throw e;
35
+ });
36
+ }
37
+ return values;
38
+ };
39
+ let client;
40
+ if (canPrefetch(options)) {
41
+ client = createPrefetchClient(options);
42
+ processCache(options, fetch);
70
43
  }
71
- }
72
- return {
73
- before: dynamodbMiddlewareBefore
74
- }
75
- }
76
-
44
+ const dynamodbMiddlewareBefore = async (request)=>{
45
+ if (!client) {
46
+ client = await createClient(options, request);
47
+ }
48
+ const { value } = processCache(options, fetch, request);
49
+ Object.assign(request.internal, value);
50
+ if (options.setToContext) {
51
+ const data = await getInternal(Object.keys(options.fetchData), request);
52
+ Object.assign(request.context, data);
53
+ }
54
+ };
55
+ return {
56
+ before: dynamodbMiddlewareBefore
57
+ };
58
+ };
77
59
  // used for TS type inference (see index.d.ts)
78
- export function dynamoDbReq (req) {
79
- return req
60
+ export function dynamoDbReq(req) {
61
+ return req;
80
62
  }
63
+ export default dynamodbMiddleware;
81
64
 
82
- export default dynamodbMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/dynamodb",
3
- "version": "5.0.0-alpha.1",
3
+ "version": "5.0.0-alpha.2",
4
4
  "description": "DynamoDB middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -58,12 +58,12 @@
58
58
  "url": "https://github.com/sponsors/willfarrell"
59
59
  },
60
60
  "dependencies": {
61
- "@middy/util": "5.0.0-alpha.1"
61
+ "@middy/util": "5.0.0-alpha.2"
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.0.0-alpha.1",
66
+ "@middy/core": "5.0.0-alpha.2",
67
67
  "@types/aws-lambda": "^8.10.101",
68
68
  "aws-xray-sdk": "^3.3.3"
69
69
  },