-
Notifications
You must be signed in to change notification settings - Fork 0
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
[Backend] Add feature: manage subscriptions #11
Conversation
5c544a3
to
4b01649
Compare
id Int @id @default(autoincrement()) | ||
name String | ||
image String | ||
createdAt DateTime @default(now()) @map("created_at") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why we name these with camelCase and map them to a snakeCase? Similar to how we do with DTO's in Klarna to validate when data is confirmed to be type safe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof nice catch! This is because databases usually has a different naming convention (plural and snake_case) from JS naming convention (singular and camelCase) and we want both to coexist.
This is described in more details in this Prisma doc. Schema file gets a little bit more verbose but code and database is standardized in a nice way
In short, in the left you read as field names while coding (client generated by Prisma) and on the right (the @Map field) is how you will see in the database tables
controllers: [AppController], | ||
providers: [ | ||
Logger, | ||
AppService, | ||
{ | ||
provide: APP_PIPE, | ||
useClass: ZodValidationPipe | ||
}, | ||
{ | ||
provide: APP_INTERCEPTOR, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what we're doing here is basically addding a validation middleware of the incoming payload? Or?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
incoming payload validation would be the block above (APP_PIPE
and ZodValidationPipe
), this one is about how you want response payload to be shown
import { Request, Response } from 'express' | ||
|
||
@Catch() | ||
export class HttpExceptionFilter implements ExceptionFilter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if I understood correctly this is an error handling middleware?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Precisely! The framework has a nice built-in global error handler already, but it was omitting errors from Zod schemas. I had no idea what was failing so I wrote another one myself, just to console.log
the error
In the end, it helped me to identify that I was typing .optional()
instead of .nullable()
Description
/subscriptions
RESTful resourcesvolta
can fetch the correct tools from monorepoMotivation and Context
Unit testing with automock
We no longer need to recreate the app module during unit tests anymore (see here). Easier to mock and write tests. You can still retrieve any dependency with
unit
andunitRef
(official doc)Zod serializer
You can think of this as the Presenter layer of the Clean Architecture. In short, neither controller nor services should be in charge of presenting the response payload to clients. Instead, we introduce tools to make it easier to present the response the way we need, hence the Serializers.
NestJS has a builtin for that as well (doc), but since we are using Zod and it has support for serializing, we can stick to that lib
How Has This Been Tested?
Screenshots (if appropriate):
Types of changes
Checklist: