How to create data resources from sub-collection


Here’s a situation. Your users are creating documents as sub-collections in your app. How can you use the Rest API resource to extract and view those in your app when they have auto generated keys?

Remember the url will have a static key while your users are generating auto random unique keys as they create documents. So how can you run and create a shema that has a sub-collection with an auto generated key?

I’ve managed to sort it out. You simply remove the document id in the url and give it an id with the curley brackets {id} while ceating a collection. That makes it dynamic.

Only challenge is the name cannot work because the id needs to be a single text and not a path. The work around is to use a Javascript andm make use of the fact that a document id only has 20 characters. So I used this code to get only the ID from the entire path. Code

return { result: inputs.group_id.slice(inputs.group_id.length-20, inputs.group_id.length) };

This code gives me the id alone which I can include on the url to get the full path of the sub collection and use it to build a dynamic schema. Hope I’ve explained well.

1 Like