Skip to content

Commit 44bd096

Browse files
author
Olivier Bonnaure
committed
feat: update postgrest connector
1 parent 5b1fc4c commit 44bd096

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

.lua/postgrest.lua

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
PGPGRestApiUrl = ""
2-
PGRestHeaders = {}
1+
local PGPGRestApiUrl = ""
2+
local PGRestHeaders = {}
33

44
local function init(db_config)
55
PGRestApiUrl = db_config["url"]
@@ -9,9 +9,14 @@ local function init(db_config)
99
}
1010
end
1111

12+
local function count(path)
13+
local ok, h, body = Fetch(PGRestApiUrl .. path, { headers = table.merge(PGRestHeaders, { Prefer = "count=exact" }) })
14+
return ok, tonumber(string.split(h["Content-Range"], "/")[2])
15+
end
16+
1217
local function read(path)
1318
local ok, h, body = Fetch(PGRestApiUrl .. path, { headers = PGRestHeaders })
14-
return DecodeJson(body)
19+
return ok, DecodeJson(body)
1520
end
1621

1722
local function update(path, data)
@@ -20,30 +25,32 @@ local function update(path, data)
2025
body = EncodeJson(data),
2126
headers = PGRestHeaders
2227
})
23-
return DecodeJson(body)
28+
return ok, DecodeJson(body)
2429
end
2530

2631
local function insert(path, data)
2732
local ok, h, body = Fetch(PGRestApiUrl .. path, {
2833
method = "POST",
2934
body = EncodeJson(data),
30-
headers = PGRestHeaders
35+
headers = table.merge(PGRestHeaders, { Prefer = "return=representation" })
3136
})
32-
return ok
37+
return ok, body
3338
end
3439

3540
local function delete(path, data)
3641
local ok, h, body = Fetch(PGRestApiUrl .. path, {
3742
method = "DELETE",
3843
headers = PGRestHeaders
3944
})
40-
return DecodeJson(body)
45+
return ok, DecodeJson(body)
4146
end
4247

4348
return {
4449
init = init,
50+
count = count,
4551
read = read,
4652
insert = insert,
4753
update = update,
48-
delete = delete
54+
delete = delete,
55+
upsert = upsert
4956
}

0 commit comments

Comments
 (0)