• Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🀝 Enforce referential and data integrity in Cloud Firestore using triggers

πš’πš—πšπšŽπšπš›πš’πšπš’

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.

More Repositories

1

realworld-dynamodb-lambda

Ξ» serverless backend implementation for RealWorld using AWS DynamoDB + Lambda
JavaScript
240
star
2

webgif

Easily generate animated GIFs from websites
JavaScript
105
star
3

playwright-test-coverage

Extends Playwright test to measure code coverage
JavaScript
79
star
4

realworld-e2e-test

End-to-end testing for a RealWorld React/NodeJS stack using Mocha and Chrome Puppeteer
JavaScript
70
star
5

realworld-nuxt

Nuxt.js implementation of RealWorld Frontend
Vue
46
star
6

playwright-test-coverage-demo

Demonstrates usage of playwright-test-coverage - a Playwright extension that collects code coverage from running end-to-end tests
JavaScript
18
star
7

realworld-firebase-gcp-cloud-functions

A Firebase + GCP Cloud Functions backend implementation for RealWorld
JavaScript
7
star
8

puppeteer-on-cloud-functions

Run Chrome Puppeteer on Google Cloud Functions
JavaScript
6
star
9

docusaurus-base

Netlify template for Docusaurus
JavaScript
4
star
10

nodejs-in-matlab

Call Node.js from within MATLAB
MATLAB
4
star
11

realworld-gcp-datastore-cloud-functions

Serverless GCP Cloud Functions + Datastore implementation of RealWorld Backend
JavaScript
3
star
12

vuepress-base

Starter template for VuePress
3
star
13

dotnet-minimal-api-example

ASP.NET Minimal API Tutorial with Postman tests and code coverage
C#
3
star
14

circleci-test-drive

2
star
15

travis-build-history

JavaScript
2
star
16

deno-api-tests-coverage

Measure code coverage of a Deno server when running Postman (newman) API tests
TypeScript
1
star
17

netlify-edge-functions-test

TypeScript
1
star
18

realworld-jasonette

A Jasonette (Android/iOS) implementation of the RealWorld frontend
1
star
19

deno-test

JavaScript
1
star
20

vuepress-test

1
star
21

vercel-test

JavaScript
1
star
22

travis-ci-test-drive

JavaScript
1
star
23

vitepress-starter

Starter repo for VueJS Vitepress documentation generator
1
star
24

firestore-timestamps

JavaScript
1
star
25

udd-action-test

1
star
26

deno-hello

The simplest possible Deno module
Dockerfile
1
star
27

meticulous-test

TypeScript
1
star