Skip to content

Instantly share code, notes, and snippets.

@AHaydar
Last active January 7, 2021 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AHaydar/684ccb8d35015c4f1dfbe2bb28e0bddc to your computer and use it in GitHub Desktop.
Save AHaydar/684ccb8d35015c4f1dfbe2bb28e0bddc to your computer and use it in GitHub Desktop.
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