repository

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2023 License: MIT Imports: 17 Imported by: 0

README

Repository

Repository,是数据访问层,负责访问 DB、MC、外部 HTTP 等接口,对上层屏蔽数据访问细节。
后续更换、升级ORM引擎,不影响业务逻辑。能提高测试效率,单元测试时,用Mock对象代替实际的数据库存取,可以成倍地提高测试用例运行速度。
应用 Repository 模式所带来的好处,远高于实现这个模式所增加的代码。只要项目分层,都应当使用这个模式。 Repository是DDD中的概念,强调 Repository 是受Domain(本项目主要是Service)驱动的。 对 Model 层只能单表操作,每一个方法都有一个参数 db *grom.DB 实例,方便事务操作。

具体职责有:

  • SQL 拼接和 DB 访问逻辑
  • DB 的拆库分表逻辑
  • DB 的缓存读写逻辑
  • HTTP 接口调用逻辑

Tips: 如果是返回的列表,尽量返回map,方便service使用。

建议:

  • 推荐使用编写原生SQL
  • 禁止使用连表查询,好处是易扩展,比如分库分表
  • 逻辑部分在程序中进行处理

一个业务一个目录,每一个repo go文件对应一个表操作,比如用户是在user目录下,涉及用户相关的都可以放到这里,
根据不同的模块分离到不同的文件,同时又避免了单个文件func太多的问题。比如:

  • 用户基础服务- user_base_repo.go
  • 用户关注- user_follow_repo.go
  • 用户喜欢- user_like_repo.go
  • 用户评论- user_comment_repo.go

单元测试

关于数据库的单元测试可以用到的几个库:

Reference

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound data is not exist
	ErrNotFound = gorm.ErrRecordNotFound
)

Functions

This section is empty.

Types

type Repository

type Repository interface {
	// BaseUser
	CreateUser(ctx context.Context, user *model.UserBaseModel) (id uint64, err error)
	UpdateUser(ctx context.Context, id uint64, userMap map[string]interface{}) error
	GetUser(ctx context.Context, id uint64) (*model.UserBaseModel, error)
	GetUsersByIds(ctx context.Context, ids []uint64) ([]*model.UserBaseModel, error)
	GetUserByPhone(ctx context.Context, phone int64) (*model.UserBaseModel, error)
	GetUserByEmail(ctx context.Context, email string) (*model.UserBaseModel, error)
	UserIsExist(user *model.UserBaseModel) (bool, error)

	// Follow
	CreateUserFollow(ctx context.Context, db *gorm.DB, userID, followedUID uint64) error
	CreateUserFans(ctx context.Context, db *gorm.DB, userID, followerUID uint64) error
	UpdateUserFollowStatus(ctx context.Context, db *gorm.DB, userID, followedUID uint64, status int) error
	UpdateUserFansStatus(ctx context.Context, db *gorm.DB, userID, followerUID uint64, status int) error
	GetFollowingUserList(ctx context.Context, userID, lastID uint64, limit int) ([]*model.UserFollowModel, error)
	GetFollowerUserList(ctx context.Context, userID, lastID uint64, limit int) ([]*model.UserFansModel, error)
	GetFollowByUIds(ctx context.Context, userID uint64, followingUID []uint64) (map[uint64]*model.UserFollowModel, error)
	GetFansByUIds(ctx context.Context, userID uint64, followerUID []uint64) (map[uint64]*model.UserFansModel, error)

	// stat
	IncrFollowCount(ctx context.Context, db *gorm.DB, userID uint64, step int) error
	IncrFollowerCount(ctx context.Context, db *gorm.DB, userID uint64, step int) error
	GetUserStatByID(ctx context.Context, userID uint64) (*model.UserStatModel, error)
	GetUserStatByIDs(ctx context.Context, userID []uint64) (map[uint64]*model.UserStatModel, error)

	Close()
}

Repository 定义用户仓库接口

func New

func New(db *gorm.DB) Repository

New new a repository and return

Jump to

Keyboard shortcuts

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