Open RobertCraigie opened 3 years ago
This feature is needed.
If anyone need this, it is possible to retrieve the _count
using this library internal methods (very unsafe, can break after any update):
db = prisma.Prisma()
class _UserCount(pydantic.BaseModel):
posts: int
class _User(prisma.models.User):
# Aliasing because of https://github.com/pydantic/pydantic/issues/2105
count_: typing.Annotated[_UserCount, pydantic.Field(alias="_count")]
@classmethod
async def select_with_post_count(
cls,
*,
where: prisma.types.UserWhereInput | None,
include: prisma.types.UserInclude | None,
):
# shut up pydantic
make_query_builder = (
db._make_query_builder # pyright: ignore [reportPrivateUsage]
)
engine = db._engine # pyright: ignore [reportPrivateUsage]
# generate a graphql query to the prisma engine
query = make_query_builder(
method="find_many",
model=models.License,
arguments={"where": where, "include": include},
root_selection=None,
).build()
# add the _count
suffix = r'\n }\n}"}'
query = query.removesuffix(suffix) + r"\n _count { posts }" + suffix
# execute the query, be sure to set tx_id if you are in a transaction
response = (await engine.query(query, tx_id=None))["data"]["result"]
return [cls.model_validate(x) for x in response]
users = await _User.select_with_count()
This will stop working once this library has been moved from GraphQL to the JSON protocol (#748).
Problem
Prisma supports including the count of a relational field, so we should too.
which returns an object like this
Prisma also have support for filtering by the count of the relation, see https://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#filter-the-relation-count