flamelink / flamelink-js-sdk

🦊 Official Flamelink JavaScript SDK for both the Firebase Realtime database and Cloud Firestore
https://flamelink.github.io/flamelink-js-sdk
MIT License
43 stars 5 forks source link

How can I organize my schema specific for each logged-in user? #116

Closed singamnv closed 4 years ago

singamnv commented 4 years ago

I am using Firebase with React Native and just started with Flamelink as my CMS.

Before using Flamelink, I was able to obtain specific content from a logged-in user via firebase realtime database because the schema was:

fruits mwaKRypUrNb8rJdsj9YUxw0sA892 -M055RlZ7HBzgRD9LS1d -M055Why2psB2kqE4mYO -M05W3LK8C1kgFsPNRqa -M05W47cbhJ2VA_Y2nnT -M05W5Wu9Pp6TZJJPc8t -M05W6Xc_iP1upbyENpj -M05W7JnVt3t3DsjNaRT -M05W8OuZph-1gDDY9UX -M05W9Fws0_hsNCAyKDt users -zFWGnGhEmbfhjAJsUT99ZbkJBZ33 As you can see, the fruits schema had user-specific content.

With Flamelink the schema looks like this:

flamelink --environments ----production ------content --------fruitEntryForm ----------en-US ------------1582070066847 ------------1582074440836 ------------1582080914427

schemas media permissions settings users --zFWGnGhEmbfhjAJsUT99ZbkJBZ33 My react native code is below:

const user = navigation.getParam("user"); const currentUserData = await firebase .database() .ref("flamelink/users") .child(user.uid) .once("value");

const fruits = await app.content.get("content"); How can I modify Flamelink so that I can manage content based on which user is signed in?

jperasmus commented 4 years ago

Hi @singamnv

Thanks for the question. From my understanding, your Fruits collection can be linked to multiple users and the main problem is that the Firebase Realtime Database does not allow querying for a value inside an array. Cloud Firestore is a bit better in that regard.

What I would suggest you do to simplify your queries is to turn the relationship on its head. I would have the Fruits collection that you currently have without a relationship to any users. Then I would create a "Users Fruit" collection that has a field for the user's UID with a relational select field that maps to multiple entries in your Fruits collection.

From your React Native app, you can then use our SDK to query the "Users Fruit" collection and expand the related Fruits.

Depending on the version of the SDK you are using, the syntax might differ a bit, but for the latest SDK it will look like this:

const userFruit = await app.content.get({
  schemaKey: 'usersFruit', // assuming schema key is this
  orderByChild: "user", // assuming your field is called 'user'
  equalTo: "mwaKRypUrNb8rJdsj9YUxw0sA892", // assuming this is your user's uid
  populate: ['fruits'] // assuming you called the select relational field 'fruits'
})
singamnv commented 4 years ago

Awesome. Got it to work. Thank you for your advice.

jperasmus commented 4 years ago

I'm glad to hear that. 🤓