prisma-client-py icon indicating copy to clipboard operation
prisma-client-py copied to clipboard

Improve support for selecting fields

Open afi-dev opened this issue 9 months ago • 3 comments

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.

afi-dev avatar Apr 28 '24 17:04 afi-dev

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)

umar-anzar avatar May 22 '24 06:05 umar-anzar

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.

afi-dev avatar May 26 '24 22:05 afi-dev

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?

RobertCraigie avatar Aug 04 '24 14:08 RobertCraigie