Skip to content

Instantly share code, notes, and snippets.

@santanaG
Last active July 9, 2022 15:22
Show Gist options
  • Save santanaG/b9096c60555784a96e06c1ccf2bef66f to your computer and use it in GitHub Desktop.
Save santanaG/b9096c60555784a96e06c1ccf2bef66f to your computer and use it in GitHub Desktop.
// 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