-
Notifications
You must be signed in to change notification settings - Fork 29
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
Decimal inconsistency #63
Comments
Actually, I haven't yet used Decimal types in my own projects. With regards to the docs and this comment, Prisma uses Decimal.js library under the hood and returns a Decimal.js object when querying the database, which will be serialized to a string value (otherwise, it will loose its precision). So, the generated DTO is wrong concerning the ApiProperty type, which should be To solve your issue, you may need to ask yourself if you really need |
The type definition for |
* brakebein/main: version 1.24.1 fix Decimal type definition Brakebein#63 version 1.24.0 update readme deprecate outputApiPropertyType fix @DtoRelationRequired Brakebein#54 change DtoApiOverrideType to DtoOverrideApiPropertyType Brakebein#54 fix encapsulateString for null Brakebein#58 fix encapsulateString for date strings Brakebein#58 add @DtoCreateRequired decorator Brakebein#55 replace DtoCastType with DtoOverrrideType Brakebein#54 fix don't generate enums.ts if there are no enums defined add flag to show fields with default attribute by default Brakebein#51 add enumName to ApiProperty Brakebein#52 generate enums if noDependencies = true Brakebein#48 wrap relations as type to fix circular dependencies Brakebein#49 shorten duplicate code checks on double imports sort all imports shorten duplicate code to make class-validator imports fix ApiProperty default value if field is a list
@Brakebein IMHO this is a valid use case. |
Well, for the CreateDto and UpdateDto, the type could actually be For the response, when just returning the results from the query, the Decimal value gets serialized as string. If it should be returned as number, the result would need to be mapped. For example: const restult = await this.prisma.product.findUnique(...);
return {
...result,
price: result.price.toNumber(),
}; The type in the generated files could be overriden using @ctjhoa How do you imagine the "feature" you are talking of? And regarding "exposing the internal tool": Decimal.js is of course not exposed to the outside (the consumers of your API), since only serialized values are transmitted. However, it's also not only used by Prisma as an internal tool, since a Decimal.js instance is returned when querying the database. Here, it needs to be handled appropriately to your needs (as stated above). Correct me, if I'm missing something. |
Thanks for the reply @Brakebein
I was thinking only of request DTOs (like CreateDto and UpdateDto), as in the original message. I was reacting to the fact that the type definition for Decimal fields is forced to be the type 'string' (which I consider to be a Decimal.js design choice). I also wasn't aware of
I was thinking more of a global parameter to control how decimals should be typed in the generated DTOs. For example: generator nestjsDto {
provider = "prisma-generator-nestjs-dto"
output = "../src/generated/nestjs-dto"
decimalsType = "number" | "string"
} Using |
Additionally, What should the output look like? Currently, the following is generated: export class ProductDto {
@ApiProperty({
type: 'string',
format: 'Decimal.js',
})
price!: Prisma.Decimal;
}
export class CreateProductDto {
@ApiProperty({
type: 'string',
format: 'Decimal.js',
})
@IsNotEmpty()
@IsDecimal()
price!: Prisma.Decimal;
} I agree that So for export class ProductDto {
@ApiProperty({
type: 'number',
format: 'float',
})
price!: number;
}
export class CreateProductDto {
@ApiProperty({
type: 'number',
format: float',
})
@IsNotEmpty()
@IsNumber()
price!: number;
} For export class ProductDto {
@ApiProperty({
type: 'string',
format: 'float',
})
price!: string;
}
export class CreateProductDto {
@ApiProperty({
type: 'string',
format: 'float',
})
@IsNotEmpty()
@IsDecimal()
price!: string;
} In both cases, it would need something like below, because the Prisma query returns a Decimal.js instance: getProduct(): Promise<ProductDto> {
const restult = await this.prisma.product.findUnique(...);
return {
...result,
price: result.price.toNumber(), // or .toString()
};
} Maybe, we add So, what do you think? |
Prisma model
The Create DTO looks like
The swagger example
So far so good. It means that I have to provide numeric value for these fields. However when calling the api I need to provide these decimals as string. such as
"PRIJS": "2.01"
to make it work. If is specify"PRIJS": 2.01
, the error message "PRIJS is not a valid decimal number."Also for the output the decimal is provided as string. So the interface implementation and the definition are inconsistent. I prefer to work with numbers instead of string. How can I solve this ?
The text was updated successfully, but these errors were encountered: