Papers (BE)

A database, a server, an API, a Covid story

"The whole world is just one giant Covid waiting room."

-Jamal Whorms-MonDésir

TL;DR

Papers (Backend) is an API built using Express and PostgreSQL the programatically allow for accessesing and updating a database of articles, comments, users, and topics.

Objectives

  • Build a database and API that serves data to specified endpoints using the MVC framework
  • Employ best practices for SQL injection prevention
  • Develop models for error handling and relaying status codes

Summary

Fresh off the back of a prolonged tour through callback hell and a stop through middleware midlands, our first solo sprint was to build an API for what would later become a fully-functioning Reddit-style web app in our frontend project.

This one week project entailed building a database with PostgreSQL, building a server and endpoints using Express.js and node-postgres, and extensive testing with Jest and Supertest. All from scratch. So no pressure there.

What a week to catch Covid!

Links
Live site
Codebase
Tickets
Tech Stack
  • PostgreSQL
  • Express
  • Node.js
  • Husky
  • Jest
  • Supertest
By the numbers
4 days to complete
1 day in bed
1 well-deserved bottle of wine

The build

I long ago accepted that the whole world is one giant Covid waiting room but after fending off fate for over two, 'The Rona' finally caught me. This wasn't a particularly convenient time for a pox on my house but, nonetheless, we move.

After some mindless staring into the black mirror looking for where to get started, I managed to skeleton a working model and controller for the first API call without much fuss. I submitted it for pull request and awaited the feedback from underneath my duvet.

For being a predominantly visual learner, I found it remarkably easy to work with invisible databases. The next several tickets became a sort of lather, rinse, repeat excercise. And for the first time on the course, even the practice of writing tests became cogently clear now that I was working with salient data and tangible outputs.

I truly wish I could use this space to describe process of seeding the database, spinning up the server, and writing SQL queries but between the covidian haze and how quickly acquainted I got with everything, not much—bar the points below—has surfaced in my memory.

The Wins

Querying the information schema

By day three, I was so comfortable with writing SQL queries that I was hungry for something more. I spotted an opportunity to advance my learning on ticket #11. Rather than hardcoding the topics into an array, the bare minimum needed to complete this ticket and prevent SQL injection, I figured out how to query the table's metadata via information schema for the topics list instead. Ultimately, this stood me well come frontend project where the topic values were different.

Finishing on schedule

Despite being full of Covid and having to sit out day for rest, I managed to finish all the core tasks within the week.

The Challenges

Testing the tragic path

While writing tests had admittedly become easier, my tutor stressed testing more sad paths. "What if a user misspells 'inc'?" he stressed in one of his pull request comments. At this point I realised I won't always be writing code for end users who have the guiding hand of a UI and so I had to consider more of the ridiculous things that could go wrong and test for some truly tragic paths.

Missing a pair of eyes

As my first solo sprint, I came to truly embrace the value of pair programming after spending hours trying to resolve an object destructuring issue. An expected return value that was meant to be an array kept coming back as an object. After finally giving up and submitting the pull request anyway, the comments in the feedback pointed out exactly which line in which file was causing the issue.

Getting to grips with next

It's easy to miss that dev packages can do some clever things. As an overthinker, I struggled to comprehend the magic that is the next function in Express.js. "BUT HOW DOES IT KNOW WHAT TO DO?!" Eventually, I just had to accept that some people are just as good at overthinking as I am.

What I'd change

Better to async/await

Throughout most the project I stuck to what I know works, which is promise chains. Funny enough, even our tutors commented that most the cohort didn't venture into using async/await despite having a much cleaner syntax. Around the point that was diving into information schema I decided to give it a whirl and realised I had been scribing long promise chains needlessly. If theres's one thing I'd absolutely do differently its this

Phone

Address

Manchester, United Kingdom