supabase setup

This commit is contained in:
VinceC
2024-11-24 03:49:23 -06:00
parent c62c1f81e4
commit 3b10e77c82
3 changed files with 330 additions and 103 deletions

View File

@@ -6,53 +6,142 @@ class SupabaseService {
const supabaseKey = process.env.SUPABASE_KEY;
if (!supabaseUrl || !supabaseKey) {
console.error('Supabase URL or key is missing. Please check your .env file.');
this.supabase = null;
console.error('Supabase URL or key is missing. Please check your .env file.');
this.supabase = null;
} else {
this.supabase = createClient(supabaseUrl, supabaseKey);
this.supabase = createClient(supabaseUrl, supabaseKey);
}
}
async getSubscriptions(guildId) {
if (!this.supabase) {
console.error('Supabase client is not initialized.');
return [];
}
const { data, error } = await this.supabase
.from('subscriptions')
.select('*')
.eq('guild_id', guildId);
if (error) throw error;
return data;
}
async addSubscription(guildId, gameName, channelId) {
if (!this.supabase) {
console.error('Supabase client is not initialized.');
return null;
}
const { data, error } = await this.supabase
.from('subscriptions')
.insert({ guild_id: guildId, game_name: gameName, channel_id: channelId });
if (error) throw error;
return data;
}
async removeSubscription(guildId, gameName) {
if (!this.supabase) {
console.error('Supabase client is not initialized.');
return null;
}
const { data, error } = await this.supabase
.from('subscriptions')
.delete()
.match({ guild_id: guildId, game_name: gameName });
if (error) throw error;
return data;
}
}
async testConnection() {
if (!this.supabase) {
console.error('Supabase client is not initialized.');
return false;
}
try {
const { data, error } = await this.supabase
.from('games')
.select('count')
.limit(1);
if (error) {
console.error('Supabase connection test error:', error);
return false;
}
console.log('Supabase connection successful');
return true;
} catch (error) {
console.error('Supabase connection test failed:', error);
return false;
}
}
async getSubscriptions(guildId) {
if (!this.supabase) {
console.error('Supabase client is not initialized.');
return [];
}
const { data, error } = await this.supabase
.from('server_game_preferences')
.select(`
games (name),
notification_channel_id,
servers!inner (discord_server_id)
`)
.eq('servers.discord_server_id', guildId);
if (error) throw error;
return data;
}
async addSubscription(guildId, gameName, channelId) {
if (!this.supabase) {
console.error('Supabase client is not initialized.');
return null;
}
// First, get or create server record
const { data: serverData, error: serverError } = await this.supabase
.from('servers')
.upsert({
discord_server_id: guildId
}, {
onConflict: 'discord_server_id',
returning: true
});
if (serverError) throw serverError;
// Get game ID
const { data: gameData, error: gameError } = await this.supabase
.from('games')
.select('id')
.eq('name', gameName)
.eq('active', true)
.single();
if (gameError) throw gameError;
// Create subscription
const { data, error } = await this.supabase
.from('server_game_preferences')
.upsert({
server_id: serverData[0].id,
game_id: gameData.id,
notification_channel_id: channelId
}, {
onConflict: '(server_id,game_id)',
returning: true
});
if (error) throw error;
return data;
}
async removeSubscription(guildId, gameName) {
if (!this.supabase) {
console.error('Supabase client is not initialized.');
return null;
}
// Get server ID
const { data: serverData, error: serverError } = await this.supabase
.from('servers')
.select('id')
.eq('discord_server_id', guildId)
.single();
if (serverError) throw serverError;
// Get game ID
const { data: gameData, error: gameError } = await this.supabase
.from('games')
.select('id')
.eq('name', gameName)
.single();
if (gameError) throw gameError;
// Delete subscription
const { data, error } = await this.supabase
.from('server_game_preferences')
.delete()
.match({
server_id: serverData.id,
game_id: gameData.id
})
.single();
if (error) throw error;
return data;
}
getClient() {
return this.supabase;
}
}
module.exports = SupabaseService;