gormmodel

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package gormmodel 定义 Gorm 持久化模型和结构化 JSON 转换工具。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSONRaw

func DecodeJSONRaw[T any](raw JSONRaw) (*T, error)

DecodeJSONRaw 将 JSONRaw 解码成指定类型。

Types

type CommonModel

type CommonModel struct {
	ID        uint       `gorm:"primarykey;comment:主键ID" json:"id"`
	CreatedBy string     `gorm:"size:100;default:'';comment:创建人" json:"createdBy"`
	UpdatedBy string     `gorm:"size:100;default:'';comment:修改人" json:"updatedBy"`
	CreatedAt *time.Time `gorm:"autoCreateTime;<-:create;comment:创建时间" json:"createdAt"`
	UpdatedAt *time.Time `gorm:"autoUpdateTime;comment:更新时间" json:"updatedAt"`
}

CommonModel 是 Gorm 模型共享的审计字段。

type EventModel

type EventModel struct {
	CommonModel
	Type         event.Type `gorm:"size:50;not null;index;comment:事件类型" json:"type"`
	Env          string     `gorm:"size:50;index;comment:环境" json:"env"`
	Workspace    string     `gorm:"size:100;not null;index;comment:工作空间" json:"workspace"`
	TraceID      string     `gorm:"size:100;not null;index;comment:TraceID" json:"traceId"`
	RunID        string     `gorm:"size:100;index;comment:RunID" json:"runId"`
	ParentRunID  string     `gorm:"size:100;index;comment:父RunID" json:"parentRunId"`
	WorkflowID   string     `gorm:"size:100;index;comment:流程ID" json:"workflowId"`
	WorkflowVer  string     `gorm:"size:50;comment:流程版本" json:"workflowVersion"`
	NodeID       string     `gorm:"size:100;index;comment:节点ID" json:"nodeId"`
	NodeName     string     `gorm:"size:100;comment:节点名称" json:"nodeName"`
	ParentNodeID string     `gorm:"size:100;index;comment:父节点ID" json:"parentNodeId"`
	NodePath     string     `gorm:"size:500;comment:节点路径" json:"nodePath"`
	NodeDepth    *int       `gorm:"comment:节点深度" json:"nodeDepth"`
	NodeIndex    *int       `gorm:"comment:节点序号" json:"nodeIndex"`
	NodeType     string     `gorm:"size:100;index;comment:节点类型" json:"nodeType"`
	Status       string     `gorm:"size:50;index;comment:流程状态" json:"status"`
	NodeStatus   string     `gorm:"size:50;index;comment:节点状态" json:"nodeStatus"`
	Error        string     `gorm:"size:1000;comment:错误信息" json:"error"`
	LogLevel     string     `gorm:"size:20;index;comment:日志级别" json:"logLevel"`
	Message      string     `gorm:"size:1000;comment:日志消息" json:"message"`
	StartedAt    *time.Time `gorm:"index;comment:开始时间" json:"startedAt"`
	EndedAt      *time.Time `gorm:"index;comment:结束时间" json:"endedAt"`
	DurationMs   int64      `gorm:"comment:耗时毫秒" json:"durationMs"`
	Attrs        JSONRaw    `gorm:"type:json;comment:扩展属性" json:"attrs"`
	Fields       JSONRaw    `gorm:"type:json;comment:日志字段" json:"fields"`
}

EventModel 是 event.Event 的 Gorm 持久化模型。

func NewEventModel

func NewEventModel(e *event.Event) (*EventModel, error)

NewEventModel 将事件协议对象转换为 Gorm 模型。

func (EventModel) TableName

func (EventModel) TableName() string

func (*EventModel) ToEvent

func (m *EventModel) ToEvent() (*event.Event, error)

type JSONRaw

type JSONRaw json.RawMessage

JSONRaw 保存已经编码好的 JSON。它只负责数据库和 JSON 编码边界,不承载领域校验。

func NewJSONRaw

func NewJSONRaw(value interface{}) (JSONRaw, error)

func (JSONRaw) MarshalJSON

