MCPcopy Index your code
hub / github.com/anishkny/integrify

github.com/anishkny/integrify @v4.1.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v4.1.2 ↗ · + Follow
23 symbols 52 edges 16 files 1 documented · 4% updated 1d ago★ 1106 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

𝚒𝚗𝚝𝚎𝚐𝚛𝚒𝚏𝚢

Build & Test Code Coverage Status npm package Mentioned in Awesome Firebase Firebase Open Source

🤝 Enforce referential and data integrity in Cloud Firestore using triggers

Introductory blog post

Usage

// index.js

const { integrify } = require('integrify');

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

integrify({ config: { functions, db } });

// Automatically replicate attributes from source to target
module.exports.replicateMasterToDetail = integrify({
  rule: 'REPLICATE_ATTRIBUTES',
  source: {
    collection: 'master',
  },
  targets: [
    {
      collection: 'detail1',
      foreignKey: 'masterId',
      attributeMapping: {
        masterField1: 'detail1Field1',
        masterField2: 'detail1Field2',
      },
    },
    {
      collection: 'detail2',
      foreignKey: 'masterId',
      attributeMapping: {
        masterField1: 'detail2Field1',
        masterField3: 'detail2Field3',
      },

      // Optional:
      isCollectionGroup: true, // Replicate into collection group, see more below
    },
  ],

  // Optional:
  hooks: {
    pre: (change, context) => {
      // Code to execute before replicating attributes
      // See: https://firebase.google.com/docs/functions/firestore-events
    },
  },
});

// Automatically delete stale references
module.exports.deleteReferencesToMaster = integrify({
  rule: 'DELETE_REFERENCES',
  source: {
    collection: 'master',
  },
  targets: [
    {
      collection: 'detail1',
      foreignKey: 'masterId',

      // Optional:
      isCollectionGroup: true, // Delete from collection group, see more below
    },
  ],

  // Optional:
  hooks: {
    pre: (snap, context) => {
      // Code to execute before deleting references
      // See: https://firebase.google.com/docs/functions/firestore-events
    },
  },
});

// Automatically maintain count
module.exports.maintainFavoritesCount = integrify({
  rule: 'MAINTAIN_COUNT',
  source: {
    collection: 'favorites',
    foreignKey: 'articleId',
  },
  target: {
    collection: 'articles',
    attribute: 'favoritesCount',
  },
});

Deploy to Firebase by executing:

$ firebase deploy --only functions

Rules File

Alternately, rules can be specified in a file named integrify.rules.js.

// index.js

const { integrify } = require('integrify');

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

integrify({ config: { functions, db } });

// Rules will be loaded from "integrify.rules.js"
module.exports = integrify();
// integrify.rules.js

module.exports = [
  {
    rule: 'REPLICATE_ATTRIBUTES',
    name: 'replicateMasterToDetail',
    // ...
  },
  // ...
];

Collection Groups (isCollectionGroup)

Firestore 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.

Integrify 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.

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:

The 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

For more help, see here.

Extension points exported contracts — how you extend this code

Config (Interface)
(no doc)
src/common.ts
ReplicateAttributesRule (Interface)
(no doc)
src/rules/replicateAttributes.ts
DeleteReferencesRule (Interface)
(no doc)
src/rules/deleteReferences.ts
MaintainCountRule (Interface)
(no doc)
src/rules/maintainCount.ts

Core symbols most depended-on inside this repo

integrify
called by 12
src/index.ts
isObject
called by 2
src/common.ts
isReplicateAttributesRule
called by 2
src/rules/replicateAttributes.ts
isDeleteReferencesRule
called by 2
src/rules/deleteReferences.ts
isMaintainCountRule
called by 2
src/rules/maintainCount.ts
updateCount
called by 2
src/rules/maintainCount.ts
isRule
called by 1
src/common.ts
isConfig
called by 1
src/common.ts

Shape

Function 18
Interface 4
Enum 1

Languages

TypeScript100%

Modules by API surface

src/rules/maintainCount.ts6 symbols
test/main.test.js4 symbols
src/common.ts4 symbols
src/rules/replicateAttributes.ts3 symbols
src/rules/deleteReferences.ts3 symbols
src/index.ts3 symbols

For agents

$ claude mcp add integrify \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact