0Pricing
Supabase Backend as a Service · Lezione

Funzioni e trigger del database

Incapsuli la logica nelle funzioni Postgres e automatizzi le reazioni alle modifiche dei dati con i trigger, rendendo tutto richiamabile da Supabase.

Funzioni e trigger del database è una lezione Supabase Backend as a Service gratuita su CoddyKit. Questa è la lezione 3 di 3. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Supabase Backend as a Service, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Supabase Backend as a Service include 3 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Logic Inside the Database

Postgres lets you store reusable logic as functions and run code automatically on data changes with triggers. This keeps critical rules close to the data.

What Is a Database Function?

A function is named, reusable SQL or PL/pgSQL that returns a value. You can call it from queries or from the Supabase client via RPC.

Writing a Simple Function

This function returns the count of active users.

create or replace function active_user_count()
returns integer
language sql
as 'select count(*)::int from users where active';

Calling It from Supabase

Use rpc to invoke a database function from the client.

const { data, error } = await supabase
  .rpc('active_user_count');
console.log(data);

Functions With Parameters

Functions can accept arguments, enabling parameterized server-side logic.

create or replace function posts_by(author_id bigint)
returns setof posts
language sql
as 'select * from posts where user_id = author_id';

What Is a Trigger?

A trigger runs a function automatically BEFORE or AFTER an INSERT, UPDATE, or DELETE on a table.

A Trigger Function

Trigger functions return trigger and access the new row via NEW.

create or replace function set_updated_at()
returns trigger
language plpgsql
as 'begin new.updated_at = now(); return new; end;';

Attaching the Trigger

Bind the function to a table and event.

create trigger touch_updated
before update on posts
for each row
execute function set_updated_at();

Common Use Cases

Triggers shine for:

  • Auto-setting timestamps
  • Maintaining denormalized counts
  • Writing audit log rows
  • Creating a profile row when a user signs up

Keep Triggers Lightweight

Triggers run inside the transaction, so heavy work slows every write. Offload long tasks to Edge Functions or queues instead.

function isLightweight(ms) { return ms < 10; }
console.log(isLightweight(4));

Putting It Together

Use functions for reusable, callable logic and triggers for automatic reactions to changes, while keeping trigger work small and fast.

Quick Check

Test your understanding of functions and triggers.

Recap

You learned to write callable database functions, invoke them with rpc, and automate logic with triggers using NEW, while keeping trigger work lightweight.

Domande Frequenti

La lezione «Funzioni e trigger del database» è gratuita?

Sì — il testo completo di «Funzioni e trigger del database» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Supabase Backend as a Service, passa a CoddyKit PRO. Il corso Supabase Backend as a Service include 3 lezioni in totale.

Cosa imparerò in «Funzioni e trigger del database»?

Incapsuli la logica nelle funzioni Postgres e automatizzi le reazioni alle modifiche dei dati con i trigger, rendendo tutto richiamabile da Supabase. Eserciti Supabase Backend as a Service con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Supabase Backend as a Service?

Non è richiesta alcuna esperienza precedente. Supabase Backend as a Service su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 3.

Quanto tempo richiede la lezione «Funzioni e trigger del database»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Supabase Backend as a Service?

Sì. Ogni lezione Supabase Backend as a Service include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Query SQL e join avanzati
  2. Indicizzazione del database per le prestazioni
  3. Funzioni e trigger del database
← Torna a Supabase Backend as a Service