model

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2021 License: MIT Imports: 8 Imported by: 0

README

Model

Model层,或者叫 Entity,实体层,用于存放我们的实体类,与数据库中的属性值基本保持一致。

通过http访问返回的结构体也都放到这里,在输出前进行结构体的转换。一般 XXXInfo 的形式。 比如: 在返回终端用户前对 userModel 进行转换,转换为结构体 UserInfo

数据库约定

这里默认使用 MySQL 数据库,尽量使用 InnoDB 作为存储引擎。

相关表采用统一前缀

比如和用户相关的,使用 user_ 作为表前缀:

user_base       // 用户基础表
user_follow     // 用户关注表
user_fans       // 用户粉丝表
user_stat       // 用户统计表
统一字段名

一个表中需要包含的三大字段:主键(id),创建时间(created_at),更新时间(updated_at)
如果需要用户id,一般用 user_id 表示即可。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DB *gorm.DB

DB 数据库全局变量

Functions

func GetDB

func GetDB() *gorm.DB

GetDB 返回默认的数据库

func Init added in v1.2.0

func Init(cfg *orm.Config) *gorm.DB

Init 初始化数据库

func WhereBuild

func WhereBuild(where map[string]interface{}) (whereSQL string, vals []interface{}, err error)

WhereBuild sql build where see: https://github.com/jinzhu/gorm/issues/2055

Types

type NullType

type NullType byte

NullType 空字节类型

const (

	// IsNull the same as `is null`
	IsNull NullType
	// IsNotNull the same as `is not null`
	IsNotNull
)

type Token

type Token struct {
	Token string `json:"token"`
}

Token represents a JSON web token.

type UserBaseModel added in v1.1.1

type UserBaseModel struct {
	ID        uint64    `gorm:"primary_key;AUTO_INCREMENT;column:id" json:"id"`
	Username  string    `json:"username" gorm:"column:username;not null" binding:"required" validate:"min=1,max=32"`
	Password  string    `json:"password" gorm:"column:password;not null" binding:"required" validate:"min=5,max=128"`
	Phone     int64     `gorm:"column:phone" json:"phone"`
	Email     string    `gorm:"column:email" json:"email"`
	Avatar    string    `gorm:"column:avatar" json:"avatar"`
	Sex       int       `gorm:"column:sex" json:"sex"`
	CreatedAt time.Time `gorm:"column:created_at" json:"-"`
	UpdatedAt time.Time `gorm:"column:updated_at" json:"-"`
}

UserBaseModel User represents a registered user.

func (*UserBaseModel) Compare added in v1.1.1

func (u *UserBaseModel) Compare(pwd string) (err error)

Compare with the plain text password. Returns true if it's the same as the encrypted one (in the `User` struct).

func (*UserBaseModel) Encrypt added in v1.1.1

func (u *UserBaseModel) Encrypt() (err error)

Encrypt the user password.

func (*UserBaseModel) TableName added in v1.1.1

func (u *UserBaseModel) TableName() string

TableName 表名

func (*UserBaseModel) Validate added in v1.1.1

func (u *UserBaseModel) Validate() error

Validate the fields.

type UserFansModel

type UserFansModel struct {
	ID          uint64    `gorm:"primary_key;AUTO_INCREMENT;column:id" json:"-"`
	FollowerUID uint64    `gorm:"column:follower_uid" json:"follower_uid"`
	Status      int       `gorm:"column:status" json:"status"`
	UserID      uint64    `gorm:"column:user_id" json:"user_id"`
	CreatedAt   time.Time `gorm:"column:created_at" json:"-"`
	UpdatedAt   time.Time `gorm:"column:updated_at" json:"-"`
}

UserFansModel 粉丝表

func (*UserFansModel) TableName

func (u *UserFansModel) TableName() string

TableName sets the insert table name for this struct type

type UserFollow

type UserFollow struct {
	FollowNum int `json:"follow_num"` // 关注数
	FansNum   int `json:"fans_num"`   // 粉丝数
	IsFollow  int `json:"is_follow"`  // 是否关注 1:是 0:否
	IsFans    int `json:"is_fans"`    // 是否是粉丝 1:是 0:否
}

UserFollow 关注

type UserFollowModel

type UserFollowModel struct {
	ID          uint64    `gorm:"primary_key;AUTO_INCREMENT;column:id" json:"-"`
	FollowedUID uint64    `gorm:"column:followed_uid" json:"followed_uid"`
	Status      int       `gorm:"column:status" json:"status"`
	UserID      uint64    `gorm:"column:user_id" json:"user_id"`
	CreatedAt   time.Time `gorm:"column:created_at" json:"-"`
	UpdatedAt   time.Time `gorm:"column:updated_at" json:"-"`
}

UserFollowModel 关注表

func (*UserFollowModel) TableName

func (u *UserFollowModel) TableName() string

TableName sets the insert table name for this struct type

type UserInfo

type UserInfo struct {
	ID         uint64      `json:"id" example:"1"`
	Username   string      `json:"username" example:"张三"`
	Avatar     string      `json:"avatar"`
	Sex        int         `json:"sex"`
	UserFollow *UserFollow `json:"user_follow"`
}

UserInfo 对外暴露的结构体

type UserList

type UserList struct {
	Lock  *sync.Mutex
	IDMap map[uint64]*UserInfo
}

UserList 用户列表结构体

type UserStatModel

type UserStatModel struct {
	ID            uint64    `gorm:"primary_key;AUTO_INCREMENT;column:id" json:"-"`
	UserID        uint64    `gorm:"column:user_id;not null" json:"user_id" binding:"required"`
	FollowCount   int       `gorm:"column:follow_count" json:"follow_count"`
	FollowerCount int       `gorm:"column:follower_count" json:"follower_count"`
	CreatedAt     time.Time `gorm:"column:created_at" json:"-"`
	UpdatedAt     time.Time `gorm:"column:updated_at" json:"-"`
}

UserStatModel 用户数据统计表

func (*UserStatModel) TableName

func (u *UserStatModel) TableName() string

TableName 表名

Jump to

Keyboard shortcuts

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