diff --git a/database/connect.ts b/database/connect.ts index ff81257..b12c313 100644 --- a/database/connect.ts +++ b/database/connect.ts @@ -6,14 +6,14 @@ import { setEnvironmentVariables } from '../util/config'; setEnvironmentVariables(); -declare global { - var postgresSqlClient: Sql | undefined; +declare module globalThis { + let postgresSqlClient: Sql; } // Connect only once to the database // https://github.com/vercel/next.js/issues/7811#issuecomment-715259370 function connectOneTimeToDatabase() { - if (!globalThis.postgresSqlClient) { + if (!('postgresSqlClient' in globalThis)) { globalThis.postgresSqlClient = postgres(postgresConfig); } @@ -34,11 +34,8 @@ function connectOneTimeToDatabase() { ...sqlParameters: Parameters ) => { noStore(); - if (!globalThis.postgresSqlClient) { - throw new Error('Postgres client is not initialized'); - } return globalThis.postgresSqlClient(...sqlParameters); - }) as Sql; + }) as typeof globalThis.postgresSqlClient; } export const sql = connectOneTimeToDatabase();