Skip to content

Commit a40fbd3

Browse files
committed
doc: Update documentation for DBConnection and session handling
1 parent 9c904da commit a40fbd3

3 files changed

Lines changed: 12 additions & 70 deletions

File tree

docs/api/db-connection-helper.md

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ The `DBConnection` class has the following methods:
4141

4242
#### init_db
4343

44-
Initialize the database tables. It also sets the session of the base model to
45-
the `async_scoped_session` async session factory:
44+
Initialize the database tables for the given base models:
4645

4746
```python
4847
from sqlactive import DBConnection
@@ -53,7 +52,7 @@ asyncio.run(conn.init_db()) # Initialize the database
5352
```
5453

5554
If your base model is not `ActiveRecordBaseModel` you must pass
56-
your base model class to this method in the `base_model` argument:
55+
your base model. You can also initialize multiple base models at once:
5756

5857
```python
5958
from sqlactive import DBConnection, ActiveRecordBaseModel
@@ -67,12 +66,13 @@ class BaseModel(ActiveRecordBaseModel):
6766
DATABASE_URL = 'sqlite+aiosqlite://'
6867
conn = DBConnection(DATABASE_URL, echo=True)
6968
asyncio.run(conn.init_db(BaseModel)) # Pass your base model
69+
# or
70+
asyncio.run(conn.init_db(BaseModel, AnotherBaseModel)) # Pass multiple base models
7071
```
7172

7273
#### close
7374

74-
Close the database connection. It also sets the session of the base model
75-
to `None`:
75+
Close the database connection and remove the session:
7676

7777
```python
7878
from sqlactive import DBConnection
@@ -83,26 +83,5 @@ asyncio.run(conn.init_db())
8383

8484
# Perform operations...
8585

86-
asyncio.run(conn.close()) # Close the database connection
87-
```
88-
89-
If your base model is not `ActiveRecordBaseModel` you should pass
90-
your base model cl0ass to this method in the `base_model` argument:
91-
92-
```python
93-
from sqlactive import DBConnection, ActiveRecordBaseModel
94-
95-
# Note that it does not matter if your base model
96-
# inherits from `ActiveRecordBaseModel`, you still
97-
# need to pass it to this method
98-
class BaseModel(ActiveRecordBaseModel):
99-
__abstract__ = True
100-
101-
DATABASE_URL = 'sqlite+aiosqlite://'
102-
conn = DBConnection(DATABASE_URL, echo=True)
103-
asyncio.run(conn.init_db())
104-
105-
# Perform operations...
106-
107-
asyncio.run(conn.close(BaseModel)) # Pass your base model
86+
asyncio.run(conn.close()) # Close the database connection and remove the sessions
10887
```
Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,21 @@
11
# Native SQLAlchemy queries
22

33
You can asynchronously execute native SQLAlchemy queries using the
4-
`sqlactive.conn.execute` function. It uses the
5-
[`AsyncSession`](session-mixin.md) of the `ActiveRecordBaseModel` class and
6-
return a buffered `sqlalchemy.engine.Result` object.
4+
`sqlactive.conn.execute` function. It uses a `sqlalchemy.ext.asyncio.async_scoped_session`
5+
instance to perform the actual query.
76

87
## Usage
98

109
```python
1110
from sqlalchemy import select, func
12-
from sqlactive import execute
11+
from sqlactive import DBConnection, execute
12+
13+
conn = DBConnection(DATABASE_URL, echo=True)
1314

1415
query = select(User.age, func.count(User.id)).group_by(User.age)
15-
result = await execute(query)
16+
result = await execute(conn.async_scoped_session, query)
1617
```
1718

1819
The `statement`, `params` and `kwargs` arguments of this function are the
1920
same as the arguments of the `execute` method of the
2021
`sqlalchemy.ext.asyncio.AsyncSession` class.
21-
22-
If your base model is not `ActiveRecordBaseModel` you must pass your base
23-
model class to the `base_model` argument of the `execute` method:
24-
25-
```python
26-
from sqlalchemy import select, func
27-
from sqlactive import ActiveRecordBaseModel, execute
28-
29-
# Note that it does not matter if your base model
30-
# inherits from `ActiveRecordBaseModel`, you still
31-
# need to pass it to this method
32-
class BaseModel(ActiveRecordBaseModel):
33-
__abstract__ = True
34-
35-
class User(BaseModel):
36-
__tablename__ = 'users'
37-
# ...
38-
39-
query = select(User.age, func.count(User.id)).group_by(User.age)
40-
result = await execute(query, BaseModel) # or execute(query, User)
41-
```
42-
43-
???+ warning
44-
45-
Your base model must have a session in order to use this method.
46-
Otherwise, it will raise an `NoSessionError` exception.
47-
If you are not using the `DBConnection` class to initialize
48-
your base model, you can call its `set_session` method
49-
to set the session.

docs/api/session-mixin.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,3 @@ def set_session(session: async_scoped_session[AsyncSession])
6767
> # Set the session
6868
> ActiveRecordBaseModel.set_session(async_scoped_session(AsyncSession))
6969
> ```
70-
71-
#### close_session
72-
73-
```python
74-
@classmethod
75-
def close_session()
76-
```
77-
78-
> Close the async session.

0 commit comments

Comments
 (0)