@@ -40,14 +40,28 @@ user = db.records.create(
40
40
)
41
41
42
42
# Find records
43
- results = db.records.find({
43
+ result = db.records.find({
44
44
" where" : {
45
45
" age" : {" $gte" : 18 },
46
46
" name" : {" $startsWith" : " J" }
47
47
},
48
48
" limit" : 10
49
49
})
50
50
51
+ # Work with SearchResult
52
+ print (f " Found { len (result)} records out of { result.total} total " )
53
+
54
+ # Iterate over results
55
+ for record in result:
56
+ print (f " User: { record.get(' name' )} (Age: { record.get(' age' )} ) " )
57
+
58
+ # Check if there are more results
59
+ if result.has_more:
60
+ print (" There are more records available" )
61
+
62
+ # Access specific records
63
+ first_user = result[0 ] if result else None
64
+
51
65
# Create relationships
52
66
company = db.records.create(
53
67
label = " COMPANY" ,
@@ -83,6 +97,166 @@ db.records.create_many("COMPANY", {
83
97
})
84
98
```
85
99
100
+ ## SearchResult API
101
+
102
+ RushDB Python SDK uses a modern ` SearchResult ` container that follows Python SDK best practices similar to boto3, google-cloud libraries, and other popular SDKs.
103
+
104
+ ### SearchResult Features
105
+
106
+ - ** List-like access** : Index, slice, and iterate like a regular list
107
+ - ** Search context** : Access total count, pagination info, and the original search query
108
+ - ** Boolean conversion** : Use in if statements naturally
109
+ - ** Pagination support** : Built-in pagination information and ` has_more ` property
110
+
111
+ ### Basic Usage
112
+
113
+ ``` python
114
+ # Perform a search
115
+ result = db.records.find({
116
+ " where" : {" status" : " active" },
117
+ " limit" : 10 ,
118
+ " skip" : 20
119
+ })
120
+
121
+ # Check if we have results
122
+ if result:
123
+ print (f " Found { len (result)} records " )
124
+
125
+ # Access search result information
126
+ print (f " Total matching records: { result.total} " )
127
+ print (f " Current page size: { result.count} " )
128
+ print (f " Records skipped: { result.skip} " )
129
+ print (f " Has more results: { result.has_more} " )
130
+ print (f " Search query: { result.search_query} " )
131
+
132
+ # Iterate over results
133
+ for record in result:
134
+ print (f " Record: { record.get(' name' )} " )
135
+
136
+ # List comprehensions work
137
+ names = [r.get(' name' ) for r in result]
138
+
139
+ # Indexing and slicing
140
+ first_record = result[0 ] if result else None
141
+ first_five = result[:5 ]
142
+ ```
143
+
144
+ ### SearchResult Properties
145
+
146
+ | Property | Type | Description |
147
+ | -------------- | --------------- | ---------------------------------------- |
148
+ | ` data ` | ` List[Record] ` | The list of record results |
149
+ | ` total ` | ` int ` | Total number of matching records |
150
+ | ` count ` | ` int ` | Number of records in current result set |
151
+ | ` limit ` | ` Optional[int] ` | Limit that was applied to the search |
152
+ | ` skip ` | ` int ` | Number of records that were skipped |
153
+ | ` has_more ` | ` bool ` | Whether there are more records available |
154
+ | ` search_query ` | ` SearchQuery ` | The search query used to generate result |
155
+
156
+ ### Pagination Example
157
+
158
+ ``` python
159
+ # Paginated search
160
+ page_size = 10
161
+ current_page = 0
162
+
163
+ while True :
164
+ result = db.records.find({
165
+ " where" : {" category" : " electronics" },
166
+ " limit" : page_size,
167
+ " skip" : current_page * page_size,
168
+ " orderBy" : {" created_at" : " desc" }
169
+ })
170
+
171
+ if not result:
172
+ break
173
+
174
+ print (f " Page { current_page + 1 } : { len (result)} records " )
175
+
176
+ for record in result:
177
+ process_record(record)
178
+
179
+ if not result.has_more:
180
+ break
181
+
182
+ current_page += 1
183
+ ```
184
+
185
+ ## Improved Record API
186
+
187
+ The Record class has been enhanced with better data access patterns and utility methods.
188
+
189
+ ### Enhanced Data Access
190
+
191
+ ``` python
192
+ # Create a record
193
+ user = db.records.create(" User" , {
194
+ " name" : " John Doe" ,
195
+ " email" : " john@example.com" ,
196
+ " age" : 30 ,
197
+ " department" : " Engineering"
198
+ })
199
+
200
+ # Safe field access with defaults
201
+ name = user.get(" name" ) # "John Doe"
202
+ phone = user.get(" phone" , " Not provided" ) # "Not provided"
203
+
204
+ # Get clean user data (excludes internal fields like __id, __label)
205
+ user_data = user.get_data()
206
+ # Returns: {"name": "John Doe", "email": "john@example.com", "age": 30, "department": "Engineering"}
207
+
208
+ # Get all data including internal fields
209
+ full_data = user.get_data(exclude_internal = False )
210
+ # Includes: __id, __label, __proptypes, etc.
211
+
212
+ # Convenient fields property
213
+ fields = user.fields # Same as user.get_data()
214
+
215
+ # Dictionary conversion
216
+ user_dict = user.to_dict() # Clean user data
217
+ full_dict = user.to_dict(exclude_internal = False ) # All data
218
+
219
+ # Direct field access
220
+ user_name = user[" name" ] # Direct access
221
+ user_id = user[" __id" ] # Internal field access
222
+ ```
223
+
224
+ ### Record Existence Checking
225
+
226
+ ``` python
227
+ # Safe existence checking (no exceptions)
228
+ if user.exists():
229
+ print (" Record is valid and accessible" )
230
+ user.update({" status" : " active" })
231
+ else :
232
+ print (" Record doesn't exist or is not accessible" )
233
+
234
+ # Perfect for validation workflows
235
+ def process_record_safely (record ):
236
+ if not record.exists():
237
+ return None
238
+ return record.get_data()
239
+
240
+ # Conditional operations
241
+ records = db.records.find({" where" : {" status" : " pending" }})
242
+ for record in records:
243
+ if record.exists():
244
+ record.update({" processed_at" : datetime.now()})
245
+ ```
246
+
247
+ ### String Representations
248
+
249
+ ``` python
250
+ user = db.records.create(" User" , {" name" : " Alice Johnson" })
251
+
252
+ print (repr (user)) # Record(id='abc-123', label='User')
253
+ print (str (user)) # User: Alice Johnson
254
+
255
+ # For records without names
256
+ product = db.records.create(" Product" , {" sku" : " ABC123" })
257
+ print (str (product)) # Product (product-id-here)
258
+ ```
259
+
86
260
## Complete Documentation
87
261
88
262
For comprehensive documentation, tutorials, and examples, please visit:
@@ -206,18 +380,18 @@ def find(
206
380
search_query : Optional[SearchQuery] = None ,
207
381
record_id : Optional[str ] = None ,
208
382
transaction : Optional[Transaction] = None
209
- ) -> List[Record]
383
+ ) -> RecordSearchResult
210
384
```
211
385
212
386
** Arguments:**
213
387
214
- - `query ` (Optional[SearchQuery]): Search query parameters
388
+ - `search_query ` (Optional[SearchQuery]): Search query parameters
215
389
- `record_id` (Optional[str ]): Optional record ID to search from
216
390
- `transaction` (Optional[Transaction]): Optional transaction object
217
391
218
392
** Returns:**
219
393
220
- - `List[Record] ` : List of matching records
394
+ - `RecordSearchResult ` : SearchResult container with matching records and metadata
221
395
222
396
** Example:**
223
397
@@ -235,7 +409,24 @@ query = {
235
409
" limit" : 10
236
410
}
237
411
238
- records = db.records.find(query = query)
412
+ result = db.records.find(query = query)
413
+
414
+ # Work with SearchResult
415
+ print (f " Found { len (result)} out of { result.total} total records " )
416
+
417
+ # Iterate over results
418
+ for record in result:
419
+ print (f " Employee: { record.get(' name' )} - { record.get(' department' )} " )
420
+
421
+ # Check pagination
422
+ if result.has_more:
423
+ print (" More results available" )
424
+
425
+ # Access specific records
426
+ first_employee = result[0 ] if result else None
427
+
428
+ # List operations
429
+ senior_employees = [r for r in result if r.get(' age' , 0 ) > 30 ]
239
430
```
240
431
241
432
### delete()
0 commit comments