Home > front end >  Database structure in Firebase Swift
Database structure in Firebase Swift

Time:11-13

I am trying to think of the best solution for storing user's data in firebase, considering scalability, queries etc...

For example: Each user can create a list of categories. I was thinking of storing it like so:

Categories (Collection)
 - User ID (Document)
    - Category ID
       - title
       - imageURL

I would like to access the categories collection, find a list of all user ID's and when accessing a user ID I find their list of all the categories they have created...

I am quite new to firebase...

Or should I create Categories(collections) - User ID (document) - Category Id (collection) - title etc (document) ? Just does not seem right....

I appreciate any assistance or tips!

Edit: This is how I am currently saving the data to Firebase from my IOS app:

Model:

struct Category: Codable, Identifiable {
@DocumentID var docId: String?
var id: String? = UUID().uuidString
var authorId: String
var title: String
var categoryImageUrl: String?

Save data function:

  guard let userId = Auth.auth().currentUser?.uid else { return } // REMOVE
     
     let db = Firestore.firestore() // REMOVE
     let collectionRef = db.collection("Categories").document(userId)
     do {
         try collectionRef.setData(from: category)
    }
     catch {
         print(error)
     }

CodePudding user response:

It's hard to give a comprehensive answer, but one thing to keep in mind is that the client-side SDKs for Firestore don't have an API to get a list of (sub)collection. So it's important that your application can know the collection names without that, either because they are hard-coded (most common) or because they are stored elsewhere (such as in the parent document).

  • Related