// api/test-pg.prg — Verify pg module works from PRG via TFNModule // GET → returns current_user, current_database, version FUNCTION Main() LOCAL oPg, oPool, oResult, hRow, hConfig // Build pg config from context (passed by server.js) hConfig := { ; "host" => ctx_get("db_host", "localhost"), ; "port" => Val(ctx_get("db_port", "5432")), ; "user" => ctx_get("db_user", "labdb"), ; "password" => ctx_get("db_password", ""), ; "database" => ctx_get("db_name", "labdb") ; } // Load pg module oPg := TFNModule():New("pg") IF oPg == NIL AP_JSONRESPONSE({ "ok" => .f., "error" => "failed to load pg module" }) RETURN NIL ENDIF // Create pool (using Pool constructor) oPool := oPg:NewInstance("Pool", hConfig) IF oPool == NIL AP_JSONRESPONSE({ "ok" => .f., "error" => "failed to create pool" }) RETURN NIL ENDIF // Run a simple query (async via aWait) oResult := aWait(oPool:Call("query", "SELECT current_user, current_database(), version()")) IF oResult == NIL AP_JSONRESPONSE({ "ok" => .f., "error" => "query returned nil" }) RETURN NIL ENDIF AP_JSONRESPONSE({ ; "ok" => .t., ; "rowCount" => oResult:rowCount, ; "rows" => oResult:rows ; }) // Cleanup aWait(oPool:Call("end")) RETURN NIL