|
1 | 1 | import { createConnection } from 'mysql2/promise'; |
2 | 2 | import { utils, writeFile } from 'xlsx'; |
3 | 3 | import config from '../../config/dbconfig.js'; |
| 4 | +import { utmFilters } from './utmFilter.js'; |
4 | 5 |
|
5 | 6 | async function exportDataToExcel(req, res) { |
6 | | - try { |
7 | | - // db 연결용 config |
8 | | - const DB_CONFIG = config.test_db_config; |
9 | | - |
10 | | - // 앞으로 많이 수정하게 될 쿼리문 |
11 | | - const SQL_QUERY = 'SELECT * FROM member'; |
12 | | - |
13 | | - const today = new Date(); |
14 | | - const year = today.getFullYear(); // 년도 |
15 | | - const month = today.getMonth() + 1; // 월 |
16 | | - const date = today.getDate(); // 날짜 |
17 | | - const hours = today.getHours(); // 시 |
18 | | - const minutes = today.getMinutes(); // 분 |
19 | | - const seconds = today.getSeconds(); // 초 |
20 | | - const nowTime = |
21 | | - year + '-' + month + '-' + date + ' ' + hours + '-' + minutes + '-' + seconds; |
22 | | - const file_name = '김성현의 테스트 엑셀파일 데이터'; |
23 | | - |
24 | | - // 파일경로이자 마지막 / 뒤에는 내가 저장하게될 파일 이름 + 확장자 현재경로는 바탕하면으로 할 예정 |
25 | | - const FILE_PATH = `/Users/User/${file_name} + ${nowTime}.xlsx`; |
26 | | - |
27 | | - // db 연결 |
28 | | - const connection = await createConnection(DB_CONFIG); |
29 | | - |
30 | | - // 쿼리 실행 후 rows 에 담기 |
31 | | - const [rows] = await connection.execute(SQL_QUERY); |
32 | | - |
33 | | - // 새로운 파일이랑 워크시트 생성 |
34 | | - const workbook = utils.book_new(); |
35 | | - const worksheet = utils.json_to_sheet(rows); |
36 | | - // console.log(worksheet); |
37 | | - |
38 | | - // 워크북에 워크시트 추가 |
39 | | - utils.book_append_sheet(workbook, worksheet); |
40 | | - |
41 | | - // 해당 경로에 워크북 파일 생성 |
42 | | - writeFile(workbook, FILE_PATH); |
43 | | - |
44 | | - // console.log(`파일 내보내기가 잘 실행되었습니다. 파일이 저장된 경로는 -> ${FILE_PATH}`); |
45 | | - |
46 | | - return res.status(200).json({ |
47 | | - isSuccess: true, |
48 | | - msg: '파일 내보내기가 잘 실행되었습니다.', |
49 | | - path: '파일이 생성된 위치는 ' + FILE_PATH + ' 입니다.', |
50 | | - }); |
51 | | - } catch (error) { |
52 | | - return res.status(400)({ |
53 | | - msg: '유효하지 않은 값입니다. 에러내용을 확인해주세요', |
54 | | - error, |
55 | | - }); |
56 | | - } |
| 7 | + const { path, file_name, query } = req.body; |
| 8 | + |
| 9 | + // db 연결용 config |
| 10 | + const DB_CONFIG = config.test_db_config; |
| 11 | + const SQL_QUERY = await utmFilters(query); |
| 12 | + console.log('여기는 쿼리 함수 돌고 돌아오 결과문:', SQL_QUERY); |
| 13 | + |
| 14 | + // const SQL_QUERY = utmFilters() |
| 15 | + /* |
| 16 | + // db 연결용 config |
| 17 | + const DB_CONFIG = config.test_db_config; |
| 18 | + |
| 19 | + // 앞으로 많이 수정하게 될 쿼리문 |
| 20 | + const SQL_QUERY = 'SELECT * FROM kshexportdb.utmtest_2'; |
| 21 | + |
| 22 | + const today = new Date(); |
| 23 | + const year = today.getFullYear(); // 년도 |
| 24 | + const month = today.getMonth() + 1; // 월 |
| 25 | + const date = today.getDate(); // 날짜 |
| 26 | + const hours = today.getHours(); // 시 |
| 27 | + const minutes = today.getMinutes(); // 분 |
| 28 | + const seconds = today.getSeconds(); // 초 |
| 29 | + const nowTime = |
| 30 | + year + '-' + month + '-' + date + ' ' + hours + '-' + minutes + '-' + seconds; |
| 31 | + const file_name = '김성현의 테스트 엑셀파일 데이터'; |
| 32 | + |
| 33 | + // 파일경로이자 마지막 / 뒤에는 내가 저장하게될 파일 이름 + 확장자 현재경로는 바탕하면으로 할 예정 |
| 34 | + const FILE_PATH = `/Users/User/${file_name} + ${nowTime}.xlsx`; |
| 35 | + */ |
| 36 | + |
| 37 | + const FILE_PATH = `${path}${file_name}.xlsx`; |
| 38 | + |
| 39 | + // db 연결 |
| 40 | + const connection = await createConnection(DB_CONFIG); |
| 41 | + |
| 42 | + // 쿼리 실행 후 rows 에 담기 |
| 43 | + const [rows] = await connection.execute(SQL_QUERY); |
| 44 | + |
| 45 | + // // 쿼리 실행 후 rows 에 담기 |
| 46 | + // const [rows] = await connection.execute(SQL_QUERY); |
| 47 | + |
| 48 | + // 새로운 파일이랑 워크시트 생성 |
| 49 | + const workbook = utils.book_new(); |
| 50 | + const worksheet = utils.json_to_sheet(rows); |
| 51 | + // console.log(worksheet); |
| 52 | + |
| 53 | + // 워크북에 워크시트 추가 |
| 54 | + utils.book_append_sheet(workbook, worksheet); |
| 55 | + |
| 56 | + // 해당 경로에 워크북 파일 생성 |
| 57 | + writeFile(workbook, FILE_PATH); |
| 58 | + |
| 59 | + // console.log(`파일 내보내기가 잘 실행되었습니다. 파일이 저장된 경로는 -> ${FILE_PATH}`); |
| 60 | + |
| 61 | + return res.status(200).json({ |
| 62 | + isSuccess: true, |
| 63 | + msg: '파일 내보내기가 잘 실행되었습니다.', |
| 64 | + path: '파일이 생성된 위치는 ' + FILE_PATH + ' 입니다.', |
| 65 | + }); |
57 | 66 | } |
58 | 67 |
|
59 | 68 | export { exportDataToExcel }; |
0 commit comments