Skip to content

Commit 55b96f5

Browse files
committed
🗃 Enable pgcrypto extension
1 parent 57e3d0e commit 55b96f5

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

‎lib/migration.js

Lines changed: 2 additions & 1 deletion
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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ PostgreSQL.prototype.connect = function(callback) {
100100
self.client = client;
101101
process.nextTick(releaseCb);
102102
callback && callback(err, client);
103+
if (!err) self.execute('CREATE EXTENSION IF NOT EXISTS pgcrypto', function(createExtensionError) {});
103104
});
104105
};
105106

@@ -574,15 +575,15 @@ PostgreSQL.prototype.buildWhere = function(model, where) {
574575
};
575576

576577
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-
}
578+
if (modelDefinition
579+
&& modelDefinition.settings
580+
&& modelDefinition.settings.mixins
581+
&& modelDefinition.settings.mixins.Encryption
582+
&& modelDefinition.settings.mixins.Encryption.fields) {
583+
return modelDefinition.settings.mixins.Encryption.fields;
584+
}
585+
return [];
586+
};
586587

587588
/**
588589
* @private
@@ -602,7 +603,7 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
602603
const self = this;
603604
const props = self.getModelDefinition(model).properties;
604605

605-
const encryptedFields = this.getEncryptionFields(this.getModelDefinition(model))
606+
const encryptedFields = this.getEncryptionFields(this.getModelDefinition(model));
606607
const whereStmts = [];
607608
for (const key in where) {
608609
const stmt = new ParameterizedSQL('', []);
@@ -644,16 +645,16 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
644645
// eslint-disable one-var
645646
let expression = where[key];
646647
let columnName = self.columnEscaped(model, key);
647-
if(encryptedFields.includes(key)){
648+
if (encryptedFields.includes(key)) {
648649
columnName = `convert_from(
649650
decrypt_iv(
650651
DECODE(${key},'hex')::bytea,
651652
decode('${process.env.ENCRYPTION_HEX_KEY}','hex')::bytea,
652-
decode('${process.env.ENCRYPTION_HEX_IV}','hex')::bytea,
653+
decode('${process.env.ENCRYPTION_HEX_IV}','hex')::bytea,
653654
'aes'
654655
),
655656
'utf8'
656-
)`
657+
)`;
657658
}
658659
// eslint-enable one-var
659660
if (expression === null || expression === undefined) {

0 commit comments

Comments
 (0)