diff --git a/src/data/database.ts b/src/data/database.ts index 0536635..5b0cefb 100644 --- a/src/data/database.ts +++ b/src/data/database.ts @@ -4,6 +4,7 @@ import {Kysely, PostgresDialect} from 'kysely' import * as fs from "node:fs"; import {z} from 'zod'; +type Env = z.infer; const envSchema = z.object({ POSTGRES_DB: z.string(), POSTGRES_HOST: z.string(), @@ -38,9 +39,14 @@ const envSchema = z.object({ } ); -const env = envSchema.parse(process.env); +const buildTime = process.env.NEXT_PHASE === 'phase-production-build'; +const env = buildTime ? 'build-time' : envSchema.parse(process.env); + +function fileOrEnv(fileKey: keyof Env, valueKey: keyof Env): string | undefined { + if (env == 'build-time') { + return undefined; + } -function fileOrEnv(fileKey: keyof typeof env, valueKey: keyof typeof env): string | undefined { const file: string = env[fileKey] as string; if (file && fs.existsSync(file)) { @@ -57,16 +63,14 @@ function getCredentials() { } } -const dialect = new PostgresDialect({ - pool: new Pool({ - database: env.POSTGRES_DB, - host: env.POSTGRES_HOST, - port: env.POSTGRES_PORT, - ...getCredentials(), - max: 10, - }) -}) - -export const db = new Kysely({ - dialect, +export const db = env === 'build-time' ? null : new Kysely({ + dialect: new PostgresDialect({ + pool: new Pool({ + database: env.POSTGRES_DB, + host: env.POSTGRES_HOST, + port: env.POSTGRES_PORT, + ...getCredentials(), + max: 10, + }) + }), }) diff --git a/src/data/fetchWithSQL.ts b/src/data/fetchWithSQL.ts index d61ee03..0ad6d7c 100644 --- a/src/data/fetchWithSQL.ts +++ b/src/data/fetchWithSQL.ts @@ -4,6 +4,14 @@ import {db} from "@/data/database"; import * as dFns from "date-fns"; export async function getDataSQL(config: OverviewConfig): Promise { + // If we're in build-time, we don't have access to the database + if (!db) { + return { + projects: [], + timeEntries: [], + } + } + let projectIds = config.subjects.map((subject) => subject.projectId.toString()); const projects = await db.selectFrom('project')