trigger

trigger is a cursor rule for Cursor from YuDefine/nuxt-supabase-starter. It costs 0 tokens per session (887 once invoked), scanned A, original, MIT.

Guidelines for writing PostgreSQL triggers, which are database actions that run automatically when data changes. They explain when a trigger is appropriate and how to keep its function safe and predictable.

In plain words
What is it for?
Use them when adding triggers for data synchronization, automatic update timestamps, or database-level rules. They cover naming, execution order, bulk operations, and secure function settings.
Why use it?
They prevent complex business logic from being hidden inside the database, where it is harder to test and maintain. They also reduce problems caused by trigger order, permissions, and transaction rollbacks.

Cursor rule for Cursor

Written for Cursor: installed under .cursor/.

Good fit Use them when adding triggers for data synchronization, automatic update timestamps, or database-level rules. They cover naming, execution order, bulk operations, and secure function settings.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/yudefine/nuxt-supabase-starter/trigger
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Clone the repo
git clone --depth 1 https://github.com/YuDefine/nuxt-supabase-starter

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for trigger

README.md
[![agentmods](https://agentmods.dev/badge/rules/yudefine/nuxt-supabase-starter/trigger.svg)](https://agentmods.dev/rules/yudefine/nuxt-supabase-starter/trigger)
Your own site
<a href="https://agentmods.dev/rules/yudefine/nuxt-supabase-starter/trigger"><img src="https://agentmods.dev/badge/rules/yudefine/nuxt-supabase-starter/trigger.svg" alt="Measured on agentmods" height="20"></a>
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 887 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00000 $0.00887
Opus 5 $0.00000 $0.00443
Sonnet 5 $0.00000 $0.00177
Haiku 4.5 $0.00000 $0.00089

Measured 3d ago against content hash b5deeb74f718, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-07, from the pricing page.

Security

Grade A, and why

trigger scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

template/.cursor/rules/trigger.mdc · 94 lines

How it starts

The opening of the file, as written. The whole thing — 94 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Trigger

本檔是 clade 投影,NEVER 就地編輯。專案特化寫進自家 .cursor/rules/local/;要改本檔請回 clade 源檔並 propagate。

新增 trigger 前先問:真的需要 trigger 嗎?

  • MUST 優先考慮在 server API handler 處理業務邏輯
  • MUST 只在以下情境用 trigger:
    • 跨 table 資料同步(denormalization、cache)
    • updated_at 自動更新(標準模式)
    • 必須在 DB 層保證的 invariant
  • NEVER 在 trigger 裡寫複雜業務邏輯(多表查詢、對外通知、複雜驗證)— 放 API handler

命名規約

  • Prefix 按用途trg_<table>_<event>_<purpose>(如 trg_posts_before_insert_set_slug
  • updated_at 統一命名set_<table>_updated_at + 對應 function public.set_updated_at()
  • 有順序依賴時:用 a_ / b_ / c_ 前綴明確表達執行順序(a_set_contextb_audit_log 之前,字母序)

核心陷阱

  • 同一事件多個 trigger 按名稱字母序執行 — 命名時注意順序依賴
  • FOR EACH ROW 在大量操作時效能差 — 批量操作考慮 FOR EACH STATEMENT + transition tables
  • Trigger function 中的 RAISE EXCEPTION 會 rollback 整個呼叫端 transaction — 謹慎使用,寫清楚 error message
  • 受限 schema 中的 trigger 無法直接 DROP — 要 drop 其依賴的 function 並加 CASCADE
  • Trigger function MUST SET search_path = '' — 防止 search_path injection
  • SECURITY DEFINER trigger function 需特別小心 — function 內做的操作會以 owner 權限執行,bypass RLS

檢查多 trigger 執行順序

select tgname, tgrelid::regclass, tgtype
from pg_trigger
where tgrelid = 'public.<table>'::regclass
  and not tgisinternal
order by tgname;  -- 實際執行順序

標準 Template:updated_at

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

create trigger set_<table>_updated_at
  before update on public.<table>
  for each row
  execute function public.set_updated_at();

通用 Template:業務 trigger

create or replace function public.<function_name>()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
  -- 業務邏輯
  return new;  -- BEFORE / INSTEAD OF
  -- or: return null;  -- AFTER(回傳值會被忽略)
end;
$$;

comment on function public.<function_name>() is '<中文描述>';

create trigger trg_<table>_<event>_<purpose>
  after insert or update on public.<table>
  for each row
  execute function public.<function_name>();

Read the full file on GitHub · 94 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago First seen · 94 lines · 0 tokens per session scan A b5deeb74f718

Subscribe to this mod's changes

trigger is a cursor rule published in the GitHub repository YuDefine/nuxt-supabase-starter (45 stars, last pushed yesterday), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 887 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.