Best practice how to store settings in MySQL?
Hi. I would like to store some settings / preferences in the database, but don't know what's the common structure, to do so.
Would it be something like so?
33 Replies
But don't know, how to make ZOD validator for each value variable, so I guess I have to create mutation with its own validator for each field
Bruh that seems complex
Does it need to be in db?
Some settings work better as an env variable
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I know it is, but some variables have to be customizable by user when the site's running, not at the build time (like ENV).
Good point. How would you structure the model? Should I just have one row with all settings inside that one Json object?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I would start out with one json column in the database. Ensure type safety with zod.
As soon as you feel the need to write that parsing logic again extract that into a helper function
make sure to reuse the helper across your app
in case you migrate the schema, you can add falbbacks / defaults here
Can I ensure type-safety for that JSON object with Prima? I did some googling, but couldnt find any good resource
https://github.com/prisma/prisma/issues/3219
no you cannot, rely on zod for runtime safety - make sure to add fallbacks
This is kinda out of my scope of knowledge. What are zod fallbacks?
zod is a library to parse input and retrieve type-safety thereby: https://github.com/colinhacks/zod
GitHub
GitHub - colinhacks/zod: TypeScript-first schema validation with st...
TypeScript-first schema validation with static type inference - GitHub - colinhacks/zod: TypeScript-first schema validation with static type inference
Yeah ik what zod does, but I got confused by the word "fallback"
really useful for developing api's - by parsing the incomming data you avoid all kinds of security breaches
So should I do for each setting field its own route with zod validator for
key
and its value
?lets say your current settings json looks like this:
{
wantsNotifications: boolean
}
now you want to add a setting, your schema would look like this:
{
wantsNotifications: boolean
useColorBlindMode: boolean
}
In your helper function you would add a fallback for the "useColorBlindMode" property -> it should default to false when it is undefined
use z.boolean().default(false) for this
Yes and would you recommend me to control the properties that are parsed in the object?