1

The Background

In an attempt to build some back-end services for my e-commerce (Shopify based) site I have set up a Firestore trigger that writes order details with every new order created which is updated by a web hook POST function provided by Shopify - (orders/Create webhook).

My current cloud function -

exports.saveOrderDetails = functions.https.onRequest((req, res) => {

var docRef = db.collection('orders').doc(req.body.name);
   const details = req.body;

var setData = docRef.set(req.body).then( a =>{
    res.status(200).send();
});

});

Which is able to capture the data from the webhook and store it in the order number's "name" document within my "orders" collection. This is how it looks in Firestore:

Deep Nesting in Firestore Fields

My question is - with the help of body-parser (already parsing out "name" which is represented as #9999 in my screenshot, to set my document name value) - how could I improve my cloud function to handle storing this webhook POST in a better data structure for Firestore and to query it later?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
tomtom215
  • 11
  • 7
  • 1
    Without knowing what your queries will be, there's really no way of saying what's "better". Good data modeling with NoSQL database is entirely dependent on knowing what the queries will be, and designing for those queries. – Doug Stevenson Feb 15 '18 at 02:05
  • @DougStevenson Good point. I figured I was asking a very wide question to and didn't provide enough details. In the manner that I'm parsing and storing it currently - I end up with multiple nested fields in one document which I feel is incorrect and would ideally like to be able to parse it down to have it go something like /orders/{order#}/line_items/{line_item#}/item data - but I am unsure of how to go about this or if I am way off in my approach – tomtom215 Feb 15 '18 at 02:13
  • If the question is broad, and there isn't going to be an obvious and clear answer, consider starting a conversation on firebase-talk, as it Stack Overflow is not really meant for open-ended conversation. https://groups.google.com/forum/#!forum/firebase-talk – Doug Stevenson Feb 15 '18 at 02:35
  • @DougStevenson joined firebase-talk, thanks for the link! After more searching and trying to rephrase how I could word my question or describe my query goals, [I came across this Stack Overflow post that seems to verify the feature is unavailable in Firestore](https://stackoverflow.com/questions/46573014/firestore-query-subcollections) – tomtom215 Feb 15 '18 at 02:48
  • That question is about querying across the subcollections of multiple documents. Your data structure doesn't show any subcollections, only nested fields. That said: you seem to be trying to have a fields array and trying to query within that array. This typically means an array is not the correct data structure, and you should consider using Sets instead. See the documentation here: https://firebase.google.com/docs/firestore/solutions/arrays or my answer here: http://stackoverflow.com/q/40656589 (the latter is for Firebase's Realtime Database, but it applies here too). – Frank van Puffelen Feb 15 '18 at 04:46
  • Btw: I think this question could be valid here, but right now you've posed it too broad and it seems a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the precise query that you'd like to do on your current data? E.g. "I want to get the name of the line item with ID 86666...39000". – Frank van Puffelen Feb 15 '18 at 04:47
  • Unrelated to your original question, but your use case for the https Cloud Function seems like a good fit for the Cloud Firestore REST API. – Jason Berryman Feb 15 '18 at 12:30

1 Answers1

0

After reviewing the comments on this question, I moved this question over to Firebase-Talk and it appears the feature I am attempting here would be close to what is known as "collection group queries" and was informed I should adjust my data model approach since this feature is currently still on the road map - and perhaps look into the Firestore REST API as suggested by @jason-berryman

Besides the REST APi, @frank-van-puffelen made a great suggestion to look into working with Arrays, Lists, Sets for Firebase/Firestore

Another approach that could mitigate this in my scenario is to have my HTTP Firestore cloud trigger have multiple parsing arguments that create top more top level documents - however this could cause a point of scaling failure or an increase of cost factor due to putting more parsing processing logic in my cloud function and adding additional latency...

I will mark my question as answered for the time being to hopefully help others to understand how to work with documents in a single collection in Firestore and not attempt to query groups of collections before they get too far into modelling and need to restructure their app.

tomtom215
  • 11
  • 7