I am relatively new to backend development and followed along with a tutorial to build a dummy first app.
Then I started to look for other projects to build solely by myself and I found one but soon enough...I ran into an issue.
I was to create an endpoint that allows users create articles and also add tags to the article, the problem now was how do I manage the articles and tags making sure they are related without having the same tag duplicated multiple times.
So I asked myself, what if a different user creates another article with the same tag(s), do I make the tags unique to each article (this didn't make a whole lot of sense).
How do I make sure the tags are "general" so if a user is the first to create an article with that tag(s) then that's fine as I'll just add a new record, however if others create and add the same tag then I need to find a way to not create another record for the already existing tag and just associate it.
I used MySQL with NodeJS, then I remembered the concept of JOIN tables which I learned during my SQL course and realized I could create separate tables for articles and tags, and then have a join table that has the id of the article and the id of the tag.
That way in my join table if I add a new record to the article table with id 1, and new tags with id 1 and 2.
Then on my article_tags table I'd create two records, so that I know which article belongs to which tag. That means I'll have something like this:
Article 1 - Tag 1
Article 1 - Tag 2.
Then I had my queries check for existence of tags in the DB when creating new articles so that I don't overwrite an existing tag when a new user uses it, but if the tag doesn't exist, then I add a new record for it on the tags table, if not I just associate the existing tag with the new article created.
I'm not quite sure if this is the most efficient, but it was how I solved the bottleneck.
HNG Internship would serve as an important step in my career as a backend developer, to get to work in a team, learn best practices and contribute to meaningful projects.
It's also a chance for me to solve real-world problems just like the one I encountered which I described above as these would help me hone my skills to become a better backend developer.
Top comments (1)
Nice!
Big fan of MySQL :)