Skip to content

Commit

Permalink
Merge pull request #45 from clay-run/master
Browse files Browse the repository at this point in the history
Feature: Adds support for JSON Encryption
  • Loading branch information
generalpiston authored May 27, 2022
2 parents d6567cc + 21f88fb commit 45558d1
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,42 @@ export class EncryptionTransformer implements ValueTransformer {
}
}
}

export class JSONEncryptionTransformer implements ValueTransformer {
constructor(private options: EncryptionOptions) {}

public from(value?: null | any): any | undefined {
if (!value || !value.encrypted) {
return;
}

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

return JSON.parse(decrypted);
}

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

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

return { encrypted }
}

if (!value) {
return;
}

// FindOperators are not supported.
throw new Error('Filter operators are not supported for JSON encrypted fields');
}
}

0 comments on commit 45558d1

Please sign in to comment.