Files
LuaMicroservices/devices-worker/db.lua
Christian van Dijk 94b4f31102
Some checks failed
CI / Lint (push) Failing after 39s
CI / Build (push) Has been skipped
CI / Test (push) Has been skipped
CI / Helm Lint (push) Successful in 13s
🎉 initial commit
2026-02-23 09:47:16 +01:00

34 lines
930 B
Lua

-- PostgreSQL using pgmoon (worker: one connection per process is fine)
local pgmoon = require("pgmoon")
local DB_HOST = os.getenv("DB_HOST") or "localhost"
local DB_PORT = tonumber(os.getenv("DB_PORT")) or 5432
local DB_NAME = os.getenv("DB_NAME") or "handheld_devices"
local DB_USER = os.getenv("DB_USER") or "devices_user"
local DB_PASSWORD = os.getenv("DB_PASSWORD") or "devices_password"
local DB_CONNECT_TIMEOUT_MS = tonumber(os.getenv("DB_CONNECT_TIMEOUT_MS")) or 5000
local config = {
host = DB_HOST,
port = tostring(DB_PORT),
database = DB_NAME,
user = DB_USER,
password = DB_PASSWORD,
socket_type = "luasocket",
}
local function get_connection()
local pg = pgmoon.new(config)
pg:settimeout(DB_CONNECT_TIMEOUT_MS)
local ok, err = pg:connect()
if not ok then
return nil, err
end
return pg
end
return {
get_connection = get_connection,
config = config,
}