npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pgfaker

v2.1.2

Published

Tool for dumping/exporting your postgres database's table data with custom masking rules.

Downloads

22

Readme

PGFaker

Linters and Tests CodeQ npm version

Tool for dumping/exporting your postgres database's table data with custom masking rules.

Usage

pgfaker [options] <config>

Currently available options are

Options:
  -o --output <outFile>  STDOUT or name of the output sql file (default: "dump.sql")
  -v --verbose <mode> verbosity - verbose | info | silent
  -h, --help             display help for command

You can also use this with npx

$ npx pgfaker config.js --output=STDOUT

Options

  • --output: Use it to either write to file or stdout.

    Examples:

    $ npx pgfaker config.js --output=STDOUT | psql database_url
    $ npx pgfaker config.js --output=dump.sql

    Defualt: dump.sql

  • --verbose: This is used to control the verbosity level of logging from pgfaker

    • silent : No output at all, only runtime errors.
    • info: Basic information during the dump process such as current table, start and finish prompts etc.
    • verbose: All possible information during the dump process. This includes, everything from info plus columns that were skipped, transformed and more.

    Default: verbose

Configuration

The config.js (or any js file) will contain the configuration for masking the data.

export const configuration = {

  connectionUrl: "postgresql://USER:PASSWORD@HOST:PORT/DATABASE",

  columns: {
    columnName: (value) => /* do something */ return value
  },

  tables: {
    tableName: {
      columnName: (value) => /* do something */ return value
    }
  },

  options:{
    skip:{
      tableName: 'output' | 'mask'
    }
  }

  defaultTransformer: (value) => value,
};

Properties

  • connectionUrl: The connection to your db

  • columns: The columns to replace values of. If a column doesn't exist it is ignored (currently)

  • tables: Use this to specify table specific column transformers. These transformers will take precedence over columns transformers.

    export const configuration = {
      connectionUrl: 'postgresql://USER:PASSWORD@HOST:PORT/DATABASE',
    
      columns: {
        description: (value) => 'This will not be used',
      },
    
      tables: {
        product: {
          description: (value) => 'This will be used',
        },
      },
    };

    For product table, description column will get This will be used, and all other tables having description column will get This will not be used

  • defaultTransformer: if this is specified, then all of the columns not mentioned in the columns and tables will get this transformer. If this isn't specified, all of the undefined columns are left unchanged.

    You can go ahead and use something like faker or your own custom maskers for complex use cases.

  • options: Additional options for the parser

    • skip: This property defines which table to either exclude from being masked or from being written to the output. This option accepts either 'mask' or 'output'

      • mask

        options: {
          skip: {
            blogs: 'mask';
          }
        }

        will make sure that the dumped sql doesn't have data masked for blogs table. This will override any other masking rules specified for this table.

      • output

        options: {
          skip: {
            users: 'output';
          }
        }

        will make sure that the dumped sql doesn't have any queries related to users table. This will override any other masking rules specified for this table.

Advanced

If you need more flexibility with masking, such as conditional masking then you can use middleware.

Middleware

Middleware in pgfaker allows you to access the record that is about to get transformed. You can modify the records here, or avoid the masking that is about to happen. The api looks like this

export const configuration = {
  connectionUrl: 'postgresql://USER:PASSWORD@HOST:PORT/DATABASE',

  columns: {
    description: (value) => 'This will not be used',
  },

  tables: {
    users: [checkIfAdmin, maskingRules],
  },
};

Where checkIfAdmin is your middleware function and maskingRules is your regular object of transformers

  1. The checkIfAdmin function will receive the column values as first parameter and column names as the seconds

    const checkIfAdmin = (columnValues, columnNames) => {
      adminColumnIndex = columnNames.indexOf('admin');
      isAdmin = columnValues[adminColumnIndex];
    
      if (isAdmin === '1') {
        return null;
      }
      return columnValues;
    };

    Returning null (or any falsy value) from this function will result in any masking defined (read next point) for it to be avoided.

  2. The maskingRules is your regular object of transformers. This can be used if you want to regular way of defining transformers along with the middleware. Note that the columns transformers are also used here

    const maskingRules = {
      users: {
        flag: (value) => 'off',
      },
    };

TroubleShooting

Please refer and raise the issues on github.

Why?

I was in search of a anonymizer for postgres and came across pg-anonymizer. This was not suitable for my requirement as it was lacking a flexible api and additional options, so I ended up creating my own version of anonymizer with inspiration from the package.