refactor: using Zod to validate .env schema
This commit is contained in:
18
packages/backend/src/env/index.ts
vendored
Normal file
18
packages/backend/src/env/index.ts
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import dotenv from 'dotenv';
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
const envSchema = z.object({
|
||||||
|
PORT: z
|
||||||
|
.string()
|
||||||
|
.refine(
|
||||||
|
(port) => parseInt(port) > 0 && parseInt(port) < 65536,
|
||||||
|
"Invalid port number"
|
||||||
|
),
|
||||||
|
DATABASE_URL: z.string().min(10)
|
||||||
|
});
|
||||||
|
|
||||||
|
type Env = z.infer<typeof envSchema>;
|
||||||
|
|
||||||
|
export const ENV: Env = envSchema.parse(process.env);
|
||||||
@ -1,11 +1,4 @@
|
|||||||
import 'dotenv/config';
|
|
||||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||||
import { pgTable, text, uuid } from 'drizzle-orm/pg-core';
|
import { ENV } from '../env';
|
||||||
|
|
||||||
const DATABASE_URL = process.env.DATABASE_URL;
|
|
||||||
if (!DATABASE_URL) {
|
|
||||||
throw new Error("DATABASE_URL is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
export const db = drizzle(DATABASE_URL);
|
|
||||||
|
|
||||||
|
export const db = drizzle(ENV.DATABASE_URL);
|
||||||
|
|||||||
Reference in New Issue
Block a user