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

Dynamic identifiers such as schema or table name #120

Open
kevbook opened this issue Nov 11, 2024 · 1 comment
Open

Dynamic identifiers such as schema or table name #120

kevbook opened this issue Nov 11, 2024 · 1 comment
Assignees

Comments

@kevbook
Copy link

kevbook commented Nov 11, 2024

In https://github.com/porsager/postgres which has a similar query syntax, there is a way to use dynamic identifiers for schema or table name. Need something similar here as well. Let me know how I can help.

const tableName = 'abc';
await sql`SELECT * FROM ${sql(tableName)}`;
@jawj jawj self-assigned this Dec 5, 2024
@jawj
Copy link
Collaborator

jawj commented Dec 5, 2024

Thanks for the report, and sorry for the delay.

Currently we don't have a mechanism for that, but certainly we should: I'll add it to my TODO list.

In the meantime, you should be able to work around this by using the sql function as an ordinary function, with (...), and providing an ordinary template string as the query. For example:

const tableName = 'abc';
await sql(`SELECT * FROM ${tableName}`);

Obviously be careful with possible SQL injection attacks in this case.

If you also need to pass parameters when using the function like this then you should use $1, $2, etc. For example:

const tableName = 'abc';
const limit = 10;
await sql(`SELECT * FROM ${tableName} LIMIT $1`, [limit]);

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

2 participants