wgm

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FindPageOptionSyncPool = sync.Pool{
	New: func() interface{} {
		return new(FindPageOption)
	},
}

FindPageOptionSyncPool is a sync.Pool that stores FindPageOption objects.

Functions

func Aggregate

func Aggregate(m IDefaultModel, pipeline any, result any) error

Aggregate @param m: 查询合集 @param pipeline: 聚合管道,必须为数组 @param result: 查询结果,必须为指向数组的指针 @Description: 聚合查询,详情见 https://www.mongodb.com/docs/manual/aggregation/

func CloseAll

func CloseAll()

func Col

func Col(name string) *qmgo.Collection

func Ctx

func Ctx() context.Context

func Delete

func Delete(m IDefaultModel) error

func Distinct

func Distinct(m IDefaultModel, filter any, field string, result any) error

Distinct @param m: 查询合集 @param filter: 查询前过滤doc @param field: 去重字段 @param resultSlice: 查询结果,必须为指向数组的指针 @Description: 去重查询,详情见 https://docs.mongodb.com/manual/reference/command/distinct/

func ExistInDB

func ExistInDB(m IDefaultModel, filter any) bool

ExistInDB 查询是否存在数据库 m 查询的合集 filter 查询条件,查询全部文档使用 nil,查询条件使用 bson.M bool 是否存在

func FindById

func FindById(colName string, id string, res any) (bool, error)

func FindOne

func FindOne(m IDefaultModel, filter map[string]any) (hasResult bool)

FindOne 查询符合条件的第一条数据 m 查询的合集,结果也会被绑定在这 filter 查询条件,查询全部文档使用 nil,查询条件使用 bson.M hasResult 是否查询到结果

func FindPage

func FindPage(m IDefaultModel, filter any, res any, pageSize int64, currentPage int64) (totalDoc int64, totalPage int64)

FindPage 数据库分页查询 m 查询的合集 filter 查询条件,查询全部文档使用 nil,查询条件使用 bson.M res 结果集指针,必须为指向切片的指针!!! pageSize 页面大小 currentPage 当前页面 totalDoc 总数据数量 totalPage 总页面数量

func FindPageWithOption

func FindPageWithOption(m IDefaultModel, filter any, res any, pageSize int64, currentPage int64, option *FindPageOption) (totalDoc int64, totalPage int64)

FindPageWithOption 数据库多条件分页查询 m 查询的合集 filter 查询条件,查询全部文档使用 nil,查询条件使用 bson.M res 结果集指针,必须为指向切片的指针!!! pageSize 页面大小 currentPage 当前页面 totalDoc 总数据数量 totalPage 总页面数量

func InitWgm

func InitWgm(connectionUri string, databaseName string) error

InitWgm initializes the connection to the specified database using the provided connection URI and database name. Parameters: - connectionUri: The URI of the MongoDB connection. - databaseName: The name of the database to connect to. Returns: - error: An error if the connection could not be established. Usage example:

err := InitWgm("mongodb://localhost:27017/", "wshop_test")
if err != nil {
	// Handle error
}

Usage example:

err := InitWgm("wrong://localhost:27017/", "wshop_test")
if err != nil {
	// Handle error
}

Usage example:

SetupDefaultConnection()

func Insert

func IsNoResult

func IsNoResult(err error) bool

IsNoResult 是否结果不存在 err 数据库查询后返回的 err bool 结果,true 为未查询到数据,反之亦然

func MustHexToObjectId

func MustHexToObjectId(strId string) primitive.ObjectID

func Ping

func Ping() error

Ping checks if the WGM instance is initialized and then performs a ping operation on the MongoDB connection. If the WGM instance is not initialized, it logs an error message and returns an error.

func Update

func Update(m IDefaultModel, filter ...map[string]any) error

func Updater

func Updater(m any) *updater

Updater creates an updater object based on the provided model.

param m: the model to be updated.

return: the updater object.

Types

type DefaultModel

type DefaultModel struct {
	Id             primitive.ObjectID `bson:"_id" json:"id"`
	CreateTime     int64              `bson:"create_time" json:"create_time"`
	LastModifyTime int64              `bson:"last_modify_time" json:"last_modify_time"`
}

DefaultModel represents a default model for MongoDB documents.

func (*DefaultModel) BeforeInsert

func (m *DefaultModel) BeforeInsert(ctx context.Context) error

BeforeInsert sets the default values for the Id, CreateTime, and LastModifyTime fields of the DefaultModel before inserting it into the database. It is called before inserting a new document. This method should be called within the context of a transaction. It does not return an error.

func (*DefaultModel) BeforeUpdate

func (m *DefaultModel) BeforeUpdate(ctx context.Context) error

BeforeUpdate updates the last modify time of the DefaultModel instance to the current time.

func (*DefaultModel) BeforeUpsert

func (m *DefaultModel) BeforeUpsert(ctx context.Context) error

BeforeUpsert sets defaults for Id, CreateTime, and LastModifyTime before upserting the DefaultModel.

func (*DefaultModel) ColName

func (m *DefaultModel) ColName() string

ColName returns the name of the collection to which the DefaultModel belongs.

func (*DefaultModel) GetId

func (m *DefaultModel) GetId() string

GetId returns the hexadecimal string representation of the Id field of the DefaultModel. It uses the Hex() method from the Id field to convert the Id to its string representation.

func (*DefaultModel) GetObjectID

func (m *DefaultModel) GetObjectID() primitive.ObjectID

GetObjectID returns the ObjectID of the DefaultModel. The ObjectID is used to uniquely identify the DefaultModel in the database. It is retrieved from the `Id` field of the DefaultModel struct.

func (*DefaultModel) PutId

func (m *DefaultModel) PutId(id string)

PutId updates the "Id" field of the DefaultModel with the specified value. The input "id" is expected to be a hexadecimal string representing an ObjectId. If the conversion from the hexadecimal string to an ObjectId fails, the "Id" field will not be modified. Example:

doc := &Doc{}
hexId := "63632c7dfc826378c8abd802"
doc.PutId(hexId)
hex, _ := primitive.ObjectIDFromHex(hexId)
require.Equal(t, hex, doc.Id)

Doc declaration:

type Doc struct {
    wgm.DefaultModel `bson:",inline"`
    Name             string `bson:"name"`
    Age              int    `bson:"age"`
}
func (d *Doc) ColName() string {
    return "Docs"
}

type FindPageOption

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

func NewFindPageOption

func NewFindPageOption() *FindPageOption

func (*FindPageOption) SetSelectField

func (o *FindPageOption) SetSelectField(bson interface{}) *FindPageOption

SetSelectField param: fieldStr bson Description: 初始化Select, bson.M{"age": 1} means that only the age field is displayed bson.M{"age": 0} means to display other fields except age

func (*FindPageOption) SetSortField

func (o *FindPageOption) SetSortField(field ...string) *FindPageOption

SetSortField param: field []string Description: 初始化SortField, {"age", "-name"}, first sort by age in ascending order, then sort by name in descending order

type IDefaultModel

type IDefaultModel interface {
	ColName() string
	GetId() string
	GetObjectID() primitive.ObjectID
	PutId(id string)
	// contains filtered or unexported methods
}

IDefaultModel represents an interface for a default model in MongoDB documents.

Jump to

Keyboard shortcuts

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