func (j JSONRaw) MarshalJSON() ([]byte, error)

MarshalJSON 把空 JSONRaw 输出为 JSON null,保证 API 响应始终是合法 JSON。

func (*JSONRaw) Scan

func (j *JSONRaw) Scan(value interface{}) error

Scan 会复制数据库驱动返回的 bytes,避免底层缓冲区复用影响模型内容。

func (*JSONRaw) UnmarshalJSON

func (j *JSONRaw) UnmarshalJSON(data []byte) error

UnmarshalJSON 复制输入数据,避免外部 slice 后续修改影响模型。

func (JSONRaw) Value

func (j JSONRaw) Value() (driver.Value, error)

Value 把空值和 JSON null 都写成 SQL NULL,避免数据库里混用空字节和 null 文本。

type NodeCapabilityModel added in v0.2.0

type NodeCapabilityModel struct {
	CommonModel
	Workspace       string    `gorm:"size:100;not null;uniqueIndex:idx_flowlink_node_capability,priority:1;index;comment:工作空间" json:"workspace"`
	Env             string    `gorm:"size:50;not null;uniqueIndex:idx_flowlink_node_capability,priority:2;index;comment:环境" json:"env"`
	Service         string    `gorm:"size:100;not null;uniqueIndex:idx_flowlink_node_capability,priority:3;index;comment:业务服务" json:"service"`
	WorkerVersion   string    `gorm:"size:100;not null;uniqueIndex:idx_flowlink_node_capability,priority:4;comment:Worker版本" json:"workerVersion"`
	InstanceID      string    `gorm:"size:200;comment:Worker实例ID" json:"instanceId"`
	ProtocolVersion string    `gorm:"size:50;not null;comment:能力协议版本" json:"protocolVersion"`
	Nodes           JSONRaw   `gorm:"type:json;not null;comment:节点元数据" json:"nodes"`
	ContainerKinds  JSONRaw   `gorm:"type:json;not null;comment:容器类型" json:"containerKinds"`
	ReportedAt      time.Time `gorm:"not null;index;comment:最近上报时间" json:"reportedAt"`
}

NodeCapabilityModel 保存一个 Worker 版本最近一次上报的节点能力。

func NewNodeCapabilityModel added in v0.2.0

func NewNodeCapabilityModel(snapshot *workercapability.Snapshot) (*NodeCapabilityModel, error)

func (NodeCapabilityModel) TableName added in v0.2.0

func (NodeCapabilityModel) TableName() string

func (*NodeCapabilityModel) ToSnapshot added in v0.2.0

func (m *NodeCapabilityModel) ToSnapshot() (*workercapability.Snapshot, error)

type OperationRecordModel

type OperationRecordModel struct {
	CommonModel
	RecordID    string                   `gorm:"size:100;not null;uniqueIndex;comment:记录ID" json:"id"`
	Kind        repository.OperationKind `gorm:"size:20;not null;index;comment:记录类型" json:"kind"`
	Workspace   string                   `gorm:"size:100;index;comment:工作空间" json:"workspace"`
	ActorID     string                   `gorm:"size:100;index;comment:操作人或提交人ID" json:"actorId"`
	Actor       string                   `gorm:"size:100;comment:操作人或提交人名称" json:"actor"`
	Resource    string                   `gorm:"size:100;index;comment:资源或审批对象类型" json:"resource"`
	ResourceID  string                   `gorm:"size:200;index;comment:资源或审批对象ID" json:"resourceId"`
	Action      repository.AuditAction   `gorm:"size:50;index;comment:操作动作" json:"action"`
	Success     bool                     `gorm:"not null;comment:操作是否成功" json:"success"`
	Message     string                   `gorm:"size:500;comment:操作消息" json:"message"`
	Attrs       JSONRaw                  `gorm:"type:json;comment:提交内容或扩展属性" json:"attrs"`
	Reason      string                   `gorm:"size:1000;comment:提交理由" json:"reason"`
	ApproverIDs JSONRaw                  `gorm:"type:json;comment:候选审批人ID列表" json:"approverIds"`
	Status      admin.ApprovalStatus     `gorm:"size:30;index;comment:审批状态" json:"status"`
	ReviewerID  string                   `gorm:"size:100;index;comment:实际处理人ID" json:"reviewerId"`
	Reviewer    string                   `gorm:"size:100;comment:实际处理人名称" json:"reviewer"`
	Comment     string                   `gorm:"size:1000;comment:处理意见" json:"comment"`
	DecidedAt   *time.Time               `gorm:"comment:处理时间" json:"decidedAt"`
}

