Documentation ¶
Overview ¶
Package model provides ...
Index ¶
- Constants
- Variables
- func GetTableName[M types.Model]() string
- func Register[M types.Model](records ...M)
- func RegisterRoutes[M types.Model](path string, verbs ...consts.HTTPVerb)deprecated
- func RegisterTo[M types.Model](dbname string, records ...M)
- func SetID(m types.Model, id ...string)
- type BIOS
- type Base
- func (*Base) CreateAfter() error
- func (*Base) CreateBefore() error
- func (*Base) DeleteAfter() error
- func (*Base) DeleteBefore() error
- func (b *Base) Excludes() map[string][]any
- func (b *Base) Expands() []string
- func (*Base) GetAfter() error
- func (*Base) GetBefore() error
- func (b *Base) GetCreatedAt() time.Time
- func (b *Base) GetCreatedBy() string
- func (b *Base) GetID() string
- func (b *Base) GetTableName() string
- func (b *Base) GetUpdatedAt() time.Time
- func (b *Base) GetUpdatedBy() string
- func (*Base) ListAfter() error
- func (*Base) ListBefore() error
- func (b *Base) MarshalLogObject(enc zapcore.ObjectEncoder) error
- func (b *Base) SetCreatedAt(t time.Time)
- func (b *Base) SetCreatedBy(s string)
- func (b *Base) SetID(id ...string)
- func (b *Base) SetUpdatedAt(t time.Time)
- func (b *Base) SetUpdatedBy(s string)
- func (*Base) UpdateAfter() error
- func (*Base) UpdateBefore() error
- func (*Base) UpdatePartialAfter() error
- func (*Base) UpdatePartialBefore() error
- type Board
- type CPU
- type CasbinRule
- type Chassis
- type Fixed
- type GormScanner
- type GormStrings
- type GormTime
- type Kernel
- type LoginLog
- type LoginStatus
- type Memory
- type Menu
- type MenuPlatform
- type NetworkDevice
- type NetworkDevices
- type Node
- type OS
- type OperationLog
- type OperationType
- type Product
- type Record
- type StorageDevice
- type StorageDevices
- type SysInfo
- type TableColumn
- type User
- type UserAgent
- type UserInfo
Constants ¶
const ( LoginStatusSuccess = "success" LoginStatusFailure = "failure" )
const ( MenuPlatformAll = "all" MenuPlatformWeb = "web" MenuPlatformMobile = "mobile" MenuPlatformDesktop = "desktop" )
Variables ¶
var ( // Records is the table records must be pr-eexists before any database curd, // its register by register function. // The underlying type of map value must be model pointer to structure, eg: *model.User // // Records is the table records that should created automatically when app bootstraping. Records []*Record = make([]*Record, 0) // Tables is the database table that should created automatically when app bootstraping. Tables []types.Model TablesWithDB []struct { Table types.Model DBName string } // Routes is a map slice that element is map[string][]Verb // The map key is the route path, eg: '/asset' or 'asset' // or '/api/asset' (the prefiex /api will be remove automatically). // The map value is the Verb slice. // - VerbCreate is equivalent to http method "POST". // - VerbDelete is equivalent to http method "DELETE". // - VerbUpdate is equivalent to http method "PUT". // - VerbUpdatePartial is equivalent to http method "PATCH". // - VerbList is equivalent to http method "GET /xxx". // - VerbGET is equivalent to http method "GET /xxx/:id". // - VerbExport is equivalent to http method "GET" but specifially used to export resources. // - VerbImport is equivalent to http method "POST" but specifically used to import resources. Routes []route = make([]route, 0) )
var ( RootId = "root" RootName = "root" UnknownId = "unknown" UnknownName = "未知" NoneId = "none" NoneName = "无" KeyName = "name" KeyId = "id" )
var ErrMobileLength = errors.New("mobile number length must be 11")
var MenuRoot = &Menu{ParentId: RootId, Base: Base{ID: RootId}}
Functions ¶
func GetTableName ¶ added in v0.0.66
func Register ¶
Register associates the model with database table and will created automatically. If records provided, they will be inserted when application bootstrapping.
Parameters:
- records: Optional initial records to be seeded into the table. Can be single or multiple records.
Examples:
// Create table 'users' only Register[*model.User]() // Create table 'users' and insert one record Register[*model.User](&model.User{ID: 1, Name: "admin"}) // Create table 'users' and insert a single user record Register[*model.User](user) // Create table 'users' and insert multiple records Register[*model.User](users...) // where users is []*model.User
NOTE:
- Always call this function in init().
- Ensure the model pacakge is imported in main.go. The init() function will only executed if the file is imported directly or indirectly by main.go.
func RegisterRoutes
deprecated
RegisterRoutes register one route path with multiple api verbs. call this function multiple to register multiple route path. If route path is same, using latest register route path.
Deprecated: use router.Register() instead. This function is a no-op.
func RegisterTo ¶
RegisterTo works identically to Register(), but registers the model on the specified database instance. more details see: Register().
Types ¶
type Base ¶
type Base struct { ID string `json:"id" gorm:"primaryKey" schema:"id" url:"-"` CreatedBy string `json:"created_by,omitempty" gorm:"index" schema:"created_by" url:"-"` UpdatedBy string `json:"updated_by,omitempty" gorm:"index" schema:"updated_by" url:"-"` CreatedAt *time.Time `json:"created_at,omitempty" gorm:"index" schema:"-" url:"-"` UpdatedAt *time.Time `json:"updated_at,omitempty" gorm:"index" schema:"-" url:"-"` Remark *string `json:"remark,omitempty" gorm:"size:10240" schema:"-" url:"-"` // 如果需要支持 PATCH 更新,则必须是指针类型 Order *uint `json:"order,omitempty" schema:"-" url:"-"` // Query parameter Page uint `json:"-" gorm:"-" schema:"page" url:"page,omitempty"` // Query parameter, eg: "page=2" Size uint `json:"-" gorm:"-" schema:"size" url:"size,omitempty"` // Query parameter, eg: "size=10" Expand *string `json:"-" gorm:"-" schema:"_expand" url:"_expand,omitempty"` // Query parameter, eg: "_expand=children,parent". Depth *uint `json:"-" gorm:"-" schema:"_depth" url:"_depth,omitempty"` // Query parameter, eg: "_depth=3". Fuzzy *bool `json:"-" gorm:"-" schema:"_fuzzy" url:"_fuzzy,omitempty"` // Query parameter, eg: "_fuzzy=true" SortBy string `json:"-" gorm:"-" schema:"_sortby" url:"_sortby,omitempty"` // Query parameter, eg: "_sortby=name" NoCache bool `json:"-" gorm:"-" schema:"_nocache" url:"_nocache,omitempty"` // Query parameter: eg: "_nocache=false" ColumnName string `json:"-" gorm:"-" schema:"_column_name" url:"_column_name,omitempty"` // Query parameter: eg: "_column_name=created_at" StartTime string `json:"-" gorm:"-" schema:"_start_time" url:"_start_time,omitempty"` // Query parameter: eg: "_start_time=2024-04-29+23:59:59" EndTime string `json:"-" gorm:"-" schema:"_end_time" url:"_end_time,omitempty"` // Query parameter: eg: "_end_time=2024-04-29+23:59:59" Or *bool `json:"-" gorm:"-" schema:"_or" url:"_or,omitempty"` // query parameter: eg: "_or=true" Index string `json:"-" gorm:"-" schema:"_index" url:"_index,omitempty"` // Query parameter: eg: "_index=name" Select string `json:"-" gorm:"-" schema:"_select" url:"_select,omitempty"` // Query parameter: eg: "_select=field1,field2" gorm.Model `json:"-" schema:"-" url:"-"` }
Base implement types.Model interface. Each model must be expands the Base structure. You can implements your custom method to overwrite the defaults methods.
Usually, there are some gorm tags that may be of interest to you. gorm:"unique" gorm:"foreignKey:ParentID" gorm:"foreignKey:ParentID,references:ID"
func (*Base) CreateAfter ¶
func (*Base) CreateBefore ¶
These methods implement types.Hooker interface. model should create custom hook to overwrite default hooks.
func (*Base) DeleteAfter ¶
func (*Base) DeleteBefore ¶
func (*Base) GetCreatedAt ¶
func (*Base) GetCreatedBy ¶
func (*Base) GetTableName ¶
func (*Base) GetUpdatedAt ¶
func (*Base) GetUpdatedBy ¶
func (*Base) ListBefore ¶
func (*Base) MarshalLogObject ¶
func (b *Base) MarshalLogObject(enc zapcore.ObjectEncoder) error
func (*Base) SetCreatedAt ¶
func (*Base) SetCreatedBy ¶
func (*Base) SetUpdatedAt ¶
func (*Base) SetUpdatedBy ¶
func (*Base) UpdateAfter ¶
func (*Base) UpdateBefore ¶
func (*Base) UpdatePartialAfter ¶
func (*Base) UpdatePartialBefore ¶
type CPU ¶ added in v0.0.65
type CPU struct { Vendor string `json:"vendor,omitempty"` Model string `json:"model,omitempty"` Speed uint `json:"speed,omitempty"` // CPU clock rate in MHz Cache uint `json:"cache,omitempty"` // CPU cache size in KB Cpus uint `json:"cpus,omitempty"` // number of physical CPUs Cores uint `json:"cores,omitempty"` // number of physical CPU cores Threads uint `json:"threads,omitempty"` // number of logical (HT) CPU cores }
type CasbinRule ¶ added in v0.0.66
type CasbinRule struct { // ID uint64 `json:"id" gorm:"primaryKey;autoIncrement"` ID uint64 `json:"id" gorm:"primaryKey"` Ptype string `json:"ptype" gorm:"size:100" schema:"ptype"` V0 string `json:"v0,omitempty" gorm:"size:100" schema:"v0"` V1 string `json:"v1,omitempty" gorm:"size:100" schema:"v1"` V2 string `json:"v2,omitempty" gorm:"size:100" schema:"v2"` V3 string `json:"v3,omitempty" gorm:"size:100" schema:"v3"` V4 string `json:"v4,omitempty" gorm:"size:100" schema:"v4"` V5 string `json:"v5,omitempty" gorm:"size:100" schema:"v5"` // 当 Ptype 为 g 时, V0 就是用户名, V1 就是角色名 // 当 Ptype 为 p 时, V0 就是角色名, V1 就是资源, V2 就是操作 Username string `json:"username" schema:"username"` RoleName string `json:"role_name" schema:"role_name"` MenuName string `json:"menu_name" schema:"menu_name"` IsMenu bool `json:"is_menu" schema:"is_menu"` // 这个 casbin_rule 是否是根据菜单创建出来的. Description string `json:"description"` Base }
CasbinRule RBAC 包中会通过 gormadapter.NewAdapterByDBWithCustomTable(database.DB, &model.CasbinRule{}) 或 gormadapter.NewAdapterByDB(database.DB) 来创建; NOTE: ID 类型必须是整型
func (*CasbinRule) CreateBefore ¶ added in v0.0.66
func (cr *CasbinRule) CreateBefore() error
func (CasbinRule) GetTableName ¶ added in v0.0.66
func (cr CasbinRule) GetTableName() string
func (*CasbinRule) UpdateBefore ¶ added in v0.0.66
func (cr *CasbinRule) UpdateBefore() error
type GormScanner ¶
type GormScanner struct {
Object any
}
func GormScannerWrapper ¶
func GormScannerWrapper(object any) *GormScanner
GormScannerWrapper converts object to GormScanner that can be used in GORM. WARN: you must pass pointer to object.
func (*GormScanner) Scan ¶
func (g *GormScanner) Scan(value any) (err error)
type GormStrings ¶
type GormStrings []string
func (*GormStrings) Scan ¶
func (gs *GormStrings) Scan(value any) error
type LoginStatus ¶ added in v0.0.42
type LoginStatus string
type Menu ¶ added in v0.0.66
type Menu struct { Api string `json:"api,omitempty" schema:"api"` // 后端路由, 如果为空则使用 "/api" + Path Path string `json:"path" schema:"path"` // path should not add `omitempty` tag, empty value means default router in react route6.x. Element string `json:"element,omitempty" schema:"element"` Label string `json:"label,omitempty" schema:"label"` Icon string `json:"icon,omitempty" schema:"icon"` Visiable *bool `json:"visiable" schema:"visiable"` // 默认路由 Default string `json:"default,omitempty" schema:"default"` // 自路由中的默认路由, 如果有 Children, Default 才可能存在 Status *uint `json:"status" gorm:"type:smallint;default:1;comment:status(0: disabled, 1: enabled)" schema:"status"` // 该路由是否启用 ParentId string `json:"parent_id,omitempty" gorm:"size:191" schema:"parent_id"` Children []*Menu `json:"children,omitempty" gorm:"foreignKey:ParentId"` // 子路由 Parent *Menu `json:"parent,omitempty" gorm:"foreignKey:ParentId;references:ID"` // 父路由 // the empty value of `Platform` means all. Platform MenuPlatform `json:"platform" schema:"platform"` DomainPattern string `json:"domain_pattern" schema:"domain_pattern"` Base }
Menu: 菜单 TODO: 加一个 api 用来指定后端路由,如果为空则使用 Path.
func (*Menu) CreateBefore ¶ added in v0.0.66
func (*Menu) MarshalLogObject ¶ added in v0.0.66
func (m *Menu) MarshalLogObject(enc zapcore.ObjectEncoder) error
func (*Menu) UpdateBefore ¶ added in v0.0.66
type MenuPlatform ¶ added in v0.0.66
type MenuPlatform string
type NetworkDevice ¶ added in v0.0.65
type NetworkDevices ¶ added in v0.0.65
type NetworkDevices []NetworkDevice
func (*NetworkDevices) Scan ¶ added in v0.0.65
func (nd *NetworkDevices) Scan(value any) error
type OperationLog ¶
type OperationLog struct { User string `json:"user,omitempty" schema:"user"` // 操作者, 本地账号该字段为空,例如 root IP string `json:"ip,omitempty" schema:"ip"` // 操作者的 ip Op OperationType `json:"op,omitempty" schema:"op"` // 动作: 增删改查 Table string `json:"table,omitempty" schema:"table"` // 操作了哪张表 Model string `json:"model,omitempty" schema:"model"` RecordId string `json:"record_id,omitempty" schema:"record_id"` // 表记录的 id RecordName string `json:"record_name,omitempty" schema:"record_name"` // 表记录的 name Record string `json:"record,omitempty" schema:"record"` // 记录全部内容 OldRecord string `json:"old_record,omitempty"` // 更新前的内容 NewRecord string `json:"new_record,omitempty"` // 更新后的内容 Method string `json:"method,omitempty" schema:"method"` URI string `json:"uri,omitempty" schema:"uri"` // request uri UserAgent string `json:"user_agent,omitempty" schema:"user_agent"` RequestId string `json:"request_id,omitempty" schema:"request_id"` Base }
type OperationType ¶
type OperationType string
const ( OperationTypeCreate OperationType = "create" OperationTypeDelete OperationType = "delete" OperationTypeUpdate OperationType = "update" OperationTypeUpdatePartial OperationType = "update_partial" OperationTypeList OperationType = "list" OperationTypeGet OperationType = "get" OperationTypeExport OperationType = "export" OperationTypeImport OperationType = "import" )
type StorageDevice ¶ added in v0.0.65
type StorageDevices ¶ added in v0.0.65
type StorageDevices []StorageDevice
func (*StorageDevices) Scan ¶ added in v0.0.65
func (sd *StorageDevices) Scan(value any) error
type SysInfo ¶ added in v0.0.65
type SysInfo struct { Node Node `json:"node" gorm:"embedded;embeddedPrefix:node_"` OS OS `json:"os" gorm:"embedded;embeddedPrefix:os_"` Kernel Kernel `json:"kernel" gorm:"embedded;embeddedPrefix:kernel_"` Product Product `json:"product" gorm:"embedded;embeddedPrefix:product_"` Board Board `json:"board" gorm:"embedded;embeddedPrefix:board_"` Chassis Chassis `json:"chassis" gorm:"embedded;embeddedPrefix:chassis_"` BIOS BIOS `json:"bios" gorm:"embedded;embeddedPrefix:bios_"` CPU CPU `json:"cpu" gorm:"embedded;embeddedPrefix:cpu_"` Memory Memory `json:"memory" gorm:"embedded;embeddedPrefix:memory_"` Storages StorageDevices `json:"storages,omitempty"` Networks NetworkDevices `json:"networks,omitempty"` Base }
func (*SysInfo) CreateBefore ¶ added in v0.0.65
func (*SysInfo) UpdateBefore ¶ added in v0.0.65
type TableColumn ¶ added in v0.0.66
type TableColumn struct { UserId string `json:"user_id,omitempty" schema:"user_id"` // 属于哪一个用户的 TableName string `json:"table_name,omitempty" schema:"table_name"` // 属于哪一张表的 Name string `json:"name,omitempty" schema:"name"` // 列名 Key string `json:"key,omitempty" schema:"key"` // 列名对应的id Width *uint `json:"width,omitempty"` // 列宽度 Sequence *uint `json:"sequence,omitempty"` // 列顺序 Visiable *bool `json:"visiable,omitempty"` // 是否显示 Fixed *Fixed `json:"fixed,omitempty"` // 固定在哪里 left,right, 必须加上 omitempty Base }
TableColumn 表格的列
type User ¶
type User struct { Username string `json:"username,omitempty" gorm:"unique" binding:"required"` Name string `json:"name,omitempty"` // 等同于 username EnName string `json:"en_name,omitempty"` Password string `json:"password,omitempty"` RePassword string `json:"re_password,omitempty" gorm:"-"` NewPassword string `json:"new_password,omitempty" gorm:"-"` Email string `json:"email,omitempty" gorm:"unique"` Avatar string `json:"avatar,omitempty"` AvatarUrl string `json:"avatar_url,omitempty"` // 用户头像 AvatarThumb string `json:"avatar_thumb,omitempty"` // 用户头像 72x72 AvatarMiddle string `json:"avatar_middle,omitempty"` // 用户头像 240x240 AvatarBig string `json:"avatar_big,omitempty"` // 用户头像 640x640 Mobile string `json:"mobile,omitempty"` Nickname string `json:"nickname,omitempty"` Introduction string `json:"introduction,omitempty"` Status uint `json:"status,omitempty" gorm:"type:smallint;default:1;comment:status(0: disabled, 1: enabled)"` // State 员工状态 // 1 在职 // 2 离职 // 3 试用期 // 4 实习生 RoleId string `json:"role_id,omitempty"` DepartmentId string `json:"department_id,omitempty"` LastLogin GormTime `json:"last_login,omitempty"` LockExpire int64 `json:"lock_expire,omitempty"` NumWrong int `json:"num_wrong,omitempty" gorm:"comment:the number of input password wrong"` Token string `json:"token,omitempty" gorm:"-"` AccessToken string `json:"access_token,omitempty" gorm:"-"` RefreshToken string `json:"refresh_token,omitempty" gorm:"-"` SessionId string `json:"session_id,omitempty" gorm:"-"` TokenExpiration GormTime `json:"token_expiration,omitempty"` Base }
func (*User) CreateBefore ¶
CreateBefore check whether user mobile is valid.
type UserInfo ¶
type UserInfo struct { AccessToken string `json:"access_token,omitempty"` // user_access_token,用于获取用户资源 TokenType string `json:"token_type,omitempty"` // token 类型 ExpiresIn int `json:"expires_in,omitempty"` // `access_token`的有效期,单位: 秒 Name string `json:"name,omitempty"` // 用户姓名 EnName string `json:"en_name,omitempty"` // 用户英文名称 AvatarUrl string `json:"avatar_url,omitempty"` // 用户头像 AvatarThumb string `json:"avatar_thumb,omitempty"` // 用户头像 72x72 AvatarMiddle string `json:"avatar_middle,omitempty"` // 用户头像 240x240 AvatarBig string `json:"avatar_big,omitempty"` // 用户头像 640x640 OpenId string `json:"open_id,omitempty"` // 用户在应用内的唯一标识 UnionId string `json:"union_id,omitempty"` // 用户统一ID Email string `json:"email,omitempty"` // 用户邮箱 EnterpriseEmail string `json:"enterprise_email,omitempty"` // 企业邮箱,请先确保已在管理后台启用飞书邮箱服务 UserId string `json:"user_id,omitempty"` // 用户 user_id Mobile string `json:"mobile,omitempty"` // 用户手机号 TenantKey string `json:"tenant_key,omitempty"` // 当前企业标识 RefreshExpiresIn int `json:"refresh_expires_in,omitempty"` // `refresh_token` 的有效期,单位: 秒 RefreshToken string `json:"refresh_token,omitempty"` // 刷新用户 `access_token` 时使用的 token Sid string `json:"sid,omitempty"` // 用户当前登录态session的唯一标识,为空则不返回 Base }
func (*UserInfo) MarshalLogObject ¶
func (u *UserInfo) MarshalLogObject(enc zapcore.ObjectEncoder) error