DEV Community

Discussion on: Routing queries to read-only PostgreSQL replica in Node.js

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

Interesting. This, however, seems to need some sort of SQL interpreter, and even if one is provided, it would have to be pretty darn smart. What if my SQL statement is a multi-statement SQL? What if in my multi-statement SQL I start with a SELECT but the next statement is an UPDATE? If the SQL interpreter/parser cannot cover things like this, I think I would just stick to layered programming and make the decision at the repository level based on the function call. Just my thoughts.

Collapse
 
gajus profile image
Gajus Kuizinas

Slonik only allows you to execute one query at a time, so you cannot combine SELECT and UPDATE into one. In the context of the chosen stack, all you need to make a sane decision are the 3 criteria outlined in the post.

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

Yes, then it's not for me, unfortunately. All my REST API does UPDATE/INSERT then SELECT to return the most current version of the record to pick up database-inserted data like timestamps. I would need a different mechanism. Still, it is interesting for sure. Thanks for sharing.

Thread Thread
 
jhelberg profile image
Joost Helberg

You can use the 'returning' keyword to make update and insert return the filled in columns. You don't need a select for that.