OperationRecordModel 用一张表保存普通审计记录和审批记录。

func NewApprovalOperationModel

func NewApprovalOperationModel(request *admin.ApprovalRequest) (*OperationRecordModel, error)

NewApprovalOperationModel 将审批对象转换为统一操作记录。

func NewAuditOperationModel

func NewAuditOperationModel(log *repository.AuditLog) (*OperationRecordModel, error)

NewAuditOperationModel 将普通审计日志转换为统一操作记录。

func (OperationRecordModel) TableName

func (OperationRecordModel) TableName() string

func (*OperationRecordModel) ToApprovalRequest

func (m *OperationRecordModel) ToApprovalRequest() (*admin.ApprovalRequest, error)

ToApprovalRequest 将统一记录转换为审批视图。

func (*OperationRecordModel) ToAuditLog

func (m *OperationRecordModel) ToAuditLog() (*repository.AuditLog, error)

ToAuditLog 将统一记录转换为审计列表视图。

type PermissionModel

type PermissionModel struct {
	CommonModel
	PermissionID string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_permission;comment:权限ID" json:"id"`
	Workspace    string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_permission;index;comment:工作空间" json:"workspace"`
	Name         string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_permission;comment:权限名称" json:"name"`
	Description  string `gorm:"size:255;comment:描述" json:"description"`
}

PermissionModel 是权限的 Gorm 持久化模型。

func NewPermissionModel

func NewPermissionModel(permission *admin.Permission, operator string) *PermissionModel

NewPermissionModel 将权限领域对象转换为 Gorm 模型。

func (PermissionModel) TableName

func (PermissionModel) TableName() string

func (*PermissionModel) ToPermission

func (m *PermissionModel) ToPermission() *admin.Permission

type RoleModel

type RoleModel struct {
	CommonModel
	RoleID      string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_role;comment:角色ID" json:"id"`
	Workspace   string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_role;index;comment:工作空间" json:"workspace"`
	Name        string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_role;comment:角色名称" json:"name"`
	Description string `gorm:"size:255;comment:描述" json:"description"`
}

RoleModel 是角色的 Gorm 持久化模型。

func NewRoleModel

func NewRoleModel(role *admin.Role, operator string) (*RoleModel, error)

NewRoleModel 将角色领域对象转换为 Gorm 模型。

func (RoleModel) TableName

func (RoleModel) TableName() string

func (*RoleModel) ToRole

func (m *RoleModel) ToRole() (*admin.Role, error)

type RolePermissionModel

type RolePermissionModel struct {
	CommonModel
	Workspace    string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_role_permission;index;comment:工作空间" json:"workspace"`
	RoleID       string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_role_permission;index;comment:角色ID" json:"roleId"`
	PermissionID string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_role_permission;index;comment:权限ID" json:"permissionId"`
}

RolePermissionModel 是角色和权限关系的 Gorm 持久化模型。

func (RolePermissionModel) TableName

func (RolePermissionModel) TableName() string

type UserModel

type UserModel struct {
	CommonModel
	UserID       string `gorm:"size:100;not null;uniqueIndex;comment:用户ID" json:"id"`
	Username     string `gorm:"size:100;not null;uniqueIndex;comment:用户名" json:"username"`
	PasswordHash string `gorm:"size:255;not null;default:'';comment:密码Hash" json:"-"`
	IsSuperAdmin bool   `gorm:"not null;default:false;comment:是否超级管理员" json:"isSuperAdmin"`
	Disabled     bool   `gorm:"not null;default:false;index;comment:是否禁用" json:"disabled"`
}

UserModel 是用户的 Gorm 持久化模型。

func NewUserModel

func NewUserModel(user *admin.User, operator string) *UserModel

NewUserModel 将用户领域对象转换为 Gorm 模型。

func (UserModel) TableName

func (UserModel) TableName() string

func (*UserModel) ToUser

func (m *UserModel) ToUser() *admin.User

type WorkflowProgressModel

type WorkflowProgressModel struct {
	CommonModel
	Workspace      string               `gorm:"size:100;not null;uniqueIndex:idx_flowlink_workflow_progress;index;comment:工作空间" json:"workspace"`
	WorkflowID     string               `gorm:"size:100;not null;uniqueIndex:idx_flowlink_workflow_progress;index;comment:流程ID" json:"workflowId"`
	Version        string               `gorm:"size:50;not null;uniqueIndex:idx_flowlink_workflow_progress;comment:版本号" json:"version"`
	CurrentEnv     string               `gorm:"size:50;index;comment:当前已推进环境" json:"currentEnv"`
	PendingEnv     string               `gorm:"size:50;index;comment:审批中或已审批待发布的目标环境" json:"pendingEnv"`
	ApprovalStatus admin.ApprovalStatus `gorm:"size:30;index;comment:审批状态" json:"approvalStatus"`
}

WorkflowProgressModel 是 workflow 版本的环境推进状态。

func NewWorkflowProgressModel

func NewWorkflowProgressModel(progress *admin.WorkflowProgress, operator string) *WorkflowProgressModel

NewWorkflowProgressModel 将 workflow 推进状态转换为 Gorm 模型。

func (WorkflowProgressModel) TableName

func (WorkflowProgressModel) TableName() string

func (*WorkflowProgressModel) ToWorkflowProgress

func (m *WorkflowProgressModel) ToWorkflowProgress() *admin.WorkflowProgress

type WorkflowVersionModel

type WorkflowVersionModel struct {
	CommonModel
	WorkflowID  string              `gorm:"size:100;not null;uniqueIndex:idx_flowlink_version,priority:2;index;comment:流程ID" json:"workflowId"`
	Env         string              `gorm:"size:50;not null;uniqueIndex:idx_flowlink_version,priority:4;index;comment:环境" json:"env"`
	Workspace   string              `gorm:"size:100;not null;uniqueIndex:idx_flowlink_version,priority:1;index;comment:工作空间" json:"workspace"`
	Service     string              `gorm:"size:100;not null;default:default;index;comment:业务服务" json:"service"`
	Version     string              `gorm:"size:50;not null;uniqueIndex:idx_flowlink_version,priority:3;comment:版本号" json:"version"`
	Revision    uint64              `gorm:"not null;default:1;comment:乐观锁版本" json:"revision"`
	Status      core.WorkflowStatus `gorm:"size:50;not null;index;comment:状态" json:"status"`
	Definition  JSONRaw             `gorm:"type:json;not null;comment:workflow完整JSON" json:"definition"`
	PublishedBy string              `gorm:"size:100;comment:发布人" json:"publishedBy"`
	PublishedAt *time.Time          `gorm:"comment:发布时间" json:"publishedAt"`
	Remark      string              `gorm:"size:255;comment:发布备注" json:"remark"`
}

WorkflowVersionModel 统一保存未发布、已发布和已停用的 workflow 版本。

func NewWorkflowVersionModel

func NewWorkflowVersionModel(version *core.WorkflowVersion) (*WorkflowVersionModel, error)

NewWorkflowVersionModel 将 workflow 版本协议对象转换为 Gorm 模型。

func (WorkflowVersionModel) TableName

func (WorkflowVersionModel) TableName() string

func (*WorkflowVersionModel) ToWorkflowVersion

func (m *WorkflowVersionModel) ToWorkflowVersion() (*core.WorkflowVersion, error)

type WorkspaceEnvironmentModel

