redux-firestore
redux-firestore copied to clipboard
feat(query): both doc and subcollection within a single query
Do you want to request a feature or report a bug?
Bug
What is the current behavior? I'm trying to request a collection and a subcollection: { collection: 'companies', doc: company, subcollections: [{ collection: 'users' }] }
What is the expected behavior? the expected behavior is to receive the doc + a subcollection
Instead the subcollection overrides all properties of the doc. So one ends only with the subcollection data.
Hm, maybe I missunderstood the docs: { collection: 'cities', doc: 'SF', subcollections: [{ collection: 'zipcodes' }] },
thought that this would get me the cities/SF + cities/sf/zipcodes? but it seems that if I want the doc cities/SF - I should just do another call?
Is this the correct behavior?
@compojoom That is correct behavior currently, but I have noticed this means repeating myself in a few places.
What are your thoughts on the potential of an option on the query to get both the document and the subcollection? Under the hood it would be two queries, but it might mean much less repeating ourselves.
Maybe something like:
{
collection: 'cities',
doc: 'SF',
includeDoc: true, // <- this option, but maybe a different name?
subcollections: [{ collection: 'zipcodes' }]
}
Well, actually - once you know that you can't do it in one go it's a no-brainer. Maybe we should change the docs and mention this behavior? You can't fetch a document and it's collection directly in firestore anyway... So what you have here seems consistent.
Hi I have slightly different problem. I figure it out that i need to do multiple queries to get document with his subcollections. So I did something like this:
firestoreConnect((props) => [
{ collection: 'games', doc: props.match.params.id },
{ collection: 'games', doc: props.match.params.id, subcollections: [{ collection: 'teams' }] },
{ collection: 'games', doc: props.match.params.id, subcollections: [{ collection: 'tasks' }] },
])
And it works correctly for firestore.data I have there:
{
"firestore": {
"data": {
"games": {
"xxx": {
"name": "Game name",
"teams": {
"xxxx": {"name": "Team 1", "password": "pswd1"},
"yyyy": {"name": "Team 2", "password": "pswd2"},
},
"tasks": {
"xxxx": {"name": "Task 1", "hint": "Hint 1"},
"yyyy": {"name": "Task 2", "hint": "Hint 2"},
},
},
},
},
},
}
But for firestore.ordered the data are rewritten instead of compose together (only tasks are there - the last query):
{
"firestore": {
"ordered": {
"games": [
{ "name": "Task 1", "hint": "Hint 1", "id": "xxxx", },
{ "name": "Task 2", "hint": "Hint 2", "id": "yyyy", },
],
},
},
}
The workaround is to use storeAs but then I have this structure:
{
"firestore": {
"data": {
"games": {},
"teams": {},
"tasks": {},
},
"ordered": {
"games": [],
"teams": [],
"tasks": [],
},
},
}
Is this a bug or intended behavior? Is there some other workaround for this?
@Sgiath What you are mentioning should no longer be an issue in the most recent versions. There was an issue in the ordered reducer which was fixed. Can you confirm?
hi guys i wanna ask,,,,can we change this option to a local state? Thx for any help
firestoreConnect([
{
collection: 'campaignModules',
doc: 'G7N0CX17W70830A' // <- this option,can be replace with local state?
, subcollections: [{ collection: 'daily', doc: fullDate }], storeAs: 'daily'
}
])
@JoshSum - before firestoreConnect is called - your redux selector is being executed. So if in the selector you return {docId: myDocId}
and you change your firestore connect to accept props as first argument, then you can access prop.docId
firestoreConnect(props => {
const {docId} = props
return [
collection...
doc: docId
...
]
})
@compojoom I have many documentId on each subcollection.... how to check if exist, then firestoreconnect, if not exist dont do firestoreconnect, but i have default connect on top of this....
firestoreConnect(props =>{
const daily = props.name
return[
{ collection: 'users' }, // <- this default connect
{ collection: 'campaignModules' }, // <- this default connect
{
collection: 'campaignModules',
doc: daily // <- not always exist,,, if user click it exist, how to avoid error before user click
, subcollections: [{ collection: 'daily', doc: fullDate }], storeAs: 'daily'
}
]})
)(AdsLocation)
firestoreConnect(props =>{
const daily = props.name
// No daily, no need to subscribe to any document in firestore
if(!daily) {
return []
}
return[
{ collection: 'users' }, // <- this default connect
{ collection: 'campaignModules' }, // <- this default connect
{
collection: 'campaignModules',
doc: daily // <- not always exist,,, if user click it exist, how to avoid error before user click
, subcollections: [{ collection: 'daily', doc: fullDate }], storeAs: 'daily'
}
]})
)(AdsLocation)
@compojoom and @prescottprue ....Please how can i automatically query a collection and its subcollection without knowing the doc id of each document? I have a collection and subcollection with different documents but i want to return each collection with its subcollection in single query. How do i go about it in react-redux-firestore?? e.g supaPosts is a collection with supaPostLikes as subcollection, so the idea now is to fetch each doc from supaPosts and use the doc id each of supaPosts to query its subcollection and return if the currentUserId is present in each supaPosts subcollection supaPostLikes... Here is the nature of my database model supaPosts collection and supaPostLikes subcollection

now am trying something like this....
`firestoreConnect((props) => {
return [
{ collection: 'supaPosts',orderBy:['supaPostTime','desc'],limit:10 },
{ collection: 'supaPosts', doc: **something**, subcollections: [{ collection: 'supaPostLikes' },where[
['likerId', '==', **currentUserId**]
]}
]
})`
Now how do i get the doc id called something of each supaPosts document and use it to query its subcollection supaPostsLikes and return if likerId is equal to currentUserId?? Or it is not possible to do so
@SUPERTEMPO That would be creating one query then another query, so first the query for all of the posts, then a query for each post to get the likes.
It would probably be best to do this using the multiple levels of your components as a way to structure your listeners:
Structure
SupaPosts - creates listener for supaposts with limit 10
connect mapStateToProps (`firestore.ordered.supaposts -> props.supaposts`)
map over items and pass id as `postId` prop and render SupaPost
SupaPost - creates a listener for the likes of the supapost based on `postId`
Listeners SupaPosts
firestoreConnect((props) => {
return [
{ collection: 'supaPosts', orderBy: ['supaPostTime', 'desc'], limit:10 }
]
})
SupaPost
firestoreConnect((props) => {
return [
{
collection: 'supaPosts',
doc: props.postId,
subcollections: [
{
collection: 'supaPostLikes',
where: [
['likerId', '==', **currentUserId**]
]
}
]
}
]
}))
@prescottprue i did exactly as you have written but rather this query
firestoreConnect((props) => {
return [
{
collection: 'supaPosts',
doc: props.postId,
subcollections: [
{
collection: 'supaPostLikes',
where: [
['likerId', '==', props.uid]
]
}
]
}
]
})
is picking just the first props.postId and returning multiple times for the number of limit i have which is 10 and even overwriting the supaPosts state. when i tried to use storeAs postLiked, it is returning but still multiple data for only the first props.postId. Dunno what to do.... dunno if there is any way round .......thanks
@SUPERTEMPO you should try making a storeAs that contains that id such as
storeAs: `${props.postId}-postLikes`
@prescottprue thanks man.... You are great..... it worked perfectly.....thanks so much.... You really saved me, keep doing the good work.....