Skip to content

Commit b2fc503

Browse files
author
yyosifov
authored
Update prisma to 5.1.1 (#532)
Updated from v4 to v5 (4.9.0 to 5.1.1)
1 parent 3a44162 commit b2fc503

18 files changed

+77
-95
lines changed

apps/api/src/benefactor/benefactor.service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Benefactor } from '@prisma/client'
2-
import { PrismaClientKnownRequestError } from '@prisma/client/runtime'
2+
import { Prisma } from '@prisma/client'
33
import { Injectable, Logger, NotFoundException } from '@nestjs/common'
44

55
import { PrismaService } from '../prisma/prisma.service'
@@ -20,7 +20,7 @@ export class BenefactorService {
2020

2121
async findOne(id: string): Promise<Benefactor> {
2222
try {
23-
return await this.prisma.benefactor.findFirst({
23+
return await this.prisma.benefactor.findFirstOrThrow({
2424
where: { id },
2525
include: {
2626
person: {
@@ -30,7 +30,6 @@ export class BenefactorService {
3030
},
3131
},
3232
},
33-
rejectOnNotFound: true,
3433
})
3534
} catch (err) {
3635
const msg = `No Document found with ID: ${id}`
@@ -48,7 +47,7 @@ export class BenefactorService {
4847
})
4948
return result
5049
} catch (error) {
51-
if (error instanceof PrismaClientKnownRequestError) {
50+
if (error instanceof Prisma.PrismaClientKnownRequestError) {
5251
Logger.warn('No record with id', +id)
5352
throw new NotFoundException('No record with id' + id)
5453
}

apps/api/src/config/shutdown.config.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { INestApplication, ShutdownSignal } from '@nestjs/common'
22
import { PrismaService } from '../prisma/prisma.service'
33

44
export function setupShutdownHooks(app: INestApplication) {
5-
const prismaService: PrismaService = app.get(PrismaService)
6-
prismaService.enableShutdownHooks(app)
7-
5+
// https://www.prisma.io/docs/guides/upgrade-guides/upgrading-versions/upgrading-to-prisma-5
86
app.enableShutdownHooks([ShutdownSignal.SIGINT, ShutdownSignal.SIGTERM])
97
}

apps/api/src/country/country.controller.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,24 @@ describe('CountryController', () => {
6060

6161
it('should get 1 country', async () => {
6262
const country = mockData[0]
63-
prismaMock.country.findFirst.mockResolvedValue(country)
63+
prismaMock.country.findFirstOrThrow.mockResolvedValue(country)
6464

6565
const result = await controller.findOne(country.id)
6666
expect(result).toEqual(country)
67-
expect(prismaMock.country.findFirst).toHaveBeenCalledWith({
67+
expect(prismaMock.country.findFirstOrThrow).toHaveBeenCalledWith({
6868
where: {
6969
id: country.id,
7070
},
7171
include: {
7272
cities: true,
7373
},
74-
rejectOnNotFound: true,
7574
})
7675
})
7776

7877
it('should throw error if trying to get a country that does not exist', async () => {
7978
const notExistingId = '12345'
8079

81-
const prismaSpy = jest.spyOn(prismaMock.country, 'findFirst').mockImplementation(() => {
80+
const prismaSpy = jest.spyOn(prismaMock.country, 'findFirstOrThrow').mockImplementation(() => {
8281
const msg = 'No Country record with ID: ' + notExistingId
8382
throw new NotFoundException(msg)
8483
})

apps/api/src/country/country.service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ export class CountryService {
2222

2323
async getCountryById(id: string): Promise<Country> {
2424
try {
25-
const country = await this.prisma.country.findFirst({
25+
const country = await this.prisma.country.findFirstOrThrow({
2626
where: {
2727
id: id,
2828
},
2929
include: {
3030
cities: true,
3131
},
32-
rejectOnNotFound: true,
3332
})
3433
return country
3534
} catch (err) {

apps/api/src/document/document.service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ export class DocumentService {
2020

2121
async findOne(id: string): Promise<Document> {
2222
try {
23-
return await this.prisma.document.findFirst({
23+
return await this.prisma.document.findFirstOrThrow({
2424
where: {
2525
id,
2626
},
27-
rejectOnNotFound: true,
2827
})
2928
} catch (err) {
3029
const msg = `No Document found with ID: ${id}`

apps/api/src/donations/donations.service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,8 @@ export class DonationsService {
407407
*/
408408
async getDonationById(id: string): Promise<Donation> {
409409
try {
410-
const donation = await this.prisma.donation.findFirst({
410+
const donation = await this.prisma.donation.findFirstOrThrow({
411411
where: { id },
412-
rejectOnNotFound: true,
413412
})
414413
return donation
415414
} catch (err) {

apps/api/src/expenses/expenses.controller.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ describe('ExpensesController', () => {
134134

135135
prismaMock.person.findFirst.mockResolvedValue(person)
136136
prismaMock.campaign.findFirst.mockResolvedValue(campaign)
137-
prismaMock.vault.findFirst.mockResolvedValue(vault)
138-
prismaMock.expense.findFirst.mockResolvedValue(expense)
137+
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
138+
prismaMock.expense.findFirstOrThrow.mockResolvedValue(expense)
139139
prismaMock.vault.update.mockResolvedValue(vault)
140140
prismaMock.expense.update.mockResolvedValue(expense)
141141
prismaMock.$transaction.mockResolvedValue([expense, vault])
@@ -181,8 +181,8 @@ describe('ExpensesController', () => {
181181
sub: '00000000-0000-0000-0000-000000000012',
182182
}
183183

184-
prismaMock.vault.findFirst.mockResolvedValue(vault)
185-
prismaMock.expense.findFirst.mockResolvedValue(expense)
184+
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
185+
prismaMock.expense.findFirstOrThrow.mockResolvedValue(expense)
186186
prismaMock.vault.update.mockResolvedValue(vault)
187187
prismaMock.expense.update.mockResolvedValue(expense)
188188
prismaMock.$transaction.mockResolvedValue([expense, vault])
@@ -222,9 +222,9 @@ describe('ExpensesController', () => {
222222
sub: '00000000-0000-0000-0000-000000000012',
223223
}
224224

225-
prismaMock.vault.findFirst.mockResolvedValue(vault)
226-
prismaMock.expense.findFirst.mockResolvedValueOnce(approvedExpense)
227-
prismaMock.expense.findFirst.mockResolvedValueOnce(cancelledExpense)
225+
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
226+
prismaMock.expense.findFirstOrThrow.mockResolvedValueOnce(approvedExpense)
227+
prismaMock.expense.findFirstOrThrow.mockResolvedValueOnce(cancelledExpense)
228228

229229
const updateDto: UpdateExpenseDto = {
230230
...approvedExpense,
@@ -252,8 +252,8 @@ describe('ExpensesController', () => {
252252
amount: 1000,
253253
blockedAmount: 350,
254254
}
255-
prismaMock.vault.findFirst.mockResolvedValue(vault)
256-
prismaMock.expense.findFirst.mockResolvedValueOnce(expense)
255+
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
256+
prismaMock.expense.findFirstOrThrow.mockResolvedValueOnce(expense)
257257

258258
const updateDto: UpdateExpenseDto = {
259259
...expense,

apps/api/src/expenses/expenses.service.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ export class ExpensesService {
6262
* Updates an expense, where status changes to approved/canceled state will finilize the expense and perform vault transaction.
6363
*/
6464
async update(id: string, dto: UpdateExpenseDto) {
65-
const expense = await this.prisma.expense.findFirst({
65+
const expense = await this.prisma.expense.findFirstOrThrow({
6666
where: { id: id },
67-
rejectOnNotFound: true,
6867
})
6968
if (
7069
[ExpenseStatus.approved.valueOf(), ExpenseStatus.canceled.valueOf()].includes(
@@ -77,11 +76,10 @@ export class ExpensesService {
7776
throw new BadRequestException('Vault or amount cannot be changed.')
7877
}
7978

80-
const vault = await this.prisma.vault.findFirst({
79+
const vault = await this.prisma.vault.findFirstOrThrow({
8180
where: {
8281
id: expense.vaultId,
8382
},
84-
rejectOnNotFound: true,
8583
})
8684

8785
// TODO: figure out how to initialize empty vault promise

apps/api/src/prisma/prisma.service.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,4 @@ export class PrismaService extends PrismaClient implements OnModuleInit {
1616

1717
await this.$connect()
1818
}
19-
20-
async enableShutdownHooks(app: INestApplication) {
21-
this.$on('beforeExit', async () => {
22-
await app.close()
23-
})
24-
}
2519
}

apps/api/src/transfer/transfer.controller.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe('TransferController', () => {
135135

136136
prismaMock.transfer.create.mockResolvedValue(transfer)
137137
prismaMock.vault.update.mockResolvedValue(vault)
138-
prismaMock.vault.findFirst.mockResolvedValue(vault)
138+
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
139139
prismaMock.$transaction.mockResolvedValue([transfer, vault])
140140

141141
const createDto: CreateTransferDto = { ...transfer }
@@ -198,9 +198,9 @@ describe('TransferController', () => {
198198
blockedAmount: 0,
199199
}
200200

201-
prismaMock.transfer.findFirst.mockResolvedValue(transfer)
202-
prismaMock.vault.findFirst.mockResolvedValueOnce(srcVault)
203-
prismaMock.vault.findFirst.mockResolvedValueOnce(dstVault)
201+
prismaMock.transfer.findFirstOrThrow.mockResolvedValue(transfer)
202+
prismaMock.vault.findFirstOrThrow.mockResolvedValueOnce(srcVault)
203+
prismaMock.vault.findFirstOrThrow.mockResolvedValueOnce(dstVault)
204204
prismaMock.transfer.update.mockResolvedValue(transfer)
205205
prismaMock.vault.update.mockResolvedValueOnce(srcVault)
206206
prismaMock.vault.update.mockResolvedValueOnce(dstVault)
@@ -256,9 +256,9 @@ describe('TransferController', () => {
256256
blockedAmount: 0,
257257
}
258258

259-
prismaMock.transfer.findFirst.mockResolvedValue(transfer)
260-
prismaMock.vault.findFirst.mockResolvedValueOnce(srcVault)
261-
prismaMock.vault.findFirst.mockResolvedValueOnce(dstVault)
259+
prismaMock.transfer.findFirstOrThrow.mockResolvedValue(transfer)
260+
prismaMock.vault.findFirstOrThrow.mockResolvedValueOnce(srcVault)
261+
prismaMock.vault.findFirstOrThrow.mockResolvedValueOnce(dstVault)
262262
prismaMock.transfer.update.mockResolvedValue(transfer)
263263
prismaMock.vault.update.mockResolvedValueOnce(srcVault)
264264
prismaMock.vault.update.mockResolvedValueOnce(dstVault)

0 commit comments

Comments
 (0)