amitdigga
amitdigga

Reputation: 7158

Firestore features and limitations on different data structure model

I have created app whose structure looks like this. Current structure for one company only.

let current = {
    products: {
        product1: {}//...
    },
    customers: {
        customer1: {},// ...
    },
    orders: {
        order1: {},// ...
    },
}

Now I have design data structure to make it multi company app. Suppose companies are ABC, PQR, XYZ but the customers are same. So, a customer can see products from different companies.

Option 1: Add Company property in every lists doc.

let option1 = {
    company: {
        products: {
            product1: {
                company: 'ABC'
            },
        },
        customers: { //Also we can put it at root with field company as array. Customers are not primary concern
            customer1: {
                company: 'PQR'
            }
        },
        orders: {
            order1: {
                company: 'ABC'
            }
        },
    }
}

My Remarks: I have to put company property in every list which may be more than these. It doesn't look like right solution. Querying products in different companies looks easy.

Option 2: Copy the current root structure for different companies.

let option2 = {
        company1: {
            products: {
                product1: {}
            },
            customers: {//Also we can put it at root with field company as array. Customers are not primary concern
                customer1: {},
            },
            orders: {
                order1: {},
            },
        },
        company2: {
            products: {
                product1: {}
            },
            customers: {
                customer1: {},
            },
            orders: {
                order1: {},
            },
        },
        // ...
    }

My Remarks: I don't know firestore limitations and upcoming features. Querying products in different companies may not be easy.

let option3= {} //your suggestions.

In same firestore project, assume customer handling will not be a problem.

What can be done here? What are things I am missing?

Upvotes: 0

Views: 158

Answers (1)

Jason Berryman
Jason Berryman

Reputation: 4898

Products

It is unusual for several companies to sell the same product, without wanting to create their own stock item, description, price, etc. Also, if you have an array / map of companies who sell the product, within the product document, customers will see where they can buy the product and there may be no loyalty to any one company. If this is what you're hoping to achieve, then your options could work for you.

Customers

Companies will also want to keep certain data about their customers separate from other companies.

My proposal

I would suggest that you create a Cloud Firestore collection companies and have a document for each company. Within that document, you can create sub-collections for customers, orders and products.

A separate users collection at the root level will allow users to maintain their own data and allow companies to collect whatever they need to keep their records up to date. Adding public and private sub-collections of the user data can manage this easily for you.

Todd Kerpelman from the Firebase team, has made an excellent video which will really help with data modelling in Cloud Firestore.

I hope that this helps

Upvotes: 2

Related Questions