Skip to content

Commit

Permalink
fix(prisma): silence the "Record to delete does not exist" error (gra…
Browse files Browse the repository at this point in the history
  • Loading branch information
leonekmi authored Aug 3, 2023
1 parent defe3e9 commit 7ce1614
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
38 changes: 38 additions & 0 deletions packages/prisma/__tests__/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { test, expect, describe, beforeEach } from 'vitest';
import { session } from 'grammy';
import { PrismaAdapter } from '../src';
import { createBot, createMessage } from '@grammyjs/storage-utils';
import prisma from './helpers/prisma';

beforeEach(async () => {
await prisma.session.deleteMany({});
});

describe('Delete test', () => {
test('A not yet stored record should be nullable without throwing', async () => {
const bot = createBot();

bot.use(
session({
initial() {
return { pizzaCount: 0 };
},
storage: new PrismaAdapter(prisma.session),
})
);

bot.hears('first', (ctx) => {
ctx.session = null;
});

bot.hears('second', (ctx) => {
expect(ctx.session).toHaveProperty('pizzaCount');
});

const firstMessage = createMessage(bot, 'first');
const secondMessage = createMessage(bot, 'second');

await bot.handleUpdate(firstMessage.update);
await bot.handleUpdate(secondMessage.update);
});
});
6 changes: 5 additions & 1 deletion packages/prisma/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class PrismaAdapter<T> implements StorageAdapter<T> {
}

async delete(key: string) {
await this.sessionDelegate.delete({ where: { key } });
await this.sessionDelegate.delete({ where: { key } }).catch((err) => {
// Record does not exist in database
if (err?.code === 'P2025') return;
return Promise.reject(err);
});
}
}

0 comments on commit 7ce1614

Please sign in to comment.