mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 15:15:36 +01:00
Fix joins and subqueries
This commit is contained in:
@@ -47,7 +47,8 @@ SELECT ARRAY(
|
|||||||
|
|
||||||
class AuthorTGRMSearchService(TRGMSearchService):
|
class AuthorTGRMSearchService(TRGMSearchService):
|
||||||
MODEL_CLASS = Author
|
MODEL_CLASS = Author
|
||||||
PREFETCH_RELATED = ["source", "annotations"]
|
SELECT_RELATED = ["source"]
|
||||||
|
PREFETCH_RELATED = ["annotations"]
|
||||||
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
|
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ SELECT ARRAY(
|
|||||||
|
|
||||||
class BookTGRMSearchService(TRGMSearchService):
|
class BookTGRMSearchService(TRGMSearchService):
|
||||||
MODEL_CLASS = BookDB
|
MODEL_CLASS = BookDB
|
||||||
PREFETCH_RELATED = ["source", "authors", "translators", "annotations"]
|
SELECT_RELATED = ["source"]
|
||||||
|
PREFETCH_RELATED = ["authors", "translators", "annotations"]
|
||||||
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
|
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ class TRGMSearchService(Generic[T]):
|
|||||||
@classmethod
|
@classmethod
|
||||||
@property
|
@property
|
||||||
def cache_prefix(cls) -> str:
|
def cache_prefix(cls) -> str:
|
||||||
return cls.CUSTOM_CACHE_PREFIX or cls.model.__class__.__name__
|
return cls.CUSTOM_CACHE_PREFIX or cls.model.Meta.tablename
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_cache_key(cls, query_data: str, allowed_langs: list[str]) -> str:
|
def get_cache_key(cls, query_data: str, allowed_langs: list[str]) -> str:
|
||||||
@@ -90,7 +90,7 @@ class TRGMSearchService(Generic[T]):
|
|||||||
data = await redis.get(key)
|
data = await redis.get(key)
|
||||||
|
|
||||||
if data is None:
|
if data is None:
|
||||||
return data
|
return None
|
||||||
|
|
||||||
return orjson.loads(data)
|
return orjson.loads(data)
|
||||||
except aioredis.RedisError as e:
|
except aioredis.RedisError as e:
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ SELECT ARRAY (
|
|||||||
|
|
||||||
class SequenceTGRMSearchService(TRGMSearchService):
|
class SequenceTGRMSearchService(TRGMSearchService):
|
||||||
MODEL_CLASS = Sequence
|
MODEL_CLASS = Sequence
|
||||||
PREFETCH_RELATED = ["source"]
|
SELECT_RELATED = ["source"]
|
||||||
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
|
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -48,5 +48,6 @@ SELECT ARRAY(
|
|||||||
class TranslatorTGRMSearchService(TRGMSearchService):
|
class TranslatorTGRMSearchService(TRGMSearchService):
|
||||||
MODEL_CLASS = Author
|
MODEL_CLASS = Author
|
||||||
CUSTOM_CACHE_PREFIX = "translator"
|
CUSTOM_CACHE_PREFIX = "translator"
|
||||||
PREFETCH_RELATED = ["source", "annotations"]
|
SELECT_RELATED = ["source"]
|
||||||
|
PREFETCH_RELATED = ["annotations"]
|
||||||
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
|
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
|
||||||
|
|||||||
@@ -27,34 +27,49 @@ author_router = APIRouter(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
PREFETCH_RELATED = ["source", "annotations"]
|
SELECT_RELATED_FIELDS = ["source"]
|
||||||
|
PREFETCH_RELATED_FIELDS = ["annotations"]
|
||||||
|
|
||||||
|
|
||||||
@author_router.get(
|
@author_router.get(
|
||||||
"/", response_model=CustomPage[Author], dependencies=[Depends(Params)]
|
"/", response_model=CustomPage[Author], dependencies=[Depends(Params)]
|
||||||
)
|
)
|
||||||
async def get_authors():
|
async def get_authors():
|
||||||
return await paginate(AuthorDB.objects.prefetch_related(PREFETCH_RELATED))
|
return await paginate(
|
||||||
|
AuthorDB.objects.select_related(SELECT_RELATED_FIELDS).prefetch_related(
|
||||||
|
PREFETCH_RELATED_FIELDS
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@author_router.post("/", response_model=Author, dependencies=[Depends(Params)])
|
@author_router.post("/", response_model=Author, dependencies=[Depends(Params)])
|
||||||
async def create_author(data: CreateAuthor):
|
async def create_author(data: CreateAuthor):
|
||||||
author = await AuthorDB.objects.create(**data.dict())
|
author = await AuthorDB.objects.create(**data.dict())
|
||||||
|
|
||||||
return await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get(id=author.id)
|
return (
|
||||||
|
await AuthorDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||||
|
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||||
|
.get(id=author.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@author_router.get("/random", response_model=Author)
|
@author_router.get("/random", response_model=Author)
|
||||||
async def get_random_author(allowed_langs: list[str] = Depends(get_allowed_langs)):
|
async def get_random_author(allowed_langs: list[str] = Depends(get_allowed_langs)):
|
||||||
author_id = await GetRandomAuthorService.get_random_id(allowed_langs)
|
author_id = await GetRandomAuthorService.get_random_id(allowed_langs)
|
||||||
|
|
||||||
return await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get(id=author_id)
|
return (
|
||||||
|
await AuthorDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||||
|
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||||
|
.get(id=author_id)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@author_router.get("/{id}", response_model=Author)
|
@author_router.get("/{id}", response_model=Author)
|
||||||
async def get_author(id: int):
|
async def get_author(id: int):
|
||||||
author = await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get_or_none(
|
author = (
|
||||||
id=id
|
await AuthorDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||||
|
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||||
|
.get_or_none(id=id)
|
||||||
)
|
)
|
||||||
|
|
||||||
if author is None:
|
if author is None:
|
||||||
@@ -92,7 +107,8 @@ async def get_author_books(
|
|||||||
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
|
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
|
||||||
):
|
):
|
||||||
return await paginate(
|
return await paginate(
|
||||||
BookDB.objects.select_related(["source", "annotations", "translators"])
|
BookDB.objects.select_related(["source"])
|
||||||
|
.prefetch_related(["annotations", "translators"])
|
||||||
.filter(authors__id=id, lang__in=allowed_langs, is_deleted=False)
|
.filter(authors__id=id, lang__in=allowed_langs, is_deleted=False)
|
||||||
.order_by("title")
|
.order_by("title")
|
||||||
)
|
)
|
||||||
@@ -121,7 +137,9 @@ async def get_translated_books(
|
|||||||
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
|
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
|
||||||
):
|
):
|
||||||
return await paginate(
|
return await paginate(
|
||||||
BookDB.objects.select_related(["source", "annotations", "authors"]).filter(
|
BookDB.objects.select_related(["source"])
|
||||||
|
.prefetch_related(["annotations", "authors"])
|
||||||
|
.filter(
|
||||||
translators__id=id,
|
translators__id=id,
|
||||||
lang__in=allowed_langs,
|
lang__in=allowed_langs,
|
||||||
is_deleted=False,
|
is_deleted=False,
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ book_router = APIRouter(
|
|||||||
dependencies=[Depends(check_token)],
|
dependencies=[Depends(check_token)],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
SELECT_RELATED_FIELDS = ["source"]
|
||||||
SELECT_RELATED_FIELDS = ["source", "authors", "translators", "annotations"]
|
PREFETCH_RELATED_FIELDS = ["authors", "translators", "annotations"]
|
||||||
|
|
||||||
|
|
||||||
@book_router.get(
|
@book_router.get(
|
||||||
@@ -38,7 +38,9 @@ SELECT_RELATED_FIELDS = ["source", "authors", "translators", "annotations"]
|
|||||||
)
|
)
|
||||||
async def get_books(book_filter: dict = Depends(get_book_filter)):
|
async def get_books(book_filter: dict = Depends(get_book_filter)):
|
||||||
return await paginate(
|
return await paginate(
|
||||||
BookDB.objects.select_related(SELECT_RELATED_FIELDS).filter(**book_filter)
|
BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||||
|
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||||
|
.filter(**book_filter)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -46,19 +48,31 @@ async def get_books(book_filter: dict = Depends(get_book_filter)):
|
|||||||
async def create_book(data: Union[CreateBook, CreateRemoteBook]):
|
async def create_book(data: Union[CreateBook, CreateRemoteBook]):
|
||||||
book = await BookCreator.create(data)
|
book = await BookCreator.create(data)
|
||||||
|
|
||||||
return await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get(id=book.id)
|
return (
|
||||||
|
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||||
|
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||||
|
.get(id=book.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@book_router.get("/random", response_model=BookDetail)
|
@book_router.get("/random", response_model=BookDetail)
|
||||||
async def get_random_book(allowed_langs: list[str] = Depends(get_allowed_langs)):
|
async def get_random_book(allowed_langs: list[str] = Depends(get_allowed_langs)):
|
||||||
book_id = await GetRandomBookService.get_random_id(allowed_langs)
|
book_id = await GetRandomBookService.get_random_id(allowed_langs)
|
||||||
|
|
||||||
return await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get(id=book_id)
|
return (
|
||||||
|
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||||
|
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||||
|
.get(id=book_id)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@book_router.get("/{id}", response_model=BookDetail)
|
@book_router.get("/{id}", response_model=BookDetail)
|
||||||
async def get_book(id: int):
|
async def get_book(id: int):
|
||||||
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id)
|
book = (
|
||||||
|
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||||
|
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||||
|
.get_or_none(id=id)
|
||||||
|
)
|
||||||
|
|
||||||
if book is None:
|
if book is None:
|
||||||
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||||
@@ -68,8 +82,10 @@ async def get_book(id: int):
|
|||||||
|
|
||||||
@book_router.get("/remote/{source_id}/{remote_id}", response_model=Book)
|
@book_router.get("/remote/{source_id}/{remote_id}", response_model=Book)
|
||||||
async def get_remote_book(source_id: int, remote_id: int):
|
async def get_remote_book(source_id: int, remote_id: int):
|
||||||
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(
|
book = (
|
||||||
source=source_id, remote_id=remote_id
|
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||||
|
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||||
|
.get_or_none(source=source_id, remote_id=remote_id)
|
||||||
)
|
)
|
||||||
|
|
||||||
if book is None:
|
if book is None:
|
||||||
@@ -80,7 +96,11 @@ async def get_remote_book(source_id: int, remote_id: int):
|
|||||||
|
|
||||||
@book_router.put("/{id}", response_model=Book)
|
@book_router.put("/{id}", response_model=Book)
|
||||||
async def update_book(id: int, data: UpdateBook):
|
async def update_book(id: int, data: UpdateBook):
|
||||||
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id)
|
book = (
|
||||||
|
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||||
|
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||||
|
.get_or_none(id=id)
|
||||||
|
)
|
||||||
|
|
||||||
if book is None:
|
if book is None:
|
||||||
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||||
|
|||||||
@@ -47,9 +47,8 @@ async def get_sequence_books(
|
|||||||
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
|
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
|
||||||
):
|
):
|
||||||
return await paginate(
|
return await paginate(
|
||||||
BookDB.objects.select_related(
|
BookDB.objects.select_related(["source"])
|
||||||
["source", "annotations", "authors", "translators"]
|
.prefetch_related(["annotations", "authors", "translators"])
|
||||||
)
|
|
||||||
.filter(sequences__id=id, lang__in=allowed_langs, is_deleted=False)
|
.filter(sequences__id=id, lang__in=allowed_langs, is_deleted=False)
|
||||||
.order_by("sequences__booksequences__position")
|
.order_by("sequences__booksequences__position")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -27,21 +27,21 @@ translation_router = APIRouter(
|
|||||||
"/", response_model=CustomPage[Translation], dependencies=[Depends(Params)]
|
"/", response_model=CustomPage[Translation], dependencies=[Depends(Params)]
|
||||||
)
|
)
|
||||||
async def get_translations():
|
async def get_translations():
|
||||||
return await paginate(TranslationDB.objects.prefetch_related(["book", "author"]))
|
return await paginate(TranslationDB.objects.select_related(["book", "author"]))
|
||||||
|
|
||||||
|
|
||||||
@translation_router.post("/", response_model=Translation)
|
@translation_router.post("/", response_model=Translation)
|
||||||
async def create_translation(data: Union[CreateTranslation, CreateRemoteTranslation]):
|
async def create_translation(data: Union[CreateTranslation, CreateRemoteTranslation]):
|
||||||
translation = await TranslationCreator.create(data)
|
translation = await TranslationCreator.create(data)
|
||||||
|
|
||||||
return await TranslationDB.objects.prefetch_related(["book", "author"]).get(
|
return await TranslationDB.objects.select_related(["book", "author"]).get(
|
||||||
id=translation.id
|
id=translation.id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@translation_router.delete("/{id}", response_model=Translation)
|
@translation_router.delete("/{id}", response_model=Translation)
|
||||||
async def delete_translation(id: int):
|
async def delete_translation(id: int):
|
||||||
translation = await TranslationDB.objects.prefetch_related(
|
translation = await TranslationDB.objects.select_related(
|
||||||
["book", "author"]
|
["book", "author"]
|
||||||
).get_or_none(id=id)
|
).get_or_none(id=id)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user