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

Question: SQL functions and string literals #1020

Open
jeofo opened this issue Jan 25, 2025 · 0 comments
Open

Question: SQL functions and string literals #1020

jeofo opened this issue Jan 25, 2025 · 0 comments

Comments

@jeofo
Copy link

jeofo commented Jan 25, 2025

Hi,
Here's an example query I'm trying to make that involves a SQL function, here I'm using the Apache AGE extension but I'd assume all functions should work similarly.

export const cypher = async (graph: string, query: string, type: string[]) => {
  const a = 'users';
  const b = 'RETURN 1';
  const q = await sql`
      SELECT * FROM cypher('${a}', $$ ${b} $$) AS (n agtype)
    `;
...
};

The query above doesn't work with error:

PostgresError: graph "$1" does not exist

Further debugging shows, when code is modified to:

export const cypher = async (graph: string, query: string, type: string[]) => {
  const a = 'users';
  const b = 'RETURN 1';
  const q = await sql`
      SELECT * FROM cypher('users', $$ ${b} $$) AS (n agtype)
    `;
...
};

I get

PostgresError: unexpected character at or near "$"

And lastly, if the entire literal is entered

export const cypher = async (graph: string, query: string, type: string[]) => {
  const a = 'users';
  const b = 'RETURN 1';
  const q = await sql`
      SELECT * FROM cypher('users', $$ RETURN 1 $$) AS (n agtype)
    `;
...
};

the query works.

How should I interact with functions? Thanks.

I know that sql.unsafe() works, but trying my best to avoid.

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

No branches or pull requests

1 participant