type WorkspaceEnvironmentModel struct {
	CommonModel
	Workspace        string  `gorm:"size:100;not null;uniqueIndex:idx_flowlink_workspace_env;index;comment:工作空间" json:"workspace"`
	Env              string  `gorm:"size:50;not null;uniqueIndex:idx_flowlink_workspace_env;index;comment:环境标识" json:"env"`
	Name             string  `gorm:"size:100;not null;comment:环境名称" json:"name"`
	Description      string  `gorm:"size:255;comment:描述" json:"description"`
	Order            int     `gorm:"column:publish_order;not null;index;comment:发布顺序" json:"order"`
	Enabled          bool    `gorm:"not null;default:true;index;comment:是否启用" json:"enabled"`
	ApprovalRequired bool    `gorm:"not null;default:false;comment:是否需要审批" json:"approvalRequired"`
	ApproverIDs      JSONRaw `gorm:"type:json;comment:审批人ID列表" json:"approverIds"`
}

WorkspaceEnvironmentModel 是工作空间环境的 Gorm 持久化模型。

func NewWorkspaceEnvironmentModel

func NewWorkspaceEnvironmentModel(env *admin.WorkspaceEnvironment, operator string) (*WorkspaceEnvironmentModel, error)

NewWorkspaceEnvironmentModel 将工作空间环境领域对象转换为 Gorm 模型。

func (WorkspaceEnvironmentModel) TableName

func (WorkspaceEnvironmentModel) TableName() string

func (*WorkspaceEnvironmentModel) ToWorkspaceEnvironment

func (m *WorkspaceEnvironmentModel) ToWorkspaceEnvironment() (*admin.WorkspaceEnvironment, error)

type WorkspaceMemberModel

type WorkspaceMemberModel struct {
	CommonModel
	Workspace string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_member;index;comment:工作空间" json:"workspace"`
	UserID    string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_member;index;comment:用户ID" json:"userId"`
}

WorkspaceMemberModel 是工作空间成员的 Gorm 持久化模型。

func NewWorkspaceMemberModel

func NewWorkspaceMemberModel(member *admin.WorkspaceMember, operator string) (*WorkspaceMemberModel, error)

NewWorkspaceMemberModel 将工作空间成员领域对象转换为 Gorm 模型。

func (WorkspaceMemberModel) TableName

func (WorkspaceMemberModel) TableName() string

func (*WorkspaceMemberModel) ToWorkspaceMember

func (m *WorkspaceMemberModel) ToWorkspaceMember() (*admin.WorkspaceMember, error)

type WorkspaceMemberRoleModel

type WorkspaceMemberRoleModel struct {
	CommonModel
	Workspace string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_member_role;index;comment:工作空间" json:"workspace"`
	UserID    string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_member_role;index;comment:用户ID" json:"userId"`
	RoleID    string `gorm:"size:100;not null;uniqueIndex:idx_flowlink_member_role;index;comment:角色ID" json:"roleId"`
}

WorkspaceMemberRoleModel 是工作空间成员和角色的关联模型。

func (WorkspaceMemberRoleModel) TableName

func (WorkspaceMemberRoleModel) TableName() string

type WorkspaceModel

type WorkspaceModel struct {
	CommonModel
	WorkspaceID string `gorm:"size:100;not null;uniqueIndex;comment:工作空间ID" json:"id"`
	Name        string `gorm:"size:100;not null;index;comment:工作空间名称" json:"name"`
	Tag         string `gorm:"size:100;not null;uniqueIndex;comment:工作空间标签" json:"tag"`
	Description string `gorm:"size:255;comment:描述" json:"description"`
	OwnerUserID string `gorm:"size:100;not null;index;comment:归属用户" json:"ownerUserId"`
}

WorkspaceModel 是工作空间的 Gorm 持久化模型。

func NewWorkspaceModel

func NewWorkspaceModel(workspace *admin.Workspace, operator string) *WorkspaceModel

NewWorkspaceModel 将工作空间领域对象转换为 Gorm 模型。

func (WorkspaceModel) TableName

func (WorkspaceModel) TableName() string

func (*WorkspaceModel) ToWorkspace

func (m *WorkspaceModel) ToWorkspace() *admin.Workspace

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL