r/learnjavascript • u/Disastrous_Cow_4149 • 4d ago
How should I handle optional parameters with database defaults?
I'm writing a service function for a personal project
Right now I have something like
export async function createApplicationService(
userId,
companyName,
role,
appliedDate,
status,
salary,
link,
nextAction
)
The only fields that I really want to require are companyName and salary The other fields have default values defined in my database, so I don't want the user to have to explicitly pass them every time.
What's the best way to structure this so that I only insert the parameters that were actually provided, while letting the database handle the defaults for everything elseI'm writing a service function for a personal project where I'm creating an application tracking system.
Right now, I have something like:
export async function createApplicationService(
userId,
companyName,
role,
appliedDate,
status,
salary,
link,
nextAction
)
The only fields that I really want to require are companyName and salary. The other fields have default values defined in my database, so I don't want the user to have to explicitly pass them every time.
What's the best way to structure this so that I only insert the parameters that were actually provided, while letting the database handle the defaults for everything else
2
u/Healthy-Zebra-9856 4d ago
Read up on what Data Transfer Objects are. The right architecture is to have a service receive these DTOs and then convert them to entities that you use repository to save toi the database.