
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?! ๐ค