dao

package
v0.0.0-...-0ca6d7b Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Database access

Index

Constants

View Source
const (
	ArticleCollName          = "articles"
	PublishedArticleCollName = "published_articles"
	DatabaseName             = "wetravel"
)

Variables

View Source
var (
	ErrDuplicatedUser = errors.New("email already exists")
	ErrRecordNotFound = gorm.ErrRecordNotFound
)
View Source
var ErrArticleNotFound = errors.New("article not found")
View Source
var ErrDuplicatedSMS = errors.New("sms already exists")

Functions

func InitCollection

func InitCollection(mdb *mongo.Database) error

func InitTable

func InitTable(db *gorm.DB) error

Types

type Article

type Article struct {
	ID      int64  `gorm:"primaryKey,autoIncrement" bson:"id,omitempty"`
	Title   string `gorm:"type=varchar(4096)"       bson:"title,omitempty"`
	Content string `gorm:"type=BLOB"                bson:"content,omitempty"`

	AuthorID int64 `gorm:"index" bson:"author_id,omitempty"`
	Status   uint8 `             bson:"status,omitempty"`
	Ctime    int64 `             bson:"ctime,omitempty"`
	Utime    int64 `             bson:"utime,omitempty"`
}

type ArticleAuthorDAO

type ArticleAuthorDAO interface {
	Create(ctx context.Context, article Article) (int64, error)
	UpdateByID(ctx context.Context, article Article) error
}

func NewArticleGORMAuthorDAO

func NewArticleGORMAuthorDAO(db *gorm.DB) ArticleAuthorDAO

type ArticleDAO

type ArticleDAO interface {
	Insert(ctx context.Context, article Article) (int64, error)
	UpdateByID(ctx context.Context, article Article) error
	Sync(ctx context.Context, article Article) (int64, error)
	Transaction(ctx context.Context, fn func(ctx context.Context, tx any) (any, error)) (any, error)
	Upsert(ctx context.Context, article PublishedArticle) error
	// model is like &Article{} or &PublishedArticle{}
	UpdateStatusByID(
		ctx context.Context,
		model any,
		userID int64,
		articleID int64,
		status uint8,
	) error
	GetByAuthor(ctx context.Context, uid int64, offset int, limit int) ([]Article, error)
	GetByID(ctx context.Context, id int64) (Article, error)
	GetPubByID(ctx context.Context, id int64) (PublishedArticle, error)
}

func NewArticleDAO

func NewArticleDAO(db *gorm.DB) ArticleDAO

func NewMongoDBArticleDAO

func NewMongoDBArticleDAO(client *mongo.Client, node *snowflake.Node) ArticleDAO

type ArticleGORMAuthorDAO

type ArticleGORMAuthorDAO struct {
	// contains filtered or unexported fields
}

func (*ArticleGORMAuthorDAO) Create

func (a *ArticleGORMAuthorDAO) Create(ctx context.Context, article Article) (int64, error)

func (*ArticleGORMAuthorDAO) UpdateByID

func (a *ArticleGORMAuthorDAO) UpdateByID(ctx context.Context, article Article) error

type ArticleGORMReaderDAO

type ArticleGORMReaderDAO struct {
	// contains filtered or unexported fields
}

func (*ArticleGORMReaderDAO) Upsert

func (a *ArticleGORMReaderDAO) Upsert(ctx context.Context, article Article) error

func (*ArticleGORMReaderDAO) UpsertV2

func (a *ArticleGORMReaderDAO) UpsertV2(ctx context.Context, article PublishedArticle) error

type ArticleReaderDAO

type ArticleReaderDAO interface {
	// Insert and Update
	Upsert(ctx context.Context, article Article) error
	UpsertV2(ctx context.Context, article PublishedArticle) error
}

func NewArticleGORMReaderDAO

func NewArticleGORMReaderDAO(db *gorm.DB) ArticleReaderDAO

type AsyncSMSDAO

