DEV Community

Cover image for Day three - async/await and db refactor
Rhymes Toasterface
Rhymes Toasterface

Posted on • Originally published at triplejdeveloping.com

Day three - async/await and db refactor

10/5/20 Step one. Still

The async/await thing has been bothering me. I didn't do it because I had never used it and never really bothered/had the time to figure it out.

So, I figured now was as good a time as any - I'm supposed to be learning, right?

So for this instalment, dear reader, I went for the async thang.

Turns out it wasn't really all that difficult. I changed the connection/client - added an async keyword:

async client

The bigger changes happened in the db functions, although they weren't really that big:

async db functions

Other than this, I made some changes to the recipe interface:

recipe interface

I also changed the recipe db functions - mostly, I wasn't happy with the db and collection names.

I was working with a db called 'Recipes'. Which kind of forced me to use collections by recipe type - main, dessert, whatever.

I wasn't happy with this - what if I wanted to change the type of a recipe - who knows why, but I might want to. I would have to change the recipe type AND migrate it across collections.

This wasn't the one - I now settled for a 'Toasterfaves' db and a recipes collection. Maybe there will be other things to save with this site? As well as this, you might note I added the dbName argument to the db functions. This will open up those functions to be usable later on if/when we add more dbs.

recipe db functions

That's pretty much it for now. I'm happier these things are done and I don't need to come back and refactor them further down the line.

I guess next, I will have to expose these functions - I assume using express. Then, I will have to get that api running on the cpanel server. That'll be fun...

Stay tuned...

  • one thing worth noting - I can't be sure any of this is correct! But it works. And I'm pretty happy with it. If you have any suggestions, let me know.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

The only thing worse than downtime? No Answers.

If you’re sometimes frustrated with opaque infrastructure, sluggish support, and mysterious outages, we prepared a webinar just for you

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️