-
-
Save AHaydar/684ccb8d35015c4f1dfbe2bb28e0bddc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { DynamoDB } = require('@aws-sdk/client-dynamodb'); | |
const { unmarshall } = require('@aws-sdk/util-dynamodb'); | |
exports.lambdaHandler = async (event, context) => { | |
try { | |
console.log('here is the event received', event); | |
const dynamodb = new DynamoDB({ region: 'us-east-1' }); | |
const params = { | |
TableName: 'companies', | |
}; | |
if (!event.queryStringParameters) { | |
params.ExpressionAttributeValues = { | |
':companyType': { | |
S: 'PUBLIC', | |
}, | |
}; | |
params.FilterExpression = 'CompanyType = :companyType'; | |
} else { | |
params.ExpressionAttributeValues = { | |
':companyId': { | |
S: `COMPANY#${event.queryStringParameters.companyId}`, | |
}, | |
}; | |
params.FilterExpression = 'CompanyId = :companyId'; | |
} | |
const results = await dynamodb.scan(params); | |
console.log('results', results); | |
let unmarshalledResults = []; | |
for (const item of results.Items) { | |
const unmarshalledRecord = unmarshall(item); | |
unmarshalledResults.push(unmarshalledRecord); | |
} | |
console.log('unmarshalled results', unmarshalledResults); | |
return { | |
statusCode: 200, | |
headers: { | |
'Content-Type': 'application/json', | |
'Access-Control-Allow-Origin': '*', | |
}, | |
body: JSON.stringify(unmarshalledResults), | |
}; | |
} catch (e) { | |
console.error('something went wrong', e); | |
return { | |
statusCode: 500, | |
body: 'Something has gone wrong, please contact the support team', | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment