Skip to content

Instantly share code, notes, and snippets.

@mattbajorek
Last active April 11, 2023 18:37
Show Gist options
  • Save mattbajorek/16d5a2f3ca3c36b14f3b47b417d4df7b to your computer and use it in GitHub Desktop.
Save mattbajorek/16d5a2f3ca3c36b14f3b47b417d4df7b to your computer and use it in GitHub Desktop.
Decimal transformer for typeorm
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