Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Dynamic Data Generation with JavaScript
Adron Hall
Adron Hall

Posted on

     

Dynamic Data Generation with JavaScript

This video shows the process detailed below in this blog entry, to provide the choice of video or a quick read! 👍🏻😁

I coded up some JavaScript to generate some data for a table recently and it seemed relatively useful, so here it is ready to use as you may. (The complete js file is below the description of the individual code segments below). This file simple data generation is something I put together to create a csv for some quick data imports into a database (Postgres, SQL Server, or anything you may want). With that in mind, I added the libraries and initialized the repo with the libraries I would need.

npm install fakernpm install fs
Enter fullscreen modeExit fullscreen mode
faker=require('faker');fs=require('fs');
Enter fullscreen modeExit fullscreen mode

Next up I included the column row of data for the csv. I decided to go ahead and setup the variable at this point, as it would be needed as I would add the rest of the csv data to the variable itself. There is probably a faster way to do this, but this was the quickest path from the perspective of getting something working right now.

After the colum row, I also setup the base 8 UUIDs that would related to the project_id values to randomly use throughout data generation. The idea behind this is that the project_id values are the range of values that would be in the data that Subhendu would have, and all the ip and other recorded data would be recorded with and related to a specific project_id. I used a UUID generation site to generate these first 8 values, that site is availablehere.

After that I went ahead and added the for loop that would be used to step through and generate each record.

vardata="id,country,ip,created_at,updated_at,project_id\n";letproject_ids=['c16f6dd8-facb-406f-90d9-45529f4c8eb7','b6dcbc07-e237-402a-bf11-12bf2226c243','33f45cab-0e14-4830-a51c-fd44a62d1adc','5d390c9e-2cfa-471d-953d-f6727972aeba','d6ef3dfd-9596-4391-b0ef-3d7a8a1a6d10','e72c0ed8-d649-4c53-97c5-da793d7a8228','bf020fd2-2514-4709-8108-a2810e61c503','ead66a4a-968a-448c-a796-51c6a1da0c20'];for(vari=0;i<500000;i++){// TODO: Generation will go here.}
Enter fullscreen modeExit fullscreen mode

The next thing that I wanted to sort out are the two dates. One would be the created_at value and the other the updated_at value. The updated_at date needed to show as occurring after the created_at date, for obvious reasons. To make sure I could get this calculated I added a function to perform the randomization! First two functions to get additions for days and hours, then getting the random value to add for each, then getting the calculated dates.

functionaddDays(datetime,days){letdate=newDate(datetime.valueOf());date.setDate(date.getDate()+days);returndate;}functionaddHours(datetime,hours){lettime=newDate(datetime.valueOf())time.setTime(time.getTime()+(hours*60*60*1000));returntime;}vardays=faker.datatype.number({min:0,max:7})varhours=faker.datatype.number({min:0,max:24})varupdated_at=newDate(faker.date.past())varcreated_at=addHours(addDays(updated_at,-days),-hours)
Enter fullscreen modeExit fullscreen mode

With the date time stamps setup for the row data generation I moved on to selecting the specific project_id for the row.

varproj_id=project_ids[faker.datatype.number({min:0,max:7})]
Enter fullscreen modeExit fullscreen mode

One other thing that I knew I'd need to do is filter for the' or, values located in the countries that would be selected. The way I clean that data to ensure it doesn't break the SQL bulk import process is kind of cheap and in production data I wouldn't do this, but it works great for generated data like this.

varcleanCountry=faker.address.country().replace(",","").replace("'","")
Enter fullscreen modeExit fullscreen mode

If you're curious why I'm calculating these before I do the general data generation and set the row up, I like to keep the row of actual data calls to either a set variable assignment or at most one dot level deep in my calls. As you'll see now in the row level data being generated below.

data2+=faker.datatype.uuid()+","+cleanCountry+","+faker.internet.ip()+","+created_at.toISOString()+","+updated_at.toISOString()+","+proj_id+"\n"
Enter fullscreen modeExit fullscreen mode

Now the last step is to create the file for all these csv rows.

fs.writeFile('kundu_table_data.csv',data,function(err){if(err)returnconsole.log(err);console.log('Data file written.');});
Enter fullscreen modeExit fullscreen mode

The results.

JavaScript csv results.

Alt Text

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A jovial coder, listening to some metal, enjoying the day.
  • Location
    Seattle, Washington
  • Education
    Got Some
  • Joined

More fromAdron Hall

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp