Skip to content

Commit 8d36632

Browse files
Update database.ts
1 parent 01fea09 commit 8d36632

File tree

1 file changed

+65
-44
lines changed

1 file changed

+65
-44
lines changed

lib/database.ts

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { SupabaseClient } from '@supabase/supabase-js'
12
import { createBrowserClient } from './supabase-client'
23
import { Message } from './messages'
34

4-
const supabase = createBrowserClient()
5+
// The supabase client will be passed as an argument to functions.
6+
// A browser client is created here for convenience on the client-side.
7+
const browserSupabase = createBrowserClient()
58

69
export interface Project {
710
id: string
@@ -34,42 +37,43 @@ let tablesChecked = false
3437
let tablesExist = false
3538

3639
// Check if tables exist once, then cache result
37-
async function ensureTablesExist(): Promise<boolean> {
38-
if (tablesChecked) return tablesExist
39-
40-
if (!supabase) {
41-
tablesChecked = true
42-
tablesExist = false
43-
return false
44-
}
45-
46-
try {
47-
// Quick check for critical tables
48-
const { error } = await supabase.from('projects').select('id').limit(1)
49-
50-
tablesChecked = true
51-
tablesExist = !error || error.code !== 'PGRST106'
52-
53-
if (!tablesExist) {
54-
console.warn('Database tables do not exist. Please run the migration.')
55-
}
56-
57-
return tablesExist
58-
} catch (error) {
59-
console.error('Table check failed:', error)
60-
tablesChecked = true
61-
tablesExist = false
62-
return false
63-
}
40+
async function ensureTablesExist(supabase: SupabaseClient<any, "public", any>): Promise<boolean> {
41+
if (tablesChecked) return tablesExist;
42+
43+
if (!supabase) {
44+
tablesChecked = true
45+
tablesExist = false
46+
return false
47+
}
48+
49+
try {
50+
// Quick check for critical tables
51+
const { error } = await supabase.from('projects').select('id').limit(1)
52+
53+
tablesChecked = true
54+
tablesExist = !error || error.code !== 'PGRST106'
55+
56+
if (!tablesExist) {
57+
console.warn('Database tables do not exist. Please run the migration.')
58+
}
59+
60+
return tablesExist
61+
} catch (error) {
62+
console.error('Table check failed:', error)
63+
tablesChecked = true
64+
tablesExist = false
65+
return false
66+
}
6467
}
6568

6669
// Wrapper to prevent API calls when tables don't exist
6770
async function safeApiCall<T>(
71+
supabase: SupabaseClient<any, "public", any>,
6872
operation: () => Promise<T>,
6973
fallback: T,
7074
operationName: string
7175
): Promise<T> {
72-
if (!(await ensureTablesExist())) {
76+
if (!(await ensureTablesExist(supabase))) {
7377
console.warn(`Skipping ${operationName} - tables do not exist`)
7478
return fallback
7579
}
@@ -87,16 +91,17 @@ async function safeApiCall<T>(
8791
// =============================================
8892

8993
export async function createProject(
94+
supabase: SupabaseClient<any, "public", any>,
9095
title: string,
9196
templateId?: string,
9297
description?: string,
9398
teamId?: string
9499
): Promise<Project | null> {
95-
return safeApiCall(async () => {
96-
const { data: { user } } = await supabase!.auth.getUser()
100+
return safeApiCall(supabase, async () => {
101+
const { data: { user } } = await supabase.auth.getUser()
97102
if (!user) throw new Error('User not authenticated')
98103

99-
const { data, error } = await supabase!
104+
const { data, error } = await supabase
100105
.from('projects')
101106
.insert({
102107
user_id: user.id,
@@ -117,10 +122,11 @@ export async function createProject(
117122
}
118123

119124
export async function getProjects(
125+
supabase: SupabaseClient<any, "public", any> | null = browserSupabase,
120126
includeArchived: boolean = false,
121127
teamId?: string
122128
): Promise<Project[]> {
123-
return safeApiCall(async () => {
129+
return safeApiCall(supabase!, async () => {
124130
const { data: { user } } = await supabase!.auth.getUser()
125131
if (!user) return []
126132

@@ -145,8 +151,11 @@ export async function getProjects(
145151
}, [], 'getProjects')
146152
}
147153

148-
export async function getProject(projectId: string): Promise<Project | null> {
149-
return safeApiCall(async () => {
154+
export async function getProject(
155+
supabase: SupabaseClient<any, "public", any> | null = browserSupabase,
156+
projectId: string
157+
): Promise<Project | null> {
158+
return safeApiCall(supabase!, async () => {
150159
const { data, error } = await supabase!
151160
.from('projects')
152161
.select('*')
@@ -159,10 +168,11 @@ export async function getProject(projectId: string): Promise<Project | null> {
159168
}
160169

161170
export async function updateProject(
171+
supabase: SupabaseClient<any, "public", any> | null = browserSupabase,
162172
id: string,
163173
updates: Partial<Project>
164174
): Promise<boolean> {
165-
return safeApiCall(async () => {
175+
return safeApiCall(supabase!, async () => {
166176
const { error } = await supabase!
167177
.from('projects')
168178
.update({ ...updates, updated_at: new Date().toISOString() })
@@ -173,8 +183,12 @@ export async function updateProject(
173183
}, false, 'updateProject')
174184
}
175185

176-
export async function deleteProject(id: string, permanent: boolean = false): Promise<boolean> {
177-
return safeApiCall(async () => {
186+
export async function deleteProject(
187+
supabase: SupabaseClient<any, "public", any> | null = browserSupabase,
188+
id: string,
189+
permanent: boolean = false
190+
): Promise<boolean> {
191+
return safeApiCall(supabase!, async () => {
178192
if (permanent) {
179193
const { error } = await supabase!
180194
.from('projects')
@@ -200,11 +214,12 @@ export async function deleteProject(id: string, permanent: boolean = false): Pro
200214
// =============================================
201215

202216
export async function saveMessage(
217+
supabase: SupabaseClient<any, "public", any> | null = browserSupabase,
203218
projectId: string,
204219
message: Message,
205220
sequenceNumber: number
206221
): Promise<boolean> {
207-
return safeApiCall(async () => {
222+
return safeApiCall(supabase!, async () => {
208223
const { error } = await supabase!
209224
.from('messages')
210225
.insert({
@@ -228,8 +243,11 @@ export async function saveMessage(
228243
}, false, 'saveMessage')
229244
}
230245

231-
export async function getProjectMessages(projectId: string): Promise<Message[]> {
232-
return safeApiCall(async () => {
246+
export async function getProjectMessages(
247+
supabase: SupabaseClient<any, "public", any> | null = browserSupabase,
248+
projectId: string
249+
): Promise<Message[]> {
250+
return safeApiCall(supabase!, async () => {
233251
const { data, error } = await supabase!
234252
.from('messages')
235253
.select('*')
@@ -247,8 +265,11 @@ export async function getProjectMessages(projectId: string): Promise<Message[]>
247265
}, [], 'getProjectMessages')
248266
}
249267

250-
export async function clearProjectMessages(projectId: string): Promise<boolean> {
251-
return safeApiCall(async () => {
268+
export async function clearProjectMessages(
269+
supabase: SupabaseClient<any, "public", any> | null = browserSupabase,
270+
projectId: string
271+
): Promise<boolean> {
272+
return safeApiCall(supabase!, async () => {
252273
const { error } = await supabase!
253274
.from('messages')
254275
.delete()
@@ -282,4 +303,4 @@ export async function generateProjectTitle(firstMessage: string): Promise<string
282303
export function resetTableCheck(): void {
283304
tablesChecked = false
284305
tablesExist = false
285-
}
306+
}

0 commit comments

Comments
 (0)