mix test Hex version badge License badge

UniPg

Compatibility layer for pg and pg2.

Why?

pg was introduced at OTP 23, and pg2 is deprecated and will be removed at OTP 24.

OTP version222324
pgXOO
pg2OOX

This means that you shouldn't directly depend on pg or pg2 if you wish your code to work with wide range of OTP versions.

Features

1. Unified API on top of pg and pg2.

UniPg provides API similar to pg and implemented on top of

  • pg for OTP >= 23.
  • pg2 for OTP < 23.

2. scope emulation for pg2

scope is new concept introduced by pg, so it is emulated for pg2.

3. Implicit scope and group creation.

  • :pg.start_link(scope) is called before scope access.
  • :pg2.create(group) is called before group access.

Calling scope/group creation everytime introduces performance penalty. But it's so small(< 5μs on my MBP 2015) that most applications can ignore it for the sake of convenience.

Usage

iex(1)> UniPg.join(:scope1, :group1, [self()])
:ok
iex(2)> UniPg.get_members(:scope1, :group1)
[#PID<0.179.0>]
iex(3)> UniPg.which_groups(:scope1)        
[:group1]
iex(4)> UniPg.leave(:scope1, :group1, [self()])
:ok
iex(5)> UniPg.get_members(:scope1, :group1)    
[]

Installation

If available in Hex, the package can be installed by adding uni_pg to your list of dependencies in mix.exs:

def deps do
  [
    {:uni_pg, "~> 0.2.0"}
  ]
end