add sqlalchemy.async_query()
This commit is contained in:
parent
76921a2841
commit
c3215c7c8b
3 changed files with 41 additions and 2 deletions
|
|
@ -5,6 +5,9 @@
|
||||||
+ `sqlalchemy`: add `unbound_fk()`, `bound_fk()`
|
+ `sqlalchemy`: add `unbound_fk()`, `bound_fk()`
|
||||||
+ Add `sqlalchemy_async` module with `SQLAlchemy()` async database binding.
|
+ Add `sqlalchemy_async` module with `SQLAlchemy()` async database binding.
|
||||||
* Supports being used as an async context manager
|
* Supports being used as an async context manager
|
||||||
|
* Automatically handles commit and rollback
|
||||||
|
+ `sqlalchemy_async` also offers `async_query()`
|
||||||
|
+ Changed `sqlalchemy.parent_children()` to use `lazy='selectin'` by default
|
||||||
+ Add `timed_cache()`, `TimedDict()`, `none_pass()`, `twocolon_list()`, `quote_css_string()`, `must_be()`
|
+ Add `timed_cache()`, `TimedDict()`, `none_pass()`, `twocolon_list()`, `quote_css_string()`, `must_be()`
|
||||||
+ Add module `calendar` with `want_*` date type conversion utilities and `age_and_days()`
|
+ Add module `calendar` with `want_*` date type conversion utilities and `age_and_days()`
|
||||||
+ Move obsolete stuff to `obsolete` package (includes configparse 0.3 as of now)
|
+ Move obsolete stuff to `obsolete` package (includes configparse 0.3 as of now)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,19 @@
|
||||||
"""
|
"""
|
||||||
"Security through obscurity" helpers for less sensitive logging
|
"Security through obscurity" helpers for less sensitive logging
|
||||||
|
|
||||||
|
NEW 0.5.0
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Copyright (c) 2025 Sakuragasaki46.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
See LICENSE for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|
||||||
|
This software is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Helpers for asynchronous user of SQLAlchemy
|
Helpers for asynchronous use of SQLAlchemy.
|
||||||
|
|
||||||
|
NEW 0.5.0
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -15,6 +17,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
|
|
||||||
from sqlalchemy import Engine, Select, func, select
|
from sqlalchemy import Engine, Select, func, select
|
||||||
|
|
@ -150,4 +153,23 @@ class AsyncSelectPagination(Pagination):
|
||||||
for i in self.items:
|
for i in self.items:
|
||||||
yield i
|
yield i
|
||||||
|
|
||||||
__all__ = ('SQLAlchemy', )
|
|
||||||
|
def async_query(db: SQLAlchemy, multi: False):
|
||||||
|
"""
|
||||||
|
Wraps a query returning function into an executor coroutine.
|
||||||
|
|
||||||
|
The query function remains available as the .q or .query attribute.
|
||||||
|
"""
|
||||||
|
def decorator(func):
|
||||||
|
@wraps(func)
|
||||||
|
async def executor(*args, **kwargs):
|
||||||
|
async with db as session:
|
||||||
|
result = await session.execute(func(*args, **kwargs))
|
||||||
|
return result.scalars() if multi else result.scalar()
|
||||||
|
executor.query = executor.q = func
|
||||||
|
return executor
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
# Optional dependency: do not import into __init__.py
|
||||||
|
__all__ = ('SQLAlchemy', 'async_query')
|
||||||
Loading…
Add table
Add a link
Reference in a new issue