1- import { Config , Rule , replaceReferenceWithFields } from '../common' ;
1+ import { Config , Rule , replaceReferencesWith , getPrimaryKey } from '../common' ;
22
33export interface DeleteReferencesRule extends Rule {
44 source : {
@@ -31,11 +31,12 @@ export function integrifyDeleteReferences(
3131 ) ;
3232
3333 return functions . firestore
34- . document ( ` ${ rule . source . collection } /{masterId}` )
34+ . document ( rule . source . collection )
3535 . onDelete ( ( snap , context ) => {
36- const masterId = context . params . masterId ;
36+ // Get the last {...} in the source collection
37+ const primaryKey = context . params [ getPrimaryKey ( rule . source . collection ) ] ;
3738 console . log (
38- `integrify: Detected delete in [${ rule . source . collection } ], id [${ masterId } ]`
39+ `integrify: Detected delete in [${ rule . source . collection } ], id [${ primaryKey } ]`
3940 ) ;
4041
4142 // Call "pre" hook if defined
@@ -53,26 +54,21 @@ export function integrifyDeleteReferences(
5354 target . isCollectionGroup ? 'group ' : ''
5455 } [${ target . collection } ] where foreign key [${
5556 target . foreignKey
56- } ] matches [${ masterId } ]`
57+ } ] matches [${ primaryKey } ]`
5758 ) ;
5859
59- try {
60- // Replace the context.params in the target collection
61- const paramSwap = replaceReferenceWithFields (
62- context . params ,
63- target . collection
64- ) ;
65- target . collection = paramSwap . targetCollection ;
60+ // Replace the context.params in the target collection
61+ const paramSwap = replaceReferencesWith (
62+ context . params ,
63+ target . collection
64+ ) ;
6665
67- // Replace the snapshot fields in the target collection
68- const fieldSwap = replaceReferenceWithFields (
69- snap . data ( ) ,
70- target . collection
71- ) ;
72- target . collection = fieldSwap . targetCollection ;
73- } catch ( error ) {
74- throw new Error ( error ) ;
75- }
66+ // Replace the snapshot fields in the target collection
67+ const fieldSwap = replaceReferencesWith (
68+ snap . data ( ) ,
69+ paramSwap . targetCollection
70+ ) ;
71+ target . collection = fieldSwap . targetCollection ;
7672
7773 // Delete all docs in this target corresponding to deleted master doc
7874 let whereable = null ;
@@ -84,7 +80,7 @@ export function integrifyDeleteReferences(
8480
8581 promises . push (
8682 whereable
87- . where ( target . foreignKey , '==' , masterId )
83+ . where ( target . foreignKey , '==' , primaryKey )
8884 . get ( )
8985 . then ( querySnap => {
9086 querySnap . forEach ( doc => {
0 commit comments