データベース関数とトリガー
Postgres関数にロジックをカプセル化し、トリガーでデータ変更への反応を自動化します。これらはすべてSupabaseから呼び出せます。
「データベース関数とトリガー」はCoddyKit上の無料Supabase Backend as a Serviceレッスンです。 これはレッスン3/3です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSupabase Backend as a Service学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Supabase Backend as a Serviceコースには全3レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「データベース関数とトリガー」レッスンは無料ですか?
はい。「データベース関数とトリガー」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Supabase Backend as a Serviceコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Supabase Backend as a Serviceコースには全3レッスンが含まれています。
「データベース関数とトリガー」で何を学びますか?
Postgres関数にロジックをカプセル化し、トリガーでデータ変更への反応を自動化します。これらはすべてSupabaseから呼び出せます。 ブラウザで直接実行するハンズオンコードでSupabase Backend as a Serviceを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Supabase Backend as a Serviceを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSupabase Backend as a Serviceは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/3です。
「データベース関数とトリガー」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSupabase Backend as a Serviceレッスンでコードを書いて実行できますか?
はい。すべてのSupabase Backend as a Serviceレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 高度なSQLクエリと結合
- パフォーマンス向上のためのデータベースインデックス
- データベース関数とトリガー