@@ -573,6 +573,17 @@ PostgreSQL.prototype.buildWhere = function(model, where) {
573573 return whereClause ;
574574} ;
575575
576+ PostgreSQL . prototype . getEncryptionFields = function ( modelDefinition ) {
577+ if ( modelDefinition
578+ && modelDefinition . settings
579+ && modelDefinition . settings . mixins
580+ && modelDefinition . settings . mixins . Encryption
581+ && modelDefinition . settings . mixins . Encryption . fields ) {
582+ return modelDefinition . settings . mixins . Encryption . fields
583+ }
584+ return [ ]
585+ }
586+
576587/**
577588 * @private
578589 * @param model
@@ -591,6 +602,7 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
591602 const self = this ;
592603 const props = self . getModelDefinition ( model ) . properties ;
593604
605+ const encryptedFields = this . getEncryptionFields ( this . getModelDefinition ( model ) )
594606 const whereStmts = [ ] ;
595607 for ( const key in where ) {
596608 const stmt = new ParameterizedSQL ( '' , [ ] ) ;
@@ -631,7 +643,18 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
631643 }
632644 // eslint-disable one-var
633645 let expression = where [ key ] ;
634- const columnName = self . columnEscaped ( model , key ) ;
646+ let columnName = self . columnEscaped ( model , key ) ;
647+ if ( encryptedFields . includes ( key ) ) {
648+ columnName = `convert_from(
649+ decrypt_iv(
650+ DECODE(${ key } ,'hex')::bytea,
651+ decode('${ process . env . ENCRYPTION_HEX_KEY } ','hex')::bytea,
652+ decode('${ process . env . ENCRYPTION_HEX_IV } ','hex')::bytea,
653+ 'aes'
654+ ),
655+ 'utf8'
656+ )`
657+ }
635658 // eslint-enable one-var
636659 if ( expression === null || expression === undefined ) {
637660 stmt . merge ( columnName + ' IS NULL' ) ;
0 commit comments