rest api get 파라미터 설정 개발중
This commit is contained in:
@@ -722,38 +722,56 @@ export class BatchService {
|
||||
const updateColumns = columns.filter(col => col !== primaryKeyColumn);
|
||||
const updateSet = updateColumns.map(col => `${col} = EXCLUDED.${col}`).join(', ');
|
||||
|
||||
// 먼저 해당 레코드가 존재하는지 확인
|
||||
const checkQuery = `SELECT COUNT(*) as count FROM ${tableName} WHERE ${primaryKeyColumn} = $1`;
|
||||
const existsResult = await prisma.$queryRawUnsafe(checkQuery, record[primaryKeyColumn]);
|
||||
const exists = (existsResult as any)[0]?.count > 0;
|
||||
|
||||
let query: string;
|
||||
if (exists && updateSet) {
|
||||
// 기존 레코드가 있으면 UPDATE (값이 다른 경우에만)
|
||||
const whereConditions = updateColumns.map((col, index) =>
|
||||
`${col} IS DISTINCT FROM $${index + 2}`
|
||||
).join(' OR ');
|
||||
// 트랜잭션 내에서 처리하여 연결 관리 최적화
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
// 먼저 해당 레코드가 존재하는지 확인
|
||||
const checkQuery = `SELECT COUNT(*) as count FROM ${tableName} WHERE ${primaryKeyColumn} = $1`;
|
||||
const existsResult = await tx.$queryRawUnsafe(checkQuery, record[primaryKeyColumn]);
|
||||
const exists = (existsResult as any)[0]?.count > 0;
|
||||
|
||||
query = `UPDATE ${tableName} SET ${updateSet.replace(/EXCLUDED\./g, '')}
|
||||
WHERE ${primaryKeyColumn} = $1 AND (${whereConditions})`;
|
||||
let operationResult = 'no_change';
|
||||
|
||||
// 파라미터: [primaryKeyValue, ...updateValues]
|
||||
const updateValues = [record[primaryKeyColumn], ...updateColumns.map(col => record[col])];
|
||||
const updateResult = await prisma.$executeRawUnsafe(query, ...updateValues);
|
||||
|
||||
if (updateResult > 0) {
|
||||
console.log(`[BatchService] 레코드 업데이트: ${primaryKeyColumn}=${record[primaryKeyColumn]}`);
|
||||
if (exists && updateSet) {
|
||||
// 기존 레코드가 있으면 UPDATE (값이 다른 경우에만)
|
||||
const whereConditions = updateColumns.map((col, index) => {
|
||||
// 날짜/시간 컬럼에 대한 타입 캐스팅 처리
|
||||
if (col.toLowerCase().includes('date') ||
|
||||
col.toLowerCase().includes('time') ||
|
||||
col.toLowerCase().includes('created') ||
|
||||
col.toLowerCase().includes('updated') ||
|
||||
col.toLowerCase().includes('reg')) {
|
||||
return `${col} IS DISTINCT FROM $${index + 2}::timestamp`;
|
||||
}
|
||||
return `${col} IS DISTINCT FROM $${index + 2}`;
|
||||
}).join(' OR ');
|
||||
|
||||
const query = `UPDATE ${tableName} SET ${updateSet.replace(/EXCLUDED\./g, '')}
|
||||
WHERE ${primaryKeyColumn} = $1 AND (${whereConditions})`;
|
||||
|
||||
// 파라미터: [primaryKeyValue, ...updateValues]
|
||||
const updateValues = [record[primaryKeyColumn], ...updateColumns.map(col => record[col])];
|
||||
const updateResult = await tx.$executeRawUnsafe(query, ...updateValues);
|
||||
|
||||
if (updateResult > 0) {
|
||||
console.log(`[BatchService] 레코드 업데이트: ${primaryKeyColumn}=${record[primaryKeyColumn]}`);
|
||||
operationResult = 'updated';
|
||||
} else {
|
||||
console.log(`[BatchService] 레코드 변경사항 없음: ${primaryKeyColumn}=${record[primaryKeyColumn]}`);
|
||||
operationResult = 'no_change';
|
||||
}
|
||||
} else if (!exists) {
|
||||
// 새 레코드 삽입
|
||||
const query = `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (${placeholders})`;
|
||||
await tx.$executeRawUnsafe(query, ...values);
|
||||
console.log(`[BatchService] 새 레코드 삽입: ${primaryKeyColumn}=${record[primaryKeyColumn]}`);
|
||||
operationResult = 'inserted';
|
||||
} else {
|
||||
console.log(`[BatchService] 레코드 변경사항 없음: ${primaryKeyColumn}=${record[primaryKeyColumn]}`);
|
||||
console.log(`[BatchService] 레코드 이미 존재 (변경사항 없음): ${primaryKeyColumn}=${record[primaryKeyColumn]}`);
|
||||
operationResult = 'no_change';
|
||||
}
|
||||
} else if (!exists) {
|
||||
// 새 레코드 삽입
|
||||
query = `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (${placeholders})`;
|
||||
await prisma.$executeRawUnsafe(query, ...values);
|
||||
console.log(`[BatchService] 새 레코드 삽입: ${primaryKeyColumn}=${record[primaryKeyColumn]}`);
|
||||
} else {
|
||||
console.log(`[BatchService] 레코드 이미 존재 (변경사항 없음): ${primaryKeyColumn}=${record[primaryKeyColumn]}`);
|
||||
}
|
||||
|
||||
return operationResult;
|
||||
});
|
||||
|
||||
successCount++;
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user