Skip to content

Commit

Permalink
feat(client): implement rejectOnNotFound (#4942)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamluke4 committed Jan 14, 2021
1 parent da3628b commit 733e424
Show file tree
Hide file tree
Showing 17 changed files with 817 additions and 44 deletions.
51 changes: 17 additions & 34 deletions src/packages/client/fixtures/blog/main.ts
@@ -1,53 +1,36 @@
import { PrismaClient } from './@prisma/client'

const prisma = new PrismaClient({
log: [
{
emit: 'event',
level: 'query',
},
],
rejectOnNotFound: {

},
log: [{
emit: 'event', level:"query"
}]
})

async function main() {
const user = await prisma.user.groupBy({
by: ['name'],
prisma.$on('query', () => {})
const res = await prisma.user.findFirst({
where: {
age: {
gt: -1,
},
},
// skip: 0,
// take: 10000,
avg: {
age: true,
},
count: true,
max: {
age: true,
},
min: {
age: true,
},
sum: {
age: true,
id: 'asdaf',
},
rejectOnNotFound: new Error('Home')
})
// const res = await prisma.user.aggregate({
// // select: true,
// // skip: 3
// count: true,
// min: {
// email: true,
// // json: true,
// console.log(res);
// const res = await prisma.user.findUnique({
// where: {
// email: 'prisma@prisma.de'
// },
// rejectOnEmpty: true
// })

console.log(user[0].count)
// console.log(res)

prisma.$disconnect()
}

main().catch((e) => {
console.error(e)
prisma.$disconnect()
})
Expand Up @@ -596,7 +596,7 @@ export class PrismaClient<
* Read more in our [docs](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client).
*/

constructor(optionsArg ?: T);
constructor(optionsArg ?: Prisma.Subset<T, Prisma.PrismaClientOptions>);
$on<V extends U>(eventType: V, callback: (event: V extends 'query' ? Prisma.QueryEvent : Prisma.LogEvent) => void): void;

/**
Expand Down Expand Up @@ -1107,9 +1107,28 @@ export namespace Prisma {
db?: Datasource
}

export type RejectOnNotFound = boolean | Error | ((error: Error) => Error)
export type RejectPerModel = { [P in ModelName]?: RejectOnNotFound }
export type RejectPerOperation = { [P in "findUnique" | "findFirst"]?: RejectPerModel | RejectOnNotFound }

export type ErrorFormat = 'pretty' | 'colorless' | 'minimal'

export interface PrismaClientOptions {
/**
* Configure findUnique/findFirst to throw an error if the query returns null.
* * @example
* \`\`\`
* // Reject on both findUnique/findFirst
* rejectOnNotFound: true
*
* // Reject only on findFirst with a custom error
* rejectOnNotFound: { findFirst: (err) => new Error("Custom Error")}
*
* // Reject on user.findUnique with a custom error
* rejectOnNotFound: { findUnique: {User: new Error("User not found")}}
* \`\`\`
*/
rejectOnNotFound?: RejectOnNotFound | RejectPerOperation
/**
* Overwrites the datasource url from your prisma.schema file
*/
Expand Down Expand Up @@ -1793,6 +1812,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: PostInclude | null
/**
* Throw an Error if a Post can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which Post to fetch.
**/
Expand All @@ -1812,6 +1835,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: PostInclude | null
/**
* Throw an Error if a Post can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which Post to fetch.
**/
Expand Down Expand Up @@ -2671,6 +2698,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: UserInclude | null
/**
* Throw an Error if a User can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which User to fetch.
**/
Expand All @@ -2690,6 +2721,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: UserInclude | null
/**
* Throw an Error if a User can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which User to fetch.
**/
Expand Down Expand Up @@ -3541,6 +3576,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: MInclude | null
/**
* Throw an Error if a M can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which M to fetch.
**/
Expand All @@ -3560,6 +3599,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: MInclude | null
/**
* Throw an Error if a M can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which M to fetch.
**/
Expand Down Expand Up @@ -4411,6 +4454,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: NInclude | null
/**
* Throw an Error if a N can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which N to fetch.
**/
Expand All @@ -4430,6 +4477,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: NInclude | null
/**
* Throw an Error if a N can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which N to fetch.
**/
Expand Down Expand Up @@ -5281,6 +5332,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: OneOptionalInclude | null
/**
* Throw an Error if a OneOptional can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which OneOptional to fetch.
**/
Expand All @@ -5300,6 +5355,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: OneOptionalInclude | null
/**
* Throw an Error if a OneOptional can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which OneOptional to fetch.
**/
Expand Down Expand Up @@ -6163,6 +6222,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: ManyRequiredInclude | null
/**
* Throw an Error if a ManyRequired can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which ManyRequired to fetch.
**/
Expand All @@ -6182,6 +6245,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: ManyRequiredInclude | null
/**
* Throw an Error if a ManyRequired can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which ManyRequired to fetch.
**/
Expand Down Expand Up @@ -7045,6 +7112,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: OptionalSide1Include | null
/**
* Throw an Error if a OptionalSide1 can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which OptionalSide1 to fetch.
**/
Expand All @@ -7064,6 +7135,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: OptionalSide1Include | null
/**
* Throw an Error if a OptionalSide1 can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which OptionalSide1 to fetch.
**/
Expand Down Expand Up @@ -7915,6 +7990,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: OptionalSide2Include | null
/**
* Throw an Error if a OptionalSide2 can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which OptionalSide2 to fetch.
**/
Expand All @@ -7934,6 +8013,10 @@ export namespace Prisma {
* Choose, which related nodes to fetch as well.
**/
include?: OptionalSide2Include | null
/**
* Throw an Error if a OptionalSide2 can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which OptionalSide2 to fetch.
**/
Expand Down Expand Up @@ -8742,6 +8825,10 @@ export namespace Prisma {
* Select specific fields to fetch from the A
**/
select?: ASelect | null
/**
* Throw an Error if a A can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which A to fetch.
**/
Expand All @@ -8757,6 +8844,10 @@ export namespace Prisma {
* Select specific fields to fetch from the A
**/
select?: ASelect | null
/**
* Throw an Error if a A can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which A to fetch.
**/
Expand Down Expand Up @@ -9501,6 +9592,10 @@ export namespace Prisma {
* Select specific fields to fetch from the B
**/
select?: BSelect | null
/**
* Throw an Error if a B can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which B to fetch.
**/
Expand All @@ -9516,6 +9611,10 @@ export namespace Prisma {
* Select specific fields to fetch from the B
**/
select?: BSelect | null
/**
* Throw an Error if a B can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which B to fetch.
**/
Expand Down Expand Up @@ -10230,6 +10329,10 @@ export namespace Prisma {
* Select specific fields to fetch from the C
**/
select?: CSelect | null
/**
* Throw an Error if a C can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which C to fetch.
**/
Expand All @@ -10245,6 +10348,10 @@ export namespace Prisma {
* Select specific fields to fetch from the C
**/
select?: CSelect | null
/**
* Throw an Error if a C can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which C to fetch.
**/
Expand Down Expand Up @@ -10951,6 +11058,10 @@ export namespace Prisma {
* Select specific fields to fetch from the D
**/
select?: DSelect | null
/**
* Throw an Error if a D can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which D to fetch.
**/
Expand All @@ -10966,6 +11077,10 @@ export namespace Prisma {
* Select specific fields to fetch from the D
**/
select?: DSelect | null
/**
* Throw an Error if a D can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which D to fetch.
**/
Expand Down Expand Up @@ -11656,6 +11771,10 @@ export namespace Prisma {
* Select specific fields to fetch from the E
**/
select?: ESelect | null
/**
* Throw an Error if a E can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which E to fetch.
**/
Expand All @@ -11671,6 +11790,10 @@ export namespace Prisma {
* Select specific fields to fetch from the E
**/
select?: ESelect | null
/**
* Throw an Error if a E can't be found
**/
rejectOnNotFound?: RejectOnNotFound
/**
* Filter, which E to fetch.
**/
Expand Down
@@ -0,0 +1 @@
!dev.db

0 comments on commit 733e424

Please sign in to comment.