@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.
- package/index.js +77 -57
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,62 +1,82 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
const dynamodbMiddleware = (opts = {})=>{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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": "
|
|
70
|
+
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
|
|
71
71
|
}
|