Handling complex requests in firebase

I am developing a shopping cart mobile app using ionic framework and FIREBASE as source. I have a requirement that I have to join JOIN, SORT, FILTER and PAGINATE with multiple data attributes.

An example data structure is shown below.

: {

prod1:{
    name: Samsung-s4,
    type: pro,
    category: Phone,
    created_datetime: 1426472828282,
    user: user1,
    price: 400

},

prod2:{
    name: iPhone 5s,
    type: pro,
    category: Phone,
    created_datetime: 1426472635846,
    user: user2,
    price: 500

},

prod3:{
    name: HP Laptop i3,
    type: regular,
    category: Computer,
    created_datetime: 1426472111111,
    user: user1,
    price: 600

}

      

}

user_profiles: {

user1:{
    name: abc_user,
    display_name: ABC,
    email: abc@mail.com
},

    user2:{
    name: xyz_user,
    display_name: XYZ,
    email: xyz@mail.com
}

      

}

I need to request products in several ways. two simple examples below.

1) Get all products using Pagination where type is "pro" then join user_profiles and SORT creating a date.

2) Get all products paginated, filter by category, then join user_profiles and SORT creating a date.

As above, there will be more and more filtering options following in Eg: price.

My main problem is that I couldn't find a direct way to do this using the FIREBASE query options. Also, I have referenced the documentation using firebase, but there also I don't see a way to do this.

As far as I can see, the only way to do this is to do most of the client side processing by getting all the data (or most of the data) on the client side and making SORT / FILTER / PAGINATE in the client end.

But we expect thousands of entries in these schemas, so there will be a huge performance impact if we do client-side processing !!

Please rate your experience / support for solving this problem.

Thanks in advance.

UPDATE:

I changed the data structure (as explained by webduvet) and tried to use firebase-util but couldn't achieve what I want. CRITERIA: Products → Filter by type / pro → Sort by products.created_date

type: {

about: {

product1: user1,
product3: user1,

      

...

}, regular: {...}

}

firebase-util - corner fire code

            var list =  $firebase(new Firebase.util.NormalizedCollection(
                    ref.child("products").orderByChild('created_datetime'),         
                    ref.child('type').child('pro')
                ).select(
                        {key: "products.name" , alias: 'name'}
                ).ref()).$asArray();

      

"products" will have one hundred thousand records, so we have to make sure we limit as much as possible at the end of the firebase and not do client side processing.

Please, help!

+3


source to share


1 answer


This should be done not by queries, but by database design. You need to save the data in an unnormalized form, for example:

type: {
  pro:{
    product1: user1,
    product3: user1,
  ...
  },
  regular:{
   ...
  }
}

      



the above structure will give you the ability to query all pro products and get the user id. Firebase offers a good sorting mechanism, so it shouldn't be a problem. Requires a more complex query, requiring a more complex data structure and more denormalized data that you will have.

But as @ Swordfish0321 pointed out, the db type in db might suit you much better.

+3


source







All Articles