

Hi everyone,
In a project involving Firebase and object types like Tickets, Schedules, and Timers, I want to structure my classes such that switching databases (potentially to MySQL) wouldn’t require a complete rewrite.
Approach 1:
Approach 2:
I like the second approach in theory, but what I’m worried about is whether the separation is too low level. What happens if the database I switch to changes schema such that taking in an object and a collection name isn’t good enough anymore? For example, will there be concerns if I switch between Vector, NoSQL, and SQL?
Any opinions are appreciated!
Hello. Appreciate your question. I think that this is a good use case for the Repository Pattern.
In your case, this might look something like this:
You can consult the Design Patterns / Gang of Four book for more details
Off topic, but personally I don’t feel you should worry too much about having to change the database in the future. I have rarely seen it happen in my career.
Look into “Hexagonal Architecture”, similar/synonymous to clean/ports&adapters/onion. As someone else said, combine it with “Repository” pattern.
I use hex arch in front end and back end. It adds a couple of layers, but it makes apps much easier to test and maintain.
I would generally say to stick to approach 1, as much as that stinks. Your observation about approach 2 especially compounds once you start to look at more complex operations that need to be optimized based on how the underlying DB works. Care still needs to be taken in approach 1 to avoid non-optimal operations, but at least you have the freedom to rework individual methods on your DatabaseProxy instead of all FirebaseProxy methods using your underlying CRUD operations.
One of the fastest ways I get people thinking about this is asking
How would you implement a
listTimers
orlistTickets
API with cursors?
Generally that’s complex enough to bring out pain points.