ivank / potygen

Postgres SQL Parser and type generator
Other
99 stars 1 forks source link

Magic comments #29

Closed wmertens closed 2 years ago

wmertens commented 2 years ago

Would it be possible to make a comment like /*sql*/ treat the next string literal as SQL to parse?

like in

/*sql*/
const foo = await db.get(`select 1`)

or

const foo = await db.get(/*sql*/ `select 1`)
ivank commented 2 years ago

Don't see why not.

It should be a simple addition to add another condition to match a string.

https://github.com/ivank/potygen/blob/60952c796ac5ced1e240f9b431b0541eda7d1c3d/packages/cli/src/traverse.ts#L49-L75

ivank commented 2 years ago

Don't have time to add this right now (working on some of my other projects), but will certainly add it soonish. If you want to give it a go I can help you out.

ivank commented 2 years ago

Actually why do you need this for? Potygen has 3 main things it kinda does for you, and not sure what effect are you after exactly?

  1. A prettier plugin, that formats the query
  2. Typescript language extension with auto complete and quick info
  3. Typescript type generation for the resulting query.

Now 2 is a typescript feature and I can't really change how it works - only string template literals. 3 kinda wouldn't work in your proposal as the string template literal does more things for you than just hold the string of the SQL - it converts the $param into positional params (?) that Postgres accepts. 1 though - I think I can do a prettier fix to auto format strings with an sql comment, but that would be pretty much it.

wmertens commented 2 years ago

I'm the author of https://github.com/StratoKit/strato-db and we use it internally a lot, and sometimes I need to run real SQL instead of calling the wrapper methods. In those rare cases, I don't use the sql template function even though I have one, because I rarely interpolate and I feel guilty about the tiny overhead :sweat_smile:.

Add to that that currently Prettier doesn't support custom template functions and I was thinking that a comment would be easier. SQLite syntax is very similar to PostGreSQL so that's how I ended up here.

So I wouldn't be using the typescript features at all, unless I could make them work on strato-db, but even they would be of limited use.

ivank commented 2 years ago

Ok so - prettier.

Have in mind that for prettier to work with potygen, you need to use a patched version of it (a few lines changed, but still), until they add official support for it, and we’re only talking about template literals, things like comment hints are something I doubt they’ll add anytime soon.

If you manage to convince them about it here - https://github.com/prettier/prettier/pull/12139 I’ll be more than happy to extend my plugin to support it.

In fact the potygen prettier plugin works by specifying “sql” as a language, as long as prettier recognizes it somewhere it would work. Its just they don’t expose “sql” as an embedded language, and I had to patch it.

On the upside you can write sql in .sql files and prettier will pick them up and format them with this plugin.

wmertens commented 2 years ago

Ok fair enough, I'lll close this issue. Hopefully template function names will be configurable in Prettier soon, but it doesn't seem likely.