주요기능 Prisma ORM으로 변경

This commit is contained in:
hyeonsu
2025-09-01 11:00:38 +09:00
parent 14218bad11
commit cb88faa68e
5 changed files with 1135 additions and 1615 deletions

View File

@@ -1,4 +1,4 @@
import { Client } from "pg";
import { PrismaClient } from "@prisma/client";
import { logger } from "../utils/logger";
import {
TableInfo,
@@ -8,21 +8,21 @@ import {
ColumnLabels,
} from "../types/tableManagement";
export class TableManagementService {
private client: Client;
const prisma = new PrismaClient();
constructor(client: Client) {
this.client = client;
}
export class TableManagementService {
constructor() {}
/**
* 테이블 목록 조회 (PostgreSQL information_schema 활용)
* 메타데이터 조회는 Prisma로 변경 불가
*/
async getTableList(): Promise<TableInfo[]> {
try {
logger.info("테이블 목록 조회 시작");
const query = `
// information_schema는 여전히 $queryRaw 사용
const tables = await prisma.$queryRaw<TableInfo[]>`
SELECT
t.table_name as "tableName",
COALESCE(tl.table_label, t.table_name) as "displayName",
@@ -38,10 +38,8 @@ export class TableManagementService {
ORDER BY t.table_name
`;
const result = await this.client.query(query);
logger.info(`테이블 목록 조회 완료: ${result.rows.length}`);
return result.rows;
logger.info(`테이블 목록 조회 완료: ${tables.length}`);
return tables;
} catch (error) {
logger.error("테이블 목록 조회 중 오류 발생:", error);
throw new Error(
@@ -52,12 +50,14 @@ export class TableManagementService {
/**
* 테이블 컬럼 정보 조회
* 메타데이터 조회는 Prisma로 변경 불가
*/
async getColumnList(tableName: string): Promise<ColumnTypeInfo[]> {
try {
logger.info(`컬럼 정보 조회 시작: ${tableName}`);
const query = `
// information_schema는 여전히 $queryRaw 사용
const columns = await prisma.$queryRaw<ColumnTypeInfo[]>`
SELECT
c.column_name as "columnName",
COALESCE(cl.column_label, c.column_name) as "displayName",
@@ -78,14 +78,12 @@ export class TableManagementService {
cl.is_visible as "isVisible"
FROM information_schema.columns c
LEFT JOIN column_labels cl ON c.table_name = cl.table_name AND c.column_name = cl.column_name
WHERE c.table_name = $1
WHERE c.table_name = ${tableName}
ORDER BY c.ordinal_position
`;
const result = await this.client.query(query, [tableName]);
logger.info(`컬럼 정보 조회 완료: ${tableName}, ${result.rows.length}`);
return result.rows;
logger.info(`컬럼 정보 조회 완료: ${tableName}, ${columns.length}`);
return columns;
} catch (error) {
logger.error(`컬럼 정보 조회 중 오류 발생: ${tableName}`, error);
throw new Error(
@@ -96,18 +94,22 @@ export class TableManagementService {
/**
* 테이블이 table_labels에 없으면 자동 추가
* Prisma ORM으로 변경
*/
async insertTableIfNotExists(tableName: string): Promise<void> {
try {
logger.info(`테이블 라벨 자동 추가 시작: ${tableName}`);
const query = `
INSERT INTO table_labels (table_name, table_label, description)
VALUES ($1, $1, '')
ON CONFLICT (table_name) DO NOTHING
`;
await prisma.table_labels.upsert({
where: { table_name: tableName },
update: {}, // 이미 존재하면 변경하지 않음
create: {
table_name: tableName,
table_label: tableName,
description: "",
},
});
await this.client.query(query, [tableName]);
logger.info(`테이블 라벨 자동 추가 완료: ${tableName}`);
} catch (error) {
logger.error(`테이블 라벨 자동 추가 중 오류 발생: ${tableName}`, error);
@@ -119,6 +121,7 @@ export class TableManagementService {
/**
* 컬럼 설정 업데이트 (UPSERT 방식)
* Prisma ORM으로 변경
*/
async updateColumnSettings(
tableName: string,
@@ -131,38 +134,42 @@ export class TableManagementService {
// 테이블이 table_labels에 없으면 자동 추가
await this.insertTableIfNotExists(tableName);
const query = `
INSERT INTO column_labels (
table_name, column_name, column_label, web_type,
detail_settings, code_category, code_value,
reference_table, reference_column, display_order, is_visible
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
ON CONFLICT (table_name, column_name) DO UPDATE SET
column_label = EXCLUDED.column_label,
web_type = EXCLUDED.web_type,
detail_settings = EXCLUDED.detail_settings,
code_category = EXCLUDED.code_category,
code_value = EXCLUDED.code_value,
reference_table = EXCLUDED.reference_table,
reference_column = EXCLUDED.reference_column,
display_order = EXCLUDED.display_order,
is_visible = EXCLUDED.is_visible,
updated_date = now()
`;
await this.client.query(query, [
tableName,
columnName,
settings.columnLabel,
settings.webType,
settings.detailSettings,
settings.codeCategory,
settings.codeValue,
settings.referenceTable,
settings.referenceColumn,
settings.displayOrder || 0,
settings.isVisible !== undefined ? settings.isVisible : true,
]);
// column_labels 업데이트 또는 생성
await prisma.column_labels.upsert({
where: {
table_name_column_name: {
table_name: tableName,
column_name: columnName,
},
},
update: {
column_label: settings.columnLabel,
web_type: settings.webType,
detail_settings: settings.detailSettings,
code_category: settings.codeCategory,
code_value: settings.codeValue,
reference_table: settings.referenceTable,
reference_column: settings.referenceColumn,
display_order: settings.displayOrder || 0,
is_visible:
settings.isVisible !== undefined ? settings.isVisible : true,
updated_date: new Date(),
},
create: {
table_name: tableName,
column_name: columnName,
column_label: settings.columnLabel,
web_type: settings.webType,
detail_settings: settings.detailSettings,
code_category: settings.codeCategory,
code_value: settings.codeValue,
reference_table: settings.referenceTable,
reference_column: settings.referenceColumn,
display_order: settings.displayOrder || 0,
is_visible:
settings.isVisible !== undefined ? settings.isVisible : true,
},
});
logger.info(`컬럼 설정 업데이트 완료: ${tableName}.${columnName}`);
} catch (error) {
@@ -178,6 +185,7 @@ export class TableManagementService {
/**
* 전체 컬럼 설정 일괄 업데이트
* Prisma 트랜잭션으로 변경
*/
async updateAllColumnSettings(
tableName: string,
@@ -188,10 +196,8 @@ export class TableManagementService {
`전체 컬럼 설정 일괄 업데이트 시작: ${tableName}, ${columnSettings.length}`
);
// 트랜잭션 시작
await this.client.query("BEGIN");
try {
// Prisma 트랜잭션 사용
await prisma.$transaction(async (tx) => {
// 테이블이 table_labels에 없으면 자동 추가
await this.insertTableIfNotExists(tableName);
@@ -207,15 +213,9 @@ export class TableManagementService {
);
}
}
});
// 트랜잭션 커밋
await this.client.query("COMMIT");
logger.info(`전체 컬럼 설정 일괄 업데이트 완료: ${tableName}`);
} catch (error) {
// 트랜잭션 롤백
await this.client.query("ROLLBACK");
throw error;
}
logger.info(`전체 컬럼 설정 일괄 업데이트 완료: ${tableName}`);
} catch (error) {
logger.error(
`전체 컬럼 설정 일괄 업데이트 중 오류 발생: ${tableName}`,
@@ -229,30 +229,37 @@ export class TableManagementService {
/**
* 테이블 라벨 정보 조회
* Prisma ORM으로 변경
*/
async getTableLabels(tableName: string): Promise<TableLabels | null> {
try {
logger.info(`테이블 라벨 정보 조회 시작: ${tableName}`);
const query = `
SELECT
table_name as "tableName",
table_label as "tableLabel",
description,
created_date as "createdDate",
updated_date as "updatedDate"
FROM table_labels
WHERE table_name = $1
`;
const tableLabel = await prisma.table_labels.findUnique({
where: { table_name: tableName },
select: {
table_name: true,
table_label: true,
description: true,
created_date: true,
updated_date: true,
},
});
const result = await this.client.query(query, [tableName]);
if (result.rows.length === 0) {
if (!tableLabel) {
return null;
}
const result: TableLabels = {
tableName: tableLabel.table_name,
tableLabel: tableLabel.table_label || undefined,
description: tableLabel.description || undefined,
createdDate: tableLabel.created_date || undefined,
updatedDate: tableLabel.updated_date || undefined,
};
logger.info(`테이블 라벨 정보 조회 완료: ${tableName}`);
return result.rows[0];
return result;
} catch (error) {
logger.error(`테이블 라벨 정보 조회 중 오류 발생: ${tableName}`, error);
throw new Error(
@@ -263,6 +270,7 @@ export class TableManagementService {
/**
* 컬럼 라벨 정보 조회
* Prisma ORM으로 변경
*/
async getColumnLabels(
tableName: string,
@@ -271,35 +279,56 @@ export class TableManagementService {
try {
logger.info(`컬럼 라벨 정보 조회 시작: ${tableName}.${columnName}`);
const query = `
SELECT
id,
table_name as "tableName",
column_name as "columnName",
column_label as "columnLabel",
web_type as "webType",
detail_settings as "detailSettings",
description,
display_order as "displayOrder",
is_visible as "isVisible",
code_category as "codeCategory",
code_value as "codeValue",
reference_table as "referenceTable",
reference_column as "referenceColumn",
created_date as "createdDate",
updated_date as "updatedDate"
FROM column_labels
WHERE table_name = $1 AND column_name = $2
`;
const columnLabel = await prisma.column_labels.findUnique({
where: {
table_name_column_name: {
table_name: tableName,
column_name: columnName,
},
},
select: {
id: true,
table_name: true,
column_name: true,
column_label: true,
web_type: true,
detail_settings: true,
description: true,
display_order: true,
is_visible: true,
code_category: true,
code_value: true,
reference_table: true,
reference_column: true,
created_date: true,
updated_date: true,
},
});
const result = await this.client.query(query, [tableName, columnName]);
if (result.rows.length === 0) {
if (!columnLabel) {
return null;
}
const result: ColumnLabels = {
id: columnLabel.id,
tableName: columnLabel.table_name || "",
columnName: columnLabel.column_name || "",
columnLabel: columnLabel.column_label || undefined,
webType: columnLabel.web_type || undefined,
detailSettings: columnLabel.detail_settings || undefined,
description: columnLabel.description || undefined,
displayOrder: columnLabel.display_order || undefined,
isVisible: columnLabel.is_visible || undefined,
codeCategory: columnLabel.code_category || undefined,
codeValue: columnLabel.code_value || undefined,
referenceTable: columnLabel.reference_table || undefined,
referenceColumn: columnLabel.reference_column || undefined,
createdDate: columnLabel.created_date || undefined,
updatedDate: columnLabel.updated_date || undefined,
};
logger.info(`컬럼 라벨 정보 조회 완료: ${tableName}.${columnName}`);
return result.rows[0];
return result;
} catch (error) {
logger.error(
`컬럼 라벨 정보 조회 중 오류 발생: ${tableName}.${columnName}`,