-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunnest.lua
49 lines (46 loc) · 960 Bytes
/
unnest.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local json = require "json"
local function RowToMap(row)
if not row then
return
end
local map = peerdb.RowTable(row)
local map2 = {} -- pairs breaks if new keys added to map
for col, val in pairs(map) do
local kind = peerdb.RowColumnKind(row, col)
if kind == 'json' then
local jsonval = json.decode(val)
local submap = json.unmark(jsonval)
if type(submap) == "table" then
for subcol, subval in pairs(submap) do
map2[subcol] = subval
end
map[col] = nil
else
map[col] = submap
end
end
end
for col, val in pairs(map2) do
map[col] = val
end
return map
end
local OPMAP = {
insert = "c",
update = "u",
delete = "d",
}
function onRecord(record)
local op = OPMAP[record.kind]
if not op then
return
end
return json.encode {
op = op,
before = RowToMap(record.old),
after = RowToMap(record.new),
commitms = record.commit_time.unix_milli,
table = record.source,
lsn = record.checkpoint,
}
end