type AsyncSMSDAO interface {
	Insert(ctx context.Context, s SMSInfo) error
	Update(ctx context.Context, s SMSInfo) error
	GetFirst(ctx context.Context) (SMSInfo, error)
	Delete(ctx context.Context, smsInfo SMSInfo) error
}

func NewAsyncSMSDAO

func NewAsyncSMSDAO(db *gorm.DB) AsyncSMSDAO

type GORMArticleDAO

type GORMArticleDAO struct {
	// contains filtered or unexported fields
}

func (*GORMArticleDAO) GetByAuthor

func (a *GORMArticleDAO) GetByAuthor(
	ctx context.Context,
	uid int64,
	offset int,
	limit int,
) ([]Article, error)

GetByAuthor implements ArticleDAO.

func (*GORMArticleDAO) GetByID

func (a *GORMArticleDAO) GetByID(ctx context.Context, id int64) (Article, error)

GetByID implements ArticleDAO.

func (*GORMArticleDAO) GetPubByID

func (a *GORMArticleDAO) GetPubByID(ctx context.Context, id int64) (PublishedArticle, error)

GetPubByID implements ArticleDAO.

func (*GORMArticleDAO) Insert

func (a *GORMArticleDAO) Insert(ctx context.Context, article Article) (int64, error)

func (*GORMArticleDAO) Sync

func (a *GORMArticleDAO) Sync(ctx context.Context, article Article) (int64, error)

Use closure (recommended)

func (*GORMArticleDAO) SyncV1

func (a *GORMArticleDAO) SyncV1(ctx context.Context, article Article) (int64, error)

func (*GORMArticleDAO) Transaction

func (a *GORMArticleDAO) Transaction(
	ctx context.Context,
	fn func(ctx context.Context, tx any) (any, error),
) (any, error)

func (*GORMArticleDAO) UpdateByID

func (a *GORMArticleDAO) UpdateByID(ctx context.Context, article Article) error

func (*GORMArticleDAO) UpdateStatusByID

func (a *GORMArticleDAO) UpdateStatusByID(
	ctx context.Context,
	model any,
	userID int64,
	articleID int64,
	status uint8,
) error

func (*GORMArticleDAO) Upsert

func (a *GORMArticleDAO) Upsert(ctx context.Context, article PublishedArticle) error

type GORMAsyncSMSDAO

type GORMAsyncSMSDAO struct {
	// contains filtered or unexported fields
}

func (*GORMAsyncSMSDAO) Delete

func (dao *GORMAsyncSMSDAO) Delete(ctx context.Context, smsInfo SMSInfo) error

func (*GORMAsyncSMSDAO) GetFirst

func (dao *GORMAsyncSMSDAO) GetFirst(ctx context.Context) (SMSInfo, error)

func (*GORMAsyncSMSDAO) Insert

func (dao *GORMAsyncSMSDAO) Insert(ctx context.Context, s SMSInfo) error

func (*GORMAsyncSMSDAO) Update

func (dao *GORMAsyncSMSDAO) Update(ctx context.Context, s SMSInfo) error

type GORMInteractiveDAO

type GORMInteractiveDAO struct {
	// contains filtered or unexported fields
}

func (*GORMInteractiveDAO) DeleteCollectionBiz

func (g *GORMInteractiveDAO) DeleteCollectionBiz(ctx context.Context, cb UserCollectionBiz) error

DeleteCollectionBiz implements InteractiveDAO.

func (*GORMInteractiveDAO) DeleteLikeInfo

func (g *GORMInteractiveDAO) DeleteLikeInfo(
	ctx context.Context,
	biz string,
	bizID int64,
	uid int64,
) error

DeleteLikeInfo implements InteractiveDAO.

func (*GORMInteractiveDAO) Get

func (g *GORMInteractiveDAO) Get(
	ctx context.Context,
	biz string,
	bizID int64,
) (Interactive, error)

Get implements InteractiveDAO.

