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.

Top comments (0)