PeerDB uses Gopher Lua for scripting

See examples for sample scripts

Scripts can be added & edited in UI:

PeerDB Streams supports an onRecord function for complete control of message content. 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

For qrep mirrors transformRow can be defined. Row fields can be set to transform row. Schema may not be modified

For cdc mirrors transformRow will be called on the record’s rows by default, unless tranformRecord is defined. The latter can be useful for comparing old & new field values

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