News for hackers. Stuff that matters.
Re:PostgREST: REST API for any Postgres database by kissgyorgy on Saturday November 21, 2020 @12:01PM (#25170837)
This is not a good idea at all. One of the main point of a REST API is to abstract away your internal data structures for the clients that makes sense. Also refactoring and database migrations should NOT change the public API of any software, especially not a REST API. Now, when using something like this, even the most basic change will have a ripple effect on your whole infrastructure and break every client.
Re:PostgREST: REST API for any Postgres database by piaste on Saturday November 21, 2020 @12:11PM (#25170924)
From the docs:

> It is recommended that you don’t expose tables on your API schema. Instead expose views and stored procedures which insulate the internal details from the outside world. This allows you to change the internals of your schema and maintain backwards compatibility. It also keeps your code easier to refactor, and provides a natural way to do API versioning.

Which is, you'll notice, the same best practice used for decades by DBAs who need to serve multiple applications connecting to the same database.

(Also, it's not exactly uncommon that a data store API would have a single client which you control - eg. a single webapp, an internal application... in which case you can go nuts with breaking changes.)

Re:PostgREST: REST API for any Postgres database by systemvoltage on Saturday November 21, 2020 @02:20PM (#25172026)
> Which is, you'll notice, the same best practice used for decades by DBAs who need to serve multiple applications connecting to the same database.

Clearly, the Database's job contrary to what the name suggests is to not only stick data into it but also a bunch of business logic that's essentially untestable. No one needs domain models when you've got database tightly coupled with behavior and logic of your business.

What could go wrong?

The whole point of the API is that it is a repository for accessing the data from one or more databases, and packaging it up for the user to consume. It imports domain models which are totally isolated from the rest of the dependencies. The data doesn't even have to be in the database and can come from multiple sources such as S3 or whatever. It can consume data from the user and kick off various processes or insert into the database. It is a generic interface that does more than just CRUD in the DB.

Re:PostgREST: REST API for any Postgres database by mojzu on Saturday November 21, 2020 @02:33PM (#25172163)
You can use pgTAP for postgres unit testing, with docker you can make the process pretty fast too. Id highly recommend looking into the features/extensions of postgres, there's a lot of great stuff that goes beyond just being a database which can save a lot of time and grief depending on the application.

There is a risk of mixing storage/business logic, and you might end up being tied to postgres, but it's manageable using functions/views/schemas. And its also a really convenient place to do some business logic, as all your data is right there and available via a very expressive/extensible query language.

Re:PostgREST: REST API for any Postgres database by piaste on Saturday November 21, 2020 @05:05PM (#25173084)
If your API needs to serve data that lives in multiple sources, then PostgREST is definitely not the right tool.

It is, however, a pretty common design to have all your data flowing into a database, and being served from there. Indeed, it's probably the simplest and most common design for a basic web application. PostgREST or Hasura can do excellent work there.

Re:PostgREST: REST API for any Postgres database by RedShift1 on Saturday November 21, 2020 @12:19PM (#25170987)
I went all in on Postgraphile, directly tying API's to the database schema. And you know what? I LOVE IT. I love it so much I've migrated all other API's in the business over and got to delete thousands of lines of code that weren't necessary anymore. 99% of the time, REST API's are just glue code, mapping HTTP data to database data and vice versa. And for that 1% that you need, for example backwards compatbility with an earlier version of your API, there's computed columns, set returning functions, views, etc... You can keep your API compatible with older clients and keep moving your schema forward. But the most liberating thing about Postgraphile is that I get to spend a lot more time on the datastructures and frontend development. And it's really paying off, features can be delivered much faster, and you don't feel useless writing glue code all the time. This thing for real improved my life.
Re:PostgREST: REST API for any Postgres database by darkteflon on Saturday November 21, 2020 @07:34PM (#25173817)
This was interesting, thanks. Have you also given Hasura a go? If not, would you? In your view are Hasura and Postgraphile reasonable substitutes? I’m not talking about non-hosted vs self-hosted, but rather capabilities, tooling, developer experience, etc. Would be great to hear your thoughts, thanks.
Re:PostgREST: REST API for any Postgres database by RedShift1 on Saturday November 21, 2020 @10:42PM (#25174769)
I've tried Hasura and it worked fine, but went with Postgraphile at the time because it was much easier to integrate into an already existing REST nodejs API. Plus I like the fact that Postgraphile leverages the database as much as possible, whilst Hasura implements things that could be done by the database. It's also much easier to extend Postgraphile, you just use Postgraphile as a library in your nodejs project and add as needed, allowing you to run entirely from one codebase. With Hasura you have to use schema stitching to extend its functionality, meaning you have to keep 2 pieces running instead of 1. When it comes to developer experience, Postgraphile is rather bare in its default form, you definitely want to add a bunch of plugins to make the resulting GraphQL side as powerful as you need it to be. Hasura is much more of a blackbox, you set it up and what you see is what you get. Postgraphile is much easier to mold into exactly what you want.

In the end they are projects with semi-same goals but different approaches. I went with Postgraphile and I haven't regretted it one bit, but no reason you can't try both, they are easy to set up and get a lay of the land.

Re:PostgREST: REST API for any Postgres database by dragonwriter on Saturday November 21, 2020 @01:46PM (#25171754)
> This is not a good idea at all.

It's an excellent idea.

> One of the main point of a REST API is to abstract away your internal data structures for the clients that makes sense.

One of the points of database views, which are much older than REST, is to abstract away your internal data structures behind facades that make sense for the clients.

> Also refactoring and database migrations should NOT change the public API of any software, especially not a REST API.

That's...inaccurate. Refactoring, including of the base storage layer shouldn't. The exposed schema of a database is an API, which is logically distinct from the base storage layer. Some designs may just expose base tables and handle mapping for the client outside of the database, but there's no fundamental reason it has to be that way.

> Now, when using something like this, even the most basic change will have a ripple effect on your whole infrastructure and break every client.

No, it won't. PostgREST exposes a single schema. There's no reason that schema should contain low-level implementation, just the views defining the public API.

Re:PostgREST: REST API for any Postgres database by systemvoltage on Saturday November 21, 2020 @02:16PM (#25171987)
> One of the points of database views, which are much older than REST, is to abstract away your internal data structures behind facades that make sense for the clients.

It makes sense to put all the logic into DB views and ditch the domain models. Forget unit tests and functional tests against company's business logic - those are pretty obsolete concepts. We don't need robust software, we need quick and dirty ducktaped APIs. Who needs data validation?

Re:PostgREST: REST API for any Postgres database by dragonwriter on Saturday November 21, 2020 @02:29PM (#25172132)
> It makes sense to put all the logic into DB views and ditch the domain models

DB views are a mechanism for representing domain models just as much as classes in an OOP language are.

> Forget unit tests and functional tests against company's business logic

Implementing functionality in the DB changes how you implement and execute tests, but it doesn't prevent testing.

If you are using a DB at all and don't understand how to test functionality implemented there, that's a problem, sure, but the correction to that problem isn't to just minimize the functionality in the database and continuing to fail to test it.

Re:PostgREST: REST API for any Postgres database by systemvoltage on Saturday November 21, 2020 @04:04PM (#25172758)
I totally disagree with this approach. Sure, SQL can be used to express domain models.

Let me raise a few questions for you:

- Let's say we want to change the database from postgres to Oracle in the future. How do I go about doing it?

- How about complex logic that needs to be done in a declarative language such as SQL? SQL was not developed to write logic. You could do a lot of things in it. It is turing complete but doesn't mean you should.

- How do you debug SQL views?

- How about version control and updating views, with traceability?

- Do you think SQL is more readable for logic code than say Python? Surely, basic logic can be represented in SQL. But IMO it suffers readability.

- How about CPU/memory consumption and how do you manage to vertically scale?

You're trying to use a tool (views), not for its intented purpose. It was not meant for sticking your company's entire domain model.

This is a completely wrong approach especially in enterprise environment. Might be ok with a small project.

Re:PostgREST: REST API for any Postgres database by dragonwriter on Saturday November 21, 2020 @06:26PM (#25173516)
> - Let's say we want to change the database from postgres to Oracle in the future. How do I go about doing it?

If by “database” you mean “storage backend” (either in whole or in part), then the answer is oracle_fdw.

If you mean the API implementation, then, just as if you wanted to use a different language/platform when it wasn't implemented via Postgres originally, it's a complete reimplementation of that layer.

But, really, changing DBs isn't a root need, it's a solution, and unless we know the actual problem, we can't determine a solution (and “switch DBs to Oracle” likely isn't the best solution.

> How about complex logic that needs to be done in a declarative language such as SQL?

Complex logic is often more clearly expressed in a declarative language.

OTOH, to the extent there is a need for procedural/imperative logic, Postgres supports a variety of procedural languages, including Python.

> How about version control and updating views, with traceability?

There are a number of variations on approaches for this with database schemas in devops pipelines. Whether your schema is just base tables or includes views/triggers/etc. doesn't really make any difference here.

> Do you think SQL is more readable for logic code than say Python?

I think most appropriate of SQL, pl/pgSQL, and Python for each component is more readable than just-Python

> You're trying to use a tool (views), not for its intented purpose. It was not meant for sticking your company's entire domain model.

That’s...exactly what views were designed for. For a long time the implementations in most RDBMSs weren't fairly limited, but that's not really the case now.

> This is a completely wrong approach especially in enterprise environment. Might be ok with a small project.

Honestly, LOB apps in an enterprise environment is probably where this approach is most valuable. It might not be right for your core application in a startup where you are aiming for hockey stick growth. At least, from the complaints I've heard about horizontally scaling Postgres, I'd assume that.

Re:PostgREST: REST API for any Postgres database by neilpanchal on Saturday November 21, 2020 @07:08PM (#25173699)
Not saying either way, but something to chew on when it comes to Domain models: https://www.cosmicpython.com/book/chapter_02_repository.html

I love this book and it explicitly addresses a lot of painpoints in designing complex systems. For example, business counterparts might request writing to CSV instead of database. You want to decouple domain models from RDBMS.

Re:PostgREST: REST API for any Postgres database by RedShift1 on Saturday November 21, 2020 @10:53PM (#25174806)
How does decoupling the domain models from the RDBMS help in having an application accept CSV files as input?
Re:PostgREST: REST API for any Postgres database by robertlagrant on Saturday November 21, 2020 @02:53PM (#25172302)
You keep talking about domain models and views as though domain models aren't written as well as read.
Re:PostgREST: REST API for any Postgres database by dragonwriter on Saturday November 21, 2020 @06:36PM (#25173557)
> You keep talking about domain models and views as though domain models aren't written as well as read.

Views can be writable (views with non-trivial relations to base tables require explicit specification of what behavior to execute on writes through triggers, but that's obviously true of classical domain models, too.)

Re:PostgREST: REST API for any Postgres database by ruslan_talpa on Saturday November 21, 2020 @03:11PM (#25172425)
You keep talking about db views as though you can only read from them (an not write to them) :)
Re:PostgREST: REST API for any Postgres database by micimize on Saturday November 21, 2020 @12:25PM (#25171045)
I'm not sure about postgrest, but with postgraphile you can control field exposure and add getters/setters fairly granularly.

Regardless, that's no reason to say it's "not a good idea at all." having a schema change ripple through the stack is just a trade-off, which is often even desirable. If engineers are shipping code to production with postgrest, and attributing it in part to their success, maybe reconsider the rigidity of your architectural thinking. https://paul.copplest.one/blog/nimbus-tech-2019-04.html#api-...

Re:PostgREST: REST API for any Postgres database by neuromanser on Saturday November 21, 2020 @01:34PM (#25171656)
This is very shallow, unimaginative thinking. Of course you'll abstract away your internal data structures: just don't expose the base tables, and give access through a client-specific schema comprised of tailored views instead.
Re:PostgREST: REST API for any Postgres database by kowlo on Saturday November 21, 2020 @12:09PM (#25170905)
After reading your comment I expected to see low to mediocre activity on their repository... however, 15.1k stars seems to indicate that many do think it's a good idea.

I know GitHub stars are not a measure of whether something is a good idea or not - but why do you think so many have positively engaged with it?

Re:PostgREST: REST API for any Postgres database by joking on Saturday November 21, 2020 @12:32PM (#25171108)
because actually is a great idea. maybe there are some cases where it’s not the best option, but many times it’s much better option than having to code a custom api code and you can invest that time in building more features instead of dealing with plumbing.
Re:PostgREST: REST API for any Postgres database by kissgyorgy on Saturday November 21, 2020 @12:15PM (#25170950)
Lots of developers in general doesn't seem to understand the concept of a REST API. What are the benefits? Why are we doing this at all? How to do it properly? Most APIs are not REST, just some RPC-like APIs over HTTP.

When you have a tool like this, it's just easier to use it and call it a day - without thinking too much or learning the concepts properly.

Re:PostgREST: REST API for any Postgres database by philwelch on Saturday November 21, 2020 @04:05PM (#25172760)
REST isn’t an architecture for designing HTTP API’s; it’s an architecture for designing HTTP itself. https://twobithistory.org/2020/06/28/rest.html
Re:PostgREST: REST API for any Postgres database by avinassh on Saturday November 21, 2020 @12:20PM (#25171001)
> Most APIs are not REST, just some RPC-like APIs over HTTP.

Can you elaborate this part? Does PostgREST does that? If not, any example of it

Re:PostgREST: REST API for any Postgres database by corytheboyd on Saturday November 21, 2020 @12:38PM (#25171149)
I think the point is that many people in the wild conflate the term “REST API” with “arbitrary interaction over HTTP”. We’ve all seen examples, the one-off endpoint to fetch that little tertiary bit of data, that one-off POST to invalidate a cache. None of that is REST
Re:PostgREST: REST API for any Postgres database by abraae on Saturday November 21, 2020 @01:12PM (#25171460)
To REST purists, APIs must use HATEOAS to be considered REST.
Re:PostgREST: REST API for any Postgres database by masklinn on Saturday November 21, 2020 @01:21PM (#25171558)
"HATEOAS" is pretty much the only thing REST contains. "REST API" without HATEOAS has no meaning, it's just a stand-in for "good HTTP citizen" (if even that).
Re:PostgREST: REST API for any Postgres database by bausano_michael on Saturday November 21, 2020 @12:09PM (#25170901)
I can see how an API like this would be a great fit for some no code solution integration. Certainly bad idea in some situations, but let's not be too quick to judge the usefulness of the project.
Re:PostgREST: REST API for any Postgres database by kissgyorgy on Saturday November 21, 2020 @12:17PM (#25170969)
I'm not saying it cannot be useful at all, but most projects should not even consider using it.
Re:PostgREST: REST API for any Postgres database by fulafel on Sunday November 22, 2020 @03:06AM (#25175833)
Depends on your use case. REST is about putting resources on the web. Sometimes it can be eg datasets backes by SQL tables.

Views / stored procedures are recommended to provide APIs.