surrealdb-orm 0.1.0__py3-none-any.whl → 0.1.1__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.

Potentially problematic release.


This version of surrealdb-orm might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: surrealdb-orm
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: SurrealDB ORM as 'DJango style' for Python with async support. Works with pydantic validation.
5
5
  Project-URL: Homepage, https://github.com/EulogySnowfall/SurrealDB-ORM
6
6
  Project-URL: Documentation, https://github.com/EulogySnowfall/SurrealDB-ORM
@@ -68,9 +68,9 @@ Description-Content-Type: text/markdown
68
68
 
69
69
  ---
70
70
 
71
- ## :white_check_mark: Version
71
+ ## Version
72
72
 
73
- Alpha 0.1.0
73
+ Alpha 0.1.1
74
74
 
75
75
  ---
76
76
 
@@ -0,0 +1,4 @@
1
+ surrealdb_orm-0.1.1.dist-info/METADATA,sha256=lioHv9d0_bregjzLLkLsXXfoKiayUA4km7PXzTVn4uA,5980
2
+ surrealdb_orm-0.1.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
3
+ surrealdb_orm-0.1.1.dist-info/licenses/LICENSE,sha256=TO3Ub0nPPx5NxwjsDuBAu3RBdBLmdDybqC5dem4oGts,1074
4
+ surrealdb_orm-0.1.1.dist-info/RECORD,,
src/__init__.py DELETED
@@ -1,3 +0,0 @@
1
- from .surreal_orm import BaseSurrealModel, SurrealDBConnectionManager, QuerySet
2
-
3
- __all__ = ["BaseSurrealModel", "SurrealDBConnectionManager", "QuerySet"]
@@ -1,6 +0,0 @@
1
- from .model_base import BaseSurrealModel
2
- from .connection_manager import SurrealDBConnectionManager
3
- from .query_set import QuerySet
4
- from .enum import OrderBy
5
-
6
- __all__ = ["SurrealDBConnectionManager", "BaseSurrealModel", "QuerySet", "OrderBy"]
@@ -1,301 +0,0 @@
1
- from typing import Any
2
- from surrealdb import AsyncSurrealDB
3
- from surrealdb.errors import SurrealDbConnectionError
4
- import logging
5
-
6
- logger = logging.getLogger(__name__)
7
-
8
-
9
- class SurrealDBConnectionManager:
10
- __url: str | None = None
11
- __user: str | None = None
12
- __password: str | None = None
13
- __namespace: str | None = None
14
- __database: str | None = None
15
- __client: AsyncSurrealDB | None = None
16
-
17
- async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
18
- await SurrealDBConnectionManager.close_connection()
19
-
20
- async def __aenter__(self) -> AsyncSurrealDB:
21
- return await SurrealDBConnectionManager.get_client()
22
-
23
- @classmethod
24
- def set_connection(cls, url: str, user: str, password: str, namespace: str, database: str) -> None:
25
- """
26
- Set the connection kwargs for the SurrealDB instance.
27
-
28
- :param kwargs: The connection kwargs for the SurrealDB instance.
29
- """
30
- cls.__url = url
31
- cls.__user = user
32
- cls.__password = password
33
- cls.__namespace = namespace
34
- cls.__database = database
35
-
36
- @classmethod
37
- async def unset_connection(cls) -> None:
38
- """
39
- Set the connection kwargs for the SurrealDB instance.
40
-
41
- :param kwargs: The connection kwargs for the SurrealDB instance.
42
- """
43
- cls.__url = None
44
- cls.__user = None
45
- cls.__password = None
46
- cls.__namespace = None
47
- cls.__database = None
48
- await cls.close_connection()
49
-
50
- @classmethod
51
- def is_connection_set(cls) -> bool:
52
- """
53
- Check if the connection kwargs are set.
54
-
55
- :return: True if the connection kwargs are set, False otherwise.
56
- """
57
- return all([cls.__url, cls.__user, cls.__password, cls.__namespace, cls.__database])
58
-
59
- @classmethod
60
- async def get_client(cls) -> AsyncSurrealDB:
61
- """
62
- Connect to the SurrealDB instance.
63
-
64
- :return: The SurrealDB instance.
65
- """
66
-
67
- if cls.__client is not None:
68
- return cls.__client
69
-
70
- if not cls.is_connection_set():
71
- raise ValueError("Connection not been set.")
72
-
73
- # Établir la connexion
74
- try:
75
- _client = AsyncSurrealDB(cls.get_connection_string())
76
- await _client.connect() # type: ignore
77
- await _client.use(cls.__namespace, cls.__database) # type: ignore
78
- await _client.sign_in(cls.__user, cls.__password) # type: ignore
79
-
80
- cls.__client = _client
81
- return cls.__client
82
- except Exception as e:
83
- logger.warning(f"Can't get connection: {e}")
84
- if isinstance(cls.__client, AsyncSurrealDB): # pragma: no cover
85
- await cls.__client.close()
86
- cls.__client = None
87
- raise SurrealDbConnectionError("Can't connect to the database.")
88
-
89
- @classmethod
90
- async def close_connection(cls) -> None:
91
- """
92
- Close the connection to the SurrealDB instance.
93
- """
94
- # Fermer la connexion
95
-
96
- if cls.__client is None:
97
- return
98
-
99
- await cls.__client.close()
100
- cls.__client = None
101
-
102
- @classmethod
103
- async def reconnect(cls) -> AsyncSurrealDB | None:
104
- """
105
- Reconnect to the SurrealDB instance.
106
- """
107
- # Fermer la connexion
108
- await cls.close_connection()
109
- # Établir la connexion
110
- return await cls.get_client()
111
-
112
- @classmethod
113
- async def validate_connection(cls) -> bool:
114
- """
115
- Validate the connection to the SurrealDB instance.
116
-
117
- :return: True if the connection is valid, False otherwise.
118
- """
119
- # Valider la connexion
120
- try:
121
- await cls.reconnect()
122
- return True
123
- except SurrealDbConnectionError:
124
- return False
125
-
126
- @classmethod
127
- def get_connection_string(cls) -> str | None:
128
- """
129
- Get the connection string for the SurrealDB instance.
130
-
131
- :return: The connection string for the SurrealDB instance.
132
- """
133
- return cls.__url
134
-
135
- @classmethod
136
- def get_connection_kwargs(cls) -> dict[str, str | None]:
137
- """
138
- Get the connection kwargs for the SurrealDB instance.
139
-
140
- :return: The connection kwargs for the SurrealDB instance.
141
- """
142
- return {
143
- "url": cls.__url,
144
- "user": cls.__user,
145
- "namespace": cls.__namespace,
146
- "database": cls.__database,
147
- }
148
-
149
- @classmethod
150
- async def set_url(cls, url: str, reconnect: bool = False) -> bool:
151
- """
152
- Set the URL for the SurrealDB instance.
153
-
154
- :param url: The URL of the SurrealDB instance.
155
- """
156
-
157
- if not cls.is_connection_set():
158
- raise ValueError("You can't change the URL when the others setting are not already set.")
159
-
160
- cls.__url = url
161
-
162
- if reconnect:
163
- if not await cls.validate_connection(): # pragma: no cover
164
- cls.__url = None
165
- return False
166
-
167
- return True
168
-
169
- @classmethod
170
- async def set_user(cls, user: str, reconnect: bool = False) -> bool:
171
- """
172
- Set the username for authentication.
173
-
174
- :param user: The username for authentication.
175
- """
176
-
177
- if not cls.is_connection_set():
178
- raise ValueError("You can't change the User when the others setting are not already set.")
179
-
180
- cls.__user = user
181
-
182
- if reconnect:
183
- if not await cls.validate_connection(): # pragma: no cover
184
- cls.__user = None
185
- return False
186
-
187
- return True
188
-
189
- @classmethod
190
- async def set_password(cls, password: str, reconnect: bool = False) -> bool:
191
- """
192
- Set the password for authentication.
193
-
194
- :param password: The password for authentication.
195
- """
196
-
197
- if not cls.is_connection_set():
198
- raise ValueError("You can't change the password when the others setting are not already set.")
199
-
200
- cls.__password = password
201
-
202
- if reconnect:
203
- if not await cls.validate_connection(): # pragma: no cover
204
- cls.__password = None
205
- return False
206
-
207
- return True
208
-
209
- @classmethod
210
- async def set_namespace(cls, namespace: str, reconnect: bool = False) -> bool:
211
- """
212
- Set the namespace to use.
213
-
214
- :param namespace: The namespace to use.
215
- """
216
-
217
- if not cls.is_connection_set():
218
- raise ValueError("You can't change the namespace when the others setting are not already set.")
219
-
220
- cls.__namespace = namespace
221
-
222
- if reconnect:
223
- if not await cls.validate_connection(): # pragma: no cover
224
- cls.__namespace = None
225
- return False
226
-
227
- return True
228
-
229
- @classmethod
230
- async def set_database(cls, database: str, reconnect: bool = False) -> bool:
231
- """
232
- Set the database to use.
233
-
234
- :param database: The database to use.
235
- """
236
- if not cls.is_connection_set():
237
- raise ValueError("You can't change the database when the others setting are not already set.")
238
-
239
- cls.__database = database
240
-
241
- if reconnect:
242
- if not await cls.validate_connection(): # pragma: no cover
243
- cls.__database = None
244
- return False
245
-
246
- return True
247
-
248
- @classmethod
249
- def get_url(cls) -> str | None:
250
- """
251
- Get the URL of the SurrealDB instance.
252
-
253
- :return: The URL of the SurrealDB instance.
254
- """
255
- return cls.__url
256
-
257
- @classmethod
258
- def get_user(cls) -> str | None:
259
- """
260
- Get the username for authentication.
261
-
262
- :return: The username for authentication.
263
- """
264
- return cls.__user
265
-
266
- @classmethod
267
- def get_namespace(cls) -> str | None:
268
- """
269
- Get the namespace to use.
270
-
271
- :return: The namespace to use.
272
- """
273
- return cls.__namespace
274
-
275
- @classmethod
276
- def get_database(cls) -> str | None:
277
- """
278
- Get the database to use.
279
-
280
- :return: The database to use.
281
- """
282
- return cls.__database
283
-
284
- @classmethod
285
- def is_password_set(cls) -> bool:
286
- """
287
- Get the database to use.
288
-
289
- :return: The database to use.
290
- """
291
- return cls.__password is not None
292
-
293
- @classmethod
294
- def is_connected(cls) -> bool:
295
- """
296
- Check if the connection to the SurrealDB instance is established.
297
-
298
- :return: True if the connection is established, False otherwise.
299
- """
300
-
301
- return cls.__client is not None
@@ -1,20 +0,0 @@
1
- LOOKUP_OPERATORS = {
2
- "exact": "=",
3
- "gt": ">",
4
- "gte": ">=",
5
- "lt": "<",
6
- "lte": "<=",
7
- "in": "IN",
8
- "like": "LIKE",
9
- "ilike": "ILIKE",
10
- "contains": "CONTAINS",
11
- "icontains": "CONTAINS",
12
- "startswith": "STARTSWITH",
13
- "istartswith": "STARTSWITH",
14
- "endswith": "ENDSWITH",
15
- "iendswith": "ENDSWITH",
16
- "match": "MATCH",
17
- "regex": "REGEX",
18
- "iregex": "REGEX",
19
- "isnull": "IS",
20
- }
src/surreal_orm/enum.py DELETED
@@ -1,12 +0,0 @@
1
- from enum import StrEnum
2
-
3
-
4
- class OrderBy(StrEnum):
5
- ASC = "ASC"
6
- DESC = "DESC"
7
-
8
-
9
- class Operator(StrEnum):
10
- AND = "AND"
11
- OR = "OR"
12
- NOT = "NOT"
@@ -1,229 +0,0 @@
1
- from typing import Any, Type, Self
2
- from pydantic import BaseModel, create_model, ConfigDict
3
- from .connection_manager import SurrealDBConnectionManager
4
- from surrealdb import RecordID, SurrealDbError
5
-
6
- import warnings
7
- import logging
8
-
9
- warnings.filterwarnings("ignore", message="fields may not start with an underscore", category=RuntimeWarning)
10
-
11
- logger = logging.getLogger(__name__)
12
-
13
-
14
- class BaseSurrealModel(BaseModel):
15
- """
16
- Base class for models interacting with SurrealDB.
17
- """
18
-
19
- __pydantic_model_cache__: Type[BaseModel] | None = None
20
-
21
- def __init__(self, **data: Any):
22
- model_cls = self._init_model()
23
- instance = model_cls(**data)
24
- object.__setattr__(self, "_data", instance.model_dump())
25
- object.__setattr__(self, "_table_name", self.__class__.__name__)
26
-
27
- def __getattr__(self, item: str) -> Any:
28
- """
29
- If the item is a field in _data, return it,
30
- otherwise, let the normal mechanism raise AttributeError.
31
- """
32
- _data = object.__getattribute__(self, "_data")
33
- if item in _data:
34
- return _data[item]
35
- raise AttributeError(f"'{type(self).__name__}' object has no attribute '{item}'.")
36
-
37
- def __str__(self) -> str:
38
- return f"{self._data}"
39
-
40
- def __setattr__(self, key: str, value: Any) -> None:
41
- """
42
- If we want to allow updates, reinstantiate a Pydantic model
43
- with the new value.
44
- """
45
- if key in ("_data",): # and other internal attributes
46
- object.__setattr__(self, key, value)
47
- else:
48
- # Update the dict, validate via Pydantic, etc.
49
- current_data = dict(object.__getattribute__(self, "_data"))
50
- current_data[key] = value
51
- instance = self._init_model()(**current_data)
52
- object.__setattr__(self, "_data", instance.model_dump())
53
-
54
- @classmethod
55
- def from_db(cls, record: dict | list) -> Any:
56
- """
57
- Create an instance from a SurrealDB record.
58
- """
59
- if isinstance(record, list):
60
- return [cls.from_db(rs) for rs in record]
61
-
62
- record = cls.__set_data(record)
63
-
64
- return cls(**record)
65
-
66
- def to_db_dict(self) -> dict[str, Any]:
67
- """
68
- Return a dictionary ready to be inserted into the database.
69
- """
70
- data_set = {key: value for key, value in self._data.items() if not key.startswith("_") and key != "id"}
71
- return data_set
72
-
73
- def show_config(self) -> ConfigDict:
74
- # Accès depuis une méthode d'instance
75
- return type(self).model_config
76
-
77
- def get_id(self) -> str | RecordID | None:
78
- if "id" in self._data:
79
- return self._data["id"]
80
-
81
- config = self.show_config()
82
- pk_field = config.get("primary_key", "id")
83
- return self._data.get(pk_field, None)
84
-
85
- @staticmethod
86
- def __set_data(data: Any) -> dict:
87
- """
88
- Set the model instance data.
89
- """
90
- if isinstance(data, dict): # pragma: no cover
91
- if "id" in data and isinstance(data["id"], RecordID): # pragma: no cover
92
- data["id"] = str(data["id"]).split(":")[1]
93
- return data
94
-
95
- raise TypeError("Data must be a dictionary.") # pragma: no cover
96
-
97
- async def refresh(self) -> None:
98
- """
99
- Refresh the model instance from the database.
100
- """
101
- client = await SurrealDBConnectionManager.get_client()
102
- record = None
103
-
104
- id = self.get_id()
105
- record = await client.select(f"{self._table_name}:{id}")
106
-
107
- self._data = self.__set_data(record)
108
-
109
- async def save(self) -> Self:
110
- """
111
- Save the model instance to the database.
112
- """
113
- client = await SurrealDBConnectionManager.get_client()
114
-
115
- data = self.to_db_dict()
116
- id = self.get_id()
117
- if id:
118
- thing = f"{self._table_name}:{id}"
119
- await client.create(thing, data)
120
- return self
121
- # Auto-generate the ID
122
- record = await client.create(self._table_name, data) # pragma: no cover
123
- if isinstance(record, dict): # pragma: no cover
124
- self._data = self.__set_data(record)
125
-
126
- return self
127
-
128
- async def update(self) -> Any:
129
- """
130
- Update the model instance to the database.
131
- """
132
- client = await SurrealDBConnectionManager.get_client()
133
-
134
- data = self.to_db_dict()
135
- id = self.get_id()
136
- if id:
137
- thing = f"{self._table_name}:{id}"
138
- return await client.update(thing, data)
139
-
140
- raise SurrealDbError("Can't update data, no id found.")
141
-
142
- async def merge(self, **data: Any) -> Any:
143
- """
144
- Update the model instance to the database.
145
- """
146
-
147
- client = await SurrealDBConnectionManager.get_client()
148
- data_set = {key: value for key, value in data.items()}
149
-
150
- id = self.get_id()
151
- if id:
152
- thing = f"{self._table_name}:{id}"
153
-
154
- await client.merge(thing, data_set)
155
- await self.refresh()
156
- return
157
-
158
- raise SurrealDbError(f"No Id for the data to merge: {data}")
159
-
160
- async def delete(self) -> None:
161
- """
162
- Delete the model instance from the database.
163
- """
164
-
165
- client = await SurrealDBConnectionManager.get_client()
166
-
167
- id = self.get_id()
168
-
169
- thing = f"{self._table_name}:{id}"
170
-
171
- deleted = await client.delete(thing)
172
-
173
- if not deleted:
174
- raise SurrealDbError(f"Can't delete Record id -> '{id}' not found!")
175
-
176
- logger.info(f"Record deleted -> {deleted}.")
177
- self._data = {}
178
- del self
179
-
180
- @classmethod
181
- def _init_model(cls) -> Any:
182
- """
183
- Generate a real Pydantic model only once (per subclass)
184
- from the fields annotated in the class inheriting from BaseSurrealModel.
185
- """
186
- if cls.__pydantic_model_cache__ is not None:
187
- return cls.__pydantic_model_cache__
188
-
189
- # Retrieve the annotations declared in the class (e.g., ModelTest)
190
- hints: dict[str, Any] = {}
191
- config_dict = None
192
- for base in reversed(cls.__mro__): # To capture all annotations
193
- hints.update(getattr(base, "__annotations__", {}))
194
- # Optionally, check if the class has 'model_config' to inject it
195
- if hasattr(base, "model_config"):
196
- config_dict = getattr(base, "model_config")
197
-
198
- # Create the Pydantic model (dynamically)
199
- fields = {}
200
- for field_name, field_type in hints.items():
201
- # Read the object already defined in the class (if Field(...))
202
- default_val = getattr(cls, field_name, ...)
203
- fields[field_name] = (field_type, default_val)
204
-
205
- # Create model
206
- if config_dict:
207
- pyd_model = create_model( # type: ignore
208
- f"{cls.__name__}PydModel",
209
- __config__=config_dict,
210
- **fields,
211
- )
212
- else:
213
- pyd_model = create_model( # type: ignore
214
- f"{cls.__name__}PydModel",
215
- __base__=BaseModel,
216
- **fields,
217
- )
218
-
219
- cls.__pydantic_model_cache__ = pyd_model
220
- return pyd_model
221
-
222
- @classmethod
223
- def objects(cls) -> Any:
224
- """
225
- Return a QuerySet for the model class.
226
- """
227
- from .query_set import QuerySet
228
-
229
- return QuerySet(cls)
@@ -1,492 +0,0 @@
1
- from .constants import LOOKUP_OPERATORS
2
- from .enum import OrderBy
3
- from .utils import remove_quotes_for_variables
4
- from surrealdb import QueryResponse, Table, AsyncSurrealDB
5
- from surrealdb.errors import SurrealDbError
6
- from . import BaseSurrealModel, SurrealDBConnectionManager
7
- from typing import Self, Any, cast
8
- from pydantic_core import ValidationError
9
-
10
- import logging
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 = {key: value for key, value in 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
- data: dict[str, Any] = {"result": []}
300
- query = self._compile_query()
301
- results = await self._execute_query(query)
302
- try:
303
- data = cast(dict, results[0])
304
-
305
- return self.model.from_db(data["result"])
306
- except ValidationError as e:
307
- logger.info(f"Pydantic invalid format for the class, returning dict value: {e}")
308
- return data["result"]
309
-
310
- async def first(self) -> Any:
311
- """
312
- Execute the query and return the first result.
313
-
314
- This method modifies the QuerySet to limit the results to one and retrieves the first record.
315
- If no records are found, it returns `None`.
316
-
317
- Returns:
318
- BaseSurrealModel | dict | None: The first model instance if available, a dictionary if
319
- model validation fails, or `None` if no results are found.
320
-
321
- Raises:
322
- SurrealDbError: If there is an issue executing the query.
323
-
324
- Example:
325
- ```python
326
- first_user = await queryset.filter(name='Alice').first()
327
- ```
328
- """
329
- self._limit = 1
330
- results = await self.exec()
331
- if results:
332
- return results[0]
333
-
334
- raise SurrealDbError("No result found.")
335
-
336
- async def get(self, id_item: Any = None) -> Any:
337
- """
338
- Retrieve a single record by its unique identifier or based on the current QuerySet filters.
339
-
340
- This method fetches a specific record by its ID if provided. If no ID is provided, it attempts
341
- to retrieve a single record based on the existing filters. It raises an error if multiple or
342
- no records are found when no ID is specified.
343
-
344
- Args:
345
- id_item (str | None, optional): The unique identifier of the item to retrieve. Defaults to `None`.
346
-
347
- Returns:
348
- BaseSurrealModel | dict[str, Any]: The retrieved model instance or a dictionary representing the raw data.
349
-
350
- Raises:
351
- SurrealDbError: If multiple records are found when `id_item` is not provided or if no records are found.
352
-
353
- Example:
354
- ```python
355
- user = await queryset.get(id_item='user:123')
356
- ```
357
- """
358
- if id_item:
359
- client = await SurrealDBConnectionManager.get_client()
360
- data = await client.select(f"{self._model_table}:{id_item}")
361
- return self.model.from_db(data)
362
- else:
363
- result = await self.exec()
364
- if len(result) > 1:
365
- raise SurrealDbError("More than one result found.")
366
-
367
- if len(result) == 0:
368
- raise SurrealDbError("No result found.")
369
- return result[0]
370
-
371
- async def all(self) -> Any:
372
- """
373
- Fetch all records from the associated table.
374
-
375
- This method retrieves every record from the table without applying any filters, limits, or ordering.
376
-
377
- Returns:
378
- list[BaseSurrealModel]: A list of model instances representing all records in the table.
379
-
380
- Raises:
381
- SurrealDbError: If there is an issue executing the query.
382
-
383
- Example:
384
- ```python
385
- all_users = await queryset.all()
386
- ```
387
- """
388
- client = await SurrealDBConnectionManager.get_client()
389
- results = await client.select(Table(self._model_table))
390
- return self.model.from_db(results)
391
-
392
- async def _execute_query(self, query: str) -> list[QueryResponse]:
393
- """
394
- Execute the given SQL query using the SurrealDB client.
395
-
396
- This internal method handles the execution of the compiled SQL query and returns the raw results
397
- from the database.
398
-
399
- Args:
400
- query (str): The SQL query string to execute.
401
-
402
- Returns:
403
- list[QueryResponse]: A list of `QueryResponse` objects containing the query results.
404
-
405
- Raises:
406
- SurrealDbError: If there is an issue executing the query.
407
-
408
- Example:
409
- ```python
410
- results = await self._execute_query("SELECT * FROM users;")
411
- ```
412
- """
413
- client = await SurrealDBConnectionManager.get_client()
414
- return await self._run_query_on_client(client, query)
415
-
416
- async def _run_query_on_client(self, client: AsyncSurrealDB, query: str) -> list[QueryResponse]:
417
- """
418
- Run the SQL query on the provided SurrealDB client.
419
-
420
- This internal method sends the query to the SurrealDB client along with any predefined variables
421
- and returns the raw query responses.
422
-
423
- Args:
424
- client (AsyncSurrealDB): The active SurrealDB client instance.
425
- query (str): The SQL query string to execute.
426
-
427
- Returns:
428
- list[QueryResponse]: A list of `QueryResponse` objects containing the query results.
429
-
430
- Raises:
431
- SurrealDbError: If there is an issue executing the query.
432
-
433
- Example:
434
- ```python
435
- results = await self._run_query_on_client(client, "SELECT * FROM users;")
436
- ```
437
- """
438
- return await client.query(remove_quotes_for_variables(query), self._variables) # type: ignore
439
-
440
- async def delete_table(self) -> bool:
441
- """
442
- Delete the associated table from the SurrealDB database.
443
-
444
- This method performs a destructive operation by removing the entire table from the database.
445
- Use with caution, especially in production environments.
446
-
447
- Returns:
448
- bool: `True` if the table was successfully deleted.
449
-
450
- Raises:
451
- SurrealDbError: If there is an issue deleting the table.
452
-
453
- Example:
454
- ```python
455
- success = await queryset.delete_table()
456
- ```
457
- """
458
- client = await SurrealDBConnectionManager.get_client()
459
- await client.delete(Table(self._model_table))
460
- return True
461
-
462
- async def query(self, query: str, variables: dict[str, Any] = {}) -> Any:
463
- """
464
- Execute a custom SQL query on the SurrealDB database.
465
-
466
- This method allows running arbitrary SQL queries, provided they operate on the correct table
467
- associated with the current model. It ensures that the query includes the `FROM` clause referencing
468
- the correct table to maintain consistency and security.
469
-
470
- Args:
471
- query (str): The custom SQL query string to execute.
472
- variables (dict[str, Any], optional): A dictionary of variables to substitute into the query.
473
- Defaults to an empty dictionary.
474
-
475
- Returns:
476
- Any: The result of the query, typically a model instance or a list of model instances.
477
-
478
- Raises:
479
- SurrealDbError: If the query does not include the correct `FROM` clause or if there is an issue executing the query.
480
-
481
- Example:
482
- ```python
483
- custom_query = "SELECT name, email FROM UserModel WHERE status = $status;"
484
- results = await queryset.query(custom_query, variables={'status': 'active'})
485
- ```
486
- """
487
- if f"FROM {self.model.__name__}" not in query:
488
- raise SurrealDbError(f"The query must include 'FROM {self.model.__name__}' to reference the correct table.")
489
- client = await SurrealDBConnectionManager.get_client()
490
- results = await client.query(remove_quotes_for_variables(query), variables)
491
- data = cast(dict, results[0])
492
- return self.model.from_db(data["result"])
src/surreal_orm/utils.py DELETED
@@ -1,6 +0,0 @@
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)
@@ -1,12 +0,0 @@
1
- src/__init__.py,sha256=xTUdxEAFyihYHcJNlF2Icz67B9tb05iLpLAQUYMmyGc,154
2
- src/surreal_orm/__init__.py,sha256=RNoDt0Gt7_jTjzxzXGPXKUqe7EnCdt31b35tnA-JcOM,243
3
- src/surreal_orm/connection_manager.py,sha256=kRAK6qhkKRVbhTjWZV6LaOGiH_jClwQ3zzmjca1A5Ro,8561
4
- src/surreal_orm/constants.py,sha256=CLavEca1M6cLJLqVl4l4KoE-cBrgVQNsuGxW9zGJBmg,429
5
- src/surreal_orm/enum.py,sha256=kR-vzkHqnqy9YaYOvWTwAHdl2-WCzPcSEch-YTyJv1Y,158
6
- src/surreal_orm/model_base.py,sha256=U9ZJeOS75uJXMvhKTNy8I2QDXMxoF5eVUkQe-vesggk,7358
7
- src/surreal_orm/query_set.py,sha256=ng-jTFWiUpxPJH-KC7SNxZ2PwmciCnkOifOWa7A2KEs,18156
8
- src/surreal_orm/utils.py,sha256=mni_dTtb4VGTdge8eWSZpBw5xoWci2m-XThKFHYPKTo,171
9
- surrealdb_orm-0.1.0.dist-info/METADATA,sha256=rwtOCPWWy-_x-qMwlx8jUUrROJsBAN6YWSSTtQPC-8Q,5995
10
- surrealdb_orm-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
11
- surrealdb_orm-0.1.0.dist-info/licenses/LICENSE,sha256=TO3Ub0nPPx5NxwjsDuBAu3RBdBLmdDybqC5dem4oGts,1074
12
- surrealdb_orm-0.1.0.dist-info/RECORD,,