44
55use Appwrite \AppwriteException ;
66use Appwrite \Client ;
7- use Appwrite \Enums \IndexType ;
7+ use Appwrite \Enums \TablesDBIndexType ;
88use Appwrite \ID ;
9+ use Appwrite \Models \Row ;
910use Appwrite \Query ;
1011use Appwrite \Services \TablesDB as TablesDBService ;
1112use Utopia \Abuse \Adapters \TimeLimit ;
1213use Utopia \Database \Document ;
13- use Utopia \Exception ;
1414
1515class TablesDB extends TimeLimit
1616{
@@ -35,7 +35,7 @@ public function __construct(string $key, int $limit, int $seconds, Client $clien
3535 }
3636
3737 /**
38- * @throws Exception| \Exception
38+ * @throws \Exception
3939 */
4040 public function setup (): void
4141 {
@@ -94,8 +94,8 @@ protected function createColumns(): void
9494 protected function createIndexes (): void
9595 {
9696 $ indexes = [
97- fn () => $ this ->tablesDB ->createIndex ($ this ->databaseId , self ::TABLE_ID , 'unique1 ' , IndexType ::UNIQUE (), ['key ' , 'time ' ]),
98- fn () => $ this ->tablesDB ->createIndex ($ this ->databaseId , self ::TABLE_ID , 'index2 ' , IndexType ::KEY (), ['time ' ])
97+ fn () => $ this ->tablesDB ->createIndex ($ this ->databaseId , self ::TABLE_ID , 'unique1 ' , TablesDBIndexType ::UNIQUE (), ['key ' , 'time ' ]),
98+ fn () => $ this ->tablesDB ->createIndex ($ this ->databaseId , self ::TABLE_ID , 'index2 ' , TablesDBIndexType ::KEY (), ['time ' ])
9999 ];
100100
101101 foreach ($ indexes as $ createIndexFunction ) {
@@ -111,12 +111,12 @@ protected function waitForResourcesReady(string $resourceType): void
111111 while ($ attempts < $ maxAttempts ) {
112112 $ attempts ++;
113113
114- $ response = $ resourceType === 'columns '
115- ? $ this ->tablesDB ->listColumns ($ this ->databaseId , self ::TABLE_ID , [Query::notEqual ('status ' , 'available ' ), Query::limit (1 )])
116- : $ this ->tablesDB ->listIndexes ($ this ->databaseId , self ::TABLE_ID , [Query::notEqual ('status ' , 'available ' ), Query::limit (1 )]);
114+ $ resources = $ resourceType === 'columns '
115+ ? $ this ->tablesDB ->listColumns ($ this ->databaseId , self ::TABLE_ID , [Query::notEqual ('status ' , 'available ' ), Query::limit (1 )])-> columns
116+ : $ this ->tablesDB ->listIndexes ($ this ->databaseId , self ::TABLE_ID , [Query::notEqual ('status ' , 'available ' ), Query::limit (1 )])-> indexes ;
117117
118- $ resources = $ response [ $ resourceType ];
119- $ resources = \array_filter ($ resources , fn ($ resource ) => $ resource[ ' status ' ] !== 'available ' );
118+ // Column models expose status as a ColumnStatus enum; index models as a plain string. Cast for both.
119+ $ resources = \array_filter ($ resources , fn ($ resource ) => ( string ) $ resource-> status !== 'available ' );
120120
121121 if (\count ($ resources ) === 0 ) {
122122 return ;
@@ -170,16 +170,15 @@ protected function count(string $key, int $timestamp): int
170170
171171 $ timestamp = $ this ->toDateTime ($ timestamp );
172172
173- $ response = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , [
173+ $ rows = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , [
174174 Query::equal ('key ' , [$ key ]),
175175 Query::equal ('time ' , [$ timestamp ]),
176- ]);
177- $ rows = $ response ['rows ' ];
176+ ])->rows ;
178177
179178 $ this ->count = 0 ;
180179
181180 if (\count ($ rows ) === 1 ) { // Unique Index
182- $ count = $ rows [0 ]['count ' ] ?? 0 ;
181+ $ count = $ rows [0 ]-> data ['count ' ] ?? 0 ;
183182 if (\is_numeric ($ count )) {
184183 $ this ->count = intval ($ count );
185184 }
@@ -203,14 +202,13 @@ protected function hit(string $key, int $timestamp): void
203202
204203 $ timestamp = $ this ->toDateTime ($ timestamp );
205204
206- $ response = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , [
205+ $ rows = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , [
207206 Query::equal ('key ' , [$ key ]),
208207 Query::equal ('time ' , [$ timestamp ]),
209- ]);
210- $ rows = $ response ['rows ' ];
211- $ data = $ rows [0 ] ?? null ;
208+ ])->rows ;
209+ $ row = $ rows [0 ] ?? null ;
212210
213- if (\is_null ($ data )) {
211+ if (\is_null ($ row )) {
214212 $ data = [
215213 'key ' => $ key ,
216214 'time ' => $ timestamp ,
@@ -224,27 +222,26 @@ protected function hit(string $key, int $timestamp): void
224222 throw $ err ;
225223 }
226224
227- $ response = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , [
225+ $ rows = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , [
228226 Query::equal ('key ' , [$ key ]),
229227 Query::equal ('time ' , [$ timestamp ]),
230- ]);
231- $ rows = $ response ['rows ' ];
228+ ])->rows ;
232229
233- $ data = $ rows [0 ] ?? null ;
230+ $ row = $ rows [0 ] ?? null ;
234231
235- if (!is_null ($ data )) {
236- $ count = $ data ['count ' ] ?? 0 ;
232+ if (!is_null ($ row )) {
233+ $ count = $ row -> data ['count ' ] ?? 0 ;
237234 if (\is_numeric ($ count )) {
238235 $ this ->count = intval ($ count );
239236 }
240237
241- $ this ->tablesDB ->incrementRowColumn ($ this ->databaseId , self ::TABLE_ID , $ data [ ' $id ' ] , 'count ' , 1 );
238+ $ this ->tablesDB ->incrementRowColumn ($ this ->databaseId , self ::TABLE_ID , $ row -> id , 'count ' , 1 );
242239 } else {
243240 throw new \Exception ('Document Not Found ' );
244241 }
245242 }
246243 } else {
247- $ this ->tablesDB ->incrementRowColumn ($ this ->databaseId , self ::TABLE_ID , $ data [ ' $id ' ] , 'count ' , 1 );
244+ $ this ->tablesDB ->incrementRowColumn ($ this ->databaseId , self ::TABLE_ID , $ row -> id , 'count ' , 1 );
248245 }
249246
250247 $ this ->count ++;
@@ -262,14 +259,13 @@ protected function set(string $key, int $timestamp, int $value): void
262259 {
263260 $ timestamp = $ this ->toDateTime ($ timestamp );
264261
265- $ response = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , [
262+ $ rows = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , [
266263 Query::equal ('key ' , [$ key ]),
267264 Query::equal ('time ' , [$ timestamp ]),
268- ]);
269- $ rows = $ response ['rows ' ];
270- $ data = $ rows [0 ] ?? null ;
265+ ])->rows ;
266+ $ row = $ rows [0 ] ?? null ;
271267
272- if (\is_null ($ data )) {
268+ if (\is_null ($ row )) {
273269 $ data = [
274270 'key ' => $ key ,
275271 'time ' => $ timestamp ,
@@ -283,22 +279,21 @@ protected function set(string $key, int $timestamp, int $value): void
283279 throw $ err ;
284280 }
285281
286- $ response = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , [
282+ $ rows = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , [
287283 Query::equal ('key ' , [$ key ]),
288284 Query::equal ('time ' , [$ timestamp ]),
289- ]);
290- $ rows = $ response ['rows ' ];
285+ ])->rows ;
291286
292- $ data = $ rows [0 ] ?? null ;
287+ $ row = $ rows [0 ] ?? null ;
293288
294- if (!is_null ($ data )) {
295- $ this ->tablesDB ->updateRow ($ this ->databaseId , self ::TABLE_ID , $ data [ ' $id ' ] , ['count ' => $ value ]);
289+ if (!is_null ($ row )) {
290+ $ this ->tablesDB ->updateRow ($ this ->databaseId , self ::TABLE_ID , $ row -> id , ['count ' => $ value ]);
296291 } else {
297292 throw new \Exception ('Unable to find abuse tracking row after race condition handling ' );
298293 }
299294 }
300295 } else {
301- $ this ->tablesDB ->updateRow ($ this ->databaseId , self ::TABLE_ID , $ data [ ' $id ' ] , ['count ' => $ value ]);
296+ $ this ->tablesDB ->updateRow ($ this ->databaseId , self ::TABLE_ID , $ row -> id , ['count ' => $ value ]);
302297 }
303298
304299 $ this ->count = $ value ;
@@ -328,9 +323,9 @@ public function getLogs(?int $offset = null, ?int $limit = 25): array
328323 $ queries [] = Query::limit ($ limit );
329324 }
330325
331- $ response = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , $ queries );
326+ $ rows = $ this ->tablesDB ->listRows ($ this ->databaseId , self ::TABLE_ID , $ queries )-> rows ;
332327
333- return \array_map (fn ($ document ) => new Document ($ document ) , $ response [ ' documents ' ] );
328+ return \array_map (fn (Row $ row ) => new Document ($ row -> toArray ()) , $ rows );
334329 }
335330
336331 /**
@@ -349,7 +344,7 @@ public function cleanup(int $timestamp): bool
349344 $ response = $ this ->tablesDB ->deleteRows ($ this ->databaseId , self ::TABLE_ID , [
350345 Query::lessThan ('time ' , $ timestamp ),
351346 ]);
352- } while ($ response[ ' total ' ] > 0 );
347+ } while ($ response-> total > 0 );
353348
354349 return true ;
355350 }
0 commit comments