RobertCraigie / prisma-client-py

Prisma Client Python is an auto-generated and fully type-safe database client designed for ease of use
https://prisma-client-py.readthedocs.io
Apache License 2.0
1.85k stars 81 forks source link

Improve support for selecting fields #960

Open afi-dev opened 5 months ago

afi-dev commented 5 months ago

Problem

Currently, when using Prisma Python, there is no direct support for selecting specific fields from a database query result. In fact, you need to use partial_types, and this is clearly not the easiest thing to do. In my opinion, it would be simpler to have the select like in the JS library.

Suggested solution

See: Prisma Docs : Select fields

I suggest adding support for the select feature in Prisma Python, similar to the existing functionality in other Prisma client libraries.

For example:

user = await prisma.user.find_first(
    where={
        "email": users.email,
    },
    select={
        "email": True,
        "name": True
    }
)

This would allow users to retrieve only the specified fields from the database query result, providing more flexibility and efficiency in data retrieval.

Alternatives

Without support for the select feature, users are forced to retrieve the entire record from the database and manually extract the required fields, which can be cumbersome and inefficient, especially for large datasets.

selected_fields = {"id": user.id, "username": user.username, "email": user.email,}
return selected_fields

Additional context

Adding support for the select feature in Prisma Python would align the functionality of the Python client with other Prisma client libraries, providing a consistent experience across different programming languages. This feature would greatly enhance the usability and versatility of Prisma Python for data retrieval tasks.

umar-anzar commented 4 months ago

prisma/schema.prisma

generator client {
  provider             = "prisma-client-py"
  interface            = "asyncio"
  recursive_type_depth = "5"
  partial_type_generator = "prisma/partial_types.py"
}

model User {
  id                  Int           @id @default(autoincrement())
  name                String        @db.VarChar(30)
 age Float
 password String
 country String
}

Run prisma generate

prisma/partial_types.py

from prisma.models import User
User.create_partial('UserWithName', include={'name'})

db.py or any where prisma client is intialize

import prisma
from prisma import Prisma

prisma_client: Prisma = Prisma()
prisma.register(prisma_client)

Run prisma generate

# Use of Partial Type
from prisma.partials import UserWithName

user = await UserWithName.prisma().find_first(
  where={
    'country': 'Scotland',
  },
)
print(user.name)

print(user.id)  # error `id` does not exist on the `UserWithName` type

Partial Types - Prisma Client Python (prisma-client-py.readthedocs.io)

Selecting Fields - Prisma Client Python (prisma-client-py.readthedocs.io)

afi-dev commented 4 months ago

I know you have to use the partial type for this, but I suggest making it less abstract by adding the select which only exists on the Prisma JS library, by dint of which I was a bit disgusted not to have the select and preferred to code directly with the prismaJS library.

RobertCraigie commented 2 months ago

I definitely agree the current situation requiring partial types is less than ideal.

Unfortunately selecting fields is a much more difficult problem to solve in the python client as the type system is much more restrictive...

Does anyone have suggestions for a better API that still supports type safety?