Record Creation Override with Relationships

When creating a new record within a collection where records have a HasMany relationship, FA makes two API calls. The first is a POST forest/{modelName} which is followed by POST /forest/{modelName}/{id}/relationships/{hasManyRelationName}.

In our implementation, it is necessary for us to perform some complex validation on the record before insertion, as well as asynchronous, post-insertion logic. The validation and logic require us to know the relation between the new record and its HasMany relation. For example, if we are creating an EmailAlias, we need to know what User that the new record will be associated with before the EmailAlias record creation.

When overriding the POST forest/{modelName} route, the related model (User in the above example) data are not made available in the request. The only time that the related model ID is made available is during the subsequent POST to /forest/{modelName}/{id}/relationships/{hasManyRelationName}.

Since we need to override the record creation, can FA make the relationship data available somewhere in the request? It could be a parameter outside of req.body.data as to not break any existing functionality, yet support integrations such as ours.

Maybe there’s another way to access this data in the route override that I’m missing. Would love to know what you think about this. Thanks!

Hi @yeti182 :wave: welcome to our community.
Unfortunately it’s not possible today.
I will push this request on our product board.
Thank you to your feedback.

Hello @yeti182 and anyone reading this thread.

It is actually possible to retrieve the parent record id while creating a record through an association. When you override your POST route, a specific header called forest-context-url can be used to access the current location URL from the user browser.

The URL is constructed like so:
https://app.forestadmin.com/[project]/[environment]/[team]/data/[parentCollectionName]/index/record/[parentCollectionName]/[parentRecordId]/has-many/[parentCollectionName]-[associationName]/create

So you can do the following to retrieve the parent record id in your route code:
const parentRecordId = request.hearders['forest-context-url'].split('/')[11]

I hope this can help you in the future !

Steve.

1 Like