Live data from Hacker News

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

vjpr.medium.com

61–70 of 184 posts

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

#61
Everybody is focused on literally replicating the data model and using SQL which obviously won't work.

But the idea is directionally correct. Replicate the subset of data the user has access to and wants locally and work on it disconnected, then sync changes asynchronously to server. This is difficult, but necessary for fully responsive and collaborative applications.

Check out https://replicache.dev for a productization of this idea (disclosure: this is my product). We will eventually add a SQL frontend for the cached data.

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

#62
If we used SQL we also would inherit it's well known shortcomings:

- no proper way to extract and reuse expressions - parameterizations are basically string concatenations - a lot that I forgot

There are of course also good things about it:

- it's declarative - it's easy to reason about (in its base form, no recursion, etc.) - it's widely used and known

So while GraphQL is something similar, and having solved some of SQL's shortcomings, it's new and has less mindshare. Also, last time I checked, the implementations in various languages (Python even) lagged behind the specification.

I'd say it's a step in the right direction, but tooling has to be improved I guess.

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

#63
I had the same question a year ago and create a side project on that topic. The solution was is very close to the firebase approach, writing security rules. Those rules whitelist sql statements and give some some flexibility by defining query parameters and having a context object that contains userIds and so on. For example:

frontend sql statement as a json object:

  {
    select: { 
      name: field(Project, 'name') 
    },
    from: table(Project),
    where: equal(field(Project, 'ownerUsername'), ''),
  }
and here a rule configured on the backend:

  const rules = [
    allow(authorized(), {
      select: {
        name: field(Project, 'name'),
      },
      from: table(Project),
      where: equal(field(Project, 'ownerUsername'), requestContext().userId),
    })
  ],
Sadly documentation is quite poor for now, but you can check it out here https://github.com/no0dles/daita

Edit: code formatting

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

#64
For offline apps, WebSQL would have been awesome. Thanks to some ivory tower types at Mozilla we can't have it [1]. We got SQLite in a computer on Mars but apparently that's not enough to base a web standard around because some people think that It WoUlD Be BaD iF wE oNlY hAve oNe implEmentation! Oh noooo!

(This despite the fact that every Linux distro uses the same kernel and Linux hasn't suffered some sort of monoculture meltdown.)

[1] https://nolanlawson.com/2014/04/26/web-sql-database-in-memor...

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

#65

For those not familiar, check out Postgraphile.

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 them exposed as you'd expect in GQL.

With a commonly-used plugin, you can even do rich ORM-like queries directly from graphql, like:

    query {
      allPeople(filter: {
        firstName: { startsWith:"John" }
      }) {
        nodes {
          firstName
          lastName
          posts(filter: {
            createdAt: { greaterThan: "2016-01-01" }
          }) {
            nodes {
              title
              body
            }
          }
        }
      }
    }
More examples here: https://github.com/graphile-contrib/postgraphile-plugin-conn...

(Personally, I think their docs are good at telling you how to use it but fairly bad at showing how great the tool is)

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

#68

There is PostgREST, which is just a thin wrapper REST api around a Postgres database: https://postgrest.org/en/stable/

And its graphql counterpart, postgraphile (described elsewhere on this thread: https://news.ycombinator.com/item?id=26823819)

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

#69
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 doin…

Yeah; reading this the only 'gain' I saw was "we don't have to define an API". But instead you get to try and ensure that raw SQL from the client is always safe.

The former isn't THAT hard to do. The latter is extraordinarily hard.

Get the former wrong and you fix it before it ever goes live. Get the latter wrong and you fix it only after you're massively pwned.

This is just a bad tradeoff.

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

#70
I actually wrote a blog post about this exact scenario. It takes ideas from the datasette project (https://simonwillison.net/2017/Nov/13/datasette/#Arbitrary_S...). My example opens a read-only sqlite db and exposes the entire SQL API through a single PHP file.

The post is here: https://ohdoylerules.com/web/sql-as-an-api/ The code is here: https://gist.github.com/james2doyle/9e4b2b4f17e33bfb236fbdaf...

Post reply on HN