integrify icon indicating copy to clipboard operation
integrify copied to clipboard

Target collection params in query

Open michaelprichardson opened this issue 5 years ago • 3 comments

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 avatar Jul 07 '20 13:07 michaelprichardson

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

anishkny avatar Jul 08 '20 01:07 anishkny

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

nbransby avatar Jul 08 '20 17:07 nbransby

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

https://github.com/anishkny/integrify/blob/d3450e65907ece4c5d87f7fcb1a0c38b807d11f0/src/rules/replicateAttributes.ts#L86

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

arthabus avatar Oct 03 '20 20:10 arthabus