Configuration
設定は YAML ファイルを通じて行われます(デフォルト: ./bobgen.yaml
)。
基本設定
# データベース接続設定
dsn: "postgres://user:password@localhost/database?sslmode=disable"
# 出力先ディレクトリ
output: "./models"
# パッケージ名
package: "models"
# 含めるテーブル
only:
- "users"
- "posts"
- "comments"
# 除外するテーブル
except:
- "migrations"
- "schema_migrations"
# 生成するタグ
tags:
- "json"
- "db"
- "validate"
Aliases
テーブルやカラムの名前を変更するためのエイリアス設定:
aliases:
tables:
user_profiles: "UserProfile"
blog_posts: "BlogPost"
columns:
id: "ID"
user_id: "UserID"
created_at: "CreatedAt"
updated_at: "UpdatedAt"
Types
カラムの型を Go の型にマッピング:
types:
match:
- name: "user_id"
type: "int64"
- name: "email"
type: "string"
replace:
- match: "json"
type: "[]byte"
- match: "uuid"
type: "string"
Replacements
文字列の置換ルール:
replacements:
- match: "Id"
replace: "ID"
- match: "Url"
replace: "URL"
- match: "Json"
replace: "JSON"
Constraints
制約に基づく設定:
constraints:
- name: "unique_email"
type: "unique"
columns: ["email"]
- name: "fk_user_id"
type: "foreign_key"
columns: ["user_id"]
references:
table: "users"
columns: ["id"]
Relationships
リレーションシップの設定:
relationships:
- name: "user_posts"
local_table: "users"
foreign_table: "posts"
local_columns: ["id"]
foreign_columns: ["user_id"]
type: "has_many"
- name: "post_author"
local_table: "posts"
foreign_table: "users"
local_columns: ["user_id"]
foreign_columns: ["id"]
type: "belongs_to"
Inflections
単数形・複数形の活用ルール:
inflections:
singular:
- irregular: "person"
replacement: "people"
- irregular: "child"
replacement: "children"
plural:
- irregular: "people"
replacement: "person"
- irregular: "children"
replacement: "child"
完全な設定例
# データベース接続
dsn: "postgres://user:password@localhost/myapp?sslmode=disable"
# 出力設定
output: "./internal/models"
package: "models"
# テーブル設定
only:
- "users"
- "posts"
- "comments"
- "tags"
# タグ設定
tags:
- "json"
- "db"
- "validate"
# エイリアス設定
aliases:
tables:
user_profiles: "UserProfile"
blog_posts: "BlogPost"
columns:
id: "ID"
user_id: "UserID"
created_at: "CreatedAt"
updated_at: "UpdatedAt"
# 型設定
types:
replace:
- match: "uuid"
type: "string"
- match: "json"
type: "[]byte"
- match: "jsonb"
type: "[]byte"
# 文字列置換
replacements:
- match: "Id"
replace: "ID"
- match: "Url"
replace: "URL"
- match: "Api"
replace: "API"
# リレーションシップ
relationships:
- name: "user_posts"
local_table: "users"
foreign_table: "posts"
local_columns: ["id"]
foreign_columns: ["user_id"]
type: "has_many"
- name: "post_comments"
local_table: "posts"
foreign_table: "comments"
local_columns: ["id"]
foreign_columns: ["post_id"]
type: "has_many"
# 活用ルール
inflections:
singular:
- irregular: "person"
replacement: "people"
plural:
- irregular: "people"
replacement: "person"
設定項目の詳細
項目 | 説明 | デフォルト |
---|---|---|
dsn | データベース接続文字列 | 必須 |
output | 出力先ディレクトリ | "./models" |
package | パッケージ名 | "models" |
only | 含めるテーブル | すべて |
except | 除外するテーブル | なし |
tags | 生成するタグ | ["json", "db"] |
この設定により、Bob は指定されたデータベーススキーマから、カスタマイズされた Go コードを生成できます。