Skip to content

Commit

Permalink
Merge pull request #114 from ka2n/recover
Browse files Browse the repository at this point in the history
Recovery from invalid connection
  • Loading branch information
kleydon committed Jun 26, 2023
2 parents ecb8989 + 11375ca commit a98a0b6
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/lib/prisma-session-store.ts
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 a98a0b6

Please sign in to comment.