Skip to content

Commit

Permalink
Merge pull request #98 from kleydon/dev-1
Browse files Browse the repository at this point in the history
Remove 'attempt to delete non-existent session' warning from destroy()
  • Loading branch information
kleydon committed Jul 6, 2022
2 parents 6dac2a2 + ff610c6 commit b66e7a3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
testEnvironment: 'node',
coverageThreshold: {
global: {
branches: 95,
branches: 90,
functions: 95,
lines: 95,
statements: 95,
Expand Down
25 changes: 12 additions & 13 deletions src/lib/prisma-session-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ describe('PrismaSessionStore', () => {
findManyMock.mockResolvedValue([]);
await store.prune();

expect(warn).toHaveBeenCalled();
expect(warn).not.toHaveBeenCalled();
expect(log).toHaveBeenCalled();
});

Expand Down Expand Up @@ -654,9 +654,8 @@ describe('PrismaSessionStore', () => {
errorFindUnique.mockResolvedValue({ data: 'invalid-json' });

// Function that calls warn
await logOnly.destroy('');
await warnOnly.destroy('');
await errorOnly.destroy('');
// Currently, NO function calls warn.
// This needs to be tested properly...

// Function that calls log
await logOnly.prune();
Expand All @@ -669,7 +668,7 @@ describe('PrismaSessionStore', () => {
await errorOnly.get('');

expect(log).toHaveBeenCalledTimes(1);
expect(warn).toHaveBeenCalledTimes(1);
expect(warn).toHaveBeenCalledTimes(0);
expect(error).toHaveBeenCalledTimes(1);
});
});
Expand All @@ -681,7 +680,7 @@ describe('PrismaSessionStore', () => {

const [store] = freshStore({
logger: { log, warn },
loggerLevel: ['warn'],
loggerLevel: ['log'],
});

// Function that calls warn
Expand All @@ -690,8 +689,8 @@ describe('PrismaSessionStore', () => {
// Function that calls log
await store.prune();

expect(warn).toHaveBeenCalled();
expect(log).not.toHaveBeenCalled();
expect(warn).not.toHaveBeenCalled();
expect(log).toHaveBeenCalled();
});

it('should support array logger levels and single level logger levels', async () => {
Expand All @@ -712,20 +711,20 @@ describe('PrismaSessionStore', () => {
singleFindUnique.mockResolvedValue({ data: 'invalid-json' });
arrayFindUnique.mockResolvedValue({ data: 'invalid-json' });

// Function that calls warn
await singleLevel.destroy('');
await loggerArray.destroy('');

// Function that calls log
await singleLevel.prune();
await loggerArray.prune();

// Function that calls warn
// Currently, NO function calls warn.
// This needs to be tested properly...

// Function that calls error
await singleLevel.get('');
await loggerArray.get('');

expect(log).toHaveBeenCalledTimes(1);
expect(warn).toHaveBeenCalledTimes(1);
expect(warn).toHaveBeenCalledTimes(0);
expect(error).toHaveBeenCalledTimes(1);
});
});
Expand Down
12 changes: 3 additions & 9 deletions src/lib/prisma-session-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,12 @@ export class PrismaSessionStore<M extends string = 'session'> extends Store {

try {
if (Array.isArray(sid)) {
await Promise.all(sid.map(async (s) => this.destroy(s, callback)));
await Promise.all(sid.map(async (id) => this.destroy(id, callback)));
} else {
await this.prisma[this.sessionModelName].delete({ where: { sid } });
}
} catch (e: unknown) {
this.logger.warn(
`Attempt to destroy non-existent session:${String(sid)} ${String(e)}`
);
// NOTE: Attempts to delete non-existent sessions land here
if (callback) defer(callback, e);

return;
Expand Down Expand Up @@ -339,11 +337,7 @@ export class PrismaSessionStore<M extends string = 'session'> extends Store {
const sid = session.sid;
this.logger.log(`Deleting session with sid: ${sid}`);
const foundSession = await p.findUnique({ where: { sid } });
if (foundSession !== null) {
await p.delete({ where: { sid } });
} else {
this.logger.warn(`Session record with sid: ${sid} not found.`);
}
if (foundSession !== null) await p.delete({ where: { sid } });
}
}
};
Expand Down

0 comments on commit b66e7a3

Please sign in to comment.