一键导入
grant-generate-model
Generate a new Grant ORM model file with proper structure, column definitions, validations, associations, and callbacks.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Generate a new Grant ORM model file with proper structure, column definitions, validations, associations, and callbacks.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Grant ORM advanced features including enum attributes, dirty tracking, serialized columns, value objects, horizontal sharding, and async operations.
Grant ORM associations including belongs_to, has_one, has_many, has_many through, polymorphic, dependent options, counter_cache, eager loading, and nested attributes.
Grant ORM lifecycle callbacks, transaction blocks, nested transactions with savepoints, isolation levels, optimistic and pessimistic locking.
Create, read, update, and delete operations in Grant ORM including bulk operations, batch processing, upserts, and convenience methods.
Defining Grant ORM models with columns, types, primary keys, timestamps, connections, converters, and serialization.
Grant ORM query builder API including where clauses, operators, WhereChain, OR/NOT groups, scopes, aggregations, raw SQL, and Enumerable integration.
| name | grant-generate-model |
| description | Generate a new Grant ORM model file with proper structure, column definitions, validations, associations, and callbacks. |
| allowed-tools | Bash, Read, Write, Glob |
| user-invocable | true |
| argument-hint | <ModelName> [column:type ...] [--with-validations] [--with-associations] |
This skill generates a new Grant ORM model file with proper structure, following the conventions established in the project.
Extract from the user's request:
Product, UserProfile)name:String, price:Float64, active:Bool)--with-validations, --with-associations, or specific association/validation requestsBefore generating, scan the project to understand existing conventions:
Look in src/ for existing model files to determine:
pg, sqlite, mysql)timestamps consistentlyCheck spec/ for test file conventions.
Create the model file at src/models/<model_name_snake_case>.cr (or the location matching existing models).
class ModelName < Grant::Base
connection <connection_name>
table <table_name_pluralized>
column id : Int64, primary: true
# ... user-specified columns ...
timestamps
end
Add appropriate validators based on column types:
String columns: validate_not_blank for required fieldsString email fields: validates_emailvalidates_numericality_of with appropriate constraintsvalidate_uniquenessAdd association macros based on the user's request:
belongs_to with foreign key columnshas_many with appropriate class nameshas_one for single-record associationshas_many :through for many-to-manyUse these Crystal-to-Grant type mappings:
| User Input | Crystal Type | Notes |
|---|---|---|
string, text | String | VARCHAR/TEXT |
string? | String? | Nullable |
int, integer | Int32 | INTEGER |
bigint, int64 | Int64 | BIGINT |
float, decimal | Float64 | DOUBLE |
bool, boolean | Bool | BOOLEAN |
time, datetime | Time? | TIMESTAMP |
json | JSON::Any? | JSON/JSONB/TEXT |
uuid | UUID | UUID type |
bytes, binary | Bytes? | BLOB |
After generating, provide a brief explanation of:
Request: Product name:String price:Float64 active:Bool
class Product < Grant::Base
connection sqlite
table products
column id : Int64, primary: true
column name : String
column price : Float64
column active : Bool = true
timestamps
end
Request: User email:String username:String age:Int32 --with-validations
class User < Grant::Base
connection sqlite
table users
column id : Int64, primary: true
column email : String
column username : String
column age : Int32
timestamps
# Validations
validate_not_blank :email
validates_email :email
validate_uniqueness :email
validate_not_blank :username
validates_length_of :username, minimum: 3, maximum: 30
validate_uniqueness :username
validates_numericality_of :age, greater_than: 0
end
Request: Post title:String content:String published:Bool --with-associations belongs_to:User has_many:Comment
class Post < Grant::Base
connection sqlite
table posts
belongs_to :user
has_many :comments, dependent: :destroy
column id : Int64, primary: true
column title : String
column content : String
column published : Bool = false
column user_id : Int64
timestamps
# Validations
validate_not_blank :title
validate_not_blank :content
# Scopes
scope :published, -> { where(published: true) }
scope :drafts, -> { where(published: false) }
end
Request: Article title:String status:enum(Draft,Published,Archived)
class Article < Grant::Base
connection sqlite
table articles
column id : Int64, primary: true
column title : String
timestamps
enum Status
Draft
Published
Archived
end
enum_attribute status : Status = :draft
# Validations
validate_not_blank :title
# Scopes
scope :recent, -> { order(created_at: :desc) }
end
timestamps unless the user specifically excludes themcolumn user_id : Int64 (or appropriate FK column) when adding belongs_to associationshas_many :through, generate the join model as wellfalse unless the user specifies otherwise