1
- """RushDB Client
2
-
3
- A modern graph database client implementation.
4
- """
1
+ """RushDB Client"""
5
2
6
3
import json
7
4
import urllib .request
8
5
import urllib .parse
9
6
import urllib .error
10
- from typing import Any , Dict , List , Optional , Union , TypeVar , TypedDict , Literal , Protocol , cast
7
+ from typing import Any , Dict , List , Optional , Union , TypedDict , Literal
11
8
from datetime import datetime
12
9
13
- # Basic type aliases
14
- Schema = TypeVar ('Schema' , bound = Dict [str , Any ])
15
- FlatObject = Dict [str , Any ]
16
- MaybeArray = Union [List [Any ], Any ]
17
-
18
- # Record types
19
- class DBRecordInternalProps (TypedDict ):
20
- """Internal properties of a database record."""
21
- __id : str
22
- __label : str
23
- __proptypes : Optional [Dict [str , str ]]
24
-
25
- class DBRecord (TypedDict , total = False ):
26
- """Database record with internal and user-defined properties."""
27
- __id : str
28
- __label : str
29
- __proptypes : Optional [Dict [str , str ]]
30
-
31
- class DBRecordDraft :
32
- """Draft for creating a new record."""
33
- def __init__ (self , label : str , payload : Union [Dict [str , Any ], List [Dict [str , Any ]]], options : Optional [Dict [str , bool ]] = None ):
34
- self .label = label
35
- self .payload = payload
36
- self .options = options or {
37
- "returnResult" : True ,
38
- "suggestTypes" : True
39
- }
40
-
41
- def to_json (self ) -> Dict [str , Any ]:
42
- """Convert draft to JSON format."""
43
- return {
44
- "label" : self .label ,
45
- "options" : self .options ,
46
- "payload" : self .payload
47
- }
48
-
49
- class DBRecordsBatchDraft :
50
- """Draft for creating multiple records in batch."""
51
- def __init__ (self , label : str , payload : Union [Dict [str , Any ], List [Dict [str , Any ]]], options : Optional [Dict [str , bool ]] = None ):
52
- self .label = label
53
- self .payload = payload
54
- self .options = options or {
55
- "returnResult" : True ,
56
- "suggestTypes" : True
57
- }
58
-
59
- def to_json (self ) -> Dict [str , Any ]:
60
- """Convert batch draft to JSON format."""
61
- return {
62
- "label" : self .label ,
63
- "options" : self .options ,
64
- "payload" : self .payload
65
- }
66
-
67
10
# Relation types
68
11
RelationDirection = Literal ['in' , 'out' ]
69
12
@@ -265,25 +208,41 @@ def create(self, label: str, data: Dict[str, Any], options: Optional[Dict[str, b
265
208
:param
266
209
"""
267
210
headers = self ._build_transaction_header (transaction .id if transaction else None )
268
- draft = DBRecordDraft (label , data , options )
269
- response = self .client ._make_request ('POST' , '/api/v1/records' , draft .to_json (), headers )
211
+
212
+ payload = {
213
+ "label" : label ,
214
+ "payload" : data ,
215
+ "options" : options or {
216
+ "returnResult" : True ,
217
+ "suggestTypes" : True
218
+ }
219
+ }
220
+ response = self .client ._make_request ('POST' , '/api/v1/records' , payload , headers )
270
221
return Record (self .client , response )
271
222
272
- def create_many (self , label : str , data : List [Dict [str , Any ]], options : Optional [Dict [str , bool ]] = None , transaction : Optional [Transaction ] = None ) -> List [Record ]:
223
+ 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 ]:
273
224
"""Create multiple records.
274
225
275
226
Args:
276
227
label: Label for all records
277
- data: List of record data
228
+ data: List or Dict of record data
278
229
options: Optional parsing and response options (returnResult, suggestTypes)
279
230
transaction: Optional transaction object
280
231
281
232
Returns:
282
233
List of Record objects
283
234
"""
284
235
headers = self ._build_transaction_header (transaction .id if transaction else None )
285
- draft = DBRecordsBatchDraft (label , data , options )
286
- response = self .client ._make_request ('POST' , '/api/v1/records/import/json' , draft .to_json (), headers )
236
+
237
+ payload = {
238
+ "label" : label ,
239
+ "payload" : data ,
240
+ "options" : options or {
241
+ "returnResult" : True ,
242
+ "suggestTypes" : True
243
+ }
244
+ }
245
+ response = self .client ._make_request ('POST' , '/api/v1/records/import/json' , payload , headers )
287
246
288
247
print ('r:' , response )
289
248
@@ -358,8 +317,6 @@ def find_unique(self, query: Dict[str, Any], transaction: Optional[Transaction]
358
317
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 ]]:
359
318
"""Import data from CSV."""
360
319
headers = self ._build_transaction_header (transaction .id if transaction else None )
361
- # if isinstance(csv_data, str):
362
- # csv_data = csv_data.encode('utf-8')
363
320
364
321
payload = {
365
322
"label" : label ,
@@ -370,12 +327,7 @@ def import_csv(self, label: str, csv_data: Union[str, bytes], options: Optional[
370
327
}
371
328
}
372
329
373
- return self .client ._make_request (
374
- 'POST' ,
375
- '/api/v1/records/import/csv' ,
376
- payload ,
377
- headers ,
378
- )
330
+ return self .client ._make_request ('POST' ,'/api/v1/records/import/csv' , payload , headers )
379
331
380
332
@staticmethod
381
333
def _build_transaction_header (transaction_id : Optional [str ] = None ) -> Optional [Dict [str , str ]]:
@@ -391,6 +343,8 @@ def _extract_target_ids(target: Union[str, List[str], Dict[str, Any], List[Dict[
391
343
return [t ['__id' ] if isinstance (t , dict ) and '__id' in t else t for t in target ]
392
344
elif isinstance (target , Record ) and '__id' in target .data :
393
345
return [target .data ['__id' ]]
346
+ elif isinstance (target , dict ) and '__id' in target :
347
+ return [target ['__id' ]]
394
348
raise ValueError ("Invalid target format" )
395
349
396
350
class PropertyAPI :
0 commit comments