Trying to build a webapp with Openresty+postgres, looking at the
lua-resty-postgres library I was wondering how should one use it?
I have a execute query function defined in my init_by_lua_block like this
db_func = function (q)
local pg = require("resty.postgres")
local db = pg:new()
db:set_timeout(3000)
local ok, err = db:connect({host=db_host,port=db_port, database=db_name,user=db_user,password=db_pass,compact=false})
if not ok then
ngx.log(ngx.ERR, err)
end
local res, err = db:query(q)
return res, err
end
and call this from locations by building queries with concatenating strings and quoting params with ndk.set_var.set_quote_pgsql_str() . Is this how it is supposed to be done? I'm sure I'm missing something, it feels bloated and error prone. Also I'm not sure whether the db connection is set up right or am I recreating it on every request?