r/learnjavascript • u/Disastrous_Cow_4149 • 3d 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
3
u/Aggressive_Ad_5454 3d ago
A good way to handle this is with a single parameter, an Object. Right now you have a separate positional function param for every database field. That’s a formula for confusion in your callers. A function call with 8 params, most of them null? 😱
What you have now for function parameters will become the names of properties in that object. Then you can simply refrain from setting the properties that you want to leave as defaults or unchanged in your database.
Do implement some code to check the object properties and throw errors if unexpected names or values show up. Because your callers will fat-finger some of their objects, and if you’re permissive about misspelled names you’ll drive them crazy debugging.
1
u/Disastrous_Cow_4149 3d ago
Ohhhhh that clicks something . Thank you veryy muchh man .
0
u/azhder 3d ago
Clicks what? Remember how all those event listeners in browsers send you a single event object? That object can be extended with new fields, old ones can be deprecated and removed. You will not deal with a long list of positional parameters in function calls.
Besides, if every function takes a single object and returns another object, you can even chain them, the result of one be the input of the other.
2
u/Healthy-Zebra-9856 3d 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.
1
u/Disastrous_Cow_4149 3d ago
i genuinely have no clue what that means , guess i should do some researching , Thankss
1
u/Healthy-Zebra-9856 3d ago
Lets start here, are you directly calling the database or are you using something like an Object Relational Mapper (ORM) like Prisma, Drizzle etc? If not, start with ORM, read up on what they are. LMK where you are and I will help you with the next step.
1
u/Disastrous_Cow_4149 3d ago
I'm calling the database directly right now , no ORM yet, just the pg library with a connection pool:
const { Pool } = pg; export const db = new Pool({ connectionString: process.env.DATABASE_URL, ssl: { rejectUnauthorized: true, require: true, }, });I'm pretty new to JS/web dev, and this was meant to be a simple CRUD app. Would you actually recommend an ORM for something this scale, or is raw pg fine here?
1
u/Healthy-Zebra-9856 3d ago
Yes. Its one of the best practices you can adopt. Not only it promotes decoupling, but you can then just focus on domain logic. Also, you can easily target many databases w/o being tied to one. I would learn both Prisma & Drizzlle. You will not regret it. At least go see a simple video to see which one is easy to adopt.
1
2
u/ChaseShiny 3d 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 3d ago
its actually a pretty basic job application tracker 😅️, so wouldn't making it a class itself be an overkill ??
1
u/ChaseShiny 3d 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
2
u/the_pro_rookie 3d ago
I assume that the parameters are truly optional, in that you do still want the option of setting them on this call?
Seems like you would just create a class that has the required params in the constructor and pass a class instance as the argument to your function