Skip to content

Commit

Permalink
fix: recover from disconnected state
Browse files Browse the repository at this point in the history
  • Loading branch information
ka2n committed Jun 20, 2023
1 parent ecb8989 commit 11375ca
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/lib/prisma-session-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,32 @@ export class PrismaSessionStore<M extends string = 'session'> extends Store {
this.invalidConnection = true;
}

/**
* Enables store, used when prisma can be connected to
*/
private enable(): void {
this.invalidConnection = false;
}

/**
* Returns if the connect is valid or not, logging an error if it is not.
*/
private async validateConnection(): Promise<boolean> {
await (
this.prisma?.$connect?.() ??
Promise.reject(new Error('Could not connect'))
).catch(() => {
this.disable();
this.stopInterval();
this.logger.error(dedent`Could not connect to 'Session' model in Prisma.
)
.then(() => {
this.enable();
this.startInterval();
})
.catch(() => {
this.disable();
this.stopInterval();
this.logger.error(dedent`Could not connect to 'Session' model in Prisma.
Please make sure that prisma is setup correctly, that 'Session' model exists, and that your migrations are current.
For more information check out https://github.com/kleydon/prisma-session-store`);
});
});

return !this.invalidConnection;
}
Expand Down Expand Up @@ -499,6 +511,8 @@ export class PrismaSessionStore<M extends string = 'session'> extends Store {
* Start an interval to prune expired sessions
*/
public startInterval(onIntervalError?: (err: unknown) => void): void {
if (this.checkInterval) return;

const ms = this.options.checkPeriod;
if (typeof ms === 'number' && ms !== 0) {
this.stopInterval();
Expand Down

0 comments on commit 11375ca

Please sign in to comment.