Skip to content

Commit 283f97c

Browse files
Update asession documentation to use @rx.event(background=True) and add state locking
Co-Authored-By: Alek Petuskey <[email protected]>
1 parent 47275cd commit 283f97c

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

docs/database/queries.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,12 @@ import reflex as rx
206206
class AsyncUserState(rx.State):
207207
users: list[User] = []
208208

209-
@rx.background
209+
@rx.event(background=True)
210210
async def get_users_async(self):
211211
async with rx.asession() as asession:
212212
result = await asession.execute(User.select())
213-
self.users = (await result.all())
213+
async with self:
214+
self.users = (await result.all())
214215
```
215216

216217
### Async Select
@@ -222,12 +223,13 @@ class AsyncQueryUser(rx.State):
222223
name: str
223224
users: list[User] = []
224225

225-
@rx.background
226+
@rx.event(background=True)
226227
async def get_users(self):
227228
async with rx.asession() as asession:
228229
stmt = User.select().where(User.username.contains(self.name))
229230
result = await asession.execute(stmt)
230-
self.users = (await result.all())
231+
async with self:
232+
self.users = (await result.all())
231233
```
232234

233235
### Async Insert
@@ -239,7 +241,7 @@ class AsyncAddUser(rx.State):
239241
username: str
240242
email: str
241243

242-
@rx.background
244+
@rx.event(background=True)
243245
async def add_user(self):
244246
async with rx.asession() as asession:
245247
asession.add(User(username=self.username, email=self.email))
@@ -255,7 +257,7 @@ class AsyncChangeEmail(rx.State):
255257
username: str
256258
email: str
257259

258-
@rx.background
260+
@rx.event(background=True)
259261
async def modify_user(self):
260262
async with rx.asession() as asession:
261263
stmt = User.select().where(User.username == self.username)
@@ -275,7 +277,7 @@ To delete a user asynchronously:
275277
class AsyncRemoveUser(rx.State):
276278
username: str
277279

278-
@rx.background
280+
@rx.event(background=True)
279281
async def delete_user(self):
280282
async with rx.asession() as asession:
281283
stmt = User.select().where(User.username == self.username)
@@ -294,10 +296,11 @@ Similar to the regular session, you can refresh an object to ensure all fields a
294296
class AsyncAddUserForm(rx.State):
295297
user: User | None = None
296298

297-
@rx.background
299+
@rx.event(background=True)
298300
async def add_user(self, form_data: dict[str, str]):
299301
async with rx.asession() as asession:
300-
self.user = User(**form_data)
302+
async with self:
303+
self.user = User(**form_data)
301304
asession.add(self.user)
302305
await asession.commit()
303306
await asession.refresh(self.user)
@@ -311,7 +314,7 @@ You can also execute raw SQL asynchronously:
311314
class AsyncRawSQL(rx.State):
312315
users: list[list] = []
313316

314-
@rx.background
317+
@rx.event(background=True)
315318
async def insert_user_raw(self, username, email):
316319
async with rx.asession() as asession:
317320
await asession.execute(
@@ -323,17 +326,19 @@ class AsyncRawSQL(rx.State):
323326
)
324327
await asession.commit()
325328

326-
@rx.background
329+
@rx.event(background=True)
327330
async def get_raw_users(self):
328331
async with rx.asession() as asession:
329332
result = await asession.execute("SELECT * FROM user")
330-
self.users = [list(row) for row in (await result.all())]
333+
async with self:
334+
self.users = [list(row) for row in (await result.all())]
331335
```
332336

333337
```md alert info
334338
# Important Notes for Async Database Operations
335-
- Always use the `@rx.background` decorator for async event handlers
339+
- Always use the `@rx.event(background=True)` decorator for async event handlers
336340
- Most operations against the `asession` must be awaited, including `commit()`, `execute()`, `refresh()`, and `delete()`
337341
- The `add()` method does not need to be awaited
338342
- Result objects from queries have methods like `all()` and `first()` that must be awaited
343+
- Use `async with self:` when updating state variables in background tasks
339344
```

0 commit comments

Comments
 (0)