0

I would like to map my object hierarchy in swift to firebase and I am trying to figure out the best pattern for doing this.

I have one parent class Parent with a property propParent and two children: Child1 with a property propChild1 and Child2 with a property propChild2.

What is the most efficient and maintainable way to map this to a firebase schema.

class Parent {
    var propParent: String
}
class Child1: Parent {
    var propChild1: String
}
class Child2: Parent {
    var propChild2: String
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The terminology in the question is a bit ambiguous and doesn't apply from Swift to Firebase. In the example, There's a class of Parent and a *SubClass* Child1 and Child2. The parent class does *not* have two children. This is a Class to SubClass relationship where the subclass inherits parents attributes (which is part of the power of subclassing). In Firebase, that's not the case; there are no classes and subclasses, only Parent and Child nodes and the only relationship is the path.. /parent_node/child1 and /parent_node/child2; there is no inheritance of attributes (because there are none). – Jay Oct 22 '17 at 12:09

1 Answers1

0

In NoSQL databases there is almost never a single most model that is most efficient for all use-cases. That is because in NoSQL you will often have to modify and expand you data model for each use-case you want to implement in your app. For a good introduction to the topic, I recommend reading NoSQL data modeling and watching Firebase for SQL developers.

That said, at its most basic you have a bidirectional many-to-many relationship, which could be modeled as:

users
  parent: {...}
  child1: {...}
  child2: {...}
parents: /* contains the parent for each user */
  child1: "parent",
  child2: "parent"
children: /* contains the children for each user */
  parent:
    child1: true,
    child2: true

The fact that a user can only have a single parent is modeled here by having a single value for each node under parents. The possibly multiple children per user is modeled as a collection for each node under children. That collection is modeled as a so-called index: a collection of String: true pairs, where the value (true) is a meaningless marker.

This uses concept from a few other answers, so I'll link you there for more information:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for the answer. I am not sure what you mean by "users" here. Also, how do you store inherited properties? In children or parent? That is the main issue here. This is confusing especially if you have an id property in the parent which is inherited by the children. – Muath Alkhalaf Oct 21 '17 at 16:57
  • Sorry for misunderstanding your problem. Can you maybe edit your question to include the code that reads/writes data, the JSON that you're considering, and the problem you see? – Frank van Puffelen Oct 21 '17 at 17:02
  • To clarify more, I am looking for a pattern for modeling inheritance in firebase that serves the same goal of this pattern for relational DB https://www.martinfowler.com/eaaCatalog/singleTableInheritance.html – Muath Alkhalaf Oct 21 '17 at 17:02
  • I did not design firebase DB yet since I am stuck with this problem: what is the best way to model inheritance in OOP languages in firebase – Muath Alkhalaf Oct 21 '17 at 17:04
  • Although I did not get a full answer yet but the NoSQL data modeling article that you shared in your answer is really great. Thanks for that – Muath Alkhalaf Oct 21 '17 at 19:05
  • @MuathAlkhalaf To follow up; there are no objects, properties or inheritance in Firebase. It's a storage mechanism based on paths to data. If you can provide a use case in your question, i.e. why are you doing this, we can probably provide an accurate answer. – Jay Oct 22 '17 at 12:14
  • @Jay I have the exact situation that I mentioned up where I have one parent class with two children classes where almost half properties are defined in the parent class. I am trying to balance the tradeoff of SWE requirements i.e. data/object modeling that would facilitate reusability and maintainability of my code VS. storage requirements i.e., modeling for fast DB queries. – Muath Alkhalaf Oct 22 '17 at 15:23
  • @MuathAlkhalaf We can probably help - however, the answer is totally dependent on what data you want to get back out of Firebase. If you can update your question with more specific information: maybe an example of what the parent and child objects are, some sample properties that would be stored in a parent class vs a subclass and what kind of queries you want to run and what kind of data you want to present to the user, we can take a look. – Jay Oct 22 '17 at 16:14