-
Notifications
You must be signed in to change notification settings - Fork 0
feat(deleteReferences): Add dynamic parameters #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/rules/deleteReferences.ts
Outdated
| context.params, | ||
| target.collection | ||
| ); | ||
| target.collection = paramSwap.targetCollection; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this confused me for a minute would it be better to omit this...
src/rules/deleteReferences.ts
Outdated
| // Replace the snapshot fields in the target collection | ||
| const fieldSwap = replaceReferenceWithFields( | ||
| snap.data(), | ||
| target.collection |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...and use paramSwap.targetCollection here?
src/rules/deleteReferences.ts
Outdated
| } catch (error) { | ||
| throw new Error(error); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no point of catching an error and simply throwing it again
test/functions/index.js
Outdated
| collection: 'somecoll/$masterId/detail2', | ||
| foreignKey: 'masterId', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this example should the detail2 collection have a masterId field as its already in the path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems strange to use a the variable $masterId which is not defined in the source path - where is it coming from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The masterId is the id of the source collection. It would come from the path though. You think it would be easier to understand if we used {...} for path variables and $... for field variables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it comes from the source path where is it defined, shouldnt $masterId in the target throw an error because the source path doesnt define the variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is masterId a magic var created from 'Id' appended on collection name?
in the security rules syntax you would include the id in the source path if you wanted to reference it the rule:
match /users/{uid} { ...
Maybe we can do the same here?
source: {
collection: 'master/{masterId}',
},
targets: [
{
collection: 'somecoll/$masterId/detail2',
...
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const paramSwap = replaceReferenceWithFields(context.params, target.collection);
This line replaces the $... with the params, which masterId is part of. The next replaceReferenceWithFields replaces the $.. with the fields. (Looking at it now the function name could be changed to some like replaceReferenceWith(...) to read better)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it has just been defined for us already and they chose masterId (line 34 in deleteReference.ts). We could use collection: 'master/{masterId}' to make it easier to understand too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so its a secret magic var caleld masterId! Really confusing as docs dont state this and their example collection is called master so you might conclude it comes from the collection name.
Ok would make a lot of sense to require it to be defined in the source path and respects the name give to it
source: {
collection: 'users/{userId}',
},
targets: [
{
collection: 'somecoll/$userId/detail2',
...
},
|
Changed the source collection to use: Added error checks for a missing primary key in the source collection. Added test for new functions and to test when primary key is left out from the source collection. |
|
Added feature #1
Can reference the masterId:
Can reference the snapshot fields from the source collection (The keys of the field need to match the {} in the target otherwise it will skip it):