Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/actions/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export async function saveSystemPrompt(
): Promise<{ success?: boolean; error?: string }> {
if (!userId) return { error: 'User ID is required' }
if (!prompt) return { error: 'Prompt is required' }
if (!db) return { error: 'Database is not available' }

try {
await db.update(users)
Expand All @@ -170,6 +171,10 @@ export async function getSystemPrompt(
userId: string
): Promise<string | null> {
if (!userId) return null
if (!db) {
console.error('getSystemPrompt: Database is not available')
return null
}

try {
const result = await db.select({ systemPrompt: users.systemPrompt })
Expand Down
35 changes: 21 additions & 14 deletions lib/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ import * as schema from './schema';

dotenv.config({ path: '.env.local' });

if (!process.env.DATABASE_URL) {
throw new Error('DATABASE_URL environment variable is not set for Drizzle client');
}
let db: ReturnType<typeof drizzle<typeof schema>> | null = null;

const poolConfig: PoolConfig = {
connectionString: process.env.DATABASE_URL,
};
try {
if (!process.env.DATABASE_URL) {
console.error('DATABASE_URL environment variable is not set for Drizzle client');
} else {
const poolConfig: PoolConfig = {
connectionString: process.env.DATABASE_URL,
};

// Conditionally apply SSL for Supabase URLs
if (process.env.DATABASE_URL && process.env.DATABASE_URL.includes('supabase.co')) {
poolConfig.ssl = {
rejectUnauthorized: false,
};
}
// Conditionally apply SSL for Supabase URLs
if (process.env.DATABASE_URL.includes('supabase.co')) {
poolConfig.ssl = {
rejectUnauthorized: false,
};
}

const pool = new Pool(poolConfig);
const pool = new Pool(poolConfig);
db = drizzle(pool, { schema, logger: process.env.NODE_ENV === 'development' });
}
} catch (error) {
console.error('Failed to initialize database connection:', error);
}

export const db = drizzle(pool, { schema, logger: process.env.NODE_ENV === 'development' });
export { db };