1
1
from typing import Dict , Any , Optional , Union , List
2
2
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
7
9
8
10
9
- class RecordsAPI :
11
+ class RecordsAPI ( BaseAPI ) :
10
12
"""API for managing records in RushDB."""
11
- def __init__ (self , client : 'RushDBClient' ):
12
- self .client = client
13
-
14
13
def set (self , record_id : str , data : Dict [str , Any ], transaction : Optional [Transaction ] = None ) -> Dict [str , str ]:
15
14
"""Update a record by ID."""
16
15
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
45
44
}
46
45
}
47
46
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' ) )
49
48
50
49
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 ]:
51
50
"""Create multiple records.
@@ -70,23 +69,19 @@ def create_many(self, label: str, data: Union[Dict[str, Any], List[Dict[str, Any
70
69
}
71
70
}
72
71
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' )]
73
73
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 ]:
79
75
"""Attach records to a source record."""
80
76
headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
81
77
source_id = self ._extract_target_ids (source )[0 ]
82
78
target_ids = self ._extract_target_ids (target )
83
79
payload = {'targetIds' : target_ids }
84
80
if options :
85
81
payload .update (options )
86
- print (payload )
87
82
return self .client ._make_request ('POST' , f'/api/v1/records/{ source_id } /relations' , payload , headers )
88
83
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 ]:
90
85
"""Detach records from a source record."""
91
86
headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
92
87
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
96
91
payload .update (options )
97
92
return self .client ._make_request ('PUT' , f'/api/v1/records/{ source_id } /relations' , payload , headers )
98
93
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 ]:
100
95
"""Delete records matching the query."""
101
96
headers = Transaction ._build_transaction_header (transaction .id if transaction else None )
102
97
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
111
106
}, headers )
112
107
return self .client ._make_request ('DELETE' , f'/api/v1/records/{ id_or_ids } ' , None , headers )
113
108
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 ]:
115
110
"""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 ]
120
111
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 []
143
119
144
120
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 ]]:
145
121
"""Import data from CSV."""
0 commit comments