Search code examples
pg-promise

How to call postgre native function wih pg-promise Columnset


I need to encrypt data and insert into table. I am using pg-promise, pgp.helper to create columset and then calling pgp.helper.insert. If I try to insert data without encrypting it, it is working fine but I want to use postgres native function "pgp_sym_encrypt" to encrypt data while inserting.


Solution

  • As per type Column, your column definition can be like this:

    {
        name: 'your-column-name',
        mod: ':raw',
        init: c => pgp.as.format('PGP_SYM_ENCRYPT($1, $2)', [c.value, 'AES_KEY'])
    }