-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DTO for users table for example 5 #29
Comments
Interesting idea! Do you think you could edit the description above to also include 2 code blocks for "Before" and "After"? I would be interested to see what you mean from a code perspective. |
so we could do something like this for the user object/class in example 5:
I'm just having some trouble with the handling of the multiple rows.... |
Looks complex... what's the benefit? (and how would you explain this benefit to beginners?) Usually we try to avoid any layers of indirection unless they are absolutely necessary. |
The idea is, that the DTO more or less defines the structure of your API (like an API contract).
but I also think about skipping this part. |
Hm... not sure how a DTO by itself would prevent exposure of an API key like this: security-vulnerability-examples-next-js-postgres/pages/example-5-secrets-exposure/vulnerable.tsx Lines 31 to 41 in ed88e44
In this case our A DTO by itself probably doesn't inherently increase security, if you were to have the secret data defined in that DTO accidentally (like in this I'm also similar skeptical about the value of a DTO in keeping public-facing APIs stable over time (if you change your internal data structure, then you may similarly mess up exposing it properly via the DTO). I've gone this route of abstraction before, but in the last years, I've been writing my code simpler and with stronger guarantees like full-stack type safety, and it seems to work pretty well to avoid these problems. |
Didn't talk about the API key, but about the password-hash contained in the user-objects in line 38. DTOs don't help you with the whole API-key problem, because this is actually a generic design issue (in my opinion) if you need an API key for a backend-service in the frontend. But I absolutely get your point regarding the additional complexity... Let's skip this whole DTO thing for now... This is maybe a little bit too complex/controversial for such a short beginners course. |
Ok, I guess my point is similar with the users with password hash though: if the DTO ( Instead of adding these extra abstractions, another (simpler) solution would be on the type level in the database file: export type User = {
id: number;
username: string;
+ passwordHash: never;
};
-export type UserWithPasswordHash = User & {
+export type UserWithPasswordHash = Omit<User, 'passwordHash'> & {
passwordHash: string;
}; Then your code doesn't even compile if a user object has a That could be added as part of one of the existing solutions, or added as a new solution too. |
at the moment, different SQL-Queries are used to either include or exclude the user pwd hash.
Best practice would be to use a DTO to make unintentional mistakes less common
The text was updated successfully, but these errors were encountered: