Last active
April 11, 2023 18:37
-
-
Save mattbajorek/16d5a2f3ca3c36b14f3b47b417d4df7b to your computer and use it in GitHub Desktop.
Decimal transformer for typeorm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Decimal from 'decimal.js'; | |
import { ValueTransformer } from 'typeorm'; | |
export class DecimalTransformer implements ValueTransformer { | |
/** | |
* Used to marshal Decimal when writing to the database. | |
*/ | |
to(decimal?: Decimal): string | null { | |
return decimal?.toString(); | |
} | |
/** | |
* Used to unmarshal Decimal when reading from the database. | |
*/ | |
from(decimal?: string): Decimal | null { | |
return decimal ? new Decimal(decimal) : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment