DEV Community

Chiamaka Ikeanyi
Chiamaka Ikeanyi

Posted on

2

How to Generate Contents of a New CSV Column Using JavaScript

I had the need to populate data in a database table after a migration to update the production database schema. I thought of different means to achieve this. Manually populating the rows with data was not an option because the database contains thousands of records.

I considered two approaches:

  • Using a regex
  • Writing code to achieve it

After a while, I settled for the second approach because of access restrictions on running the regex.

Here is how I achieved it:

  • I exported the data,
  • Wrote the JavaScript code to generate the content of the new column
  • Created a temporary table and imported the newly generated CSV data
  • Imported the data to the actual table using an inner join

A sample data

"id", "label"
1,"Name"
2,"Age"
3,"Gender"
4,"Date of birth"
Enter fullscreen mode Exit fullscreen mode

I assigned the data to a variable

var data = `"id", "label"
1, "Name"
2,"Age"
3,"Gender"
4,"Date of birth"`
Enter fullscreen mode Exit fullscreen mode

Initially, I achieved the result using a nested for loop. Considering performance, I eventually refactored the code to use only one for loop

The JavaScript code to generate the content of the new column

const dataAsArray = data.split('\n');

for(let datum of dataAsArray) {
  let currentRow = datum.split(',');
  let newColumn = currentRow[1].trim().toLowerCase().split(' ').join('_');
  currentRow += `,${newColumn}`;

  console.log(currentRow);   
}
Enter fullscreen mode Exit fullscreen mode

The code above generated the data I needed. Which is, the content of the second column without spaces and separated by an underscore. I saved the result in a CSV file.

Then, I created a temporary table with the columns "id","label","code" and loaded the data using the command

LOAD DATA LOCAL INFILE '/Users/Chiamaka/Desktop/query_result.csv'
INTO TABLE `testtable` 
CHARACTER SET 'utf8' FIELDS ESCAPED BY '\\' TERMINATED BY ',' ENCLOSED BY '"' LINES 
TERMINATED BY '\n'
IGNORE 1 LINES
Enter fullscreen mode Exit fullscreen mode

To update the actual table with the newly generated data using backticks to ensure that reserved names are accepted as strings.

UPDATE testtable tt
inner join actualtable actb on (tt.id = actb.id)
set actb.code = tt.`code`
where tt.id = actb.id;
Enter fullscreen mode Exit fullscreen mode

This yielded the desired result

"id","label","code"
1,"Name","name"
2,"Age","age"
3,"Gender","gender"
4,"Date of birth","date_of_birth"
Enter fullscreen mode Exit fullscreen mode

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

Top comments (0)

Neon image

Set up a Neon project in seconds and connect from a Next.js application

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay