Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pg-promise Query Files with Named Parameters #556

Open
cmcnicoll opened this issue Jan 31, 2024 · 2 comments
Open

Support pg-promise Query Files with Named Parameters #556

cmcnicoll opened this issue Jan 31, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@cmcnicoll
Copy link

I use sqlfmt for all my dbt projects. Thank you @tconbeer for creating this wonderful tool!

It would be great if sqlfmt could also support this:

select * from users where id = ${id}

Docs

@tconbeer tconbeer added the enhancement New feature or request label Apr 5, 2024
@tconbeer
Copy link
Owner

tconbeer commented Apr 5, 2024

From pg-promise docs:

Named Parameters

When a query method is parameterized with values as an object, the formatting engine expects the query to use the Named Parameter syntax $*propName*, with * being any of the following open-close pairs: {}, (), <>, [], //.

// We can use every supported variable syntax at the same time, if needed:
await db.none('INSERT INTO users(first_name, last_name, age) VALUES(${name.first}, $<name.last>, $/age/)', {
    name: {first: 'John', last: 'Dow'},
    age: 30
});

@cmcnicoll
Copy link
Author

Here is the workaround I've been using:

  1. Run a script to quote named parameters in query file example.sql:
select * from users where id = '${id}'
  1. Use sqlfmt via dbt Power User
  2. Test query manually
  3. Run another script to unquote named parameters
  4. Test query in app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants