How to write more readable queries? [Question] #1542
-
It's almost impossible to read a long query string without code indentation.
I would like to have more indentation; something like this:
Would that be possible? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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 // assuming queries object contains name:query pairs loaded for example from files in queries/[name].sql
const [rows] = await connection.execute(queries.my_big_query_with_parameters, [parameter]); |
Beta Was this translation helpful? Give feedback.
I guess the easiest way to keep it formatted in the source code is to use string literals:
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:/…