Live data from Hacker News

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

vjpr.medium.com

81–90 of 184 posts

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

#81

Yes. My point of pain has been ElasticSearch. The Json happy queries with far too many squiggly braces and square braces are just difficult to read. A simple statement like SELECT Age FROM Employee WHERE name = 'Frank' turns into a monstrosity like POST Employee/_search { "query": { "bool": { "must": [ { "match": { "name": "Frank" } } ] } }, "fields": [ "Age" ] } Thankfully they added rudimentary SQL support so I cou…

I wish someone took the SQLite query parser and used it to output Elasticsearch's horrid language so that we could write queries in a language that our devs don't universally loathe.

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

#82
post #57

Earlier quoted context omitted.

This is a really interesting interface design, but seems like for any non-trivial database it would be too easy to make mistakes and give the client too much access. Also seems like it's be really easy to end up with performance issues. Also, there's basically zero information hiding. The database is your interface. Changing the schema in any way whatsoever is a breaking change to the API! You haven't really saved yo…

> You've just turned your database schema into the API It might actually be quite easy to version an SQL api, because you have the DDL migrations files and could actually make things backwards compatible to some degree. You would have a translation layer to modify the queries. Of course, your migrations would need to be more detailed tracking how things map to each other. But probably it would get too complex in the…

This sounds like a good way to spend 2 years happy and then in the 3rd year spend a monumental amount of money hiring a world-class compilers/databases expert to 10x the perf of DB migrations :)

But, yeah, this happens with REST APIs too.

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

#83
Setting aside the security issues (legion!) and the implementation issues (manageable but hard!) you have to think about the maintainability of the codebase. Retrofitting schema changes into a codebase full of SQL statements fucking sucks. Every time I've dealt with this I've found that having an API on top of your DB is far better for maintainability.

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

#84
post #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.

I'm sure some did, but I got sick of the slowness and lack of expressive ability with every ORM I tried and just went back to SQL strings.

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

#85
post #47

If you have a brand new SaaS app where there is very little data (kilobytes to megabytes) and each user/team is separated from the others, you could have: - a sqlite database for each user on the backend - then on page load, have them download their whole sqlite db on the frontend - sync the two with something like litestream.io compiled for webassembly

i think the use-cases for that are pretty few and far between, at least at the 'team' level. If you have a team, you need some way to manage members, which means adding/removing members. with this model, you effectively would not be able to remove members because you wouldnt know which members had downloaded an offline copy of the team's database. for individual users, this might have a lot more potential.

This is the same as a removed member still having their browser cache containing team data.

The advantage of this design is you get an app that is extremely responsive and offline capable. Then you just need a reliable way to sync changes. And you always have the option of just re-downloading the entire team db again if it gets corrupted somehow. You could even have logic to indicate the sync status of each table so that you don't need to download the whole table...you could just populate it as your need the data...like a cache.

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

#86
This is, effectively, what Meteor (JS) does; just with MongoDB instead of SQL. It embeds a mini-MongoDB JS client on the frontend to store cached data, queries are ran against that, and missing data is requested, streamed, and rendered asynchronously.

I'll scream from the rooftops: Yes. Meteor has its shortcomings, MongoDB being a big one, but this pattern of unification is so fantastic for every party involved that even reading about the "new-fangled acronym for building apps" disappoints me.

Now, we've got the JAM Stack (that's the new one, right?): you got your separate web frontend and service-oriented backend, probably communicating over GraphQL, then you've got your database language, probably SQL, but wait, lets put Prisma in front of that so that speaks GraphQL, but it'll be a different GraphQL schema than your frontend because you don't want to expose data, and jeeze maybe serverless functions as well, that sounds good, and i'll just stop typing here because the level of complexity and intricacy we've reached just to build a fully-featured web application is far beyond useful.

The new-generation tools we've built to make development easy for teams of 200 engineers now demand that every team have 200 engineers. Its a self-fulfilling prophecy; congratulations, you just universally raised the cost of software for every human on the planet.

