gorm-hooks

Intercept GORM model lifecycle hooks to apply validation and transactional side effects.

Updated Apr 4, 2026
One-click install
npx skills add https://github.com/liurida/gorm-development-skill --skill gorm-hooks
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: gorm-hooks
Source: https://github.com/liurida/gorm-development-skill/tree/main/hooks
Command: npx skills add https://github.com/liurida/gorm-development-skill --skill gorm-hooks

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

GORM hooks let you inject model-level validation, defaulting, audit logging, and transactional side effects directly into CRUD operations so that data integrity and side effects are enforced close to the data layer.

Core Features & Use Cases

  • Validation & Normalization: Run BeforeSave/BeforeCreate/BeforeUpdate hooks to enforce required fields and normalize values.
  • Defaults & IDs: Generate UUIDs and set defaults before persistence.
  • Audit & Cascading Actions: Create audit records or perform cascading updates in AfterCreate/AfterUpdate while remaining inside the same transaction.
  • Protection & Short-circuiting: Prevent destructive operations (for example, block deletion of admin users) by returning errors from hooks.
  • Use Case: Enforce per-record business rules and create audit logs for user creation without scattering logic across services.

Quick Start

Add a BeforeCreate hook that sets a UUID, validates required fields, and records an audit entry within the same transaction.

Frequently Asked Questions about gorm-hooks

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add validation and audit logging to GORM lifecycle operations?

You add validation and audit logging by implementing GORM lifecycle hooks like BeforeSave and AfterCreate with a func(*gorm.DB) error signature, enforcing data integrity directly within CRUD operations.

Can I prevent specific record deletions in GORM using hooks?

Yes, you can short-circuit destructive GORM operations by returning an error from the hook method, which blocks actions like deleting admin users within the transaction.

What is the best way to generate UUIDs and set defaults before saving GORM records?

Using BeforeCreate lifecycle hooks is the best way to generate UUIDs and set default values before persistence, ensuring records are properly initialized before entering the database transaction.

Do GORM hooks execute within the same database transaction as the main operation?

Yes, GORM hooks execute within the same database transaction as the triggering CRUD operation, allowing audit records and cascading updates to commit or rollback atomically.

Why should I avoid long-running external calls inside GORM lifecycle hooks?

You should avoid long-running external calls inside GORM hooks to prevent transaction timeouts, as the hook executes within the same database transaction and holds resources until completion.

How do I normalize data and enforce required fields during GORM updates?

Implement BeforeUpdate and BeforeSave hooks to normalize values and enforce required fields during GORM updates, ensuring per-record business rules are validated before writing to the database.