pg-mem and uuid_generate_v4() mock

pg-mem is an open-source project for javascript (works in Node.js and the browser) where you can mock a Postgres server in memory, is pretty cool to use it on unit and local tests.

And it comes with its own limitations, by default, pg-mem has no native extensions.

The most common I use is the uuid_generate_v4 as default for the records ids, to emulate the same behavior on pg-mem here is the code:

  import { v4 } from 'uuid';

  db.registerExtension('uuid-ossp', (schema) => {
    schema.registerFunction({
      name: 'uuid_generate_v4',
      returns: DataType.uuid,
      implementation: v4,
      impure: true,
    });
  });

More information on the GitHub project

https://github.com/oguimbal/pg-mem/wiki/FAQ#-what-if-i-need-an-extension-like-uuid-ossp-

0
Subscribe to my newsletter

Read articles from Miguel Angel Acevedo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Miguel Angel Acevedo
Miguel Angel Acevedo