Server Action calling Server Action question
Question
Application Type
Reactive

If i have a Server Action calling another one. Should I pass the Id as an Input and fetch the data again or set the whole record as an input? What is happening behind the scenes?

Performance-wise, passing the record would almost always be the better solution, since you are saving unecessary calls to the database. Even better if you have a scenario where you only need a few attributes of the record, in that case you can pass a structure only with those fields.

Passing only the id might be better if you predict that the server action may be reused in the future in scenarios where you don't have the full record. So this solution is better in terms of code maintability and reusability.


It all depends on your use case, really.

Hi Tony,

I think it's best that you only pass the necessary values to be used on the second server action instead of passing the whole record.

You may create individual input parameters or create a structure to be used for the input parameter as well.

Thank you and regards,

Chris

If your database doesn't have any major issues and the query to request the data isn't heavy, I'd rather choose to pass only the Id and fetch the data.

mvp_badge
MVP

Hi Tony,


I think it depends on the use case:

  • If you are passing the record, you may be creating a bigger dependency between modules if the two server actions are in the different modules (for instance, if you add an attribute on the record, the consumer server action will need to be updated);
  • If you are passing the id and making a query, it's less performant as you are adding an extra call to the database (this has more importance if it originates on a screen action where the user is "waiting" for the outcome of the action, rather than an action to run asynchronously).


Given that said, I would tend to pick #2, to pass the whole record for performance reasons (one less call to the database) if there are not any architecture, security or maintainability reasons which justify otherwise.


Kind Regards,
João

Performance-wise, passing the record would almost always be the better solution, since you are saving unecessary calls to the database. Even better if you have a scenario where you only need a few attributes of the record, in that case you can pass a structure only with those fields.

Passing only the id might be better if you predict that the server action may be reused in the future in scenarios where you don't have the full record. So this solution is better in terms of code maintability and reusability.


It all depends on your use case, really.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.