func (*GORMInteractiveDAO) GetCollectInfo

func (g *GORMInteractiveDAO) GetCollectInfo(
	ctx context.Context,
	biz string,
	bizID int64,
	uid int64,
) (UserCollectionBiz, error)

func (*GORMInteractiveDAO) GetLikeInfo

func (g *GORMInteractiveDAO) GetLikeInfo(
	ctx context.Context,
	biz string,
	bizID int64,
	uid int64,
) (UserLikeBiz, error)

GetLikeInfo implements InteractiveDAO.

func (*GORMInteractiveDAO) IncrReadCnt

func (g *GORMInteractiveDAO) IncrReadCnt(ctx context.Context, biz string, bizID int64) error

IncrReadCnt implements InteractiveDAO.

func (*GORMInteractiveDAO) InsertCollectionBiz

func (g *GORMInteractiveDAO) InsertCollectionBiz(ctx context.Context, cb UserCollectionBiz) error

InsertCollectionBiz implements InteractiveDAO.

func (*GORMInteractiveDAO) InsertLikeInfo

func (g *GORMInteractiveDAO) InsertLikeInfo(
	ctx context.Context,
	biz string,
	bizID int64,
	uid int64,
) error

InsertLikeInfo implements InteractiveDAO.

type GORMUserDAO

type GORMUserDAO struct {
	// contains filtered or unexported fields
}

func (*GORMUserDAO) FindByEmail

func (dao *GORMUserDAO) FindByEmail(ctx context.Context, email string) (User, error)

func (*GORMUserDAO) FindByID

func (dao *GORMUserDAO) FindByID(ctx context.Context, id int64) (User, error)

func (*GORMUserDAO) FindByPhone

func (dao *GORMUserDAO) FindByPhone(ctx context.Context, phone string) (User, error)

func (*GORMUserDAO) Insert

func (dao *GORMUserDAO) Insert(ctx context.Context, u User) (User, error)

func (*GORMUserDAO) UpdateProfile

func (dao *GORMUserDAO) UpdateProfile(ctx context.Context, user User) error

type Interactive

type Interactive struct {
	ID int64 `gorm:"primaryKey,autoIncrement"`

	// <biz_id, biz>
	BizID      int64  `gorm:"uniqueIndex:biz_type_id"`
	Biz        string `gorm:"uniqueIndex:biz_type_id,length:128"`
	ReadCnt    int64
	LikeCnt    int64
	CollectCnt int64
	Utime      int64
	Ctime      int64
}

type InteractiveDAO

type InteractiveDAO interface {
	IncrReadCnt(ctx context.Context, biz string, bizID int64) error
	InsertLikeInfo(ctx context.Context, biz string, bizID int64, uid int64) error
	DeleteLikeInfo(ctx context.Context, biz string, bizID int64, uid int64) error
	InsertCollectionBiz(ctx context.Context, cb UserCollectionBiz) error
	DeleteCollectionBiz(ctx context.Context, cb UserCollectionBiz) error
	Get(ctx context.Context, biz string, bizID int64) (Interactive, error)
	GetLikeInfo(ctx context.Context, biz string, bizID int64, uid int64) (UserLikeBiz, error)
	GetCollectInfo(
		ctx context.Context,
		biz string,
		bizID int64,
		uid int64,
	) (UserCollectionBiz, error)
}

func NewGORMInteractiveDAO

func NewGORMInteractiveDAO(db *gorm.DB) InteractiveDAO

type MongoDBArticleDAO

type MongoDBArticleDAO struct {
	// contains filtered or unexported fields
}

func (*MongoDBArticleDAO) GetByAuthor

func (m *MongoDBArticleDAO) GetByAuthor(
	ctx context.Context,
	uid int64,
	offset int,
	limit int,
) ([]Article, error)

GetByAuthor implements ArticleDAO.

func (*MongoDBArticleDAO) GetByID

func (m *MongoDBArticleDAO) GetByID(ctx context.Context, id int64) (Article, error)

