trigger

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package trigger builds database triggers as type-checked Go values, independent of any SQL dialect. BeforeInsert/BeforeUpdate/ BeforeDelete/AfterInsert/AfterUpdate/AfterDelete start a trigger on a gorm model; Set/SetColumns assign NEW columns, validated against the model's real gorm schema at Build time, and Body supplies raw SQL for anything Set can't express -- required for DELETE triggers, since there's no NEW row to assign into.

tr := trigger.BeforeUpdate(&User{}).Set("updated_at", trigger.Now())

A built Definition is dialect-agnostic; github.com/bg12345/gorm-dbobjects's Client resolves it to the connected engine's actual DDL.

Example
package main

import (
	"fmt"

	"github.com/bg12345/gorm-dbobjects/trigger"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Name string
}

func main() {
	tr := trigger.BeforeUpdate(&User{}).
		Set("updated_at", trigger.Now())

	def, err := tr.Build()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(def.Table, def.Timing, def.Event)
}
Output:
users BEFORE UPDATE

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Definition

type Definition struct {
	Table  string
	Timing string // "BEFORE" or "AFTER"
	Event  string // "INSERT", "UPDATE", or "DELETE"
	Sets   map[string]Expr
	Body   string // raw full trigger body; required for DELETE (no NEW row to assign into)
	Name   string // optional; if empty, dbobjects will generate one

	// PrimaryKey is the table's single primary key column, resolved
	// best-effort whenever Sets is non-empty (regardless of Timing) and
	// the table has exactly one primary key field; empty otherwise. Used
	// by dialects that render Set() as a follow-up UPDATE targeting this
	// column instead of NEW.col = expr -- Postgres/MySQL for AFTER
	// triggers (NEW isn't assignable once an AFTER trigger fires, the
	// row's already written) and SQL Server for every Set()-based
	// trigger (SQL Server has no BEFORE DML trigger at all, so Set()
	// always renders AFTER there). Unused by SQLite, which targets
	// rowid instead. See dialect.go's renderTrigger per dialect.
	PrimaryKey string
}

Definition is the DB-agnostic description of a trigger, produced by Build() and consumed by whatever applies it (e.g. dbobjects.Register).

type Expr

type Expr struct {
	Kind ExprKind
	Raw  string
}

Expr represents a value assigned to a column inside the trigger body. Raw is written into the generated SQL verbatim, so callers should only build it via Now()/Raw() rather than embedding untrusted input.

func Now

func Now() Expr

Now renders as the Postgres NOW() function.

func Raw

func Raw(sql string) Expr

Raw renders sql verbatim in the trigger body, e.g. Raw("version + 1").

type ExprKind

type ExprKind int

ExprKind distinguishes what an Expr represents, so a dialect can render it in its own engine's spelling (Postgres/MySQL's NOW() vs SQL Server's SYSUTCDATETIME() vs Oracle's SYSDATE, for ExprNow) instead of Expr baking in one dialect's spelling itself.

const (
	ExprRaw ExprKind = iota // zero value: Raw holds the literal SQL to emit verbatim
	ExprNow                 // "current timestamp" -- each dialect resolves its own spelling
)

type Trigger

type Trigger interface {
	Kind() string
	Set(column string, value Expr) Trigger
	SetColumns(columns map[string]Expr) Trigger
	Body(sql string) Trigger
	Name(n string) Trigger
	Build() (*Definition, error)
}

func AfterDelete added in v0.2.0

func AfterDelete(model any) Trigger

func AfterInsert added in v0.2.0

func AfterInsert(model any) Trigger

func AfterUpdate added in v0.2.0

func AfterUpdate(model any) Trigger

func BeforeDelete added in v0.2.0

func BeforeDelete(model any) Trigger

func BeforeInsert

func BeforeInsert(model any) Trigger

func BeforeUpdate

func BeforeUpdate(model any) Trigger

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL