gorm-custom-data-types

Implement custom GORM data types mapping Go values to database columns.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Many Go types cannot be directly mapped to database column representations, making it difficult to store structured, encrypted, or dialect-specific values reliably; this Skill provides patterns and interfaces to convert complex Go values into database-compatible formats for GORM models.

Core Features & Use Cases

  • Scanner and Valuer Integration: Implement sql.Scanner and driver.Valuer to control how values are read from and written to the database.
  • Dialect-Specific Types: Define GormDBDataType to return database-specific column types for migrations across MySQL, Postgres, SQLite, and others.
  • SQL Expression Support: Provide GormValue/GormValuer implementations to persist values using SQL expressions or database functions, useful for spatial types or context-aware encryption.
  • Use Case: Store JSON objects as JSONB on Postgres, JSON on MySQL, or TEXT on SQLite while preserving queryability and migration correctness.

Quick Start

Create a GORM model field that implements Scanner and Valuer, implement GormDBDataType for your target dialect, and run AutoMigrate to verify the generated column type.

Frequently Asked Questions about gorm-custom-data-types

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

FAQPage Schema
How do I store complex Go types as database fields using GORM?â–¼

To store complex Go types as database fields using GORM, implement custom data types that satisfy the sql.Scanner and driver.Valuer interfaces, controlling how values are read from and written to the database during CRUD operations.

How does GORM map custom types to different database dialects during AutoMigrate?â–¼

GORM maps custom types to different database dialects by implementing the GormDBDataType interface, which returns database-specific column types for migrations across MySQL, Postgres, and SQLite, ensuring schema correctness.

What is the best way to save a Go struct as JSONB in Postgres with GORM?â–¼

The best way to save a Go struct as JSONB in Postgres with GORM is to implement a custom data type using sql.Scanner and driver.Valuer, optionally defining GormDataType to map the field to JSONB while preserving queryability.

Can I use SQL expressions to persist encrypted or spatial data in GORM models?â–¼

Yes, you can use SQL expressions to persist encrypted or spatial data in GORM models by providing GormValue or GormValuer implementations, allowing values to be saved using database functions or context-aware expressions.

When do I need to implement Scanner and Valuer interfaces for GORM?â–¼

You need to implement Scanner and Valuer interfaces for GORM when your Go types cannot be directly mapped to database columns, requiring custom conversion logic to reliably store structured, encrypted, or dialect-specific values.