Hamilton T da Silva
BRBuildShip + Rowy
•Created by Hamilton T da Silva on 3/3/2024 in #❓・buildship-help
How upload data to MySQL from a CSV file?
I am trying to create a node to upload data to my MySQL table, using a CSV file as source. My code is this:
import mysql from 'mysql2/promise';
import {
createReadStream
} from 'fs';
import {
parse
} from 'fast-csv';
export default async function loadCsvToMySql({
host,
database,
username,
password,
fileName
}) {
const connection = await mysql.createConnection({
host,
user: username,
password,
database,
});
const fullFilePath = process.env.BUCKET_FOLDER_PATH + "/" + fileName;
// Definir a função streamFactory
const streamFactory = () => createReadStream(fullFilePath);
await connection.query(
await connection.query(
LOAD DATA LOCAL INFILE '${fullFilePath}' INTO TABLE shapes_aux FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS
,
[ ],
{ streamFactory }
);
const [rows] = await connection.query('SELECT COUNT(*) AS count FROM shapes_aux');
await connection.end();
return {
count: rows[0].count
};
}
But I am receiving this error:
"error": "As a result of LOCAL INFILE command server wants to read arquivo.csv file, but as of v2.0 you must provide streamFactory option returning ReadStream."
I couldn´t find a solution. Can someone help me? Thanks!4 replies