gorm-scopes

Encapsulate GORM query logic into reusable scope functions.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Reusable query conditions in GORM often get duplicated across handlers and models, making maintenance error-prone and inconsistent; this Skill consolidates query logic into composable scope functions to enforce consistent filtering, pagination, and routing across an application.

Core Features & Use Cases

  • Reusable Scopes: Define functions that accept and return *gorm.DB to encapsulate common WHERE, JOIN, and ORDER conditions for reuse.
  • Parameterized Scopes: Create closure-based scopes to accept dynamic values for statuses, amounts, tenant IDs, and date ranges.
  • Pagination & Composition: Provide a single pagination pattern and combine multiple scopes to build complex queries without duplicating logic.
  • Dynamic Table Routing & Multi-Tenancy: Swap table names or schemas at runtime for sharding or multi-org data separation.
  • Update/Delete Safety: Apply row-level constraints as scopes to safely restrict update and delete operations by organization or tenant.

Quick Start

Create parameterized GORM scopes for pagination and tenant isolation and show example usage combining them with db.Scopes to query orders.

Frequently Asked Questions about gorm-scopes

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

FAQPage Schema
How do I build reusable GORM query filters to avoid duplicating WHERE conditions across handlers?

You can build reusable GORM query filters by encapsulating conditions into scope functions of type func(*gorm.DB) *gorm.DB, then combining them with db.Scopes. This approach ensures consistent filtering across handlers without duplicating query logic.

What is the best way to implement multi-tenant row isolation in a GORM application?

Multi-tenant row isolation in a GORM application is implemented using parameterized scope functions that filter queries by tenant ID. Applying these scopes to update and delete operations also enforces row-level constraints for safe data separation across organizations.

How do I set up parameterized pagination in GORM using scopes?

Parameterized pagination in GORM is set up by creating closure-based scope functions that accept dynamic values for Offset and Limit. You combine these pagination scopes with other filtering scopes using db.Scopes to build complex queries without duplicating logic.

Can I use GORM scopes for dynamic table routing and database sharding?

Yes, GORM scopes can be used for dynamic table routing and sharding by swapping table names or schemas at runtime within the scope function. This allows the application to route queries to specific shards or separate multi-org data dynamically.

How do I safely constrain update and delete operations in GORM to prevent accidental data loss?

To safely constrain update and delete operations in GORM, apply row-level constraints as scope functions before executing the mutation. This restricts the operation scope by organization or tenant and requires explicit error propagation for the mutating operation.

Does GORM support composable query logic for filtering by dynamic date ranges and statuses?

GORM supports composable query logic by using closure-based parameterized scopes that accept dynamic values for date ranges and statuses. Multiple scopes are chained together with db.Scopes to build complex, reusable queries from simple filtering components.