realph
realph

Reputation: 4671

Three-Way Relationship in Firebase

I've been learning a lot about denormalised data over the past few months, but I wanted to know if the following is possible in a flattened architecture world. I know how to handle two-way relationships in Firebase, but what about a three-way relationship. Let me explain...

I have 5 items in my database, services, providers, serviceAtProvider, reviews and users. I want to be able to add providers to services and vice versa.

I'd also like there to be a specific page for a provider inside a service, and for there to be reviews linked to the provider at that specific service. The page url might look like this (site.com/serviceId/providerId). The rating is unique to that providerId inside of that serviceId – you can't rate serviceIds or providerIds separately.

I'm not sure how to go about creating such a complex relationship. How would I join the serviceId and providerId in that serviceAtProvider item?

This is what I've got so far:

 "services": {
   "service1": {
     "name": "Hernia Repair",
     "providers": {
       "provider1": true,
       "provider2": true
     }
   }
 },
 "providers": {
   "provider1": { "name": "The Whittington Hospital" },
   "provider2": { "name": "Homerton Hospital" }
 },
 "serviceAtProvider": {
   "service1AtProvider1": { "rating": 4 },
   "service1AtProvider2": { "rating": 3 }
 },
 "reviews": {
   "service1AtProvider1": {
     "review1": {
       "body": "A review from user 1",
       "user": "user1"
     }
   },
   "service1AtProvider2": {
     "review1": {
       "body": "A review from user 2",
       "user": "user2"
     }
   }
 },
 "users": {
   "user1": { "name": "Ben Thomas" },
   "user2": { "name": "Beatrix Potter" }
 }

I don't know how to create that serviceAtProviderjoin, or how would I go about accessing the service1.name, provider1.name, service1AtProvider1.rating, reviews.service1AtProvider1 on one page. Can anyone explain how to do this?

Also, are there any best practices I should follow?

Any help is appreciated. Thanks in advance!

UPDATE

{
  "availableServices": {
    "service1": { "name": "Hernia Repair" },
    "service2": { "name": "Chemotherapy" }
  },

  "services": {
    "provider": {
      "name": "The Whittington Hospital",
      "service": {
        "service1": {
          "rating": 4,
          "reviewId1": true
        },
        "service2": {
          "rating": 3,
          "reviewId2": true
        },
      }
    }
  },

  "reviews": {
    "reviewId1": {
      "review1": {
        "rating": 4,
        "body": "A review from user 1",
        "user": "user1"
      }
    }
  },

  "users": {
    "user1": { "name": "Raphael Essoo-Snowdon" },
    "user2": { "name": "Sharlyne Slassi" }
  }
}

Upvotes: 1

Views: 488

Answers (1)

Kato
Kato

Reputation: 40582

I would start by making the data structure a bit simpler and more direct. It's hard to determine the correct data structure for your needs without a detailed use case. I'll do my best to make some generic assumptions here. You'll have to adapt as necessary.

{
  "service": {
    "service1": { "name": "Foo Service" }, 
    ...
  },

  "provider": {
     "provider1": { name: "Foo Place" }, 
     ...
  },

  "ratings": {
    "service1": { // service id
      "provider1": {  // provider id
        "average_rating": 4
      },
      ...
    },
    ...
  },

  "reviews": {
    "service1": { // service id
      "provider1": {  // provider id
        "user": "user1",
        "rating": 4
      },
      ...
    },
    ...
  },

  "user": {
    "user1": { "name": "Foo Bar" },
    ...
  }
}

Now, to look up the providers who offer a given service, and grab their reviews, I would do the following:

var ref = new Firebase(...);
ref.child('ratings/service1').on('child_added', function(reviewSnap) {
   console.log(
      'Provider ' + reviewSnap.key(),
      'Average rating ' + reviewSnap.val().average_rating
   );
});

Joining in the names of the services and providers could be done in several ways. Here's a manual technique:

var ref = new Firebase(...);
ref.child('ratings/service1').on('child_added', accumulateReview);

function accumulateReview(reviewSnap) {
  var reviewData = reviewSnap.val();
  var reviewid = reviewSnap.key();
  fetchService(reviewSnap.parent().key(), function(serviceSnap) {
    loadRec('provider', reviewSnap.key(), function(providerSnap) {
        console.log('Provider: ', providerSnap.key(), providerSnap.val().name);
        console.log('Service: ', serviceSnap.key(), serviceSnap.val().name);
        console.log('Average rating: ', reviewData.average_rating);
    });
  });
}

var serviceCache = {};
function fetchService(serviceid, done) {
  // demonstrates creating a local cache for things that will be
  // looked up frequently 
  if( !serviceCache.hasOwnProperty(serviceid) ) {
    cacheService(serviceid, done);
  }
  else {
    done(serviceCache[serviceid]);
  }
}

function cacheService(serviceid, done) {
  loadRec('service', function(ss) {
    serviceCache[serviceid] = ss;
    fetchService(serviceid, done);
  });
}

function loadRec(type, key, done) {
  ref.child(type).child(key).once('value', done);
}

I could also automate some of this process with Firebase.util's NormalizedCollection:

var ref = new Firebase(...);
var nc = Firebase.util.NormalizedCollection(
   [ref.child('reviews/service1'), 'review'],
   ref.child('provider'),
   ref.child('user')
)
.select('review.rating', {key: 'provider.name', alias: 'providerName'}, {key: 'user.name', alias: 'userName'})
.ref();

nc.on('child_added', function(snap) {
  var data = snap.val();
  console.log('Provider', data.providerName);
  console.log('User', data.userName);
  console.log('Rating', data.rating);
});

Note that nothing here is set in stone. This is how I would approach it. There are probably dozens of structures at least as good or better.

Upvotes: 2

Related Questions