简体   繁体   中英

Firebase: How should I structure my database? Like Offerup and Letgo

I am currently building an iOS app that basically allows users to post items that they want to sell and other users can then search for items to purchase based on location. How should I structure my database?

I currently have the following structure in mind:

users {
    userID: {
        profileImage:
        username:
    }
    .
    .
    .
}


posts: {
    userID: {
        postID: {
            description:
            imageUrl:
            creationDate:
            price:
            location:
        }
        .
        .
        .
        .
    }
    .
    .
    .
    .
}

Hope what I am asking makes sense. In other words, how would an app like OfferUp or Letgo structure their database? Thanks!!!

It's hard to say - it depends how the rest of your application is going to work, but IMO instead of nesting postID under userID you would be better to add the userID to the post object.

posts: {
    postID: {
        description:
        imageUrl:
        creationDate:
        price:
        location:
        userID:
    }
}

Reasons;

  • Will allow you to run searches across all items, you don't need to search each user - then each item. Deep searching records isn't possible.

  • Your database rules can keep posts readable by all but then only editable/writeable where the auth.uid = userID .

  • Can still each by userID by nesting this way.

The only thing that I would change is that if you want to be able to show a list of the different items on sale, you should extract the information needed for the list to another root-level collection. You should put less details in that section, as Google says that it will allow the data to be downloaded faster, while using less data. (source: https://firebase.google.com/docs/firestore/manage-data/structure-data )

So, something like this:

{
    users: {
        userID: {
            profileImage:
            username:
        }
        .
        .
        .
    }


    posts: {
        zipCode: {
            postID: {
                userID: 
                description:
                imageUrl:
                creationDate:
                price:
                location:
            }
            .
            .
            .
            .
        }
        .
        .
        .
        .
    }

    listOfPosts: {
        zipCode: {
            postID: { //Only put essential information for your list of posts here
                imageURL:
                price:
                location:
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM