11from typing import Dict , Any , Optional , Union , List
22
3- from src .rushdb import RushDBClient , RushDBError
4- from src .rushdb .transaction import Transaction
5- from src .rushdb .common import RelationOptions , RelationDetachOptions
6- from src .rushdb .record import Record
3+ from .base import BaseAPI
4+ from ..common import RushDBError
5+ from ..models .relationship import RelationshipOptions , RelationshipDetachOptions
6+ from ..models .search_query import SearchQuery
7+ from ..models .transaction import Transaction
8+ from ..models .record import Record
79
810
9- class RecordsAPI :
11+ class RecordsAPI ( BaseAPI ) :
1012 """API for managing records in RushDB."""
11- def __init__ (self , client : 'RushDBClient' ):
12- self .client = client
13-
1413 def set (self , record_id : str , data : Dict [str , Any ], transaction : Optional [Transaction ] = None ) -> Dict [str , str ]:
1514 """Update a record by ID."""
1615 headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
@@ -45,7 +44,7 @@ def create(self, label: str, data: Dict[str, Any], options: Optional[Dict[str, b
4544 }
4645 }
4746 response = self .client ._make_request ('POST' , '/api/v1/records' , payload , headers )
48- return Record (self .client , response )
47+ return Record (self .client , response . get ( 'data' ) )
4948
5049 def create_many (self , label : str , data : Union [Dict [str , Any ], List [Dict [str , Any ]]], options : Optional [Dict [str , bool ]] = None , transaction : Optional [Transaction ] = None ) -> List [Record ]:
5150 """Create multiple records.
@@ -70,23 +69,19 @@ def create_many(self, label: str, data: Union[Dict[str, Any], List[Dict[str, Any
7069 }
7170 }
7271 response = self .client ._make_request ('POST' , '/api/v1/records/import/json' , payload , headers )
72+ return [Record (self .client , record ) for record in response .get ('data' )]
7373
74- print ('r:' , response )
75-
76- return [Record (self .client , {"data" : record }) for record in response .get ('data' )]
77-
78- def attach (self , source : Union [str , Dict [str , Any ]], target : Union [str , List [str ], Dict [str , Any ], List [Dict [str , Any ]]], options : Optional [RelationOptions ] = None , transaction : Optional [Transaction ] = None ) -> Dict [str , str ]:
74+ def attach (self , source : Union [str , Dict [str , Any ]], target : Union [str , List [str ], Dict [str , Any ], List [Dict [str , Any ]]], options : Optional [RelationshipOptions ] = None , transaction : Optional [Transaction ] = None ) -> Dict [str , str ]:
7975 """Attach records to a source record."""
8076 headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
8177 source_id = self ._extract_target_ids (source )[0 ]
8278 target_ids = self ._extract_target_ids (target )
8379 payload = {'targetIds' : target_ids }
8480 if options :
8581 payload .update (options )
86- print (payload )
8782 return self .client ._make_request ('POST' , f'/api/v1/records/{ source_id } /relations' , payload , headers )
8883
89- def detach (self , source : Union [str , Dict [str , Any ]], target : Union [str , List [str ], Dict [str , Any ], List [Dict [str , Any ]]], options : Optional [RelationDetachOptions ] = None , transaction : Optional [Transaction ] = None ) -> Dict [str , str ]:
84+ def detach (self , source : Union [str , Dict [str , Any ]], target : Union [str , List [str ], Dict [str , Any ], List [Dict [str , Any ]]], options : Optional [RelationshipDetachOptions ] = None , transaction : Optional [Transaction ] = None ) -> Dict [str , str ]:
9085 """Detach records from a source record."""
9186 headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
9287 source_id = self ._extract_target_ids (source )[0 ]
@@ -96,7 +91,7 @@ def detach(self, source: Union[str, Dict[str, Any]], target: Union[str, List[str
9691 payload .update (options )
9792 return self .client ._make_request ('PUT' , f'/api/v1/records/{ source_id } /relations' , payload , headers )
9893
99- def delete (self , query : Dict [ str , Any ] , transaction : Optional [Transaction ] = None ) -> Dict [str , str ]:
94+ def delete (self , query : SearchQuery , transaction : Optional [Transaction ] = None ) -> Dict [str , str ]:
10095 """Delete records matching the query."""
10196 headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
10297 return self .client ._make_request ('PUT' , '/api/v1/records/delete' , query , headers )
@@ -111,35 +106,16 @@ def delete_by_id(self, id_or_ids: Union[str, List[str]], transaction: Optional[T
111106 }, headers )
112107 return self .client ._make_request ('DELETE' , f'/api/v1/records/{ id_or_ids } ' , None , headers )
113108
114- def find (self , query : Optional [Dict [ str , Any ] ] = None , record_id : Optional [str ] = None , transaction : Optional [Transaction ] = None ) -> List [Record ]:
109+ def find (self , query : Optional [SearchQuery ] = None , record_id : Optional [str ] = None , transaction : Optional [Transaction ] = None ) -> List [Record ]:
115110 """Find records matching the query."""
116- headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
117- path = f'/api/v1/records/{ record_id } /search' if record_id else '/api/v1/records/search'
118- response = self .client ._make_request ('POST' , path , data = query , headers = headers )
119- return [Record (self .client , record ) for record in response ]
120111
121- def find_by_id (self , id_or_ids : Union [str , List [str ]], transaction : Optional [Transaction ] = None ) -> Union [Record , List [Record ]]:
122- """Find records by ID(s)."""
123- headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
124- if isinstance (id_or_ids , list ):
125- response = self .client ._make_request ('POST' , '/api/v1/records' , {'ids' : id_or_ids }, headers )
126- return [Record (self .client , record ) for record in response ]
127- response = self .client ._make_request ('GET' , f'/api/v1/records/{ id_or_ids } ' , None , headers )
128- return Record (self .client , response )
129-
130- def find_one (self , query : Dict [str , Any ], transaction : Optional [Transaction ] = None ) -> Optional [Record ]:
131- """Find a single record matching the query."""
132- headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
133- query = {** query , 'limit' : 1 , 'skip' : 0 }
134- result = self .client ._make_request ('POST' , '/api/v1/records/search' , query , headers )
135- return Record (self .client , result [0 ]) if result else None
136-
137- def find_unique (self , query : Dict [str , Any ], transaction : Optional [Transaction ] = None ) -> Record :
138- """Find a unique record matching the query."""
139- result = self .find_one (query , transaction )
140- if not result :
141- raise RushDBError ("No records found matching the unique query" )
142- return result
112+ try :
113+ headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
114+ path = f'/api/v1/records/{ record_id } /search' if record_id else '/api/v1/records/search'
115+ response = self .client ._make_request ('POST' , path , data = query or {}, headers = headers )
116+ return [Record (self .client , record ) for record in response .get ('data' )]
117+ except :
118+ return []
143119
144120 def import_csv (self , label : str , csv_data : Union [str , bytes ], options : Optional [Dict [str , bool ]] = None , transaction : Optional [Transaction ] = None ) -> List [Dict [str , Any ]]:
145121 """Import data from CSV."""
0 commit comments