Skip to content

Conversation

@michaelprichardson
Copy link
Collaborator

Added feature #1

Can reference the masterId:

source: {
  collection: 'master',
},
targets: [{
    collection: 'detail1',
    foreignKey: 'masterId',
  },
  {
    collection: 'somecoll/{masterId}/detail2',
    foreignKey: '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):

source: {
  collection: 'master',
},
targets: [{
    collection: 'detail1',
    foreignKey: 'masterId',
  },
  {
    collection: 'somecoll/{testId}/detail2',
    foreignKey: 'masterId',
  },
],

context.params,
target.collection
);
target.collection = paramSwap.targetCollection;
Copy link
Member

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

// Replace the snapshot fields in the target collection
const fieldSwap = replaceReferenceWithFields(
snap.data(),
target.collection
Copy link
Member

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?

Comment on lines 73 to 75
} catch (error) {
throw new Error(error);
}
Copy link
Member

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

Comment on lines 76 to 77
collection: 'somecoll/$masterId/detail2',
foreignKey: 'masterId',
Copy link
Member

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?

Copy link
Member

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?

Copy link
Collaborator Author

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?

Copy link
Member

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?

Copy link
Member

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',
       ...
    },

Copy link
Collaborator Author

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)

Copy link
Collaborator Author

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

Copy link
Member

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',
       ...
    },

@michaelprichardson
Copy link
Collaborator Author

Changed the source collection to use:

source: {
  collection: 'users/{userId}',
},
targets: [{
    collection: 'somecoll/$userId/detail2',
    ...
  },
]

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.

@michaelprichardson
Copy link
Collaborator Author

source: {
  collection: 'users/{userId}', <-- Optional now, will default to masterId as before
}

@nbransby nbransby merged commit f9e8130 into master Jul 16, 2020
@michaelprichardson michaelprichardson linked an issue Jul 22, 2020 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dynamic target collection path

3 participants