Skip to content

Commit 25416cf

Browse files
committed
docs: add docs for temporary records functionality
1 parent 955ef88 commit 25416cf

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,19 @@ await user.delete()
257257
> (i.e. a boolean `is_deleted` column).
258258
259259
> [!TIP]
260-
> Check the [Active Record Mixin API Reference](https://daireto.github.io/sqlactive/api/active-record-mixin/#api-reference)
260+
> If you need to create a record for a short period of time, you can use the
261+
> `with` statement:
262+
> ```python
263+
> with User(name='Bob', age=30) as user:
264+
> ...
265+
> ```
266+
> The `with` statement will create the record and delete it at the end of the
267+
> block.
268+
>
269+
> Check the [Temporary Records documentation](https://daireto.github.io/sqlactive/api/active-record-mixin/#temporary-records)
270+
> for more information.
271+
>
272+
> Also, check the [Active Record Mixin API Reference](https://daireto.github.io/sqlactive/api/active-record-mixin/#api-reference)
261273
> to see all the available methods.
262274
263275
### 4. Perform Bulk Operations

docs/api/active-record-mixin.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,48 @@ await User.delete_all(users)
195195
await User.destroy(1, 2, 3) # Deletes users with IDs 1, 2, and 3
196196
```
197197

198+
#### Temporary Records
199+
200+
If you need to create a record for a short period of time, you can use the
201+
`with` statement:
202+
203+
```python
204+
with User(name='Bob', age=30) as user:
205+
...
206+
```
207+
208+
The `with` statement will create the record using the [`save()`](#save) method.
209+
The record will be deleted at the end of the block using the [`delete()`](#delete).
210+
211+
Here is an example of using temporary records to test the [`isnull`](smart-query-mixin.md#isnull) filter operator:
212+
213+
```python
214+
post1 = Post(
215+
title='Lorem ipsum',
216+
body='Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
217+
rating=4,
218+
user_id=1,
219+
topic='Some topic', # this post has a topic
220+
)
221+
post2 = Post(
222+
title='Lorem ipsum',
223+
body='Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
224+
rating=4,
225+
user_id=1, # this post does not have a topic
226+
)
227+
228+
# post1 and post2 will be deleted at the end of the block
229+
async with post1, post2:
230+
231+
# If isnull=True, only posts without a topic must be returned
232+
posts = await Post.where(topic__isnull=True).all()
233+
assert all([p.topic is None for p in posts]) is True
234+
235+
# If isnull=False, only posts with a topic must be returned
236+
posts = await Post.where(topic__isnull=False).all()
237+
assert all([post.topic is not None for p in posts]) is True
238+
```
239+
198240
### Querying
199241

200242
#### Basic Queries

docs/getting-started.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,21 @@ await user.delete()
191191

192192
???+ tip
193193

194-
Check the [Active Record Mixin API Reference](api/active-record-mixin.md#api-reference)
194+
If you need to create a record for a short period of time, you can use the
195+
`with` statement:
196+
197+
```python
198+
with User(name='Bob', age=30) as user:
199+
...
200+
```
201+
202+
The `with` statement will create the record and delete it at the end of the
203+
block.
204+
205+
Check the [Temporary Records documentation](api/active-record-mixin.md#temporary-records)
206+
for more information.
207+
208+
Also, check the [Active Record Mixin API Reference](api/active-record-mixin.md#api-reference)
195209
to see all the available methods.
196210

197211
### 4. Perform Bulk Operations

0 commit comments

Comments
 (0)