PeerDB uses Lua scripting for packaging messages for queues. In particular, Gopher Lua

Supported queues:

  1. Kafka
  2. PubSub
  3. EventHubs

See examples for sample scripts

The script should define an onRecord function. Result may be a table instead of a string to specify partition key, metadata, etc. See queue specific pages for supported fields

If multiple return values are given, multiple records will be written

Scripts can be added & edited in UI: scripts overview

scripts editor

peerdb functions

The set of functions in global peerdb table are:

RowColumns(row) table

Returns array of row’s column names

RowColumnKind(row, column) string

Returns underlying Kind of row column, see qvalue/kind.go for list of kinds

Now() [Time]

Returns current time as [Time]

UUID(string?) [UUID]

Returns random [UUID], or parses passed in string

Decimal(string | number)

Returns Decimal based on passed in value

type(any) string

Returns fmt.Sprintf("%T", value) on underlying value of UserData. Returns nil for other types

tostring(any) string

Returns fmt.Sprint(value) on underlying value of UserData. Returns nil for other types

types

PeerDB exposes records as UserData:

Record

Represents a CDC change

kind

One of insert, update, delete, or relation. You will likely want to ignore relation messages

row

Canonical row values of change. For update this is the new row. Keep in mind that Postgres does not send unchanged toast column values in CDC updates

old

Previous row values, nil for insert

new

New row values, nil for delete

checkpoint

LSN of CDC message

commit_time

CommitTime of record’s preceding BeginMessage

target

Destination table of record defined by mirror

source

Source table of record

unchanged_columns

Update records have a table mapping unchanged toast columns to true

Row

PeerDB row values which cannot be represented as Lua numbers or strings are wrapped in UserData. All of these types implement __tostring. Available types:

I64 / U64

Represents signed/unsigned 64 bit integers. Implements comparisons with each other & __tostring support. There are 3 conversion properties:

  • i64 cast to [I64]
  • u64 cast to [U64]
  • float64 cast to lua number
  • hi returns highest 32 bits as an unsigned number
  • lo returns lowest 32 bits as an unsigned number

Time

Has properties for durations since Unix epoch. These are [I64] results, except unix which is a lua number

  • unix_nano
  • unix_micro
  • unix_milli
  • unix_second
  • unix

& properties for datetime components as numbers:

  • year
  • month
  • day
  • yearday
  • hour
  • minute
  • second
  • nanosecond

UUID

Index with [0..16) to access bytes (first element at index 0)

BigInt

sign

1, 0, or -1 depending on sign of value

bytes

string of *big.Int Bytes method

int64

value as [I64]

is64

whether value can be represented by int64

Decimal

coefficient

[BigInt] representing digits

coefficient64

[I64] representing digits

exponent

Exponent, value of decimal is coefficient * (10 ^ exponent)

bigint

Value of decimal as a BigInt

int64

Value of decimal as a [I64]

float

Value of decimal as a number

package.preload

PeerDB offers some preloaded libraries, accessible via require:

librarydescription
bit32bit32 API included in Lua 5.2
flatbuffersflatbuffers API with 64-bit integer support, compatible with Lua generated using our patched flatc
jsonJSON encoding/decoding
msgpackmsgpack encoding
utf8utf8 API included in Lua 5.3