Live data from Hacker News

Don’t we all just want to use SQL on the front end?

vjpr.medium.com

21–30 of 184 posts

Re: Don’t we all just want to use SQL on the front end?

#21
No. I want and need to do manky shit on the backend that I desperately need to hide from users or the front end. If I can't hide that then basically all my users get to see my underwear and that's no good for many reasons (e.g. exploits, abusing the api, etc).

and all the security can of worms. Seriously, if you don't respect the benefit of the text layer in JSON then you need to go have a look at why .NET Remoting failed. You want loose coupling here.

Perhaps I'm missing the point of the article?

Re: Don’t we all just want to use SQL on the front end?

#22
I've spent the last six months working with a codebase that does exactly this. Aside from the obvious problems with exposing your schema to potential attackers, opening up potential DOS vectors, and training front-end engineers on yet another technology, you end up with some code that is very, very difficult to test. If you're using it in your hobby project and you're aware of the pitfalls - fine, go ahead and do whatever you like. But if you're expecting to incorporate this as part of an engineering organization: Save everyone the hassle now and don't.

Re: Don’t we all just want to use SQL on the front end?

#24
post #15

a thing a I tried, and ran out of steam with, was writing SQL queries in my client side code but statically extracting them with babel, so you have some code like this: const users = await sql.query`select * from users`; console.log(users); and in production this becomes something like const users = await fetch('/query?id=19a1f14efc0f221d30afcb1e1344bebd'); console.log(users); and the query itself stays on the server…

https://blitzjs.com/ does something similar, where you can write server side code inside components and it turns it into an API

Re: Don’t we all just want to use SQL on the front end?

#25
Well, back in the "olden days" in the late 90's, this was actually pretty common: ColdFusion allowed SQL statements to be embedded in the templates, and JSP had sql tags. IIRC, most people moved away from that model primarily for flexibility and maintainability: having SQL statements mixed in with the presentation led to a lot of code duplication and made putting a different front-end (say, a third party API) nearly impossible.

Re: Don’t we all just want to use SQL on the front end?

#26
post #17

I‘m using AlaSQL for years in my projects ... is that the wrong approach or am I missing something here? http://alasql.org/

Looks cool - wonder why its not more popular.

I think the read/write-through cache to a backend database is the missing piece...at least as far as I can see.

Re: Don’t we all just want to use SQL on the front end?

#27
Because it exposes a lot of inner details and potential security/privacy risks if clients are able to change parameters.

But... it seems like if you're willing to expose your data model to the client, then it seems like some combination of code signing the SQL (with parameters left empty) along with the acceptable list of named parameters, e.g.:

Signed blob:

    {
      sql: "SELECT name, email, ... FROM users WHERE users.id = %(userid)s"
      allowedParameters: []
    }
    
Such that the client is allowed to send this query, but they're not allowed to provide any parameters, the backend has to provide the userid. That seems workable to me.

Likewise:

    {
      sql: "SELECT order.price, order.... FROM orders WHERE orders.user_id = %(userid)s AND orders.id = %(orderid)s"
      allowedParameters: ["orderid"]
    }
    
This would allow the frontend to provide an order ID, but not the user ID - so the user still can't look at other people's orders and if not logged in, it's an invalid query without a userid. And the client isn't allowed to change the "sql" or parameters sent or the signature is invalid.

Edit: Someone else replied that if you have sufficiently advanced build tooling, you can statically extract the SQL/GraphQL from the client side code and replace them with IDs, and then move the SQL to the backend. That's very cool, but the average startup isn't using bazel/buck/etc. Those tools aren't yet small app friendly enough (which is unfortunate, IMO.)

Re: Don’t we all just want to use SQL on the front end?

#28
Don’t know about web support these days but a lot of mobile and desktop apps use a pattern of having a local SQLite db which is “synced” to a remote database using a sync protocol.

That sync protocol could be anything (rest, rpc, graphql, or even xml changesets in zip files on webDAV - I’m looking at you OmniFocus)

This setup has the benefits of being able to work offline (just sync later when network is back) and the ability to perform local SQL queries to populate UI views. But comes with all the extra complexity related to synchronising local and remote database. Stuff like handling conflicts.

Re: Don’t we all just want to use SQL on the front end?

#29
post #22

I've spent the last six months working with a codebase that does exactly this. Aside from the obvious problems with exposing your schema to potential attackers, opening up potential DOS vectors, and training front-end engineers on yet another technology, you end up with some code that is very, very difficult to test. If you're using it in your hobby project and you're aware of the pitfalls - fine, go ahead and do wha…

These are the types APIs that I CONSTANTLY have to tell our internal guys "Hey, don't do that!"

I get WHY you'd want to be able to throw in any random filter/sort to get the exact datapoints you are after, but that's both really hard to scale and often ends up being highly coupled to the underlying datastore.

I get it, requirements gathering and making a clean API is hard and time consuming. What's harder is not doing that and needing to eventually figure out "How on earth can I get constant response times or stop someone from sending an app crashing query all while supporting current usages".

Re: Don’t we all just want to use SQL on the front end?

#30
How many people are actually using raw SQL on the backend? I thought people got tired of maintaining strings of raw SQL and migrated to query builders or ORMs.

> I usually end up with a bunch of Lodash (groupBy, filter, map, reduce) to shape the data I get from the server.

I mean, we are also doing the same thing with ORMs on the backend.

Post reply on HN