Preset modules

Preset modules

By default Balerter distributed with some preset modules, which placed in the modules folder.

They are not needed for the balerter functional

h (helper)

Usage:

local h = require('h')

Methods

tableToMap(table, keyFieldName) table, error

Convert lua table to hash-table with keys as ‘keyFieldName’ value

An example:

table = { 
    { date = '2020-01-01', id = 'a', name = 'aa'}, 
    { date = '2020-01-01', id = 'b', name = 'bb' },
    ... 
}

res, err = h.tableToMap(table, 'id')

-- res  
{
    'a' = { date = '2020-01-01', id = 'a', name = 'aa'}, 
    'b' = { date = '2020-01-01', id = 'b', name = 'bb' },
    ...
}

print(table)

Beautify print table or any value to the console

local t = { ['foo'] = { ['bar'] = 42 } }

h.print(t)

-- a result
{
    foo = {
        bar = 42
    }
}

json

https://github.com/rxi/json.lua

Allows encode/decode json

json = require('json')

json.encode({ 1, 2, 3, { x = 10 } }) -- Returns '[1,2,3,{"x":10}]'
json.decode('[1,2,3,{"x":10}]') -- Returns { 1, 2, 3, { x = 10 } }

csv

https://github.com/FourierTransformer/ftcsv

Work with csv

An example:

local db = require('datasource.postgres.pg1')
local log = require('log')
local csv = require('csv')
local h = require('h')

res, err = db.query('SELECT * FROM users')
if err ~= nil then
    log.error('query error: ' .. err)
    return
end

h.print(csv.encode(res, ","))

See more on https://github.com/FourierTransformer/ftcsv