Skip to content

Commit

Permalink
fix: save data as json
Browse files Browse the repository at this point in the history
  • Loading branch information
theo committed Dec 7, 2021
1 parent 67906d7 commit 7eff2ac
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,31 @@ export class EncryptionTransformer implements ValueTransformer {
export class JSONEncryptionTransformer implements ValueTransformer {
constructor(private options: EncryptionOptions) {}

public from(value?: string | null | object): Object | undefined {
public from(value?: null | any): any | undefined {
if (!value) {
return;
}

const decrypted = decryptData(
Buffer.from(value as string, 'base64'),
Buffer.from(value.encrypted ?? '' as string, 'base64'),
this.options
).toString('utf8');

return JSON.parse(decrypted);
}

public to(value?: any | FindOperator<any> | null): string | FindOperator<any> | undefined {
public to(value?: any | FindOperator<any> | null): Object | FindOperator<any> | undefined {
if ((value ?? null) === null) {
return;
}

if (typeof value === 'object' && !value?.type) {
return encryptData(
const encrypted = encryptData(
Buffer.from(JSON.stringify(value) as string, 'utf8'),
this.options
).toString('base64');

return { encrypted }
}

if (!value) {
Expand Down

0 comments on commit 7eff2ac

Please sign in to comment.