Skip to content

How to write more readable queries? [Question] #1542

Answered by sidorares
PedroDivergence asked this question in Q&A
Discussion options

You must be logged in to vote

I guess the easiest way to keep it formatted in the source code is to use string literals:

const [rows] = await connection.query(`
SELECT
  T_1.field_with_big_name,
   T_1.another_big_field_name, 
   T_2.again_another_big_field 
FROM 
   MY_TABLE_NAME_NUMBER_1 as T_1 
WHERE 
  \`T_1.another_big_field_name\` = "ABCDEF"
JOIN
`);

if you have lots of backticks in the query text this might still look a bit noisy ( SO: how to escape backicks in string literal ). Possible other solutions: have a set of plain text files ( with .sql extension ) and read them at startup into a dictionary { file_name: string_with_sql_content }. As a bonus you'll get syntax highlighting. The code will look like this:

/…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@PedroDivergence
Comment options

Answer selected by PedroDivergence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1541 on March 25, 2022 00:02.