@@ -2,231 +2,227 @@ Adb = {}
22Adb .__index = Adb
33
44function 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
1515end
1616
1717function Adb :Api_url (path )
18- return self ._arangoAPI .. path
18+ return self ._arangoAPI .. path
1919end
2020
2121function 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
3430end
3531
3632function 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
5345end
5446
5547function 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
7670end
7771
7872function 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
8478end
8579
8680function 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 )
8983end
9084
9185function Adb :without_Params (endpoint , method , handle )
92- return self :Api_run (endpoint .. handle , method )
86+ return self :Api_run (endpoint .. handle , method )
9387end
9488
9589-- Documents
9690
9791function Adb :UpdateDocument (handle , params )
98- return self :with_Params (" /document/" , " PATCH" , handle , params )
92+ return self :with_Params (" /document/" , " PATCH" , handle , params )
9993end
10094
10195function Adb :CreateDocument (handle , params )
102- return self :with_Params (" /document/" , " POST" , handle , params )
96+ return self :with_Params (" /document/" , " POST" , handle , params )
10397end
10498
10599function Adb :GetDocument (handle )
106- return self :without_Params (" /document/" , " GET" , handle )
100+ return self :without_Params (" /document/" , " GET" , handle )
107101end
108102
109103function Adb :DeleteDocument (handle )
110- return self :without_Params (" /document/" , " DELETE" , handle )
104+ return self :without_Params (" /document/" , " DELETE" , handle )
111105end
112106
113107--- Collections
114108
115109function Adb :UpdateCollection (collection , params )
116- return self :with_Params (" /collection/" , " PUT" , collection .. " /properties" , params )
110+ return self :with_Params (" /collection/" , " PUT" , collection .. " /properties" , params )
117111end
118112
119113function Adb :RenameCollection (collection , params )
120- return self :with_Params (" /collection/" , " PUT" , collection .. " /rename" , params )
114+ return self :with_Params (" /collection/" , " PUT" , collection .. " /rename" , params )
121115end
122116
123117function 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 )
128122end
129123
130124function 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 )
152146end
153147
154148function Adb :GetCollection (collection )
155- return self :without_Params (" /collection/" , " GET" , collection )
149+ return self :without_Params (" /collection/" , " GET" , collection )
156150end
157151
158152function Adb :DeleteCollection (collection )
159- return self :without_Params (" /collection/" , " DELETE" , collection )
153+ return self :without_Params (" /collection/" , " DELETE" , collection )
160154end
161155
162156function Adb :TruncateCollection (collection , params )
163- return self :with_Params (" /collection/" , " PUT" , collection .. " /truncate" , params )
157+ return self :with_Params (" /collection/" , " PUT" , collection .. " /truncate" , params )
164158end
165159
166160-- Databases
167161
168162function 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 )
173169end
174170
175171function Adb :DeleteDatabase (name )
176- return self :without_Params (" /database/" , " DELETE" , name )
172+ return self :without_Params (" /database/" , " DELETE" , name )
177173end
178174
179175-- Indexes
180176
181177function Adb :GetAllIndexes (collection )
182- return self :without_Params (" /index?collection=" .. collection , " GET" , " " )
178+ return self :without_Params (" /index?collection=" .. collection , " GET" , " " )
183179end
184180
185181function Adb :CreateIndex (handle , params )
186- return self :with_Params (" /index?collection=" .. handle , " POST" , " " , params )
182+ return self :with_Params (" /index?collection=" .. handle , " POST" , " " , params )
187183end
188184
189185function Adb :DeleteIndex (handle )
190- return self :without_Params (" /index/" , " DELETE" , handle )
186+ return self :without_Params (" /index/" , " DELETE" , handle )
191187end
192188
193189-- QueryCache
194190
195191function Adb :GetQueryCacheEntries ()
196- return self :without_Params (" /query-cache/entries" , " GET" , " " )
192+ return self :without_Params (" /query-cache/entries" , " GET" , " " )
197193end
198194
199195function Adb :GetQueryCacheConfiguration ()
200- return self :without_Params (" /query-cache/properties" , " GET" , " " )
196+ return self :without_Params (" /query-cache/properties" , " GET" , " " )
201197end
202198
203199function Adb :UpdateCacheConfiguration (params )
204- return self :with_Params (" /query-cache/properties" , " PUT" , " " , params )
200+ return self :with_Params (" /query-cache/properties" , " PUT" , " " , params )
205201end
206202
207203function Adb :DeleteQueryCache ()
208- return self :without_Params (" /query-cache" , " DELETE" , " " )
204+ return self :without_Params (" /query-cache" , " DELETE" , " " )
209205end
210206
211207-- Stream transactions
212208-- POST /_api/transaction/begin
213209function Adb :BeginTransaction (params )
214- return self :with_Params (" /transaction/begin" , " POST" , " " , params )
210+ return self :with_Params (" /transaction/begin" , " POST" , " " , params )
215211end
216212
217213function Adb :CommitTransaction (transaction_id )
218- return self :without_Params (" /transaction/" , " PUT" , transaction_id )
214+ return self :without_Params (" /transaction/" , " PUT" , transaction_id )
219215end
220216
221217function Adb :AbortTransaction (transaction_id )
222- return self :without_Params (" /transaction/" , " DELETE" , transaction_id )
218+ return self :without_Params (" /transaction/" , " DELETE" , transaction_id )
223219end
224220
225221function 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
230226end
231227
232228return Adb
0 commit comments