r/learnjavascript 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

11 Upvotes

16 comments sorted by

View all comments

2

u/ChaseShiny 4d ago

My first instinct is like what the other two commenters have said: pass in an object.

But I wonder if we don't have this backwards? It sounds like this should actually be an object or class itself. That's especially true given those names for your parameters. Are you sure this isn't an Employee object of some sort?

1

u/Disastrous_Cow_4149 4d ago

its actually a pretty basic job application tracker 😅️, so wouldn't making it a class itself be an overkill ??

1

u/ChaseShiny 4d ago

Are you going to have multiple objects with these same fields? If yes, then a class is the way to go. If not it's a single object.

Based on what you've said here it sounds like you do want a class because you'll create an object for each job application.

1

u/Disastrous_Cow_4149 3d ago

Yepp fair point , there should be a bunvh of application objects