Last active
July 9, 2022 15:22
-
-
Save santanaG/b9096c60555784a96e06c1ccf2bef66f to your computer and use it in GitHub Desktop.
cleaned up version of getUserbyEmail as seen here: https://gajus.medium.com/how-would-you-reduce-nesting-in-this-example-http-gajus-com-blog-8-using-mysql-in-node-js-4c8c71c3a59d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. move queries to their own variables | |
const userQ = 'SELECT `id` FROM `user` WHERE `email` = ?' | |
const permissionsQ = 'SELECT `id`, `name` FROM `permission` WHERE `user_id` = ?' | |
// 2. remove codeblocks/explicit return statements | |
// 3. use 2 spaces instead of 4 for tabs | |
// 4. leverage ternary operator for that check | |
const getUserByEmail = (connection, userEmail) => connection | |
.queryAsync(userQ, [userEmail]) | |
.spread(user => user === undefined // reverse the logic | |
? {} // i favor an empty object over null | |
: connection.queryAsync(permissionsQ, [user.id]) | |
.then(permissions => ({ ...user, permissions })) // object literal property shorthand was introduced in es6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment