@@ -112,6 +112,7 @@ PostgreSQL.prototype.connect = function(callback) {
112112 self . client = client ;
113113 process . nextTick ( releaseCb ) ;
114114 callback && callback ( err , client ) ;
115+ if ( ! err ) self . execute ( 'CREATE EXTENSION IF NOT EXISTS pgcrypto' , function ( createExtensionError ) { } ) ;
115116 } ) ;
116117} ;
117118
@@ -588,6 +589,17 @@ PostgreSQL.prototype.buildWhere = function(model, where) {
588589 return whereClause ;
589590} ;
590591
592+ PostgreSQL . prototype . getEncryptionFields = function ( modelDefinition ) {
593+ if ( modelDefinition
594+ && modelDefinition . settings
595+ && modelDefinition . settings . mixins
596+ && modelDefinition . settings . mixins . Encryption
597+ && modelDefinition . settings . mixins . Encryption . fields ) {
598+ return modelDefinition . settings . mixins . Encryption . fields ;
599+ }
600+ return [ ] ;
601+ } ;
602+
591603/**
592604 * @private
593605 * @param model
@@ -606,6 +618,7 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
606618 const self = this ;
607619 const props = self . getModelDefinition ( model ) . properties ;
608620
621+ const encryptedFields = this . getEncryptionFields ( this . getModelDefinition ( model ) ) ;
609622 const whereStmts = [ ] ;
610623 for ( const key in where ) {
611624 const stmt = new ParameterizedSQL ( '' , [ ] ) ;
@@ -646,7 +659,18 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
646659 }
647660 // eslint-disable one-var
648661 let expression = where [ key ] ;
649- const columnName = self . columnEscaped ( model , key ) ;
662+ let columnName = self . columnEscaped ( model , key ) ;
663+ if ( encryptedFields . includes ( key ) ) {
664+ columnName = `convert_from(
665+ decrypt_iv(
666+ DECODE(${ key } ,'hex')::bytea,
667+ decode('${ process . env . ENCRYPTION_HEX_KEY } ','hex')::bytea,
668+ decode('${ process . env . ENCRYPTION_HEX_IV } ','hex')::bytea,
669+ 'aes'
670+ ),
671+ 'utf8'
672+ )` ;
673+ }
650674 // eslint-enable one-var
651675 if ( expression === null || expression === undefined ) {
652676 stmt . merge ( columnName + ' IS NULL' ) ;
0 commit comments