From 11375cad776c0dba429a858b37b000693998598e Mon Sep 17 00:00:00 2001 From: Katsuma Ito Date: Tue, 20 Jun 2023 19:49:32 +0900 Subject: [PATCH] fix: recover from disconnected state --- src/lib/prisma-session-store.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/lib/prisma-session-store.ts b/src/lib/prisma-session-store.ts index f8f0436..7b9fc05 100755 --- a/src/lib/prisma-session-store.ts +++ b/src/lib/prisma-session-store.ts @@ -154,6 +154,13 @@ export class PrismaSessionStore 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. */ @@ -161,13 +168,18 @@ export class PrismaSessionStore extends Store { 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; } @@ -499,6 +511,8 @@ export class PrismaSessionStore 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();