1- PGPGRestApiUrl = " "
2- PGRestHeaders = {}
1+ local PGPGRestApiUrl = " "
2+ local PGRestHeaders = {}
33
44local function init (db_config )
55 PGRestApiUrl = db_config [" url" ]
@@ -9,9 +9,14 @@ local function init(db_config)
99 }
1010end
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+
1217local function read (path )
1318 local ok , h , body = Fetch (PGRestApiUrl .. path , { headers = PGRestHeaders })
14- return DecodeJson (body )
19+ return ok , DecodeJson (body )
1520end
1621
1722local 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 )
2429end
2530
2631local 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
3338end
3439
3540local 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 )
4146end
4247
4348return {
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