nestJs, postgres, CSV

욱2·2023년 5월 29일

정리된 파일

서비스

    async processCSVFile(inputFile: string): Promise<void> {
        const batchSize = 100;
     
        return new Promise<void>((resolve, reject) => {
            let currentBatch: any[] = []
            createReadStream(inputFile, { encoding: 'utf-8' })
                .pipe(csvParser())
                .on('error', (error) => {
                    console.error('Error reading CSV file:', error);
                    reject(error);
                })
                .on('data', async (row: any) => {
                    if (row['상세영업상태코드'] === '01') {
                        currentBatch.push(row); 

                    
                        if (currentBatch.length === batchSize) {
                            await this.storesRepository.processCSVFile(currentBatch); 
                            currentBatch = []; 
                        }
                    }
                })
                .on('end', async () => {
                    if (currentBatch.length > 0) {
                        await this.storesRepository.processCSVFile(currentBatch);
                    }
                    resolve();
                })
                .on('finish', () => {
                    console.log('CSV processing completed.');
                });
        });
    }

리포지토리

  async processCSVFile(rows: any): Promise<void> {
        for (const rowData of rows) {

            {
                const La = 0;
                const Ma = 0;
                const description = 'string';
                const maxWaitingCnt = 0;
                const currentWaitingCnt = 0;
                const tableForTwo = Math.floor(Math.random() * 10);
                const tableForFour = Math.floor(Math.random() * 10);
                const storeName = rowData['사업장명'];
                const category = rowData['위생업태명'];
                const address = rowData['도로명전체주소'];

                const store = this.create({
                    storeName,
                    description,
                    maxWaitingCnt,
                    currentWaitingCnt,
                    La,
                    Ma,
                    tableForFour,
                    tableForTwo,
                    category,
                    address,
                });

                try {
                    const result = await this.save(store);
                    console.log('Inserted', result, 'row:', store);
                } catch (error) {
                    console.error('Error occurred during insert:', error);
                }
            }
        }
    }
profile
성장하는 날 위한 기록

0개의 댓글