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

Target collection params in query #57

Open
michaelprichardson opened this issue Jul 7, 2020 · 3 comments
Open

Target collection params in query #57

michaelprichardson opened this issue Jul 7, 2020 · 3 comments

Comments

@michaelprichardson
Copy link

Would it be possible to use the parameters from the source collection in the target collection? I was thinking of being able to use the parameters in the source to the target collection.

source: {
    collection: 'master/{someId}/detail',
  },
  targets: [
    {
      collection: 'details/{someId}/more_detail',
      foreignKey: 'masterId',
    },
  ],

Very basic example but the idea to use the someId in the target collection query.

@michaelprichardson michaelprichardson changed the title Target collection params in Target collection params in query Jul 7, 2020
@anishkny
Copy link
Owner

anishkny commented Jul 8, 2020

On the face of it, this seems difficult to achieve. Leaving issue open for a little while though...

@nbransby
Copy link

nbransby commented Jul 8, 2020

I guess this would help with replacing a trigger such as:

export const onDeleteUser = db.document('users/{userId}').onDelete(async (user, _) => {

    const batch = firestore().batch();
    //delete all comments
    const comments = await user.ref.collection(`comments`).get();
    comments.forEach(({ ref }) => batch.delete(ref));
    await batch.commit();
});

Seems like a basic thing to be able to do: delete a subcollection on deletion of parent

@arthabus
Copy link

arthabus commented Oct 3, 2020

I think adding the below code next to this line enables setting the wildcarded parameters in the target collection as needed:

const targetCollection = target.collection;

let targetCollection = target.collection; //line 86, change from const to let
    Object.keys(context.params).forEach(paramName => {
    const value = context.params[paramName]
    targetCollection = targetCollection.replace("{" + paramName + "}", value)
})

@nbransby would it be possible for you to add it to the library?

This will enable using nested collections in such cases without the need of creating group collection index - this will save extra indexes which is helpful since there is a limit on the number of such indexes in firestore.

I guess the same can be applied to the deleteReferences and maintainCount rules as well

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

No branches or pull requests

4 participants