rap2hpoutre / pg-anonymizer

Dump anonymized PostgreSQL database with a NodeJS CLI
https://raph.site
MIT License
228 stars 32 forks source link

Support for JSON values #50

Open Laykou opened 1 year ago

Laykou commented 1 year ago

Is it possible to anonymize values in JSONs (simple) structure?

Let's say I have JSONB column:

data
----
{"first_name":"Michael","phone":"+123456"}

and I want to anonymize data->>'first_name'. Would that work too?

jmmuve commented 10 months ago

I'm interested too

JargeZ commented 6 days ago

Here is rough workaround

const { abn } = require('/usr/local/lib/node_modules/@phuocng/fake-numbers');
const faker = require('/usr/local/lib/node_modules/pg-anonymizer/node_modules/faker');

const omitJsonField = (field, val, newFieldValue = 'Omitted') => {
  const parsed = JSON.parse(val);
  if (!parsed) {
    return val;
  }

  // TODO: You can recursively search for the field as well

  const newVal = {
    ...parsed,
    [field]: newFieldValue
  }

  return JSON.stringify(newVal);
}

module.exports = {
  omitReference: (val) => {
    return omitJsonField("Reference", val);
  }
};