hello, I have `rejectOnNotFound` enabled on a glob...
# orm-help
d
hello, I have
rejectOnNotFound
enabled on a global level for all
findFirst
and
findUnique
queries but it doesnt seem to work. I am using
nestjs
tho so the
prisma
configuration is different
Copy code
// prisma config
export class PrismaService extends PrismaClient<LogOptions> implements OnModuleInit, OnModuleDestroy {
	constructor() {
		super({
			log: [
				{
					emit: 'event',
					level: 'query'
				}
			],
			rejectOnNotFound: {
				findFirst: () => new Error('error'),
				findUnique: () => new Error('error')
			}
		});
	}

	async onModuleInit() {
		await this.$connect();
	}

	async onModuleDestroy() {
		await this.$disconnect();
	}
}

// findFirst query
const buyer = await this.prisma.buyer.findFirst({
	where: {
        id
    },
	rejectOnNotFound: true
});
1. so when I execute this
findFirst
it does throw an error but not the error i defined in the
prisma
config. Instead it says
[NotFoundError: No Buyer found] { clientVersion: '2.26.0' }
2. Also, when I do NOT provide the
rejectOnNotFound
in the
findFirst
query it seems that the globally configured
rejectOnNotFound
is not even doing anything... 3. as last, when I configure
rejectOnNotFound
on a global level i'd expect that my
buyer
variable can not be a possible
null
value because it will be rejected if not found but it still says is
possibly 'null'
seems that point 1 and 2 are suddenly working now, but I am still curious about point 3
t
My guess about point 3 would be that it's not simple to create perfect typescript types and since the types are generated for the PrismaClient itself, typescript doesn't know about your initialization of the client (which has the
rejectOnNotFound
). I'm probably wrong to some extent tho
r
Related issue: https://github.com/prisma/prisma/issues/6774 @Dev__ Can you try the workaround as specified in the last comment?
d
I am using nestjs, not sure how I'd do that with nestjs
r
I’m not sure. Does adding it in the constructor not work? Otherwise maybe create a static method.
t
I think the soulution should be replacing
extends PrismaClient<LogOptions>
with
extends PrismaClient<LogOptions & { rejectOnNotFound: true; }>
👍 1
Depending on what
LogOptions
contains of course, you might need to override it.
d
Reject on error is already available without implementing your suggestion. Plus I want a custom error for findFirst and findUnique
Would it make a difference if I implement your suggestion?
t
My suggestion is just for typescript to understand how you configure prisma. It has nothing to do with custom errors and whatnot. Just try it and see if it works.
d
LogOptions
is an interface
Copy code
interface LogOptions extends Prisma.PrismaClientOptions {
	log: [
		{
			emit: 'event';
			level: 'query';
		}
	];
}
doing
LogOptions & { rejectOnNotFound: true }
will not work
t
It does seem to work for me:
d
Copy code
interface LogOptions extends Prisma.PrismaClientOptions {
	log: [
		{
			emit: 'event';
			level: 'query';
		}
	];
}

@Injectable()
export class PrismaService
	extends PrismaClient<LogOptions & { rejectOnNotFound: true }>
	implements OnModuleInit, OnModuleDestroy
{
	constructor() {
		super({
			log: [
				{
					emit: 'event',
					level: 'query'
				}
			],
			rejectOnNotFound: true
		});
	}

	async onModuleInit() {
		await this.$connect();
	}

	async onModuleDestroy() {
		await this.$disconnect();
	}
}
this works
but this doesnt