Building a Website Builder: Seed Database with Default Data

For my Coding with Callie website builder to function properly, an admin user (aka me), a home page, and a wild card page need to exist in my database ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป

So, I wrote a Seed function that my Go API calls when it starts up to create everything automatically ๐ŸŒฑ

โœ… It pulls my admin password from my environment variables, hashes that password using bcrypt, and then adds an admin user to the Users table.

โœ… Then, it creates a published home page so that end users have something to see immediately when they load โ€˜www.coding-with-callie.comโ€™.

โœ… Finally, it creates a wildcard page so that any other route an end user types into their browser shows a โ€˜Page Not Foundโ€™ component.

A potential issue with this plan is that we only want the Seed function to add these items if they donโ€™t already exist. I didnโ€™t want to make extra database calls to check for an admin user, home page, or wildcard page before first; so, instead, I added a unique constraint on the username and email columns in the Users table and the menu_name and path columns in the Pages table ๐Ÿ™…๐Ÿปโ€โ™€๏ธ

Then, I added โ€˜ON CONFLICT (columns with unique constraints) DO NOTHINGโ€™ to my SQL commands. This way, my Seed function works as I intended and only adds the data I want to the database one time ๐Ÿฅณ

What do you think?! ๐Ÿค”

You May Also Like