Skip to content

Commit 2479e99

Browse files
committed
Now lua module returns Client and Pool instead of modifying global table
1 parent c5bf2b1 commit 2479e99

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

async_postgres.lua

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ if async_postgres.LUA_API_VERSION ~= 1 then
119119
"expected 1, got " .. async_postgres.LUA_API_VERSION)
120120
end
121121

122+
123+
---@class async_postgres_module : async_postgres
124+
local module = setmetatable({}, { __index = async_postgres })
125+
122126
local Queue = {}
123127
Queue.__index = Queue
124128

@@ -692,7 +696,7 @@ end
692696
--- connectiong url format can be found at https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
693697
---@param url string connection url, see libpq documentation for more information
694698
---@return PGClient
695-
function async_postgres.Client(url)
699+
function module.Client(url)
696700
---@class PGClient
697701
local client = setmetatable({
698702
url = url,
@@ -887,7 +891,7 @@ function Pool:processQueue()
887891
local waiters = self.queue:size()
888892
local threshold = clients * self.threshold
889893
if clients < self.max and waiters > threshold then
890-
local client = async_postgres.Client(self.url)
894+
local client = module.Client(self.url)
891895
client.onError = function(client, message)
892896
return self:onError(message)
893897
end
@@ -1075,11 +1079,11 @@ end
10751079
--- Creates a new connection pool with given connection url,
10761080
--- then use :connect() to get available connection,
10771081
--- and then :release() to release it back to the pool
1078-
function async_postgres.Pool(url)
1082+
function module.Pool(url)
10791083
---@class PGPool
10801084
local pool = setmetatable({
10811085
url = url,
1082-
clients = { async_postgres.Client(url) },
1086+
clients = { module.Client(url) },
10831087
queue = Queue.new(),
10841088
max = 10,
10851089
threshold = 5,
@@ -1093,3 +1097,5 @@ function async_postgres.Pool(url)
10931097

10941098
return pool
10951099
end
1100+
1101+
return module

0 commit comments

Comments
 (0)