{"id":13799105,"url":"https://github.com/anishkny/integrify","last_synced_at":"2025-04-09T22:10:16.536Z","repository":{"id":34039036,"uuid":"166447860","full_name":"anishkny/integrify","owner":"anishkny","description":"🤝 Enforce referential and data integrity in Cloud Firestore using triggers","archived":false,"fork":false,"pushed_at":"2025-03-26T14:26:55.000Z","size":292,"stargazers_count":110,"open_issues_count":12,"forks_count":9,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-02T16:02:08.218Z","etag":null,"topics":["data-integrity","firebase","firebase-functions","firebase-trigger","firestore","referential-integrity","serverless"],"latest_commit_sha":null,"homepage":"https://firebaseopensource.com/projects/anishkny/integrify","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anishkny.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-18T17:32:46.000Z","updated_at":"2024-12-20T22:52:13.000Z","dependencies_parsed_at":"2024-04-21T03:35:06.076Z","dependency_job_id":"1b4ed7d5-5c5a-4ed6-8629-4a0fe2c6f7e2","html_url":"https://github.com/anishkny/integrify","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anishkny%2Fintegrify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anishkny%2Fintegrify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anishkny%2Fintegrify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anishkny%2Fintegrify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anishkny","download_url":"https://codeload.github.com/anishkny/integrify/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247947781,"owners_count":21023067,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-integrity","firebase","firebase-functions","firebase-trigger","firestore","referential-integrity","serverless"],"created_at":"2024-08-04T00:00:58.867Z","updated_at":"2025-04-09T22:10:16.515Z","avatar_url":"https://github.com/anishkny.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","서버 사이드 (Cloud Functions, BigQuery 등)"],"sub_categories":["iOS"],"readme":"# 𝚒𝚗𝚝𝚎𝚐𝚛𝚒𝚏𝚢\n\n[![Build \u0026 Test](https://github.com/anishkny/integrify/actions/workflows/build-and-test.yml/badge.svg?branch=main)](https://github.com/anishkny/integrify/actions/workflows/build-and-test.yml)\n[![Code Coverage Status](https://codecov.io/gh/anishkny/integrify/branch/main/graph/badge.svg)](https://codecov.io/gh/anishkny/integrify)\n[![npm package](https://img.shields.io/npm/v/integrify.svg)](https://www.npmjs.com/package/integrify)\n[![Mentioned in Awesome Firebase](https://awesome.re/mentioned-badge.svg)](https://github.com/jthegedus/awesome-firebase)\n[![Firebase Open Source](https://img.shields.io/badge/❤-Firebase_Open_Source-FFCA28)](https://firebaseopensource.com/projects/anishkny/integrify/)\n\n🤝 Enforce referential and data integrity in [Cloud Firestore](https://firebase.google.com/docs/firestore/) using [triggers](https://firebase.google.com/docs/functions/firestore-events)\n\n[Introductory blog post](https://dev.to/anishkny/---firestore-referential-integrity-via-triggers-kpb)\n\n## Usage\n\n```js\n// index.js\n\nconst { integrify } = require('integrify');\n\nconst functions = require('firebase-functions');\nconst admin = require('firebase-admin');\nadmin.initializeApp();\nconst db = admin.firestore();\n\nintegrify({ config: { functions, db } });\n\n// Automatically replicate attributes from source to target\nmodule.exports.replicateMasterToDetail = integrify({\n  rule: 'REPLICATE_ATTRIBUTES',\n  source: {\n    collection: 'master',\n  },\n  targets: [\n    {\n      collection: 'detail1',\n      foreignKey: 'masterId',\n      attributeMapping: {\n        masterField1: 'detail1Field1',\n        masterField2: 'detail1Field2',\n      },\n    },\n    {\n      collection: 'detail2',\n      foreignKey: 'masterId',\n      attributeMapping: {\n        masterField1: 'detail2Field1',\n        masterField3: 'detail2Field3',\n      },\n\n      // Optional:\n      isCollectionGroup: true, // Replicate into collection group, see more below\n    },\n  ],\n\n  // Optional:\n  hooks: {\n    pre: (change, context) =\u003e {\n      // Code to execute before replicating attributes\n      // See: https://firebase.google.com/docs/functions/firestore-events\n    },\n  },\n});\n\n// Automatically delete stale references\nmodule.exports.deleteReferencesToMaster = integrify({\n  rule: 'DELETE_REFERENCES',\n  source: {\n    collection: 'master',\n  },\n  targets: [\n    {\n      collection: 'detail1',\n      foreignKey: 'masterId',\n\n      // Optional:\n      isCollectionGroup: true, // Delete from collection group, see more below\n    },\n  ],\n\n  // Optional:\n  hooks: {\n    pre: (snap, context) =\u003e {\n      // Code to execute before deleting references\n      // See: https://firebase.google.com/docs/functions/firestore-events\n    },\n  },\n});\n\n// Automatically maintain count\nmodule.exports.maintainFavoritesCount = integrify({\n  rule: 'MAINTAIN_COUNT',\n  source: {\n    collection: 'favorites',\n    foreignKey: 'articleId',\n  },\n  target: {\n    collection: 'articles',\n    attribute: 'favoritesCount',\n  },\n});\n```\n\nDeploy to Firebase by executing:\n\n```bash\n$ firebase deploy --only functions\n```\n\n### Rules File\n\nAlternately, rules can be specified in a file named `integrify.rules.js`.\n\n```js\n// index.js\n\nconst { integrify } = require('integrify');\n\nconst functions = require('firebase-functions');\nconst admin = require('firebase-admin');\nadmin.initializeApp();\nconst db = admin.firestore();\n\nintegrify({ config: { functions, db } });\n\n// Rules will be loaded from \"integrify.rules.js\"\nmodule.exports = integrify();\n```\n\n```js\n// integrify.rules.js\n\nmodule.exports = [\n  {\n    rule: 'REPLICATE_ATTRIBUTES',\n    name: 'replicateMasterToDetail',\n    // ...\n  },\n  // ...\n];\n```\n\n### Collection Groups (`isCollectionGroup`)\n\nFirestore allows searching over multiple collections (a.k.a. collection group) with the same name at any level in the database. This is called a [collection group query](https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query).\n\nIntegrify allows you to replicate tracked master attributes into (optionally) collection groups linked by a foreign key using the `isCollectionGroup` parameter (see above) in the `REPLICATE_ATTRIBUTES` rule. Similarly, you can delete references in a collection group (instead of just a collection) using the `isCollectionGroup` in the `DELETE_REFERENCES` rule.\n\n**Note:** You need to first create the appropriate index to be able to use Collection Group Queries. The first time you attempt to use it, Firebase will throw an error message with a link which when clicked will prompt you to create the appropriate index. For example:\n\n```\nThe query requires a COLLECTION_GROUP_ASC index for collection detail1 and field masterId. You can create it here: https://console.firebase.google.com/project/integrify-dev/database/firestore/indexes/single_field?create_exemption=ClNwcm9qZWNxxxxxx3RlcklkEAE\n```\n\nFor more help, see [here](https://firebase.google.com/docs/firestore/query-data/indexing).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanishkny%2Fintegrify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanishkny%2Fintegrify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanishkny%2Fintegrify/lists"}