Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): both doc and subcollection within a single query #52

Open
compojoom opened this issue Jan 28, 2018 · 14 comments
Open

feat(query): both doc and subcollection within a single query #52

compojoom opened this issue Jan 28, 2018 · 14 comments

Comments

@compojoom
Copy link
Contributor

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.

@compojoom
Copy link
Contributor Author

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?

@prescottprue
Copy link
Owner

@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' }]
}

@prescottprue prescottprue changed the title bug: wrong merge of subcollection feat(query): both doc and subcollection within a single query Jan 31, 2018
@compojoom
Copy link
Contributor Author

compojoom commented Jan 31, 2018

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.

@Sgiath
Copy link

Sgiath commented Mar 14, 2018

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?

@prescottprue
Copy link
Owner

@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?

@JoshSum
Copy link

JoshSum commented Apr 15, 2019

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'
        }
    ])

@compojoom
Copy link
Contributor Author

compojoom commented Apr 15, 2019

@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
...
]
})

@JoshSum
Copy link

JoshSum commented Apr 16, 2019

@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)

@compojoom
Copy link
Contributor Author

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)

@techfortified
Copy link

techfortified commented Oct 3, 2019

@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

data-model

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

@prescottprue
Copy link
Owner

prescottprue commented Oct 3, 2019

@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**]
          ]
        }
      ]
    }
  ]
}))

@techfortified
Copy link

techfortified commented Oct 6, 2019

@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

@prescottprue
Copy link
Owner

prescottprue commented Oct 6, 2019

@SUPERTEMPO you should try making a storeAs that contains that id such as

storeAs: `${props.postId}-postLikes`

@techfortified
Copy link

@prescottprue thanks man.... You are great..... it worked perfectly.....thanks so much.... You really saved me, keep doing the good work.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants