Skip to content

Commit a088cbd

Browse files
author
Olivier Bonnaure
committed
fix: arangodb lua files
1 parent 6143958 commit a088cbd

File tree

1 file changed

+108
-112
lines changed

1 file changed

+108
-112
lines changed

.lua/arangodb.lua

Lines changed: 108 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2,231 +2,227 @@ Adb = {}
22
Adb.__index = Adb
33

44
function Adb.new(db_config)
5-
local self = setmetatable({}, Adb)
5+
local self = setmetatable({}, Adb)
66

7-
self._lastDBConnect = GetTime()
8-
self._db_config = db_config
9-
self._arangoAPI = ""
10-
self._arangoJWT = ""
7+
self._lastDBConnect = GetTime()
8+
self._db_config = db_config
9+
self._arangoAPI = ""
10+
self._arangoJWT = ""
1111

12-
self:Auth()
12+
self:Auth()
1313

14-
return self
14+
return self
1515
end
1616

1717
function Adb:Api_url(path)
18-
return self._arangoAPI .. path
18+
return self._arangoAPI .. path
1919
end
2020

2121
function Adb:Api_run(path, method, params, headers)
22-
params = params or {}
23-
headers = headers or {}
24-
local ok, h, body =
25-
Fetch(
26-
self:Api_url(path),
27-
{
28-
method = method,
29-
body = EncodeJson(params) or "",
30-
headers = table.append({ ["Authorization"] = "bearer " .. self._arangoJWT }, headers)
31-
}
32-
)
33-
return DecodeJson(body), ok, h
22+
params = params or {}
23+
headers = headers or {}
24+
local ok, h, body = Fetch(self:Api_url(path), {
25+
method = method,
26+
body = EncodeJson(params) or "",
27+
headers = table.append({ ["Authorization"] = "bearer " .. self._arangoJWT }, headers),
28+
})
29+
return DecodeJson(body), ok, h
3430
end
3531

3632
function Adb:Auth()
37-
local ok, headers, body =
38-
Fetch(
39-
self._db_config.url .. "/_open/auth",
40-
{
41-
method = "POST",
42-
body = '{ "username": "' .. self._db_config.username .. '", "password": "' .. self._db_config.password .. '" }'
43-
}
44-
)
33+
local ok, headers, body = Fetch(self._db_config.url .. "/_open/auth", {
34+
method = "POST",
35+
body = '{ "username": "' .. self._db_config.username .. '", "password": "' .. self._db_config.password .. '" }',
36+
})
4537

46-
self._arangoAPI = self._db_config.url .. "/_db/" .. self._db_config.db_name .. "/_api"
38+
self._arangoAPI = self._db_config.url .. "/_db/" .. self._db_config.db_name .. "/_api"
4739

48-
if ok == 200 then
49-
self._arangoJWT = DecodeJson(body)["jwt"]
50-
end
40+
if ok == 200 then
41+
self._arangoJWT = DecodeJson(body)["jwt"]
42+
end
5143

52-
return self._arangoJWT
44+
return self._arangoJWT
5345
end
5446

5547
function Adb:Raw_aql(stm)
56-
local body, status_code = self:Api_run("/cursor", "POST", stm)
57-
local result = body["result"]
58-
local has_more = body["hasMore"]
59-
local extra = body["extra"]
60-
61-
while has_more do
62-
body = self:Api_run("/cursor/" .. body["id"], "PUT")
63-
result = table.append(result, body["result"])
64-
has_more = body["hasMore"]
65-
end
66-
67-
if result == nil then
68-
result = {}
69-
end
70-
71-
if body.error then
72-
return body
73-
else
74-
return { result = result, extra = extra }
75-
end
48+
local body, status_code = self:Api_run("/cursor", "POST", stm)
49+
assert(body)
50+
local result = body["result"]
51+
local has_more = body["hasMore"]
52+
local extra = body["extra"]
53+
54+
while has_more do
55+
body = self:Api_run("/cursor/" .. body["id"], "PUT")
56+
assert(body)
57+
result = table.append(result, body["result"])
58+
has_more = body["hasMore"]
59+
end
60+
61+
if result == nil then
62+
result = {}
63+
end
64+
65+
if body.error then
66+
return body
67+
else
68+
return { result = result, extra = extra }
69+
end
7670
end
7771

7872
function Adb:Aql(str, bindvars, options)
79-
bindvars = bindvars or {}
80-
options = options or { fullCount = true }
73+
bindvars = bindvars or {}
74+
options = options or { fullCount = true }
8175

82-
local request = self:Raw_aql({ query = str, cache = true, bindVars = bindvars, options = options })
83-
return request
76+
local request = self:Raw_aql({ query = str, cache = true, bindVars = bindvars, options = options })
77+
return request
8478
end
8579

8680
function Adb:with_Params(endpoint, method, handle, params)
87-
params = params or {}
88-
return self:Api_run(endpoint .. handle, method, params)
81+
params = params or {}
82+
return self:Api_run(endpoint .. handle, method, params)
8983
end
9084

9185
function Adb:without_Params(endpoint, method, handle)
92-
return self:Api_run(endpoint .. handle, method)
86+
return self:Api_run(endpoint .. handle, method)
9387
end
9488

9589
-- Documents
9690

9791
function Adb:UpdateDocument(handle, params)
98-
return self:with_Params("/document/", "PATCH", handle, params)
92+
return self:with_Params("/document/", "PATCH", handle, params)
9993
end
10094

10195
function Adb:CreateDocument(handle, params)
102-
return self:with_Params("/document/", "POST", handle, params)
96+
return self:with_Params("/document/", "POST", handle, params)
10397
end
10498

10599
function Adb:GetDocument(handle)
106-
return self:without_Params("/document/", "GET", handle)
100+
return self:without_Params("/document/", "GET", handle)
107101
end
108102

109103
function Adb:DeleteDocument(handle)
110-
return self:without_Params("/document/", "DELETE", handle)
104+
return self:without_Params("/document/", "DELETE", handle)
111105
end
112106

113107
---Collections
114108

115109
function Adb:UpdateCollection(collection, params)
116-
return self:with_Params("/collection/", "PUT", collection .. "/properties", params)
110+
return self:with_Params("/collection/", "PUT", collection .. "/properties", params)
117111
end
118112

119113
function Adb:RenameCollection(collection, params)
120-
return self:with_Params("/collection/", "PUT", collection .. "/rename", params)
114+
return self:with_Params("/collection/", "PUT", collection .. "/rename", params)
121115
end
122116

123117
function Adb:CreateCollection(collection, options)
124-
options = options or {}
125-
local params = { name = collection }
126-
params = table.merge(params, options)
127-
return self:with_Params("/collection/", "POST", "", params)
118+
options = options or {}
119+
local params = { name = collection }
120+
params = table.merge(params, options)
121+
return self:with_Params("/collection/", "POST", "", params)
128122
end
129123

130124
function Adb:CreateCollectionWithTimestamps(collection, options)
131-
options = options or {}
132-
local params = { name = collection }
133-
params = table.merge(params, options)
134-
params = table.merge(params, {
135-
["computedValues"] = {
136-
{
137-
["computeOn"] = { "insert" },
138-
expression = "RETURN DATE_NOW()",
139-
name = "c_at",
140-
overwrite = true
141-
},
142-
{
143-
["computeOn"] = { "insert", "update" },
144-
expression = "RETURN DATE_NOW()",
145-
name = "u_at",
146-
overwrite = true
147-
}
148-
}
149-
})
150-
151-
return self:with_Params("/collection/", "POST", "", params)
125+
options = options or {}
126+
local params = { name = collection }
127+
params = table.merge(params, options)
128+
params = table.merge(params, {
129+
["computedValues"] = {
130+
{
131+
["computeOn"] = { "insert" },
132+
expression = "RETURN DATE_NOW()",
133+
name = "c_at",
134+
overwrite = true,
135+
},
136+
{
137+
["computeOn"] = { "insert", "update" },
138+
expression = "RETURN DATE_NOW()",
139+
name = "u_at",
140+
overwrite = true,
141+
},
142+
},
143+
})
144+
145+
return self:with_Params("/collection/", "POST", "", params)
152146
end
153147

