Skip to content

Commit db944e6

Browse files
committed
feat: add prisma extensions
1 parent c0a76d7 commit db944e6

File tree

13 files changed

+83
-57
lines changed

13 files changed

+83
-57
lines changed

app/middlewares/authenticate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { NextFunction, Request, Response } from 'express'
22

33
import { Unauthorized } from 'http-errors'
44
import { verify } from 'jsonwebtoken'
5-
import { prisma } from 'prisma.client'
5+
import { prisma } from 'prisma/prisma.client'
66

77
export const authenticate = async (
88
req: Request,

app/middlewares/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './authenticate'
2+
export * from './multer'
23
export * from './validateRequest'

app/prisma.client.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Prisma } from '@prisma/client'
2+
3+
export const deleteIgnoreNotFoundExtension = Prisma.defineExtension(client => {
4+
return client.$extends({
5+
name: 'deleteIgnoreNotFound',
6+
model: {
7+
$allModels: {
8+
async deleteIgnoreNotFound<T, A>(
9+
this: T,
10+
args: Prisma.Exact<A, Prisma.Args<T, 'delete'>>
11+
): Promise<Prisma.Result<T, A, 'delete'> | null> {
12+
try {
13+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14+
const context = Prisma.getExtensionContext(this) as any
15+
return await context.delete(args)
16+
} catch (err) {
17+
if (
18+
err instanceof Prisma.PrismaClientKnownRequestError &&
19+
err.code === 'P2025'
20+
) {
21+
return null
22+
}
23+
throw err
24+
}
25+
}
26+
}
27+
}
28+
})
29+
})

app/prisma/extensions/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './delete-ignore-not-found.extension'
2+
export * from './update-ignore-not-found.extension'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Prisma } from '@prisma/client'
2+
3+
export const updateIgnoreNotFoundExtension = Prisma.defineExtension(client => {
4+
return client.$extends({
5+
name: 'updateIgnoreNotFound',
6+
model: {
7+
$allModels: {
8+
async updateIgnoreNotFound<T, A>(
9+
this: T,
10+
args: Prisma.Exact<A, Prisma.Args<T, 'update'>>
11+
): Promise<Prisma.Result<T, A, 'update'> | null> {
12+
try {
13+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14+
const context = Prisma.getExtensionContext(this) as any
15+
return await context.update(args)
16+
} catch (err) {
17+
if (
18+
err instanceof Prisma.PrismaClientKnownRequestError &&
19+
err.code === 'P2025'
20+
) {
21+
return null
22+
}
23+
throw err
24+
}
25+
}
26+
}
27+
}
28+
})
29+
})

app/prisma/prisma.client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PrismaClient } from '@prisma/client'
2+
3+
import {
4+
deleteIgnoreNotFoundExtension,
5+
updateIgnoreNotFoundExtension
6+
} from './extensions'
7+
8+
export const prisma = new PrismaClient()
9+
.$extends(updateIgnoreNotFoundExtension)
10+
.$extends(deleteIgnoreNotFoundExtension)

app/routes/api/auth.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { compare, hash } from 'bcrypt'
33
import { OAuth2Client } from 'google-auth-library'
44
import { Conflict, Forbidden, Unauthorized } from 'http-errors'
55
import { sign, verify } from 'jsonwebtoken'
6-
import { prisma } from 'prisma.client'
6+
import { prisma } from 'prisma/prisma.client'
77

8-
import { validateRequest } from 'middlewares'
9-
import { authenticate } from 'middlewares/authenticate'
8+
import { authenticate, validateRequest } from 'middlewares'
109

1110
import {
1211
GoogleAuthSchema,

app/routes/api/board.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Router } from 'express'
22
import boardImages from 'data/board-bg-images.json'
33
import { NotFound } from 'http-errors'
4-
import { prisma } from 'prisma.client'
4+
import { prisma } from 'prisma/prisma.client'
55

66
import { authenticate, validateRequest } from 'middlewares'
77

app/routes/api/card.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Router } from 'express'
22
import { BadRequest, NotFound } from 'http-errors'
3-
import { prisma } from 'prisma.client'
3+
import { prisma } from 'prisma/prisma.client'
44

55
import { authenticate, validateRequest } from 'middlewares'
66

0 commit comments

Comments
 (0)