nene / prettier-plugin-sql-cst

Prettier SQL plugin that uses sql-parser-cst
https://nene.github.io/prettier-sql-playground/
GNU General Public License v3.0
123 stars 7 forks source link

Is there a way to format SQL inside JavaScript files? #12

Closed KyleAMathews closed 8 months ago

KyleAMathews commented 10 months ago

E.g. writing queries for SQLite in node.js. Can you teach a plugin to recognize certain objects keys and format the string?

nene commented 10 months ago

Not as far as I know.

The Prettier API allows me to do things the other way around. I can tell it to format JavaScript inside SQL. Like this plugin already does with JavaScript inside BigQuery functions. But to my knowledge there is no way to hook into another language (like JavaScript) to format contents of its strings.

The simpler approach to achieve this would be an editor plugin where you could select the SQL you want to format. Unfortunately one won't then have an ability to automatically format the SQL.

Sec-ant commented 9 months ago

Hello, I wrote a plugin to format embedded languages like SQL in JavaScript: https://github.com/Sec-ant/prettier-plugin-embed

It currently requires prettier-plugin-sql to format SQL in JavaScript, which uses sql-formatter and node-sql-parser internally for parsing and formatting.

I'd love to add support for prettier-plugin-sql-cst once it's ready in Prettier v3.

nene commented 9 months ago

This looks awesome, @Sec-ant.

I basically have the migration to Prettier 3 done. Hopefully I can manage to release it next week.

karlhorky commented 9 months ago

@KyleAMathews in case you are open to trying out sql-formatter via prettier-plugin-sql before prettier-plugin-sql-cst is supported, here's a configuration guide:

karlhorky commented 9 months ago

I'd love to add support for prettier-plugin-sql-cst once it's ready in Prettier v3.

I basically have the migration to Prettier 3 done. Hopefully I can manage to release it next week.

This is great, I'll be happy to test this as well! I think because of issues like https://github.com/sql-formatter-org/sql-formatter/issues/495, sql-parser-cst may be a better fit overall for our formatting configuration.

KyleAMathews commented 9 months ago

@karlhorky nice! Do you plan to support formatting anything other than template literals? The library I use takes the sql string as a key on an object passed to a function.

Sec-ant commented 9 months ago

@karlhorky nice! Do you plan to support formatting anything other than template literals? The library I use takes the sql string as a key on an object passed to a function.

Does this work in your case? @KyleAMathews :

const a = {
  sql: /* sql */ `select * from table1`;
}
KyleAMathews commented 9 months ago

ah, that's supported? I'll give it a try tomorrow.

Sec-ant commented 9 months ago

ah, that's supported? I'll give it a try tomorrow.

Yes, /* identifier */ `...` and identifier`...` are supported and you can customize the identifiers :)

nene commented 9 months ago

@Sec-ant I have now released prettier-plugin-sql-cst 0.9.0 which has Prettier 3 support.

Sec-ant commented 9 months ago

@nene Great news, I'll let you know when it is ready in prettier-plugin-embed. :)

nene commented 8 months ago

I'm closing this issue as I think the embed plugin solves the problem.

I've added link to the embed plugin to README.