Skip to content

Commit d11b39a

Browse files
authored
Async ORM mutations and queries (#134)
- Disable `thread_sensitive` where it's not needed - Perform queries and mutations using async - `use_query` now supports async functions. - `use_mutation` now supports async functions. - `django_idom.types.QueryOptions.thread_sensitive` option to customize how sync queries are executed. - `django_idom.hooks.use_mutation` now accepts `django_idom.types.MutationOptions` option to customize how mutations are executed. - Use Python's arg/kwarg handlers to properly interpret and differentiate between `kwarg` and `args`. This allow things such as `query=my_function` to be properly handled. - The `mutate` argument on `django_idom.hooks.use_mutation` has been renamed to `mutation`. - Add tests for our database routing feature - Reduce runtime for tests from ~181 seconds to ~11 seconds by only starting the Django server once - Fix bug where ReactPy utilizes Django's default cache timeout, which can prematurely expire our cache entries.
1 parent 1bea69f commit d11b39a

33 files changed

+855
-184
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ logs
88
*.pyc
99
.dccachea
1010
__pycache__
11-
db.sqlite3
11+
*.sqlite3
12+
*.sqlite3-journal
1213
media
1314
cache
1415
static-deploy

CHANGELOG.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,22 @@ Using the following categories, list your changes in this order:
3434

3535
## [Unreleased]
3636

37-
- Nothing (yet)!
37+
### Added
38+
39+
- `use_query` now supports async functions.
40+
- `use_mutation` now supports async functions.
41+
- `reactpy_django.types.QueryOptions.thread_sensitive` option to customize how sync queries are executed.
42+
- `reactpy_django.hooks.use_mutation` now accepts `reactpy_django.types.MutationOptions` option to customize how mutations are executed.
43+
44+
### Changed
45+
46+
- The `mutate` argument on `reactpy_django.hooks.use_mutation` has been renamed to `mutation`.
47+
48+
### Fixed
49+
50+
- Fix bug where ReactPy utilizes Django's default cache timeout, which can prematurely expire the component cache.
3851

39-
## [3.0.1] - 2023-03-31
52+
## [3.0.1] - 2023-04-06
4053

4154
### Changed
4255

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</td>
2828
<td>
2929
<a href="https://github.com/reactive-python/reactpy-django">Django</a>,
30-
<a href="https://github.com/idom-team/idom-jupyter">Jupyter</a>,
30+
<a href="https://github.com/reactive-python/reactpy-jupyter">Jupyter</a>,
3131
<a href="https://github.com/idom-team/idom-dash">Plotly-Dash</a>
3232
</td>
3333
</tr>

docs/python/use-query-async.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from channels.db import database_sync_to_async
2+
from example.models import TodoItem
3+
from reactpy import component, html
4+
5+
from reactpy_django.hooks import use_query
6+
7+
8+
async def get_items():
9+
return await database_sync_to_async(TodoItem.objects.all)()
10+
11+
12+
@component
13+
def todo_list():
14+
item_query = use_query(get_items)
15+
16+
if item_query.loading:
17+
rendered_items = html.h2("Loading...")
18+
elif item_query.error or not item_query.data:
19+
rendered_items = html.h2("Error when loading!")
20+
else:
21+
rendered_items = html.ul([html.li(item, key=item) for item in item_query.data])
22+
23+
return html.div("Rendered items: ", rendered_items)

docs/python/use-query-postprocessor-change.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def execute_io_intensive_operation():
1717

1818

1919
@component
20-
def todo_list():
20+
def my_component():
2121
query = use_query(
2222
QueryOptions(
2323
postprocessor=my_postprocessor,

docs/python/use-query-postprocessor-disable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def execute_io_intensive_operation():
1010

1111

1212
@component
13-
def todo_list():
13+
def my_component():
1414
query = use_query(
1515
QueryOptions(postprocessor=None),
1616
execute_io_intensive_operation,

docs/python/use-query-postprocessor-kwargs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_model_with_relationships():
1616

1717

1818
@component
19-
def todo_list():
19+
def my_component():
2020
query = use_query(
2121
QueryOptions(
2222
postprocessor_kwargs={"many_to_many": False, "many_to_one": False}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from reactpy import component
2+
3+
from reactpy_django.hooks import use_query
4+
from reactpy_django.types import QueryOptions
5+
6+
7+
def execute_thread_safe_operation():
8+
"""This is an example query function that does some thread-safe operation."""
9+
pass
10+
11+
12+
@component
13+
def my_component():
14+
query = use_query(
15+
QueryOptions(thread_sensitive=False),
16+
execute_thread_safe_operation,
17+
)
18+
19+
if query.loading or query.error:
20+
return None
21+
22+
return str(query.data)

docs/python/use-query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ def todo_list():
1717
elif item_query.error or not item_query.data:
1818
rendered_items = html.h2("Error when loading!")
1919
else:
20-
rendered_items = html.ul(html.li(item, key=item) for item in item_query.data)
20+
rendered_items = html.ul([html.li(item, key=item) for item in item_query.data])
2121

2222
return html.div("Rendered items: ", rendered_items)

docs/src/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ postprocessing
3030
serializable
3131
postprocessor
3232
preprocessor
33+
middleware
3334
backends
3435
backend
3536
frontend

0 commit comments

Comments
 (0)