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

[prql-js] Very bad template literal example #4164

Open
2 tasks done
leumasme opened this issue Feb 2, 2024 · 2 comments
Open
2 tasks done

[prql-js] Very bad template literal example #4164

leumasme opened this issue Feb 2, 2024 · 2 comments
Labels

Comments

@leumasme
Copy link

leumasme commented Feb 2, 2024

What happened?

In the prql-js bindings readme, one can find the following usage example:

const prqljs = require("prql-js");
const prql = (string) => prqljs.compile(string[0] || "");

const sql = prql`from employees | select first_name`;
console.log(sql);

Here, a custom template literal formatter "prql" is defined and then used.
Such template literal formatters are often seen in ORMs to handle automatically escaping variables, e.g.

let username = "this_will_be_escaped"
sql`select * from users where username = ${username}`

the misguided use in the example was presumably picked up from such usecases and then implemented incorrectly.
A template literal formatter will get the raw strings as an array via the first argument, then the variables to insert as spread arguments, e.g.

const sql = (strings, ...variables) => // escape variables and insert them between elements of strings array

The example implementation simply returns the first string element. This means that if, for example

prql`hello ${variable} world`

is called, only hello will be passed to prqljs.compile.

A template literal function has no purpose here in the first place, as template literals can just be passed to functions directly (where they will first get joined into a string):

compile(`none
of that
nonsense`)

this is not the same as the prql example above; here, the compile function will simply always be called with one string (the result of the template literal).

TLDR the template literal formatter function in the example is not only broken, but also useless and should be avoided due to the convention of template literal formatter functions preprocessing what is passed to them

PRQL input

-

SQL output

-

Expected SQL output

No response

MVCE confirmation

  • Minimal example
  • New issue

Anything else?

the error catching (example) of parsing json out of the error message is also pretty horrendous

@leumasme leumasme added the bug Invalid compiler output or panic label Feb 2, 2024
@max-sixty
Copy link
Member

Thanks for the issue @leumasme

Does anyone here who knows more about JS than me have a view?

@aljazerzen
Copy link
Member

I agree, using a template in the example is an overkill, even if prql`from x` looks cool.

@leumasme, do you care opening a PR?

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

No branches or pull requests

3 participants