feat: integrate @t3-oss/env for environment variable management across the application

This commit is contained in:
tranminhquang
2025-07-15 13:29:16 +07:00
parent 55978c30fa
commit 9d5fb821c7
16 changed files with 118 additions and 37 deletions
+3 -5
View File
@@ -1,17 +1,15 @@
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";
import { keys } from "./keys";
const { DATABASE_URL } = keys();
// Create a lazy database instance that only initializes when accessed
let _db: ReturnType<typeof drizzle> | null = null;
function getDb() {
if (!process.env.DATABASE_URL) {
throw new Error("DATABASE_URL is not set");
}
if (!_db) {
const client = postgres(process.env.DATABASE_URL);
const client = postgres(DATABASE_URL);
_db = drizzle(client, { schema });
}
+19
View File
@@ -0,0 +1,19 @@
import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";
export const keys = () =>
createEnv({
server: {
NODE_ENV: z
.enum(["development", "production", "test"])
.default("development"),
DATABASE_URL: z
.string()
.startsWith("postgres://")
.or(z.string().startsWith("postgresql://")),
},
runtimeEnv: {
NODE_ENV: process.env.NODE_ENV,
DATABASE_URL: process.env.DATABASE_URL,
},
});