@@ -21,34 +21,47 @@ export class FeedRepository {
2121 VALUES (?, ?, ?, ?, ?, ?)
2222 ` ;
2323
24- const insertPromises = resultData . map ( async ( feed ) => {
25- return this . dbConnection . executeQuery ( query , [
26- feed . blogId ,
27- feed . pubDate ,
28- feed . title ,
29- feed . link ,
30- feed . imageUrl ,
31- feed . summary ,
32- ] ) ;
24+ const insertPromises = resultData . map ( async ( feed , index ) => {
25+ try {
26+ const result = await this . dbConnection . executeQueryStrict ( query , [
27+ feed . blogId ,
28+ feed . pubDate ,
29+ feed . title ,
30+ feed . link ,
31+ feed . imageUrl ,
32+ feed . summary ,
33+ ] ) ;
34+ return { result, index, success : true } ;
35+ } catch ( error ) {
36+ if ( error . code === 'ER_DUP_ENTRY' ) {
37+ logger . info ( `중복 피드 스킵: ${ feed . title } (${ feed . link } )` ) ;
38+ return { result : null , index, success : false , duplicate : true } ;
39+ }
40+ throw error ;
41+ }
3342 } ) ;
3443
3544 const promiseResults = await Promise . all ( insertPromises ) ;
3645
3746 const insertedFeeds = promiseResults
38- . map ( ( feed : any , index ) => {
39- if ( feed ) {
40- const insertId = feed . insertId ;
41- return {
42- ...resultData [ index ] ,
43- id : insertId ,
44- } ;
45- }
46- } )
47- . filter ( ( feed ) => feed ) ;
47+ . filter ( ( result ) => result . success )
48+ . map ( ( result ) => ( {
49+ ...resultData [ result . index ] ,
50+ id : result . result . insertId ,
51+ } ) ) ;
52+
53+ const duplicateCount = promiseResults . filter (
54+ ( result ) => result . duplicate ,
55+ ) . length ;
4856
4957 logger . info (
50- `[MySQL] ${ insertedFeeds . length } 개의 피드 데이터가 성공적으로 데이터베이스에 삽입되었습니다.` ,
58+ `[MySQL] ${
59+ insertedFeeds . length
60+ } 개의 피드 데이터가 성공적으로 데이터베이스에 삽입되었습니다.${
61+ ! ! duplicateCount ? ' ' + duplicateCount + '개의 중복 피드 발생' : ''
62+ } `,
5163 ) ;
64+
5265 return insertedFeeds ;
5366 }
5467
0 commit comments