DEV Community

Discussion on: 𝚒𝚗𝚝𝚎𝚐𝚛𝚒𝚏𝚢 - Firestore referential integrity via triggers

Collapse
 
camillo777 profile image
camillo777

Hello very nice!
What about if target is a subcollection of a document?
Like:

users:[
user: {
userID
}
]

comments: [
comment: {
liked: [
userID
]
}
]

Collapse
 
anishkny profile image
Anish Karandikar

Thats a great question @camillo777 .

As of v2.2.0, you can now replicate into and delete references from subcollections by specifying isCollectionGroup: true in the target collection:

integrify({
  rule: 'REPLICATE_ATTRIBUTES',
  source: {
    collection: 'master',
  },
  targets: [
    {
      collection: 'detail2',
      foreignKey: 'masterId',
      attributeMapping: {
        masterField1: 'detail2Field1',
        masterField3: 'detail2Field3',
      },

      // Optional:
      isCollectionGroup: true,  // <---- Replicate into group
    },
  ],
});

See README for full details.

Collapse
 
camillo777 profile image
camillo777

Yes thank you I managed to do it.
Works like a charm.

Another question for my example.

Can the foreignKey be the id of the target Firestore doc? What is the exact field name?

And: is it safe to use the same document id in multiple collections?

For example I have a users collection and I want to add "liked" users to a doc as a subcollection. I would use the same id of the user, but is it safe at a Firestore model level?

Thank you