Home/Partnerships/Docs/Supabase Integration
Official Partner Integration Guide

Integrating Richard Ewing Diagnostics with Supabase

This guide walks you through connecting Supabase PostgreSQL database, pgvector embeddings, Row-Level Security (RLS), and Supabase Edge Functions to the Richard Ewing R&D Capital Audit platform.

1

Configure Supabase Connection Credentials

Retrieve your Supabase Project URL and API Keys from your Supabase Dashboard (Project Settings → API). Configure these environment variables in your deployment environment:

NEXT_PUBLIC_SUPABASE_URL="https://your-project-ref.supabase.co"
NEXT_PUBLIC_SUPABASE_ANON_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
SUPABASE_SERVICE_ROLE_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
2

Apply Database Migration & Row-Level Security (RLS)

Execute the following SQL migration in your Supabase SQL Editor. This enables the vector extension for semantic code indexing and establishes strict Row-Level Security policies:

-- 1. Enable pgvector for code debt indexing
CREATE EXTENSION IF NOT EXISTS vector;

-- 2. Create Audit Events Ledger Table
CREATE TABLE IF NOT EXISTS public.rd_audit_events (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id UUID NOT NULL,
    repo_id TEXT NOT NULL,
    commit_sha VARCHAR(40) NOT NULL,
    pdi_score NUMERIC(5,2) NOT NULL CHECK (pdi_score >= 0 AND pdi_score <= 100),
    aper_value NUMERIC(12,2) NOT NULL,
    embedding vector(1536),
    metadata JSONB DEFAULT '{}'::jsonb,
    recorded_at TIMESTAMPTZ DEFAULT clock_timestamp() NOT NULL,
    created_at TIMESTAMPTZ DEFAULT now() NOT NULL
);

-- 3. Enforce Row-Level Security (RLS)
ALTER TABLE public.rd_audit_events ENABLE ROW LEVEL SECURITY;

-- Tenant Isolation: Only authenticated tenant members can view their audit records
CREATE POLICY "Tenant Audit Isolation Policy"
    ON public.rd_audit_events
    FOR SELECT
    USING (auth.uid() = tenant_id OR auth.jwt() ->> 'role' = 'service_role');

-- Service Ingestion: Authorized background tasks can write audit telemetry
CREATE POLICY "Service Ingestion Policy"
    ON public.rd_audit_events
    FOR INSERT
    WITH CHECK (auth.uid() = tenant_id OR auth.jwt() ->> 'role' = 'service_role');
3

Initialize Supabase Client & Dispatch Ingestion Telemetry

Use the official @supabase/supabase-js client to ingest R&D audit calculations into the sovereign ledger:

import { createClient } from '@supabase/supabase-js';

const supabase = createClient(
  process.env.NEXT_PUBLIC_SUPABASE_URL!,
  process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
);

export async function submitAuditRecord({
  tenantId,
  repoId,
  commitSha,
  pdiScore,
  aperValue
}: {
  tenantId: string;
  repoId: string;
  commitSha: string;
  pdiScore: number;
  aperValue: number;
}) {
  const { data, error } = await supabase
    .from('rd_audit_events')
    .insert([{
      tenant_id: tenantId,
      repo_id: repoId,
      commit_sha: commitSha,
      pdi_score: pdiScore,
      aper_value: aperValue,
      metadata: { source: 'supabase-integration-v3' }
    }])
    .select();

  if (error) {
    throw new Error(`Failed to record audit event: ${error.message}`);
  }

  return data;
}
4

Deploy Realtime Telemetry with Supabase Edge Functions

Deploy a Deno-based Supabase Edge Function to process webhook triggers and recalculate the Product Debt Index (PDI) with sub-second execution latency:

import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";

serve(async (req) => {
  const supabase = createClient(
    Deno.env.get('SUPABASE_URL') ?? '',
    Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
  );

  const { event, repository, pdiCalculations } = await req.json();

  // Record calculation in sovereign PostgreSQL database
  const { error } = await supabase.from('rd_audit_events').insert({
    tenant_id: repository.owner_id,
    repo_id: repository.id,
    commit_sha: event.commit_sha,
    pdi_score: pdiCalculations.score,
    aper_value: pdiCalculations.aper
  });

  return new Response(JSON.stringify({ status: error ? 'error' : 'success' }), {
    headers: { "Content-Type": "application/json" },
  });
});