Configures database INSERT triggers that offload document chunking and embedding to Edge Functions.
94%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
High
Do not use without reviewing
supabase-mcp-verification tile installed and passing.pgvector-hnsw-index-selection tile installed (vector storage target exists). See that skill for the target vector table schema.edge-function-background-orchestration tile installed (Edge Function runtime available). See that skill for Edge Function deployment patterns.pgvector and pg_net extensions enabled.id UUID PRIMARY KEY DEFAULT gen_random_uuid(), content TEXT NOT NULL, metadata JSONB DEFAULT '{}', status TEXT DEFAULT 'pending', tenant_id UUID NOT NULL, created_at TIMESTAMPTZ DEFAULT now().pg_net.http_post() to call the embedding Edge Function.id, content, and metadata as the request payload.status to 'processing' via an explicit UPDATE after the pg_net call.AFTER INSERT to ensure the row is committed before the Edge Function fetches it.CREATE OR REPLACE FUNCTION trigger_embedding_pipeline()
RETURNS TRIGGER LANGUAGE plpgsql AS $$
BEGIN
PERFORM pg_net.http_post(
url := '{EDGE_FUNCTION_URL}',
body := jsonb_build_object('id', NEW.id, 'content', NEW.content, 'metadata', NEW.metadata),
headers := '{"Authorization": "Bearer {SERVICE_ROLE_KEY}"}'::jsonb
);
UPDATE {table} SET status = 'processing' WHERE id = NEW.id;
RETURN NULL;
END;
$$;CREATE TRIGGER on_document_insert AFTER INSERT ON {table} FOR EACH ROW EXECUTE FUNCTION trigger_embedding_pipeline();.Scaffold supabase/functions/embed-document/index.ts with the template below. Replace {EMBEDDING_MODEL}, {CHUNK_SIZE}, {VECTOR_TABLE}, and {table} with caller-supplied values. For full deployment patterns, refer to the edge-function-background-orchestration skill.
Tip: Save this template as
templates/embed-document.tsin your project and reference it to keep the skill invocation lightweight on repeat use.
import { serve } from 'https://deno.land/std@0.168.0/http/server.ts';
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2';
const supabase = createClient(
Deno.env.get('SUPABASE_URL')!,
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!,
);
serve(async (req) => {
const { id, content, metadata } = await req.json();
try {
const chunkSize = {CHUNK_SIZE};
const chunks = [];
for (let i = 0; i < content.length; i += chunkSize) {
chunks.push(content.slice(i, i + chunkSize));
}
const vectors = [];
for (let idx = 0; idx < chunks.length; idx++) {
const embeddingRes = await fetch('https://api.openai.com/v1/embeddings', {
method: 'POST',
headers: { 'Authorization': `Bearer ${Deno.env.get('OPENAI_API_KEY')}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ model: '{EMBEDDING_MODEL}', input: chunks[idx] }),
});
const { data } = await embeddingRes.json();
vectors.push({ document_id: id, chunk_index: idx, content: chunks[idx], embedding: data[0].embedding, metadata });
}
const { error: insertError } = await supabase.from('{VECTOR_TABLE}').insert(vectors);
if (insertError) throw insertError;
await supabase.from('{table}').update({ status: 'embedded' }).eq('id', id);
return new Response(JSON.stringify({ ok: true, chunks: vectors.length }), { status: 200 });
} catch (err) {
await supabase.from('{table}').update({ status: 'failed' }).eq('id', id);
return new Response(JSON.stringify({ error: String(err) }), { status: 500 });
}
});Deploy via supabase functions deploy embed-document.
apply_migration.pg_net request appears in net._http_response.pending → processing → embedded / failed).