3 ms·
A very interesting idea to be sure, but IME the biggest downside (which tbf is mentioned in the article) is the contract. If you have clients with knowledge of
by freedomben 3y ago
A very interesting idea to be sure, but IME the biggest downside (which tbf is mentioned in the article) is the contract. If you have clients with knowledge of and dependency on the schema, you can't change it in a breaking way unless you update all the client's code.
I've tried various patterns in the past like one that just exposes database columns as an API, and this pain point always comes calling and it hurts. Keeping your data model simple and lean as possible is an important part of limiting complexity, which directly correlates with maintainability.
The only pattern/approach that I consistently return to is the Rails pattern (I use Elixir/Phoenix now but same pattern). It is certainly not the most sexy, and having a client be able to graphql exactly what they need can be really helpful, but at least for me it has rarely turned out to be worth the tradeoffs.
- danielvaughn 3y agoThis is basically how Firebase is designed and it drives me nuts. I've freelanced with several teams, primarily comprised of front-end devs, who decided to use Firebase for their product. When they first told me they query the database directly from both their website and their mobile app, I immediately was like "so...what happens if you need to change the structure of the database"? Crickets.
- endisneigh 3y agoCrickets, really? It's very obvious how to do as recommended by Google - you use a Cloud Function (aka Cloud Functions for Firebase) to do a "migration", while simultaneously handling both cases in client code until this is done. You must have been working with a bunch of amateurs if such an obvious solution (even without reading the documentation) didn't come to mind apparently by you or your clients (since you're the freelancer). This particular problem isn't even Firebase specific - you have to do this for any store that doesn't strictly have a schema. This issue you're describing isn't even in the top 5 biggest issues about using Firebase.
- danielvaughn 3y agoAs I stated in my comment, I was working with front end developers, who had little-to-no experience working with backends and hadn’t considered migrations. That documentation really didn’t help because it’s such a laborious and error-prone process, especially when dealing with mobile applications that live on users devices. My implied point was that an API layer resolves the need for any of that bullshit to begin with. And yeah, there are tons of other issues with Firebase, but this was the first warning siren that went off in my mind. I hate Firebase.
- 10000truths 3y agoThe solution to this is the same as with APIs: versioning. Instead of naming your table "my_foo", you name it "my_foo_v1". Then, when you want to make a breaking change to the schema, you: 1. Create a new table "my_foo_v2" with your desired schema 2. Modify write queries for "my_foo_v1" so that they also write to "my_foo_v2" 3. Copy over existing data in "my_foo_v1" to "my_foo_v2" with a migration script 4. Modify read queries for "my_foo_v1" so that they read from "my_foo_v2" instead 5. Remove all write queries to "my_foo_v1" 6. Drop the "my_foo_v1" table
- deleted 3y ago[deleted]
- singron 3y agoWith postgres, simple views are updatable, so you can often do this pattern without copying tables or dual writes. It's particularly useful for renaming columns, but you can also do some other things. You also don't have to use version numbers on all your tables since you only need the view temporarily. ALTER TABLE my_foo RENAME TO my_foo_tmp; ALTER TABLE my_foo_tmp <your desired change here>; CREATE VIEW my_foo AS SELECT <how you make your new table seem like your old table>; COMMIT; <update your apps to use my_foo_tmp> DROP VIEW my_foo; ALTER TABLE my_foo_tmp RENAME TO my_foo; CREATE VIEW my_foo_tmp as select * from my_foo; COMMIT; <update your apps to use my_foo> DROP VIEW my_foo_tmp; COMMIT;
- munk-a 3y agoWe actually used a similar tactic when transitioning our data to supporting soft deletion. We have a limited number of queries that update data or present data for administrative review and processing - but a lot of wild ones around reporting. We'd rename table `foo` to `foowithdeleted` and create a new `foo` view that excludes any soft-deleted rows. Our reporting queries keep on trucking like normal and read out of the `foo` table (now a view - but they don't care) and we only need to adjust the administrative view to show soft-deleted rows for undeletion operations. Shell-gaming tables with views can be incredibly useful in postgres.
- 3y ago
- SOLAR_FIELDS 3y agoI think about this with Postgrest/Supabase which has a similar problem (autogenerated code based on the db schema is inherently going to conflict with api stability guarantees). I think that this approach is just fundamentally at odds with making a stable versioned controlled API. However: I think the best thing to do if you want a setup like this and want to have your cake and eat it too is something like this: Use your autogenned client for your “internal” api. This is for your clients with the autogenned schema that you directly control only so that you can ship changes and not have to worry about versioning/backwards compatibility. Then for external users that have their own clients, you have that slimmed down more traditional API that offers less functionality but it’s properly versioned, tested etc I think this kind of hybrid setup can work well for SaaS setups where you have a cloud product that does internal stuff plus things external that end users need to operate on. You get the benefit of being able to iterate quickly without breaking your clients and since your external API is smaller it’s less maintenance overhead to keep it updated and versioned
- steve-chavez 3y agoPostgREST docs have always recommended to use only views + functions in a dedicated schema for the API: https://postgrest.org/en/v10.2/schema_structure.html https://postgrest.org/en/v10.2/schema_structure.html
- SOLAR_FIELDS 3y agoHuh, interesting, since at least with Supabase the default setup kind of pushes you towards or at least doesn’t discourage you from using tables instead
- floodle 3y agoIf you are in the situation where you are shipping a web app where one dev team controls the backend and the one and only client and ships both together, is this a big issue?
- rmbyrro 3y agoThey mention one partial "solution" to this: have your clients query a view, instead of tables directly. When you need to change the schema, also update the view accordingly. Of course, there are still limitations. For one: you can't "massage" data with a view as you could in the backend with a full featured programming language. PS: I don't think this is a good solution, just mentioning it.
- deleted 3y ago[deleted]