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•3y 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•3y 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?
can you exlain what you mean more precisely?
One sec, I will create a demo.
As I've check the docs about JSON objects in Prisma, the manipulation sucks. Adding new properties to existing object is nearly impossible, or am I wrong?
very limited atleast
you can push to json arrays
that can useful
but for regular data where super performant code is unneccesary i would rely on first querying the database for the curent JSON, modify it and store it again the database.
But there's no other way than just getting the object first, editing it and then saving the edited object, right?
Reqires one more db call than absolutely neccessary, but is way more declarative
For arrays there is
when the performance implication does not allow for this use seperate columns for every setting , that needs to be changed often
This is not the case really. Data won't be updated often. I was just curious 😄
perfectly, that sounds like the use case the json column was intended for - in modern relational databases 👍🏼
According to your tips, this is what I created. I am not sure if I understood fully to your thought, but feedback would be appreciated
looks good to me!
highly recommended: use immer to update the settings json - makes your life a lot easier
GitHub
GitHub - immerjs/immer: Create the next immutable state by mutating...
Create the next immutable state by mutating the current one - GitHub - immerjs/immer: Create the next immutable state by mutating the current one
One last recommendation: insteaf of storing all such settings in an own settings table, add a settings column to the specific table (for user settings the user table would be right)
Yeah, I wanted to do it like so, but can't think of any suitable table to add this column to
in that case I would tend to create a new table called discordSettings / dashboardSettings for storing this specific data format. it can still be easily extended with additional columns.
just remember to add default values when you add new columns - else your prisma migration will fail with a unqiue constraint error. Been there a few times 😄
Good point. As long as the input field for the mutation is required, this shouldn't happen
and when it happens the good point is that you atleast exactly now when & where a logic error was introduced - 100x nicer than finding these bugs weeks to months in with almost no sane method to debug
done ✅
I wish you continued maximum success with the project
Thank you for your help & time. 🥰