Building single-page-apps with PostgREST
blog.polyglot.network
blog.polyglot.network
The share of discourse on HN about PostgreSQL, SQL, and Databases is growing noticeably. [0] I, for one, hope we are seeing the pendulum swing back from code-centric architectures and NoSQL to a new golden age of data-centric architectures.
Next you need push and pub/sub semantics in PostgreSQL so you can build something like this [1]
[0] https://toddwschneider.com/dashboards/hacker-news-trends/?q=... [1] https://tonsky.me/blog/the-web-after-tomorrow/
On top of this library you can do all sorts of things: replicate data to Kafka or SQS, write a web socket server to publish changes to clients, etc.
It's fairly basic (then again, does it need to be more?), but NOTIFY exists: https://www.postgresql.org/docs/current/sql-notify.html
All you need to do is map JWT to a Phantom DB User in PostgreSQL, then you can query SQL directly from the browser.
[0]: https://www.postgresql.org/docs/10/logical-replication.html
Supabase has zero differentiation. They have a nice "PGAdmin" if you may, but no one will pay for entire database hosting because of an admin interface. When it comes to hosting DB AWS, Azure etc have massive advantage.
Supabase will simply popularise the stack, and then when time to actually host the database comes people will move from Supabase to AWS etc.
Also the best language for UI is not JS, but something like FTD if we continue to make the progress we are making. Hopefully FTD + SQL Is all you need to know soon. Checkout this design that we are working towards for making this a reality: https://fpm.dev/journal/#explanation-of-dynamic-documents-an...
I'm glad you like the stack
> kind of sucks the stack success requires Supabase to die
this is a zero-sum view of the world. I'm sure that as the stack becomes popular, Supabase will too (and vice-versa).
> when time to actually host the database comes people will move from Supabase to AWS etc.
Databases are inherently sticky - it's the reason we started with the "Jamstack" crowd rather than enterprise use-cases. If you start on Supabase, and we handle all your scaling, there should be no reason to migrate to a different cloud.
> When it comes to hosting DB AWS, Azure etc have massive advantage.
given the (in)stability of cloud providers in 2021, I think we will see a trend toward multi-cloud in the the next few years. I'm not sure that they have such a large advantage (we use AWS so it's not like they can offer superior hardware).
FTD is my proposal for one such language. https://ftd.dev/philosophy/:
> If the vision is for every human to own their data, one must ask, data in what format? XML? JSON? YML? Markdown? FTD hopes to be that format.
Further what about the arguments of those components, can they themselves contain markdown? How about markdown with React or Svelte component? Would you really with straight face say this is powerful:
<Penguin text="Some text, which itself contains <Another />" />
Just try nesting it or writing multiline text, its complete torture.I'd suggest at least doing some research before trying to put down a potential "competitor"/alternative to your own tech. This whole exchange is a tad too unprofessional to me IMO.
<person>
<name>markdown is okay here</name>
<bio>markdown is okay here</bio>
</person>
If you do this it goes in the order you wrote things down, the <person> can not decide to show bio before name for example.If you write:
<person name="markdown difficult here" bio="markdown difficult here />
Now `person` has better control over things but it's not ergonomic to write. All the examples are "simplistic", when you have a complicated data modelling etc, this approach of leaving floating tags, eg `<name>` is not good modelling, its name of what? A person's name? A projects name? And can you get this data out of these files? You will have to write a full blown parser etc, any data in such file would be lost.Also if you want to create a new component you have to jump to the Javascript world. And you have to learn CSS. Along with Markdown and the front matter (yml). Each with non trivial amount of learning curves.
FTD is being designed as a unified whole. All components of FTD are written in FTD. There is no boundary between "author" code and "components". A reasonable programmer can pick up FTD in an hr, check out the first video on https://ftd.dev. A non developer can pick it in a week, check out the other three videos.
Svelte etc are cool, but haven't you heard of huge number of backend developers who can't get into frontend? Current state of frontend is extremely complex. You can not tell me billions of people can learn Svelte thing when even backend developers are facing difficulties. To use mdsvex effectively you will have to write components. Which means the entire web pack, bundling etc etc. Do not pretend these are easy, anybody, even non programmers can do it.
FTD is being designed to be easy to teach. The audience of FTD is people at large, not developers (and yes they can learn some programming, Excel is used by 100s of millions). I do not believe you are saying Svelte is the super best and no such project as FTD must exist. I believe there is scope of improvement and we are working on it, help us improve.
Things like unreadable elements with near 0 contrast between font and background and a dark mode with pieces of widgets with a white background does not fill me with confidence for a UI focused technology.
Thanks for the design feedback. The theme is new. The language itself is new. The static generator is new. Sidebar issue on mobile, and contrast issue we are aware of and are working on. Markdown styling is not a trivial issue to fix as we do not allow authors or theme builders to access CSS directly, [1] is the design we have come up with.
https://i.imgur.com/CfI5lPC.jpg
It looks fine on light mode:
- Scaling Databases is hard, - It's easier to scale a stateless application server rather than a stateful database.
- SQL is still hard to maintain - Adding features using Postgres Functions means you have an additional language to maintain. The code might be spread across different migration files. Maintaining these functions long term has it's own issues.
But I think the tooling is not really there, and developer experience directly translates to both productivity and team attrition so that's a really big problem. Also, lot of logic does not fit into SQL very well, say "give me a pdf" or something.
Most importantly, most developers are used to think procedurally and SQL is not natural to them. And to let my inner corporate manager speak, Java or Python or node backend developers are everywhere, while for something like this you need specialized people.
I do think dbt is the answer for most of analytics, but for CRUD stuff with business logic it's not going to help you.
Back in the 90's we used to call direct access from the client "fat client" and we stopped doing that. We moved to n-tier with the server in the middle. It takes more work but we still did that move. Here's why:
* Writing logic in server code is easier * SQL servers are more powerful and harder to secure * Caching and scale - direct DB access just couldn't handle even 90's traffic * Vendor lock-in - pretty much everything here is Postgress specific
I haven't kept up on DB progress as much as I should but honestly, most of what you have here can be done in a few lines of python and maybe a bit more in Java/Spring Boot/NodeJS etc.
I'm trying to think of a use case for this other than "we can do that cool thing". Can't come up with it.
It simplifies the 3-tier architecture (DB, server, client) into effectively 2-tier because the server environment is replaced by a standard binary (PostgREST) with zero hand-written code. This is 33% improvement in language/framework complexity if nothing else.
If needed, one can also write postgresql functions/triggers in Javascript/Python/etc for productivity - although this doesn't seem to have taken off yet.
It might just be personal bias, but I think these kinds of projects are common and important. They're near invisible, but even small-ish companies need somewhere to store their domain-specific data, and we should be thinking of ways to make that kind of software better.
The times are changing though. If there's anybody here from Supabase team, would love to chat about these topics.
Your article is very cool.
> write postgresql triggers and functions in standard languages (Python, Javascript) seems to be vastly underutilized.
This is definitely something we provide tooling for over time. We have plv8 installed on all databases so it should be simple enough
But, the first step in developing an MVP should probably be running all of the code on a single server. But again, that assumes you’re the one setting up the server.
What I am saying is that nobody ever, ever does this. It’s considered a strict antipattern for any app, even dopey internal MVPs. And I think it should be the default pattern until you need to scale.
I really think there's a lot of value in this model for users to self-host apps. PostgreSQL can run cron jobs, operate on external data with foreign data wrappers, and even do some number-crunching with Madlib right within SQL.
So, the user only has to manage a postgresql db, retains control on data and can easily get upgrades of webapps via CDN and mobile apps via app stores. Developers benefit because (a) there is no server-side programming language to deploy/maintain and (b) PostgREST API is very well thought-out and very predictable - leading to easier development across frontend/mobile frameworks.
[1]: https://gist.github.com/steve-chavez/c1435a8c9583d2524e87e4f...
(macOS is the only significant desktop platform that uses overlay scrollbars by default, where the difference is not generally visible.)
Decided on PostgREST as backend, with a thin VueJS frontend. We get a backend and API for free, and our developers know SQL very well.
I think it's a brilliant choice for our use case, although admittedly not something I would use for some other projects where I'd need more scaling on DB level, etc.
I'm pretty tempted by this stack for business applications that really are mainly storing stuff in a database.
In the meantime you can use something like WatermelonDb: https://nozbe.github.io/WatermelonDB/
Pet projects with simplest logic? ToDo app examples?
It won’t work in a large scale projects because this approach doesn’t scale well.
On a small scale it adds very uneven comlexity curve. Until some point everything is quite easy, but one day you need to write a stored function in plsql, and it’s not an easy task. Python/js won’t help in this case, because there will no be maturity of nodejs/django in decades.
Why should anyone prefer this thing to Rails?
- Scaling is not a worry.
- Simplicity is essential because developers are volunteering their time.
- Users also benefit because they only need to manage a database and not 6 different server language runtimes. Can even use managed services like Supabase or RDS.
Anyone stuck at that page trying to follow the link?
Edit: it finally worked for me… had to click the link half a dozen times before I stopped getting the refreshing cloudflare page
I still work on an old cold fusion application that works fine / makes money.
There’s something super fast in some cases where you write your sql, markup in one file and you are done.
[1] https://learnbchs.org/index.html [2] https://htmx.org/ https://unpoly.com/ https://hotwired.dev/
Whenever I return to the project, hell breaks loose: "npm install" (whoops, 51 vulnerabilities!), "npm audit --fix" (what now? 72 vulnerabilities? I thought this was supposed to go down not up), initialize a fresh project with the CLI so as to get the latest possible scaffolding and then migrate stuff over from the old project etc. etc. This really appears brittle to me.
In the article HTML is just treated as a `text` type inside the SQL function, you could do the same for rendering different pages - create functions that return HTML as text.
You can find tutorial here : https://www.youtube.com/watch?v=8hYtZ38wuz4
This approach has been tried several times over the past few years: I first used DreamFactory[1] several years ago for a small production app (less than a few thousand users) and now I see Supabase [2] trying the same. But in my experience these don't go past very limited internal enterprise use-cases, MVPs and toy frontend apps.
Also, the HTML page on CDN wouldn’t know what endpoint (domain, port) user has deployed their API on. One more thing for user to configure. Here, the HTML configures window.postgrest_url which the JS code can use.
As for configuration, I see it as a minor improvement over manually deploying one (or several) index.html with the API URL conf.
The CORS issue is the bigget sbenefit I think, but easy to accomplish in a CDN with custom headers, or with a custom domain.
So I think that it's smart, but it brings no benefit over existing approaches, and opens a whole new class of problem (like sizing your Postgrest instance for when the site is under attack, or visited by a bot).