question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting rejectOnNotFound globally doesn't provide type safety

See original GitHub issue

Bug description

Setting rejectOnNotFound globally doesn’t change the typing of findUnique or findFirst

const prisma = new PrismaClient({
  rejectOnNotFound: true,
});

Then when I use findUnique, It still returns Prisma.Prisma__UserClient<User | null> instead of Prisma.Prisma__UserClient<User>

Expected behavior

It will be better if it’s typed. This might mean, generating types at runtime just like nexus does. But a better approach will be to just include this property rejectOnNotFound in the prisma schema itself within the generator block like this:

generator client {
  provider = "prisma-client-js"
  rejectOnNotFound = true
}

Environment & setup

  • OS: Windows
  • Database: PostgreSQL
  • Node.js version: 14.16.1
  • Prisma version:
 2.21.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pantharshit00commented, Jul 20, 2021

@ahmedosama7450 I don’t think we can have this option in the schema as people can do multiple instances of the client with different rejectOnNotFound option (eg. when sharing client in a monorepo) and the schema approach restricts that.

I opened a documentation issue about the generic usage: https://github.com/prisma/docs/issues/1564#issuecomment-883668535

1reaction
pantharshit00commented, Jul 8, 2021

@toy-crane You need to do the following:

import { PrismaClient } from "@prisma/client";

export const createPrismaClient = (): PrismaClient<{
  rejectOnNotFound: true;
}> => {
  const prisma = new PrismaClient({
    rejectOnNotFound: true,
  });
  return prisma;
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nextjs build: Property does not exist on type 'PrismaClient
I generated the migration file and ran the migrations in both local and production and verified the new table is present in both...
Read more >
Prisma Client API (Reference)
If rejectOnNotFound is true, findUnique throws an error ( NotFoundError by default, customizable globally) instead of returning null . Reference. findUnique ...
Read more >
nexus-prisma - npm
Type -safe Generated Library Code; Project Enums; Project Scalars ... For example if you set rejectOnNotFound globally on your Prisma Client ...
Read more >
Features - Docs | Nexus Prisma - GitHub Pages
Prisma Client's rejectOnNotFound feature is effectively ignored by Nexus Prisma. For example if you set rejectOnNotFound globally on your Prisma Client it will ......
Read more >
Prisma 4.0 Released: The Next Gen ORM - Morioh
We expect the new APIs to be easier to understand and more type-safe. ... statement:column "c" of relation "t1" does not exist Query:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found