23 lines
560 B
TypeScript
23 lines
560 B
TypeScript
![]() |
import { decrypt, encrypt } from "../utils/crypt";
|
||
|
import { ValueTransformer } from "typeorm";
|
||
|
|
||
|
interface EncryptedJsonTransformer<T> extends ValueTransformer {
|
||
|
from(dbValue: any): T;
|
||
|
to(entityValue: T): any;
|
||
|
}
|
||
|
|
||
|
export function createEncryptedJsonTransformer<T>(): EncryptedJsonTransformer<T> {
|
||
|
return {
|
||
|
// Database -> Entity
|
||
|
from(dbValue) {
|
||
|
const decrypted = decrypt(dbValue);
|
||
|
return JSON.parse(decrypted) as T;
|
||
|
},
|
||
|
|
||
|
// Entity -> Database
|
||
|
to(entityValue) {
|
||
|
return encrypt(JSON.stringify(entityValue));
|
||
|
},
|
||
|
};
|
||
|
}
|