gorm-context

Propagate Go context through GORM database operations for timeouts and tracing.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Manage request contexts for GORM to enforce timeouts, cancellation, and traceability across database operations.

Core Features & Use Cases

  • Context propagation through WithContext, tx.Statement.Context, and hooks for consistent cancellation and tracing.
  • Middleware integration with web frameworks (Chi, Gin) to attach context-aware DB sessions to requests.
  • Per-request lifecycle management for queries, updates, and transactions with safe shutdown.

Quick Start

Run a context-aware query by creating a context with timeout and applying it via db.WithContext(ctx) before your first operation.

Frequently Asked Questions about gorm-context

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

FAQPage Schema
How do I propagate context cancellation through GORM database operations?

Yes, you can attach context-aware DB sessions to web frameworks like Chi and Gin. Middleware integration binds the context to each request, ensuring GORM operations automatically respect per-request timeouts and cancellations managed by the framework.

How do I enforce a query timeout for a specific GORM request?

To enforce a GORM query timeout, create a Go context with context.WithTimeout and pass it via db.WithContext(ctx). This applies per-request lifecycle management to queries, updates, and transactions, enabling safe shutdown when the deadline is exceeded.

What is the role of tx.Statement.Context in GORM tracing and cancellation?

tx.Statement.Context carries the request context through GORM hooks and operations, ensuring consistent cancellation and tracing. By using WithContext, you bind the context to the statement, allowing middleware and framework glue code to observe and safely terminate data access.

Does GORM context propagation work for transactions and updates?

Yes, GORM context propagation works for transactions and updates. Applying db.WithContext(ctx) ensures that the per-request lifecycle, including timeouts and cancellation signals, is consistently managed across all database operations for safe and observable data access.

When do I need to manage Go context for GORM in API backends?

You need to manage Go context for GORM in API backends when you require per-request lifecycle control. This allows you to enforce timeouts, propagate cancellations from web frameworks, and apply tracing across database operations to ensure safe, observable data access.