GetByID implements ArticleDAO.

func (*MongoDBArticleDAO) GetPubByID

func (m *MongoDBArticleDAO) GetPubByID(ctx context.Context, id int64) (PublishedArticle, error)

GetPubByID implements ArticleDAO.

func (*MongoDBArticleDAO) Insert

func (m *MongoDBArticleDAO) Insert(ctx context.Context, article Article) (int64, error)

Insert implements ArticleDAO.

func (*MongoDBArticleDAO) Sync

func (m *MongoDBArticleDAO) Sync(ctx context.Context, article Article) (int64, error)

Sync implements ArticleDAO.

func (*MongoDBArticleDAO) Transaction

func (m *MongoDBArticleDAO) Transaction(
	ctx context.Context,
	fn func(ctx context.Context, tx any) (any, error),
) (any, error)

Transaction implements ArticleDAO.

func (*MongoDBArticleDAO) UpdateByID

func (m *MongoDBArticleDAO) UpdateByID(ctx context.Context, article Article) error

UpdateByID implements ArticleDAO.

func (*MongoDBArticleDAO) UpdateStatusByID

func (m *MongoDBArticleDAO) UpdateStatusByID(
	ctx context.Context,
	model any,
	userID int64,
	articleID int64,
	status uint8,
) error

UpdateStatusByID implements ArticleDAO.

func (*MongoDBArticleDAO) Upsert

func (m *MongoDBArticleDAO) Upsert(ctx context.Context, article PublishedArticle) error

Upsert implements ArticleDAO.

type PublishedArticle

type PublishedArticle Article

same DB, different tables

type SMSInfo

type SMSInfo struct {
	gorm.Model

	ToNb string `gorm:"index:sms,unique"`
	Body string `gorm:"index:sms,unique"`
	Args string // JSON array

	RetryTimes int
}

type User

type User struct {
	Password string
	Name     string         `gorm:"type=varchar(128)"`
	Profile  string         `gorm:"type=varchar(4096)"`
	Email    sql.NullString `gorm:"unique"`
	Phone    sql.NullString `gorm:"unique"`

	// NOTE: autoIncrement for performance:
	// 1. rows physically stored in key order
	// 2. Read-ahead
	ID int64 `gorm:"primaryKey,autoIncrement"`

	// NOTE: UTC-0
	Ctime    int64
	Utime    int64
	Birthday int64
}

type UserCollectionBiz

type UserCollectionBiz struct {
	ID int64 `gorm:"primaryKey,autoIncrement"`
	// One ressource can only be put into one collection.
	// Otherwise the composite index should include CID too
	UID   int64  `gorm:"uniqueIndex:uid_biz_type_id"`
	BizID int64  `gorm:"uniqueIndex:uid_biz_type_id"`
	Biz   string `gorm:"uniqueIndex:uid_biz_type_id,length:128"`
	// collection ID
	CID   int64 `gorm:"index"`
	Utime int64
	Ctime int64
}

type UserDAO

type UserDAO interface {
	Insert(ctx context.Context, u User) (User, error)
	FindByEmail(ctx context.Context, email string) (User, error)
	FindByPhone(ctx context.Context, phone string) (User, error)
	FindByID(ctx context.Context, id int64) (User, error)
	UpdateProfile(ctx context.Context, user User) error
}

func NewUserDAO

func NewUserDAO(db *gorm.DB) UserDAO

type UserLikeBiz

type UserLikeBiz struct {
	ID     int64  `gorm:"primaryKey,autoIncrement"`
	UID    int64  `gorm:"uniqueIndex:uid_biz_type_id"`
	BizID  int64  `gorm:"uniqueIndex:uid_biz_type_id"`
	Biz    string `gorm:"uniqueIndex:uid_biz_type_id,length:128"`
	Status int
	Utime  int64
	Ctime  int64
}

Directories

Path Synopsis
Package daomocks is a generated GoMock package.
Package daomocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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