A few days ago, I installed Nextcloud on a little $10/month Digital Ocean instance. I'm blown away by the performance this PHP app puts down. Blown. Away. Using Google Drive, a pretty damn "snappy" app relatively speaking, feels like walking through mud in comparison, and lets not even go down the rabbit hole of the billion dollar data centers and 56 core Xeon Platinum processors behind Google Drive.

Modern web stacks fail along every metric, except "how easy is this for our massive team of developers to maintain." (Of course, they don't actually "fail"; we developers just move the goalposts so 'master' passes, future changes might fail it but we can fix that). They're insane to get started on, hard to maintain, hard to monitor, and at the end of the day have far worse performance for end-users. We started with the monolithic systems which share a ton between server and client, decided those were a mistake, then overindexed in the entire opposite direction instead of iteratively addressing what was wrong with them.

I predict a move back in the opposite direction. The situation has become insane, and I want PHP, Meteor, and Rails. Fortunately, they're all still there, but Meteor has mostly fallen into maintenance mode (not to mention, MongoDB), and JavaScript really doesn't have another solution like this.

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

#87

As many SQL Injection issues (and various other injection forms, including Javascript) as we have with backend code, fuck no we don't want SQL being issued from the frontend. For one, permissions around SQL are already crap. It takes the smallest screwup to expose data in co-mingled databases. No need to make it even worse. For two, at least if the SQL is on the backend, a fix for an exponential query DDOSing your DB…

It seems that the typical software engineer doesn't give security a single thought.

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

#88
Sure, for the first 10% of the lifetime of a system. When you are starting, the schema is simple, there aren't a lot of special cases or bad abstractions, and things are easy.

10 years later, there are special cases on top of the special cases. The reality of compromising over and over when adding new features has added up and it's hard to remember the right way to get the correct currency converted total for an invoice (so you need to get the total in each currency, but not add the line items that are marked as 'deleted', then apply discounts, then convert currencies to USD, then add tax, then add shipping, then convert to the local currency, except when the delivery address is in Russia where legally you have to ...). Lots and lots of things are like this, and with no abstraction to ensure that these computations are isolated in a single component in the system you are going to get nowhere.

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

#89
post #80
post #65

Earlier quoted context omitted.

Came here to say this. To expound a bit, you can use it as a highly pluggable/extensible express plugin which you can point at any postgres db and get all CRUD and other operations exposed in GraphQL, without writing any code. You can easily hide or rename fields that you don't want exposed, override their behavior in Node, and add generated columns or fancy SQL mutations either in JS or directly in your DB and have…

But what about optimistic UI updates or offline support in the client. The frontend has no notion of the relationships between the entities, which means you will be making a lot more queries than you need to, or just ignoring optimistic updates (we may be fine for some apps).

As you note in your article, Apollo's gql client has optimistic UI, but it looks like a pain.

The Apollo cache seems to be powerful enough normalize the way you want, perhaps with some extra code: https://www.apollographql.com/docs/react/caching/cache-confi...

Apollo also claims support for cache persistence, eg to localstorage: https://www.apollographql.com/docs/react/caching/advanced-to...

I haven't used Apollo myself so maybe it's not as usable or powerful as it claims to be.

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

#90
I think we've all wondered this at some point in our careers.

It's something you should never do. Off the top of my head:

1) Security (as has been beaten to death here)

2) Interface versioning. If you expose a generalized SQL interface to your consumers directly, good luck ever making a change to your database schema. You'll have no idea whose workflows you break. This high coupling becomes very painful very fast.

3) Abstraction. The way data is stored is often not the way data should be surfaced. What about application layer data integrity, things like that? This would require the frontend client to have far too specialized of knowledge as to how the backend works.

4) Optimization. How do you optimize for queries you don't control? Someone will craft something that can bring your database to its knees under load without even trying to.

Post reply on HN