Home > database >  Best Practice for writing nested Express.js Queries?
Best Practice for writing nested Express.js Queries?

Time:07-10

I'm new to using Express.js. I'm working on my first endpoint which is to create a user. To accomplish this, it first has to be checked whether the username or email address already exists. After some research on how to do this, here's the code I've come up with:

  // Check whether the user_name or email already exists
  pool.query('SELECT CASE WHEN EXISTS (SELECT * FROM users WHERE user_name = $1 OR email = $2) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END', [values.user_name, values.email], (error, results) => {
    if (error) {
      throw error
    }

    if (results.rows[0].case === '1') {
      console.log('User exists so send message to this effect back to client');
    } else {
      console.log('User does not exist so we can call INSERT query');
      pool.query('INSERT INTO users (first_name, last_name, user_name, email, password, last_password, password_salt) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *', [values.first_name, values.last_name, values.user_name, values.email, values.password, values.password, 'tmp_salt'], (error, results) => {
        if (error) {
          console.error('Something went wrong');
          throw error;
        }
        console.log('Results from INSERT:', results);
        res.status(201).send(`User added with user_id: ${results.rows[0].user_id}`);
      });
    }
  });

It's obviously not finished yet but I'm curious if this nested approach I've used is the best way to do it? In other words, first I'm checking for the existence of user_name and/or email and only if both don't exist am I performing the INSERT.

What do you think?

CodePudding user response:

There are really two different questions there:

  1. Is checking then inserting the right approach?

  2. Is nesting the right approach?

Is checking then inserting the right approach?

Not usually, no, it leaves you open to a race condition:

  • Client A sends [email protected] / funkypassword
  • Client B sends [email protected] / somethingelse
  • The main thread picks up the task for Client A's request and starts the asynchronous check to see if the user exists
  • While waiting for that asynchronous result, the main thread picks up Client B's request and starts the asynchronous check to see if the user exists
  • Both checks come back clear (no user exists)
  • The main thread inserts one of those sets of details (Client A's or Client B's)
  • The main thread tries to insert the other one of those sets of details (Client B's or Client A's)

At this point, if the database is set up correctly, you'll get an insertion error (primary or unique key violation).

Instead, you ensure the DB is set up that way and expect to get an error if the user already exists, and don't do the check at all. Just do the insert, and look at the error if you get one to see if it's a primary or unique key constraint violation.

Is nesting the right approach?

This particular task may not need multiple DB operations (see above), but many others will. So is nesting the right way to handle that?

It's certainly been the main way to handle it for a long time, but it has the issue of quickly becoming "callback hell" where you have lots of nested operations, each in its own callback to a previous operation, etc., etc. It can get very hard to manage. But you can do it that way if you like, and many have done for some time.

The more modern alternative is to use promises and async/await. In an async function, you can make the function wait for an asynchronous process to complete before continuing with its logic. That way, your logic isn't buried in callbacks.

Suppose you have to do two or three database operations, where whether it's two or three depends on the first operation's result, and where information from earlier calls is needed by later ones. With nesting, you might do something like this:

pool.query("DO THE FIRST THING", (error1, results1) => {
    if (error1) {
        /*...handle/report error...*/
        return;
    }
    const thirdThing = (results2) => {
        pool.query("DO THE THIRD THING with results1 and results2 (which may be null)", (error3, results3) => {
            if (error3) {
                /*...handle/report error...*/
                return;
            }
            /*...finish your work using `results1`, `results2`, and `results3`...*/
        });
    };
    if (/*results1 says we should do the second thing*/) {
        pool.query("DO THE SECOND THING with results1", (error2, results2) => {
            if (error2) {
                /*...handle/report error...*/
                return;
            }
            thirdThing(results2);
        });
    } else {
        thirdThing(null);
    }
});

You might isolate the three operations as functions, but while that's good for reuse if it's relevant and possibly for debugging, it doesn't help much with the callback hell:

function firstThing(callback) {
    pool.query("DO THE FIRST THING", callback);
}
function secondThing(paramA, callback) {
    pool.query("DO THE SECOND THING with paramA", callback);
}
function thirdThing(paramA, paramB, callback) {
    pool.query("DO THE THIRD THING with paramA and paramB (which may be null)", callback);
}

// ...and then where your code was:
const done = (error, results1, results2, results3) => {
    if (error) {
        /*...handle/report error...*/
    } else {
        /*...do your final work here using `results1`, `results2` (which may be `null`), and `results3`...*/
    }
});
firstThing((error1, results1) => {
    if (error1) {
        done(error1);
    } else if (/*results1 says we should do the second thing*/) {
        secondThing(results1, (error2, results2) => {
            if (error2) {
                done(error2);
            } else {
                thirdThing(results1, results2, (error3, results3) => {
                    done(error3, results1, results2, results3);
                });
            }
        });
    } else {
        thirdThing(results1, null, (error3, results3) => {
            done(error3, results1, null, results3);
        });
    }
});

But suppose we had a poolQuery function that put a promise wrapper around pool.query. Here's how that could look:

async function firstThing() {
    return await poolQuery("DO THE FIRST THING");
}
async function secondThing(paramA) {
    return await poolQuery("DO THE SECOND THING with paramA");
}
async function thirdThing(paramA, paramB) {
    return await poolQuery("DO THE THIRD THING with paramA and paramB (which may be null)");
}

// ...and then where your code was, making it an `async` function:
try {
    const results1 = await firstThing();
    const results2 = (/*results1 says we should do the second thing*/)
        ? await secondThing(results2)
        : null;
    const results3 = await thirdThing(results1, results2);
    /*...do your final work here using `results1`, `results2` (which may be `null`), and `results3`...*/
} catch (error) {
    /*...handle/report error...*/
}

Or if you aren't going to reuse those queries, then:

try {
    const results1 = await poolQuery("DO THE FIRST THING");
    const results2 = (/*results1 says we should do the second thing*/)
        ? await poolQuery("DO THE SECOND THING with results1")
        : null;
    const results3 = await poolQuery("DO THE THIRD THING with results1 and results2 (which may be null)");
    /*...do your final work here using `results1`, `results2` (which may be `null`), and `results3`...*/
} catch (error) {
    /*...handle/report error...*/
}

How simple and clear is that? :-) It gets even more powerful when you have loops and such involved.

  • Related