Skip to content

Commit c1a2074

Browse files
committed
🗃 Enable pgcrypto extension
Signed-off-by: SAMI BETTAYEB <[email protected]>
1 parent 8899f59 commit c1a2074

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

‎lib/migration.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ function mixinMigration(PostgreSQL) {
351351
});
352352
// default extension
353353
if (!createExtensions) {
354-
createExtensions = 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";';
354+
createExtensions = `CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
355+
CREATE EXTENSION IF NOT EXISTS "pgcrypto";`;
355356
}
356357

357358
// Please note IF NOT EXISTS is introduced in postgresql v9.3

‎lib/postgresql.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -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

@@ -589,15 +590,15 @@ PostgreSQL.prototype.buildWhere = function(model, where) {
589590
};
590591

591592
PostgreSQL.prototype.getEncryptionFields = function(modelDefinition) {
592-
if(modelDefinition
593-
&& modelDefinition.settings
594-
&& modelDefinition.settings.mixins
595-
&& modelDefinition.settings.mixins.Encryption
596-
&& modelDefinition.settings.mixins.Encryption.fields){
597-
return modelDefinition.settings.mixins.Encryption.fields
598-
}
599-
return []
600-
}
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+
};
601602

602603
/**
603604
* @private
@@ -617,7 +618,7 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
617618
const self = this;
618619
const props = self.getModelDefinition(model).properties;
619620

620-
const encryptedFields = this.getEncryptionFields(this.getModelDefinition(model))
621+
const encryptedFields = this.getEncryptionFields(this.getModelDefinition(model));
621622
const whereStmts = [];
622623
for (const key in where) {
623624
const stmt = new ParameterizedSQL('', []);
@@ -659,16 +660,16 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
659660
// eslint-disable one-var
660661
let expression = where[key];
661662
let columnName = self.columnEscaped(model, key);
662-
if(encryptedFields.includes(key)){
663+
if (encryptedFields.includes(key)) {
663664
columnName = `convert_from(
664665
decrypt_iv(
665666
DECODE(${key},'hex')::bytea,
666667
decode('${process.env.ENCRYPTION_HEX_KEY}','hex')::bytea,
667-
decode('${process.env.ENCRYPTION_HEX_IV}','hex')::bytea,
668+
decode('${process.env.ENCRYPTION_HEX_IV}','hex')::bytea,
668669
'aes'
669670
),
670671
'utf8'
671-
)`
672+
)`;
672673
}
673674
// eslint-enable one-var
674675
if (expression === null || expression === undefined) {

0 commit comments

Comments
 (0)