Code Critique for NodeJS Replit Database Functions

Hello all,

Question:
I was tired of re-writing arrow functions in my code, so I decided to write a standard library of functions for the ReplitDB. This might be something somebody else has already done, but I figured it would be a nice mental exercise. I invite anybody to please add constructive feedback to my Repo. It would be greatly appreciated.

Repl link:
ReplitDB

1 Like

Nice library, only thing is, none of the methods can return any value. It would probably be good to return db.list(); here for example, so you could actually do something with the results. (Because presumably, printing keys isn’t the only thing you’re going to be doing.)

list: () => {
  db.list()
    .then(keys => {
      keys.forEach((dbkey) => {
        console.log(dbkey);
      });
    });
}, 

I’ve actually rewritten the Replit Database library myself because… I felt like it… and I made some minor improvements and added more functionality.

Interesting, I would like to know more about this perspective. I did this because when I write UnitTests for this project, I planned to dest against the values in the database rather than the values within the method. Obviously I think that whatever would be returned by the function is what I want in the database - am I being overly cautious?

1 Like

Well, if you rewrote the list method like this, you could call it and see the results printed to the console, but you could also do something with these values if you wanted.

list: () => {
  return db.list()
    .then(keys => {
      keys.forEach((dbkey) => {
        console.log(dbkey);
      });
      return keys;
    });
}, 
1 Like

I’m going to try that out, but is that legal? You’d be trying to return a value from within a promise out of a promise. Or is this only not possible when trying to assign the value to a variable?

I am exercising my understanding. Promises are a concept I’ve been wrestling with for a while. I’m still getting a handle on them.

Edit
Oooh, is my dbfn.list() method thenable because it inherits the fact that it invokes a promise?

Edit
It looks like neither of those are what you mean. Could you please explain a little more about returning the value within then()?

These both yielded errors

index.js

const arrayofkeys = dbfn.list(); // exactly what I expected
console.log('arrayofkeys contains:', arrayofkeys);

dbfn.list() // what I thought you meant
  .then( (theSameKeys) => {
    console.log('the samekeys contains:', theSameKeys);
  });

Edit
Oh, or do you mean added the additional .then() so that if a forker wants to, it’s there already?

1 Like

What you thought I meant was right, you can still use the values for more than just printing them. Or you could use await to do the first (if in an async function.

1 Like