surreal-orm-lite 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,503 @@
1
+ import logging
2
+ from typing import Any, Self, cast
3
+
4
+ from pydantic_core import ValidationError
5
+
6
+ from . import BaseSurrealModel, SurrealDBConnectionManager
7
+ from .constants import LOOKUP_OPERATORS
8
+ from .enum import OrderBy
9
+ from .exceptions import SurrealDbError
10
+ from .utils import remove_quotes_for_variables
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+
15
+ class QuerySet:
16
+ """
17
+ A class used to build, execute, and manage queries on a SurrealDB table associated with a specific model.
18
+
19
+ The `QuerySet` class provides a fluent interface to construct complex queries using method chaining.
20
+ It supports selecting specific fields, filtering results, ordering, limiting, and offsetting the results.
21
+ Additionally, it allows executing custom queries and managing table-level operations such as deletion.
22
+
23
+ Example:
24
+ ```python
25
+ queryset = QuerySet(UserModel)
26
+ users = await queryset.filter(age__gt=21).order_by('name').limit(10).all()
27
+ ```
28
+ """
29
+
30
+ def __init__(self, model: type[BaseSurrealModel]) -> None:
31
+ """
32
+ Initialize the QuerySet with a specific model.
33
+
34
+ This constructor sets up the initial state of the QuerySet, including the model it operates on,
35
+ default filters, selected fields, and other query parameters.
36
+
37
+ Args:
38
+ model (type[BaseSurrealModel]): The model class associated with the table. This model should
39
+ inherit from `BaseSurrealModel` and define the table name either via a `_table_name` attribute
40
+ or by defaulting to the class name.
41
+
42
+ Attributes:
43
+ model (type[BaseSurrealModel]): The model class associated with the table.
44
+ _filters (list[tuple[str, str, Any]]): A list of filter conditions as tuples of (field, lookup, value).
45
+ select_item (list[str]): A list of field names to be selected in the query.
46
+ _limit (int | None): The maximum number of records to retrieve.
47
+ _offset (int | None): The number of records to skip before starting to return records.
48
+ _order_by (str | None): The field and direction to order the results by.
49
+ _model_table (str): The name of the table in SurrealDB.
50
+ _variables (dict): A dictionary of variables to be used in the query.
51
+ """
52
+ self.model = model
53
+ self._filters: list[tuple[str, str, Any]] = []
54
+ self.select_item: list[str] = []
55
+ self._limit: int | None = None
56
+ self._offset: int | None = None
57
+ self._order_by: str | None = None
58
+ self._model_table: str = getattr(model, "_table_name", model.__name__)
59
+ self._variables: dict = {}
60
+
61
+ def select(self, *fields: str) -> Self:
62
+ """
63
+ Specify the fields to retrieve in the query.
64
+
65
+ By default, all fields are selected (`SELECT *`). This method allows you to specify
66
+ a subset of fields to be retrieved, which can improve performance by fetching only necessary data.
67
+
68
+ Args:
69
+ *fields (str): Variable length argument list of field names to select.
70
+
71
+ Returns:
72
+ Self: The current instance of QuerySet to allow method chaining.
73
+
74
+ Example:
75
+ ```python
76
+ queryset.select('id', 'name', 'email')
77
+ ```
78
+ """
79
+ # Store the list of fields to retrieve
80
+ self.select_item = list(fields)
81
+ return self
82
+
83
+ def variables(self, **kwargs: Any) -> Self:
84
+ """
85
+ Set variables for the query.
86
+
87
+ Variables can be used in parameterized queries to safely inject values without risking SQL injection.
88
+
89
+ Args:
90
+ **kwargs (Any): Arbitrary keyword arguments representing variable names and their corresponding values.
91
+
92
+ Returns:
93
+ Self: The current instance of QuerySet to allow method chaining.
94
+
95
+ Example:
96
+ ```python
97
+ queryset.variables(status='active', role='admin')
98
+ ```
99
+ """
100
+ self._variables = dict(kwargs.items())
101
+ return self
102
+
103
+ def filter(self, **kwargs: Any) -> Self:
104
+ """
105
+ Add filter conditions to the query.
106
+
107
+ This method allows adding one or multiple filter conditions to narrow down the query results.
108
+ Filters are added using keyword arguments where the key represents the field and the lookup type,
109
+ and the value represents the value to filter by.
110
+
111
+ Supported lookup types include:
112
+ - exact
113
+ - contains
114
+ - gt (greater than)
115
+ - lt (less than)
116
+ - gte (greater than or equal)
117
+ - lte (less than or equal)
118
+ - in (within a list of values)
119
+
120
+ Args:
121
+ **kwargs (Any): Arbitrary keyword arguments representing filter conditions. The key should be in the format
122
+ `field__lookup` (e.g., `age__gt=30`). If no lookup is provided, `exact` is assumed.
123
+
124
+ Returns:
125
+ Self: The current instance of QuerySet to allow method chaining.
126
+
127
+ Example:
128
+ ```python
129
+ queryset.filter(age__gt=21, status='active')
130
+ ```
131
+ """
132
+ for key, value in kwargs.items():
133
+ field_name, lookup = self._parse_lookup(key)
134
+ self._filters.append((field_name, lookup, value))
135
+ return self
136
+
137
+ def _parse_lookup(self, key: str) -> tuple[str, str]:
138
+ """
139
+ Parse the lookup type from the filter key.
140
+
141
+ This helper method splits the filter key into the field name and the lookup type.
142
+ If no lookup type is specified, it defaults to `exact`.
143
+
144
+ Args:
145
+ key (str): The filter key in the format `field__lookup` or just `field`.
146
+
147
+ Returns:
148
+ tuple[str, str]: A tuple containing the field name and the lookup type.
149
+
150
+ Example:
151
+ ```python
152
+ _parse_lookup('age__gt') # Returns ('age', 'gt')
153
+ _parse_lookup('status') # Returns ('status', 'exact')
154
+ ```
155
+ """
156
+ if "__" in key:
157
+ field_name, lookup_name = key.split("__", 1)
158
+ else:
159
+ field_name, lookup_name = key, "exact"
160
+ return field_name, lookup_name
161
+
162
+ def limit(self, value: int) -> Self:
163
+ """
164
+ Set a limit on the number of results to retrieve.
165
+
166
+ This method restricts the number of records returned by the query, which is useful for pagination
167
+ or when only a subset of results is needed.
168
+
169
+ Args:
170
+ value (int): The maximum number of records to retrieve.
171
+
172
+ Returns:
173
+ Self: The current instance of QuerySet to allow method chaining.
174
+
175
+ Example:
176
+ ```python
177
+ queryset.limit(10)
178
+ ```
179
+ """
180
+ self._limit = value
181
+ return self
182
+
183
+ def offset(self, value: int) -> Self:
184
+ """
185
+ Set an offset for the results.
186
+
187
+ This method skips a specified number of records before starting to return records.
188
+ It is commonly used in conjunction with `limit` for pagination purposes.
189
+
190
+ Args:
191
+ value (int): The number of records to skip.
192
+
193
+ Returns:
194
+ Self: The current instance of QuerySet to allow method chaining.
195
+
196
+ Example:
197
+ ```python
198
+ queryset.offset(20)
199
+ ```
200
+ """
201
+ self._offset = value
202
+ return self
203
+
204
+ def order_by(self, field_name: str, type: OrderBy = OrderBy.ASC) -> Self:
205
+ """
206
+ Set the field and direction to order the results by.
207
+
208
+ This method allows sorting the query results based on a specified field and direction
209
+ (ascending or descending).
210
+
211
+ Args:
212
+ field_name (str): The name of the field to sort by.
213
+ type (OrderBy, optional): The direction to sort by. Defaults to `OrderBy.ASC`.
214
+
215
+ Returns:
216
+ Self: The current instance of QuerySet to allow method chaining.
217
+
218
+ Example:
219
+ ```python
220
+ queryset.order_by('name', OrderBy.DESC)
221
+ ```
222
+ """
223
+ self._order_by = f"{field_name} {type}"
224
+ return self
225
+
226
+ def _compile_query(self) -> str:
227
+ """
228
+ Compile the QuerySet parameters into a SQL query string.
229
+
230
+ This method constructs the final SQL query by combining the selected fields, filters,
231
+ ordering, limit, and offset parameters.
232
+
233
+ Returns:
234
+ str: The compiled SQL query string.
235
+
236
+ Example:
237
+ ```python
238
+ query = queryset._compile_query()
239
+ # Returns something like:
240
+ # "SELECT id, name FROM users WHERE age > 21 AND status = 'active' ORDER BY name ASC LIMIT 10 START 20;"
241
+ ```
242
+ """
243
+ where_clauses = []
244
+ for field_name, lookup_name, value in self._filters:
245
+ op = LOOKUP_OPERATORS.get(lookup_name, "=")
246
+ if lookup_name == "in":
247
+ # Assuming value is iterable for 'IN' operations
248
+ formatted_values = ", ".join(repr(v) for v in value)
249
+ where_clauses.append(f"{field_name} {op} [{formatted_values}]")
250
+ else:
251
+ where_clauses.append(f"{field_name} {op} {repr(value)}")
252
+
253
+ # Construct the SELECT clause
254
+ if self.select_item:
255
+ fields = ", ".join(self.select_item)
256
+ query = f"SELECT {fields} FROM {self._model_table}"
257
+ else:
258
+ query = f"SELECT * FROM {self._model_table}"
259
+
260
+ # Append WHERE clauses if any
261
+ if where_clauses:
262
+ query += " WHERE " + " AND ".join(where_clauses)
263
+
264
+ # Append LIMIT if set
265
+ if self._limit is not None:
266
+ query += f" LIMIT {self._limit}"
267
+
268
+ # Append OFFSET (START) if set
269
+ if self._offset is not None:
270
+ query += f" START {self._offset}"
271
+
272
+ # Append ORDER BY if set
273
+ if self._order_by:
274
+ query += f" ORDER BY {self._order_by}"
275
+
276
+ query += ";"
277
+ return query
278
+
279
+ async def exec(self) -> Any:
280
+ """
281
+ Execute the compiled query and return the results.
282
+
283
+ This method runs the constructed SQL query against the SurrealDB database and processes
284
+ the results. If the data conforms to the model schema, it returns a list of model instances;
285
+ otherwise, it returns a list of dictionaries.
286
+
287
+ Returns:
288
+ list[BaseSurrealModel] | list[dict]: A list of model instances if validation is successful,
289
+ otherwise a list of dictionaries representing the raw data.
290
+
291
+ Raises:
292
+ SurrealDbError: If there is an issue executing the query.
293
+
294
+ Example:
295
+ ```python
296
+ results = await queryset.exec()
297
+ ```
298
+ """
299
+ query = self._compile_query()
300
+ results = await self._execute_query(query)
301
+ try:
302
+ # SDK 1.0.8 returns list directly from query()
303
+ if isinstance(results, list):
304
+ return self.model.from_db(results)
305
+ # Fallback for older response format
306
+ data = cast(dict, results[0])
307
+ return self.model.from_db(data.get("result", []))
308
+ except ValidationError as e:
309
+ logger.info(f"Pydantic invalid format for the class, returning dict value: {e}")
310
+ return results if isinstance(results, list) else []
311
+
312
+ async def first(self) -> Any:
313
+ """
314
+ Execute the query and return the first result.
315
+
316
+ This method modifies the QuerySet to limit the results to one and retrieves the first record.
317
+ If no records are found, it returns `None`.
318
+
319
+ Returns:
320
+ BaseSurrealModel | dict | None: The first model instance if available, a dictionary if
321
+ model validation fails, or `None` if no results are found.
322
+
323
+ Raises:
324
+ SurrealDbError: If there is an issue executing the query.
325
+
326
+ Example:
327
+ ```python
328
+ first_user = await queryset.filter(name='Alice').first()
329
+ ```
330
+ """
331
+ self._limit = 1
332
+ results = await self.exec()
333
+ if results:
334
+ return results[0]
335
+
336
+ raise SurrealDbError("No result found.")
337
+
338
+ async def get(self, id_item: Any = None) -> Any:
339
+ """
340
+ Retrieve a single record by its unique identifier or based on the current QuerySet filters.
341
+
342
+ This method fetches a specific record by its ID if provided. If no ID is provided, it attempts
343
+ to retrieve a single record based on the existing filters. It raises an error if multiple or
344
+ no records are found when no ID is specified.
345
+
346
+ Args:
347
+ id_item (str | None, optional): The unique identifier of the item to retrieve. Defaults to `None`.
348
+
349
+ Returns:
350
+ BaseSurrealModel | dict[str, Any]: The retrieved model instance or a dictionary representing the raw data.
351
+
352
+ Raises:
353
+ SurrealDbError: If multiple records are found when `id_item` is not provided or if no records are found.
354
+
355
+ Example:
356
+ ```python
357
+ user = await queryset.get(id_item='user:123')
358
+ ```
359
+ """
360
+ if id_item:
361
+ client = await SurrealDBConnectionManager.get_client()
362
+ data = await client.select(f"{self._model_table}:{id_item}")
363
+ # SDK 1.0.8 returns a list even for single record select
364
+ if isinstance(data, list):
365
+ if len(data) == 0:
366
+ raise SurrealDbError("No result found.")
367
+ data = data[0]
368
+ return self.model.from_db(data)
369
+ else:
370
+ result = await self.exec()
371
+ if len(result) > 1:
372
+ raise SurrealDbError("More than one result found.")
373
+
374
+ if len(result) == 0:
375
+ raise SurrealDbError("No result found.")
376
+ return result[0]
377
+
378
+ async def all(self) -> Any:
379
+ """
380
+ Fetch all records from the associated table.
381
+
382
+ This method retrieves every record from the table without applying any filters, limits, or ordering.
383
+
384
+ Returns:
385
+ list[BaseSurrealModel]: A list of model instances representing all records in the table.
386
+
387
+ Raises:
388
+ SurrealDbError: If there is an issue executing the query.
389
+
390
+ Example:
391
+ ```python
392
+ all_users = await queryset.all()
393
+ ```
394
+ """
395
+ client = await SurrealDBConnectionManager.get_client()
396
+ results = await client.select(self._model_table)
397
+ return self.model.from_db(results)
398
+
399
+ async def _execute_query(self, query: str) -> list[dict[str, Any]]:
400
+ """
401
+ Execute the given SQL query using the SurrealDB client.
402
+
403
+ This internal method handles the execution of the compiled SQL query and returns the raw results
404
+ from the database.
405
+
406
+ Args:
407
+ query (str): The SQL query string to execute.
408
+
409
+ Returns:
410
+ list[QueryResponse]: A list of `QueryResponse` objects containing the query results.
411
+
412
+ Raises:
413
+ SurrealDbError: If there is an issue executing the query.
414
+
415
+ Example:
416
+ ```python
417
+ results = await self._execute_query("SELECT * FROM users;")
418
+ ```
419
+ """
420
+ client = await SurrealDBConnectionManager.get_client()
421
+ return await self._run_query_on_client(client, query)
422
+
423
+ async def _run_query_on_client(self, client: Any, query: str) -> list[dict[str, Any]]:
424
+ """
425
+ Run the SQL query on the provided SurrealDB client.
426
+
427
+ This internal method sends the query to the SurrealDB client along with any predefined variables
428
+ and returns the raw query responses.
429
+
430
+ Args:
431
+ client (AsyncSurrealDB): The active SurrealDB client instance.
432
+ query (str): The SQL query string to execute.
433
+
434
+ Returns:
435
+ list[QueryResponse]: A list of `QueryResponse` objects containing the query results.
436
+
437
+ Raises:
438
+ SurrealDbError: If there is an issue executing the query.
439
+
440
+ Example:
441
+ ```python
442
+ results = await self._run_query_on_client(client, "SELECT * FROM users;")
443
+ ```
444
+ """
445
+ return await client.query(remove_quotes_for_variables(query), self._variables) # type: ignore
446
+
447
+ async def delete_table(self) -> bool:
448
+ """
449
+ Delete the associated table from the SurrealDB database.
450
+
451
+ This method performs a destructive operation by removing the entire table from the database.
452
+ Use with caution, especially in production environments.
453
+
454
+ Returns:
455
+ bool: `True` if the table was successfully deleted.
456
+
457
+ Raises:
458
+ SurrealDbError: If there is an issue deleting the table.
459
+
460
+ Example:
461
+ ```python
462
+ success = await queryset.delete_table()
463
+ ```
464
+ """
465
+ client = await SurrealDBConnectionManager.get_client()
466
+ await client.delete(self._model_table)
467
+ return True
468
+
469
+ async def query(self, query: str, variables: dict[str, Any] | None = None) -> Any:
470
+ """
471
+ Execute a custom SQL query on the SurrealDB database.
472
+
473
+ This method allows running arbitrary SQL queries, provided they operate on the correct table
474
+ associated with the current model. It ensures that the query includes the `FROM` clause referencing
475
+ the correct table to maintain consistency and security.
476
+
477
+ Args:
478
+ query (str): The custom SQL query string to execute.
479
+ variables (dict[str, Any], optional): A dictionary of variables to substitute into the query.
480
+ Defaults to an empty dictionary.
481
+
482
+ Returns:
483
+ Any: The result of the query, typically a model instance or a list of model instances.
484
+
485
+ Raises:
486
+ SurrealDbError: If the query does not include the correct `FROM` clause or if there is an issue executing the query.
487
+
488
+ Example:
489
+ ```python
490
+ custom_query = "SELECT name, email FROM UserModel WHERE status = $status;"
491
+ results = await queryset.query(custom_query, variables={'status': 'active'})
492
+ ```
493
+ """
494
+ if f"FROM {self.model.__name__}" not in query:
495
+ raise SurrealDbError(f"The query must include 'FROM {self.model.__name__}' to reference the correct table.")
496
+ client = await SurrealDBConnectionManager.get_client()
497
+ results = await client.query(remove_quotes_for_variables(query), variables or {})
498
+ # SDK 1.0.8 returns list directly from query()
499
+ if isinstance(results, list):
500
+ return self.model.from_db(results)
501
+ # Fallback for older response format
502
+ data = cast(dict, results[0])
503
+ return self.model.from_db(data.get("result", []))
@@ -0,0 +1,6 @@
1
+ import re
2
+
3
+
4
+ def remove_quotes_for_variables(query: str) -> str:
5
+ # Regex for remove single cote on variables ($)
6
+ return re.sub(r"'(\$[a-zA-Z_]\w*)'", r"\1", query)