```sql SELECT * FROM comments WHERE post_id IN ( ...
# help
d
Copy code
sql
SELECT * 
FROM comments
WHERE post_id IN (
  SELECT post_id FROM posts WHERE user_id = '1234'
)
s
You can create this as a view and use the SDK to retrieve that view
Or you can create a database function if you need to pass it parameters
d
I am querying from my Express app and I want to run the above query using JS SDK. What do you mean by view?
s
If you are passing a parameter you will have to use a database function to do such a query as views don't take variables from what I can remember. Take a look at https://www.postgresqltutorial.com/postgresql-create-function/ for more information on how Postgres functions work.
Also that website is a good resource to lean more about Postgres, which is the underlying database that Supabase uses
You can call a function with the SDK by using the
.rpc
method https://supabase.io/docs/reference/javascript/rpc
d
Thanks, I"ll checkout. I tried this and it's working but I am not sure if this is good thing to do:
Copy code
js
const userId = "1234"

const {data} = supabase.from("comments").select(`
  *
  WHERE post_id IN (
    SELECT post_id FROM posts WHERE user_id = ${userId}
  )
`)
Is that what I am looking for to create a function ?
s
Yes, but make sure you go through the guide I posted above on Postgres create function
d
Sure I am reading that atm. Can you please confirm if the current query using JS SDK above is not ideal to do?
They are alpha too
How can I run queries in SQL syntax directly ?
s
Personally I wouldn't do it, I'm actually not even sure how that query is even working, the SDK shouldn't allow raw SQL to work as this can lead to malicious actions by a user, what I mean is if the
userId
is accessible to the user, they could change it to something like
"1234"; SELECT * FROM posts;
and this is a harmless query but people could possible drop tables with that sort of access too.
Go to the SQL tab, its one of the links in the sidebar.
d
*from a server (not client)
Yes but how do I run them in a Node JS app
s
You cannot do that using the SDK, its a security issue to allow such a feature.
d
not even from my server? That's sad 😦
s
You can connect to the database directly using a postgres library and then run your queries like that.
d
That sounds promising, let me look for one
s
It doesn't matter, its a security risk through the SDK.
d
hmm maybe this will do
s
Yeah that will work, I use this one https://github.com/porsager/postgres#readme
d
Thanks
Docs have some similar query too
s
Thats a completely different query, no where clauses inside of the select
d
oh ok
s
Also note the cities there is representing a relationship