DEV Community

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

Anish Karandikar on January 23, 2019

Firestore is amazing, but... Google Cloud Firestore is a serverless NoSQL document database that scales horizontally - which means it ad...
Collapse
 
ayyappa99 profile image
Ayyappa • Edited

Regarding "MAINTAIN_COUNT", does it handle for higher parallel updates? Firestore document has a limit of 1 write per sec and the have an extension to handle this drawback.

Can you please let me know internally Integrify uses sharded counters or has 1 write/sec limit?

Overall, this is a serious pain point and glad your package solves this. Great work!

Collapse
 
anishkny profile image
Anish Karandikar

Thanks for the question @ayyappa99

Currently, writes in MAINTAIN_COUNT are not sharded and limited to max 1/sec - but I have opened a enhancement issue to track it for future github.com/anishkny/integrify/issu...

Collapse
 
felixcollins profile image
Felix Collins

Looks useful. Can it do Many to many? How do I delete a stale reference from an array of foreign keys. given:

/parents/
parentId/
mychildren (array of FK childId)

/children/
childId/

if I delete a child, how do I delete from mychildren array?

Or do you think I should use a "joining" document?

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