@middy/dynamodb
Advanced tools
Comparing version 5.1.0 to 5.2.0
134
index.js
@@ -1,62 +0,82 @@ | ||
import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache } from '@middy/util'; | ||
import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb'; | ||
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'; | ||
import { | ||
canPrefetch, | ||
createPrefetchClient, | ||
createClient, | ||
getCache, | ||
getInternal, | ||
processCache, | ||
modifyCache | ||
} from '@middy/util' | ||
import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb' | ||
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb' | ||
const defaults = { | ||
AwsClient: DynamoDBClient, | ||
awsClientOptions: {}, | ||
awsClientAssumeRole: undefined, | ||
awsClientCapture: undefined, | ||
fetchData: {}, | ||
disablePrefetch: false, | ||
cacheKey: 'dynamodb', | ||
cacheKeyExpiry: {}, | ||
cacheExpiry: -1, | ||
setToContext: false | ||
}; | ||
const dynamodbMiddleware = (opts = {})=>{ | ||
const options = { | ||
...defaults, | ||
...opts | ||
}; | ||
for(const internalKey in options.fetchData){ | ||
options.fetchData[internalKey].Key = marshall(options.fetchData[internalKey].Key); | ||
AwsClient: DynamoDBClient, | ||
awsClientOptions: {}, | ||
awsClientAssumeRole: undefined, | ||
awsClientCapture: undefined, | ||
fetchData: {}, | ||
disablePrefetch: false, | ||
cacheKey: 'dynamodb', | ||
cacheKeyExpiry: {}, | ||
cacheExpiry: -1, | ||
setToContext: false | ||
} | ||
const dynamodbMiddleware = (opts = {}) => { | ||
const options = { | ||
...defaults, | ||
...opts | ||
} | ||
// force marshall of Key during cold start | ||
for (const internalKey in options.fetchData) { | ||
options.fetchData[internalKey].Key = marshall( | ||
options.fetchData[internalKey].Key | ||
) | ||
} | ||
const fetch = (request, cachedValues = {}) => { | ||
const values = {} | ||
for (const internalKey in options.fetchData) { | ||
if (cachedValues[internalKey]) continue | ||
const inputParameters = options.fetchData[internalKey] | ||
values[internalKey] = client | ||
.send(new GetItemCommand(inputParameters)) | ||
.then((resp) => unmarshall(resp.Item)) | ||
.catch((e) => { | ||
const value = getCache(options.cacheKey).value ?? {} | ||
value[internalKey] = undefined | ||
modifyCache(options.cacheKey, value) | ||
throw e | ||
}) | ||
} | ||
const fetch = (request, cachedValues = {})=>{ | ||
const values = {}; | ||
for(const internalKey in options.fetchData){ | ||
if (cachedValues[internalKey]) continue; | ||
const inputParameters = options.fetchData[internalKey]; | ||
values[internalKey] = client.send(new GetItemCommand(inputParameters)).then((resp)=>unmarshall(resp.Item)).catch((e)=>{ | ||
const value = getCache(options.cacheKey).value ?? {}; | ||
value[internalKey] = undefined; | ||
modifyCache(options.cacheKey, value); | ||
throw e; | ||
}); | ||
} | ||
return values; | ||
}; | ||
let client; | ||
if (canPrefetch(options)) { | ||
client = createPrefetchClient(options); | ||
processCache(options, fetch); | ||
return values | ||
} | ||
let client | ||
if (canPrefetch(options)) { | ||
client = createPrefetchClient(options) | ||
processCache(options, fetch) | ||
} | ||
const dynamodbMiddlewareBefore = async (request) => { | ||
if (!client) { | ||
client = await createClient(options, request) | ||
} | ||
const dynamodbMiddlewareBefore = async (request)=>{ | ||
if (!client) { | ||
client = await createClient(options, request); | ||
} | ||
const { value } = processCache(options, fetch, request); | ||
Object.assign(request.internal, value); | ||
if (options.setToContext) { | ||
const data = await getInternal(Object.keys(options.fetchData), request); | ||
Object.assign(request.context, data); | ||
} | ||
}; | ||
return { | ||
before: dynamodbMiddlewareBefore | ||
}; | ||
}; | ||
export function dynamoDbReq(req) { | ||
return req; | ||
const { value } = processCache(options, fetch, request) | ||
Object.assign(request.internal, value) | ||
if (options.setToContext) { | ||
const data = await getInternal(Object.keys(options.fetchData), request) | ||
Object.assign(request.context, data) | ||
} | ||
} | ||
return { | ||
before: dynamodbMiddlewareBefore | ||
} | ||
} | ||
export default dynamodbMiddleware; | ||
// used for TS type inference (see index.d.ts) | ||
export function dynamoDbReq (req) { | ||
return req | ||
} | ||
export default dynamodbMiddleware |
{ | ||
"name": "@middy/dynamodb", | ||
"version": "5.1.0", | ||
"version": "5.2.0", | ||
"description": "DynamoDB middleware for the middy framework", | ||
@@ -61,3 +61,3 @@ "type": "module", | ||
"dependencies": { | ||
"@middy/util": "5.1.0" | ||
"@middy/util": "5.2.0" | ||
}, | ||
@@ -67,7 +67,7 @@ "devDependencies": { | ||
"@aws-sdk/util-dynamodb": "^3.245.0", | ||
"@middy/core": "5.1.0", | ||
"@middy/core": "5.2.0", | ||
"@types/aws-lambda": "^8.10.101", | ||
"aws-xray-sdk": "^3.3.3" | ||
}, | ||
"gitHead": "bbdaf5843914921804ba085dd58117273febe6b5" | ||
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
116
9476
+ Added@middy/util@5.2.0(transitive)
- Removed@middy/util@5.1.0(transitive)
Updated@middy/util@5.2.0