Seeding your database

Hillary Jenkins
3 min readFeb 19, 2021

When I first started writing code my bootcamp supplied files with seed data. Without any real discussion we were let loose to create our own for the next project. Naturally copying the style previously used was first approached. This was tedious, time consuming, and to be honest it created boring data.

I had to repeatedly look at seed data while continuing to build my code so I wanted something interesting to look at. Instead of taking the route of mockaroo or another site that supplies lists of data I wanted to create my own. I wanted to make it not entirely predictable so each time I ran my seed file I could be delighted by the new data.

My first project was an e-commerce site. We needed seed data of username, emails, and products. To accomplish this I first created multiple arrays: first names, last names, and adjectives. Each array had roughly 40 items and for good measure I created a short array of three email domains. It was a fun creative side project figuring out how to fill these arrays with words that would later bring delight to a challenging day(s) of coding that I knew would be in the future as I just began this project.

Once my arrays were set up it was a simple matter of setting up a randomization loop to put the items together. Here are a few examples:

Random user info includes first name, last name, email, password, and an order

By first creating a loop that will iterate 200 times my outcome will be 200 randomly created users for my database. Each will have a first name, last name, email that consists of the first and last name accompanied by an email domain, a password of ‘password’ so I can easily login as any user while testing my code, and an order (which was the total value of their cart).

Random product info include name, quantity, and price

Here I created a loop that will iterate as many times as there are adjectives in the adjective array. I did this to avoid repetition in the product names (plus I added a fallback unique: true statement in the Product model). After running this loop each product will have a name that includes the adjective, a random quantity in the database between 0 and 100, and a random price between 0 and $10,000.

The project also needed associations for the PostgreSQL database so we could have prepared orders on our e-commerce site while testing. These I created using Sequelize magic methods. Magic methods are numerous and powerful. I hardly got into magic methods, but I learned a lot about associations while working on this.

creating associations

I had already created one association when creating the users by adding an amount for the order. Before more associations could be added to the database I needed to find some products and orders to associate; which I did by utilizing findAll() with a where condition. This gave me two new arrays of data in the database that I could then associate using the magic method “add<name of model>”, in this case it was addProducts.

Lastly, I added the database syne force:true to my seeding function so that each time I ran my seed function the previous database would clear, and new data would be added. This added delight to my days of coding, and I’m so glad I took the time to figure this out.

--

--