154148
function Adb:GetCollection(collection)
155-
return self:without_Params("/collection/", "GET", collection)
149+
return self:without_Params("/collection/", "GET", collection)
156150
end
157151

158152
function Adb:DeleteCollection(collection)
159-
return self:without_Params("/collection/", "DELETE", collection)
153+
return self:without_Params("/collection/", "DELETE", collection)
160154
end
161155

162156
function Adb:TruncateCollection(collection, params)
163-
return self:with_Params("/collection/", "PUT", collection .. "/truncate", params)
157+
return self:with_Params("/collection/", "PUT", collection .. "/truncate", params)
164158
end
165159

166160
-- Databases
167161

168162
function Adb:CreateDatabase(name, options, users)
169-
local params = { name = name, options = (options or {}) }
170-
if users then params.users = users end
163+
local params = { name = name, options = (options or {}) }
164+
if users then
165+
params.users = users
166+
end
171167

172-
return self:with_Params("/database", "POST", "", params)
168+
return self:with_Params("/database", "POST", "", params)
173169
end
174170

175171
function Adb:DeleteDatabase(name)
176-
return self:without_Params("/database/", "DELETE", name)
172+
return self:without_Params("/database/", "DELETE", name)
177173
end
178174

179175
-- Indexes
180176

181177
function Adb:GetAllIndexes(collection)
182-
return self:without_Params("/index?collection=" .. collection, "GET", "")
178+
return self:without_Params("/index?collection=" .. collection, "GET", "")
183179
end
184180

185181
function Adb:CreateIndex(handle, params)
186-
return self:with_Params("/index?collection=" .. handle, "POST", "", params)
182+
return self:with_Params("/index?collection=" .. handle, "POST", "", params)
187183
end
188184

189185
function Adb:DeleteIndex(handle)
190-
return self:without_Params("/index/", "DELETE", handle)
186+
return self:without_Params("/index/", "DELETE", handle)
191187
end
192188

193189
-- QueryCache
194190

195191
function Adb:GetQueryCacheEntries()
196-
return self:without_Params("/query-cache/entries", "GET", "")
192+
return self:without_Params("/query-cache/entries", "GET", "")
197193
end
198194

199195
function Adb:GetQueryCacheConfiguration()
200-
return self:without_Params("/query-cache/properties", "GET", "")
196+
return self:without_Params("/query-cache/properties", "GET", "")
201197
end
202198

203199
function Adb:UpdateCacheConfiguration(params)
204-
return self:with_Params("/query-cache/properties", "PUT", "", params)
200+
return self:with_Params("/query-cache/properties", "PUT", "", params)
205201
end
206202

207203
function Adb:DeleteQueryCache()
208-
return self:without_Params("/query-cache", "DELETE", "")
204+
return self:without_Params("/query-cache", "DELETE", "")
209205
end
210206

211207
-- Stream transactions
212208
-- POST /_api/transaction/begin
213209
function Adb:BeginTransaction(params)
214-
return self:with_Params("/transaction/begin", "POST", "", params)
210+
return self:with_Params("/transaction/begin", "POST", "", params)
215211
end
216212

217213
function Adb:CommitTransaction(transaction_id)
218-
return self:without_Params("/transaction/", "PUT", transaction_id)
214+
return self:without_Params("/transaction/", "PUT", transaction_id)
219215
end
220216

221217
function Adb:AbortTransaction(transaction_id)
222-
return self:without_Params("/transaction/", "DELETE", transaction_id)
218+
return self:without_Params("/transaction/", "DELETE", transaction_id)
223219
end
224220

225221
function Adb:RefreshToken()
226-
if GetTime() - self._lastDBConnect > 600 then
227-
self:Auth(self._db_config)
228-
self._lastDBConnect = GetTime()
229-
end
222+
if GetTime() - self._lastDBConnect > 600 then
223+
self:Auth()
224+
self._lastDBConnect = GetTime()
225+
end
230226
end
231227

232228
return Adb

0 commit comments

Comments
 (0)