Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): trying typeorm's value transformers
- Loading branch information
Showing
5 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
apps/api/src/app/shared/transformers/big-number.transformer.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// import { ValueTransformer } from 'typeorm' | ||
// import { BigNumber } from 'bignumber.js' | ||
// | ||
// export class BigNumberTransformer implements ValueTransformer { | ||
// | ||
// to(value: BigNumber): string | undefined { | ||
// if (!value) return undefined | ||
// return value.toString(10) | ||
// } | ||
// | ||
// from(value: string): BigNumber | undefined { | ||
// if (!value) return undefined | ||
// return new BigNumber(value, 10) | ||
// } | ||
// | ||
// } |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ValueTransformer } from 'typeorm'; | ||
|
||
export class BoardTransformer implements ValueTransformer { | ||
// Store the board as a stringified 3d string array, | ||
// Access the board as a parsed 3d array. | ||
to(board: string[][]): string { | ||
return JSON.stringify(board); | ||
} | ||
|
||
from(stringified: string): string[] { | ||
return JSON.parse(stringified); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { BoardTransformer } from './board.transformer'; | ||
export { LowerCaseTransformer } from './lower-case.transformer'; | ||
export { PasswordTransformer } from './password.transformer'; |
10 changes: 10 additions & 0 deletions
10
apps/api/src/app/shared/transformers/lower-case.transformer.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ValueTransformer } from 'typeorm'; | ||
|
||
export class LowerCaseTransformer implements ValueTransformer { | ||
to(entityValue: string) { | ||
return entityValue.toLocaleLowerCase(); | ||
} | ||
from(databaseValue: string) { | ||
return databaseValue; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
apps/api/src/app/shared/transformers/password.transformer.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as crypto from 'crypto'; | ||
import { ValueTransformer } from 'typeorm'; | ||
|
||
export class PasswordTransformer implements ValueTransformer { | ||
to(value) { | ||
return crypto.createHmac('sha256', value).digest('hex'); | ||
} | ||
from(value) { | ||
return value; | ||
} | ||
} |