DynamoDB에 데이터를 갱신하기 위해, 현재까지 나의 코드는 아래와 같았다.
var documentClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: 'sample_table_name',
Key: {
key_name: 'sample_key_name'
},
UpdateExpression: 'SET sample=sample',
ExpressionAttributeValues: 'sample',
ReturnValues: 'UPDATED_NEW'
};
documentClient.update(params, function(err, data) {
if(err) {
// 생성되지 않은 Key값에 대한 처리
res.redirect('/another');
//res.json({"type":"error"});
}
else {
res.json({"type":"success"});
}
});
위와 같이, error가 발생하면 무조건적으로 생성되지 않았다고 가정하고, 초기화를 하는 루틴으로 보냈었다.
하지만, DynamoDB는 초당 읽기/쓰기 횟수에 제한이 있다.
특히, 나의 경우에는 쓰기 횟수 제한이 걸려있는 상태다.
해당 경우를 구분하기 위해, 우선 아래와 같이 테이블이 없는 경우의 예외처리만 별도로 처리해두었다.
var documentClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: 'sample_table_name',
Key: {
key_name: 'sample_key_name'
},
UpdateExpression: 'SET sample=sample',
ExpressionAttributeValues: 'sample',
ReturnValues: 'UPDATED_NEW'
};
documentClient.update(params, function(err, data) {
if(err) {
// 생성되지 않은 Key값에 대한 처리
if(err.code == 'ValidationException') {
res.redirect('/another');
}
else {
res.json({"type":"error"});
}
}
else {
res.json({"type":"success"});
}
});
아직까지는 별 문제가 없지만, 추후 작업을 위해 err.code 종류를 아는만큼 나열해두려 한다.
if (err.code && err.code === 'ValidationException') {
// 없는 항목 또는 없는 테이블에 넣으려 시도한 경우
// 위에서 처리한 내용과 동일
}
if (err.code && err.code === 'ConditionalCheckFailedException') {
// 아직 조사하지 못한 항목
}
if (err.code && err.code === 'ProvisionedThroughputExceededException') {
// 용량초과. process Queue 등을 만들어 처리할 예정
}
특히, 용량초과인 경우의 예외처리를 별도로 할 예정이며
자체 Queue 사용을 위해 Kue 라이브러리를 사용하거나
SQS 서비스 등을 사용하여 예외처리를 할 예정이다. (쓰기용량 10회인 경우, 100ms Interval queue)
'Node.js' 카테고리의 다른 글
Node.js Express + ejs로 WebServer 프로젝트 생성 (0) | 2020.10.13 |
---|