Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to exclude fields from method return value based on query parameters? #23990

Open
ludralph opened this issue Apr 26, 2024 Discussed in #23947 · 1 comment
Open

How to exclude fields from method return value based on query parameters? #23990

ludralph opened this issue Apr 26, 2024 Discussed in #23947 · 1 comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: omit topic: omitApi topic: typescript

Comments

@ludralph
Copy link
Contributor

Discussed in #23947

Originally posted by Cat7373 April 24, 2024

Question

My model is defined as follows:

model User {
  id       Int      @id @default(autoincrement())
  name     String   @unique
  password String
  regionId Int
  role     Role
  addtime  DateTime
  regUid   Int?
  lock     Boolean
}

When querying using findMany, the return value is defined as follows:

const r = await prisma.user.findMany({ omit: { password: true } })

image

As you can see, there is no password field.


I'm trying to write a general utility method whose return value is deduced as follows:

async function testQuery<M, A extends Prisma.Args<M, 'findMany'>>(model: M, queryParams: A) {
  return await (model as any).findMany({ ...queryParams }) as Result<M, A, 'findMany'>
}

const r = await testQuery(usePrisma().user, { omit: { password: true } })

image

As you can see, the password field was not successfully excluded.

Please help, how to define the parameters and return values of testQuery so that parameter exclusion can be handled correctly, thank you.

How to reproduce (optional)

No response

Expected behavior (optional)

No response

Information about Prisma Schema, Client Queries and Environment (optional)

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["omitApi"]
}

datasource db {
  provider          = "mysql"
  url               = env("DATABASE_URL")
  shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}

enum Role {
  ADMIN
  USER
}

model User {
  id       Int      @id @default(autoincrement())
  name     String   @unique
  password String
  regionId Int
  role     Role
  addtime  DateTime
  regUid   Int?
  lock     Boolean
}
  • OS: MacOS
  • Database: MySQL
  • Node.js version: v21.7.3
Environment variables loaded from .env
prisma                  : 5.13.0
@prisma/client          : 5.13.0
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v21.7.3
Query Engine (Node-API) : libquery-engine b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b (at node_modules/.pnpm/@prisma+engines@5.13.0/node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b (at node_modules/.pnpm/@prisma+engines@5.13.0/node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.13.0-23.b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b
Default Engines Hash    : b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b
Studio                  : 0.500.0
Preview Features        : omitApi
@ludralph ludralph added the kind/bug A reported bug. label Apr 26, 2024
@SevInf SevInf added bug/2-confirmed Bug has been reproduced and confirmed. topic: typescript topic: omitApi team/client Issue for team Client. labels Apr 26, 2024
@ludralph
Copy link
Contributor Author

@Cat7373 also mentioned that setting the select field is valid:

const r = await testQuery(usePrisma().user, { select: { id: true, name: true } })
image

In other words, it may be that the new omit has not been adapted yet.

@SevInf shared a workaround show below

First, I can confirm the issue on our side. The problem seems to be TS infering type of A to be broader that it actually is. Second, I have a workaround for you until the problem is fixed from our side. You need to add a const modifier to A in your testQuery definition, like this:

async function testQuery<M, const A extends Prisma.Args<M, 'findMany'>>(model: M, queryParams: A) {
  return (await (model as any).findMany({ ...queryParams })) as Prisma.Result<M, A, 'findMany'>
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: omit topic: omitApi topic: typescript
Projects
None yet
Development

No branches or pull requests

3 participants