Skip to content

Commit

Permalink
Variable supabase implicitly has type "any" in some locations
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjonas committed Jul 25, 2023
1 parent 4a0f23e commit df51909
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/database/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FeedbackPayload } from "@/types";
import { createClient } from "@supabase/supabase-js";
import { createClient, SupabaseClient } from "@supabase/supabase-js";
import getConfig from "next/config";

// Access the environment variables
Expand All @@ -10,26 +10,32 @@ const DB_NAME = publicRuntimeConfig.DB_NAME;

export const isSupabaseInitialized = SUPABASE_URL !== undefined && SUPABASE_ANON_KEY !== undefined && SUPABASE_URL !== "" && SUPABASE_ANON_KEY !== "";

// Initialize Supabase client
let supabase = null;
let supabase: SupabaseClient | null;

if (SUPABASE_URL) {
supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
} else {
supabase = null;
console.error('SUPABASE_URL is not defined in .env file');
}

// Example usage: Fetch all rows from a table named "tasks"
export class SupaBaseDatabase {
static getInstance() {
private client: SupabaseClient;

constructor(client: SupabaseClient) {
this.client = client;
}

static getInstance(): SupaBaseDatabase {
if (!supabase) {
throw new Error('Supabase has not been initialized because SUPABASE_URL is not defined');
}
return new SupaBaseDatabase();
return new SupaBaseDatabase(supabase);
}

async fetchData() {
const { data, error } = await supabase.from(DB_NAME).select("*");
const { data, error } = await this.client.from(DB_NAME).select("*");

if (error) {
console.error("Error fetching tasks:", error);
Expand All @@ -38,9 +44,15 @@ export class SupaBaseDatabase {
}
}
async insertData(payload: any) {
if (!this.client) {
throw new Error('Supabase client is not initialized');
}

payload.question = payload.question.toLowerCase();
payload.author_name = payload.author_name.toLowerCase();
const { data, error } = await supabase.from(DB_NAME).insert([payload]);

const { data, error } = await this.client.from(DB_NAME).insert([payload]);

if (error) {
console.error("Error inserting Q&A:", error);
} else {
Expand All @@ -49,7 +61,7 @@ export class SupaBaseDatabase {
}
async addFeedback(payload: FeedbackPayload) {
const { answerQuality, feedbackId, rating, timestamp } = payload;
const { data, error, status } = await supabase
const { data, error, status } = await this.client
.from(DB_NAME)
.update({
answerQuality,
Expand All @@ -69,7 +81,7 @@ export class SupaBaseDatabase {
async getAnswerByQuestion(question: string, author?: string) {
const oneDayBefore = new Date();
oneDayBefore.setDate(oneDayBefore.getDate() - 1);
let query = supabase
let query = this.client
.from(DB_NAME)
.select("answer, createdAt")
.eq('question', question);
Expand Down

0 comments on commit df51909

Please sign in to comment.