@middy/dynamodb 4.6.5 → 5.0.0-alpha.1
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.d.ts +28 -8
- package/index.js +78 -56
- package/package.json +5 -11
- package/index.cjs +0 -70
package/index.d.ts
CHANGED
|
@@ -1,26 +1,46 @@
|
|
|
1
1
|
import middy from '@middy/core'
|
|
2
2
|
import { Options as MiddyOptions } from '@middy/util'
|
|
3
3
|
import { Context as LambdaContext } from 'aws-lambda'
|
|
4
|
-
import { DynamoDBClient, DynamoDBClientConfig } from '@aws-sdk/client-dynamodb'
|
|
4
|
+
import { DynamoDBClient, DynamoDBClientConfig, GetItemCommandInput } from '@aws-sdk/client-dynamodb'
|
|
5
|
+
import { NativeAttributeValue } from '@aws-sdk/util-dynamodb'
|
|
5
6
|
|
|
6
|
-
export type
|
|
7
|
+
export type ParamType<T extends Record<string, NativeAttributeValue>> = GetItemCommandInput & { __returnType?: T }
|
|
8
|
+
export declare function dynamoDbReq<T extends Record<string, NativeAttributeValue>> (req: GetItemCommandInput): ParamType<T>
|
|
9
|
+
|
|
10
|
+
export type DynamoDbOptions<AwsDynamoDBClient = DynamoDBClient> =
|
|
7
11
|
Omit<MiddyOptions<AwsDynamoDBClient, DynamoDBClientConfig>, 'fetchData'>
|
|
8
12
|
&
|
|
9
13
|
{
|
|
10
14
|
fetchData?: {
|
|
11
|
-
|
|
12
|
-
[key: string]: Record<string, any>
|
|
15
|
+
[key: string]: GetItemCommandInput | ParamType<Record<string, NativeAttributeValue>>
|
|
13
16
|
}
|
|
14
17
|
}
|
|
15
18
|
|
|
16
|
-
export type Context<TOptions extends
|
|
19
|
+
export type Context<TOptions extends DynamoDbOptions | undefined> = TOptions extends {
|
|
17
20
|
setToContext: true
|
|
18
21
|
}
|
|
19
|
-
?
|
|
22
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
23
|
+
? LambdaContext & {
|
|
24
|
+
[Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
|
|
25
|
+
? T
|
|
26
|
+
: NativeAttributeValue
|
|
27
|
+
}
|
|
28
|
+
: never
|
|
20
29
|
: LambdaContext
|
|
21
30
|
|
|
22
|
-
|
|
31
|
+
export type Internal<TOptions extends DynamoDbOptions | undefined> =
|
|
32
|
+
TOptions extends DynamoDbOptions
|
|
33
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
34
|
+
? {
|
|
35
|
+
[Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
|
|
36
|
+
? T
|
|
37
|
+
: NativeAttributeValue
|
|
38
|
+
}
|
|
39
|
+
: {}
|
|
40
|
+
: {}
|
|
41
|
+
|
|
42
|
+
declare function dynamodbMiddleware<TOptions extends DynamoDbOptions | undefined> (
|
|
23
43
|
options?: TOptions
|
|
24
|
-
): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
|
|
44
|
+
): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal<TOptions>>
|
|
25
45
|
|
|
26
46
|
export default dynamodbMiddleware
|
package/index.js
CHANGED
|
@@ -1,60 +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
|
-
|
|
23
|
-
|
|
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
|
+
})
|
|
24
52
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
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)
|
|
43
70
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
};
|
|
55
|
-
return {
|
|
56
|
-
before: dynamodbMiddlewareBefore
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
export default dynamodbMiddleware;
|
|
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
|
|
80
|
+
}
|
|
60
81
|
|
|
82
|
+
export default dynamodbMiddleware
|
package/package.json
CHANGED
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/dynamodb",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "DynamoDB middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=18"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"main": "./index.cjs",
|
|
14
13
|
"module": "./index.js",
|
|
15
14
|
"exports": {
|
|
16
15
|
".": {
|
|
17
16
|
"import": {
|
|
18
17
|
"types": "./index.d.ts",
|
|
19
18
|
"default": "./index.js"
|
|
20
|
-
},
|
|
21
|
-
"require": {
|
|
22
|
-
"types": "./index.d.ts",
|
|
23
|
-
"default": "./index.cjs"
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
21
|
},
|
|
27
22
|
"types": "index.d.ts",
|
|
28
23
|
"files": [
|
|
29
24
|
"index.js",
|
|
30
|
-
"index.cjs",
|
|
31
25
|
"index.d.ts"
|
|
32
26
|
],
|
|
33
27
|
"scripts": {
|
|
@@ -64,14 +58,14 @@
|
|
|
64
58
|
"url": "https://github.com/sponsors/willfarrell"
|
|
65
59
|
},
|
|
66
60
|
"dependencies": {
|
|
67
|
-
"@middy/util": "
|
|
61
|
+
"@middy/util": "5.0.0-alpha.1"
|
|
68
62
|
},
|
|
69
63
|
"devDependencies": {
|
|
70
64
|
"@aws-sdk/client-dynamodb": "^3.245.0",
|
|
71
65
|
"@aws-sdk/util-dynamodb": "^3.245.0",
|
|
72
|
-
"@middy/core": "
|
|
66
|
+
"@middy/core": "5.0.0-alpha.1",
|
|
73
67
|
"@types/aws-lambda": "^8.10.101",
|
|
74
68
|
"aws-xray-sdk": "^3.3.3"
|
|
75
69
|
},
|
|
76
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
77
71
|
}
|
package/index.cjs
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(module, "exports", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return _default;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _util = require("@middy/util");
|
|
12
|
-
const _clientdynamodb = require("@aws-sdk/client-dynamodb");
|
|
13
|
-
const _utildynamodb = require("@aws-sdk/util-dynamodb");
|
|
14
|
-
const defaults = {
|
|
15
|
-
AwsClient: _clientdynamodb.DynamoDBClient,
|
|
16
|
-
awsClientOptions: {},
|
|
17
|
-
awsClientAssumeRole: undefined,
|
|
18
|
-
awsClientCapture: undefined,
|
|
19
|
-
fetchData: {},
|
|
20
|
-
disablePrefetch: false,
|
|
21
|
-
cacheKey: 'dynamodb',
|
|
22
|
-
cacheKeyExpiry: {},
|
|
23
|
-
cacheExpiry: -1,
|
|
24
|
-
setToContext: false
|
|
25
|
-
};
|
|
26
|
-
const dynamodbMiddleware = (opts = {})=>{
|
|
27
|
-
const options = {
|
|
28
|
-
...defaults,
|
|
29
|
-
...opts
|
|
30
|
-
};
|
|
31
|
-
// force marshall of Key during cold start
|
|
32
|
-
for(const internalKey in options.fetchData){
|
|
33
|
-
options.fetchData[internalKey].Key = (0, _utildynamodb.marshall)(options.fetchData[internalKey].Key);
|
|
34
|
-
}
|
|
35
|
-
const fetch = (request, cachedValues = {})=>{
|
|
36
|
-
const values = {};
|
|
37
|
-
for(const internalKey in options.fetchData){
|
|
38
|
-
if (cachedValues[internalKey]) continue;
|
|
39
|
-
const inputParameters = options.fetchData[internalKey];
|
|
40
|
-
values[internalKey] = client.send(new _clientdynamodb.GetItemCommand(inputParameters)).then((resp)=>(0, _utildynamodb.unmarshall)(resp.Item)).catch((e)=>{
|
|
41
|
-
const value = (0, _util.getCache)(options.cacheKey).value ?? {};
|
|
42
|
-
value[internalKey] = undefined;
|
|
43
|
-
(0, _util.modifyCache)(options.cacheKey, value);
|
|
44
|
-
throw e;
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
return values;
|
|
48
|
-
};
|
|
49
|
-
let client;
|
|
50
|
-
if ((0, _util.canPrefetch)(options)) {
|
|
51
|
-
client = (0, _util.createPrefetchClient)(options);
|
|
52
|
-
(0, _util.processCache)(options, fetch);
|
|
53
|
-
}
|
|
54
|
-
const dynamodbMiddlewareBefore = async (request)=>{
|
|
55
|
-
if (!client) {
|
|
56
|
-
client = await (0, _util.createClient)(options, request);
|
|
57
|
-
}
|
|
58
|
-
const { value } = (0, _util.processCache)(options, fetch, request);
|
|
59
|
-
Object.assign(request.internal, value);
|
|
60
|
-
if (options.setToContext) {
|
|
61
|
-
const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
|
|
62
|
-
Object.assign(request.context, data);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
return {
|
|
66
|
-
before: dynamodbMiddlewareBefore
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
const _default = dynamodbMiddleware;
|
|
70
|
-
|