Skip to content

Commit dcc2d96

Browse files
SAMIBETTAYEBachrinza
authored andcommitted
🗃 Enable pgcrypto extension
Signed-off-by: SAMI BETTAYEB <sami3639@gmail.com>
1 parent 673cdcd commit dcc2d96

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
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: 25 additions & 1 deletion
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

@@ -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

Comments
 (0)