Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anishkny/integrify
π€ Enforce referential and data integrity in Cloud Firestore using triggers
https://github.com/anishkny/integrify
data-integrity firebase firebase-functions firebase-trigger firestore referential-integrity serverless
Last synced: about 18 hours ago
JSON representation
π€ Enforce referential and data integrity in Cloud Firestore using triggers
- Host: GitHub
- URL: https://github.com/anishkny/integrify
- Owner: anishkny
- License: mit
- Created: 2019-01-18T17:32:46.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-08-30T00:41:24.000Z (5 months ago)
- Last Synced: 2024-10-13T22:28:43.904Z (3 months ago)
- Topics: data-integrity, firebase, firebase-functions, firebase-trigger, firestore, referential-integrity, serverless
- Language: TypeScript
- Homepage: https://firebaseopensource.com/projects/anishkny/integrify
- Size: 238 KB
- Stars: 109
- Watchers: 8
- Forks: 9
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-firebase - Integrify - 미리 μ€λΉλ Cloud Functions νΈλ¦¬κ±°λ₯Ό μ¬μ©νμ¬ Firestoreμμ μ°Έμ‘° λ° λ°μ΄ν° 무결μ±μ μνν©λλ€. (μλ² μ¬μ΄λ (Cloud Functions, BigQuery λ±) / iOS)
README
# πππππππππ’
[![Build & 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)
[![Code Coverage Status](https://codecov.io/gh/anishkny/integrify/branch/main/graph/badge.svg)](https://codecov.io/gh/anishkny/integrify)
[![npm package](https://img.shields.io/npm/v/integrify.svg)](https://www.npmjs.com/package/integrify)
[![Mentioned in Awesome Firebase](https://awesome.re/mentioned-badge.svg)](https://github.com/jthegedus/awesome-firebase)
[![Firebase Open Source](https://img.shields.io/badge/β€-Firebase_Open_Source-FFCA28)](https://firebaseopensource.com/projects/anishkny/integrify/)π€ Enforce referential and data integrity in [Cloud Firestore](https://firebase.google.com/docs/firestore/) using [triggers](https://firebase.google.com/docs/functions/firestore-events)
[Introductory blog post](https://dev.to/anishkny/---firestore-referential-integrity-via-triggers-kpb)
## Usage
```js
// index.jsconst { 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:
```bash
$ firebase deploy --only functions
```### Rules File
Alternately, rules can be specified in a file named `integrify.rules.js`.
```js
// index.jsconst { 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();
``````js
// integrify.rules.jsmodule.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](https://firebase.google.com/docs/firestore/query-data/queries#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](https://firebase.google.com/docs/firestore/query-data/indexing).