feat: integrate Supabase for database operations and migrations
Add support for Supabase database operations, including migrations and queries. Implement new Supabase-related types, actions, and components to handle database interactions. Enhance the prompt system to include Supabase-specific instructions and constraints. Ensure data integrity and security by enforcing row-level security and proper migration practices.
This commit is contained in:
@@ -37,11 +37,15 @@ function parseCookies(cookieHeader: string): Record<string, string> {
|
||||
}
|
||||
|
||||
async function chatAction({ context, request }: ActionFunctionArgs) {
|
||||
const { messages, files, promptId, contextOptimization } = await request.json<{
|
||||
const { messages, files, promptId, contextOptimization, supabase } = await request.json<{
|
||||
messages: Messages;
|
||||
files: any;
|
||||
promptId?: string;
|
||||
contextOptimization: boolean;
|
||||
supabase?: {
|
||||
isConnected: boolean;
|
||||
hasSelectedProject: boolean;
|
||||
};
|
||||
}>();
|
||||
|
||||
const cookieHeader = request.headers.get('Cookie');
|
||||
@@ -181,6 +185,7 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
|
||||
|
||||
// Stream the text
|
||||
const options: StreamingOptions = {
|
||||
supabaseConnection: supabase,
|
||||
toolChoice: 'none',
|
||||
onFinish: async ({ text: content, finishReason, usage }) => {
|
||||
logger.debug('usage', JSON.stringify(usage));
|
||||
|
||||
92
app/routes/api.supabase.query.ts
Normal file
92
app/routes/api.supabase.query.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { type ActionFunctionArgs } from '@remix-run/cloudflare';
|
||||
import { createScopedLogger } from '~/utils/logger';
|
||||
|
||||
const logger = createScopedLogger('api.supabase.query');
|
||||
|
||||
export async function action({ request }: ActionFunctionArgs) {
|
||||
if (request.method !== 'POST') {
|
||||
return new Response('Method not allowed', { status: 405 });
|
||||
}
|
||||
|
||||
const authHeader = request.headers.get('Authorization');
|
||||
|
||||
if (!authHeader) {
|
||||
return new Response('No authorization token provided', { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const { projectId, query } = (await request.json()) as any;
|
||||
logger.debug('Executing query:', { projectId, query });
|
||||
|
||||
const response = await fetch(`https://api.supabase.com/v1/projects/${projectId}/database/query`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: authHeader,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ query }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
let errorData;
|
||||
|
||||
try {
|
||||
errorData = JSON.parse(errorText);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
errorData = { message: errorText };
|
||||
}
|
||||
|
||||
logger.error(
|
||||
'Supabase API error:',
|
||||
JSON.stringify({
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
error: errorData,
|
||||
}),
|
||||
);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
error: {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
message: errorData.message || errorData.error || errorText,
|
||||
details: errorData,
|
||||
},
|
||||
}),
|
||||
{
|
||||
status: response.status,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
return new Response(JSON.stringify(result), {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Query execution error:', error);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
error: {
|
||||
message: error instanceof Error ? error.message : 'Query execution failed',
|
||||
stack: error instanceof Error ? error.stack : undefined,
|
||||
},
|
||||
}),
|
||||
{
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
67
app/routes/api.supabase.ts
Normal file
67
app/routes/api.supabase.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { json } from '@remix-run/node';
|
||||
import type { ActionFunction } from '@remix-run/node';
|
||||
import type { SupabaseProject } from '~/types/supabase';
|
||||
|
||||
export const action: ActionFunction = async ({ request }) => {
|
||||
if (request.method !== 'POST') {
|
||||
return json({ error: 'Method not allowed' }, { status: 405 });
|
||||
}
|
||||
|
||||
// Inside the action function
|
||||
try {
|
||||
const { token } = (await request.json()) as any;
|
||||
|
||||
const projectsResponse = await fetch('https://api.supabase.com/v1/projects', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!projectsResponse.ok) {
|
||||
const errorText = await projectsResponse.text();
|
||||
console.error('Projects fetch failed:', errorText);
|
||||
|
||||
return json({ error: 'Failed to fetch projects' }, { status: 401 });
|
||||
}
|
||||
|
||||
const projects = (await projectsResponse.json()) as SupabaseProject[];
|
||||
|
||||
// Create a Map to store unique projects by ID
|
||||
const uniqueProjectsMap = new Map<string, SupabaseProject>();
|
||||
|
||||
// Only keep the latest version of each project
|
||||
for (const project of projects) {
|
||||
if (!uniqueProjectsMap.has(project.id)) {
|
||||
uniqueProjectsMap.set(project.id, project);
|
||||
}
|
||||
}
|
||||
|
||||
// Debug log to see unique projects
|
||||
console.log(
|
||||
'Unique projects:',
|
||||
Array.from(uniqueProjectsMap.values()).map((p) => ({ id: p.id, name: p.name })),
|
||||
);
|
||||
|
||||
const uniqueProjects = Array.from(uniqueProjectsMap.values());
|
||||
|
||||
// Sort projects by creation date (newest first)
|
||||
uniqueProjects.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
|
||||
|
||||
return json({
|
||||
user: { email: 'Connected', role: 'Admin' },
|
||||
stats: {
|
||||
projects: uniqueProjects,
|
||||
totalProjects: uniqueProjects.length,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Supabase API error:', error);
|
||||
return json(
|
||||
{
|
||||
error: error instanceof Error ? error.message : 'Authentication failed',
|
||||
},
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user