nene / prettier-plugin-sql-cst

Prettier SQL plugin that uses sql-parser-cst

Home Page:https://nene.github.io/prettier-sql-playground/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to format SQL inside JavaScript files?

KyleAMathews opened this issue · comments

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

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.

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.

This looks awesome, @Sec-ant.

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

@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:

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 sql-formatter-org/sql-formatter#495, sql-parser-cst may be a better fit overall for our formatting configuration.

@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.

@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`;
}

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

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

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

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

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

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

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