개발/Node
NestJS, DynamoDB에 있는 글들을 hbs에서 보여주기
Seobs
2021. 7. 27. 11:51
다음은 DynamoDB에서 항목들을 가져와서 보여주는 방법입니다.
DynamoDB 항목 가져오기
저는 전체 내용을 가져오기 위해 scan을 사용했습니다.
const dynamoDB = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: table,
};
const lists = await dynamoDB.scan(params).promise();
const array = [];
lists.Items.forEach((data) => {
array.push(data);
});
hbs에 보여주기
위에서 생성된 array를 lists로 리턴을 받아서 hbs를 이용해 보여줍니다.
제가 원하는 오더를 위해서 중간에 sort를 진행했습니다.
lists.then(function (results) {
results.sort(function (a, b) {
const A = new Date(a.date);
const B = new Date(b.date);
if (A < B) return 1;
if (A > B) return -1;
return 0;
});
response.render('admin/lists', {
lists: results,
});
});
hbs파일에서는 each를 이용해 보여줍니다.
<tbody class="divide-y divide-gray-200">
{{#each lists}}
<tr>
<td class="px-6 py-4 whitespace-nowrap">{{name}}</td>
<td class="px-6 py-4 whitespace-nowrap">{{date}}</td>
</tr>
{{/each}}
</tbody>