From 3b662a23511a75f172c7142cbe75d82b8a9b2c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Fri, 23 Sep 2022 15:10:59 +0800 Subject: [PATCH] - update --- bun.go | 4 ++-- const.go | 2 +- gorm.go | 6 ++---- gorm_mysql.go | 10 +++++----- gorm_postgresql.go | 20 ++++++++++---------- mongo_collection_curd.go | 10 ++++++++++ mongo_session_collection_curd.go | 10 ++++++++++ redis.go | 6 +++--- xorm.go | 6 +++--- xorm_mysql.go | 2 +- xorm_postgres.go | 2 +- 11 files changed, 48 insertions(+), 30 deletions(-) diff --git a/bun.go b/bun.go index 46b5fe1..a6320ac 100644 --- a/bun.go +++ b/bun.go @@ -4,7 +4,7 @@ import ( "github.com/uptrace/bun" ) -type ConfigBunClient struct { +type BunClientConfig struct { Dns string // 地址 } @@ -12,5 +12,5 @@ type ConfigBunClient struct { // https://bun.uptrace.dev/ type BunClient struct { Db *bun.DB // 驱动 - config *ConfigBunClient // 配置 + config *BunClientConfig // 配置 } diff --git a/const.go b/const.go index 8b0fd3f..3548bbc 100644 --- a/const.go +++ b/const.go @@ -1,3 +1,3 @@ package dorm -const Version = "1.0.43" +const Version = "1.0.44" diff --git a/gorm.go b/gorm.go index c08eeb3..dcf5fcc 100644 --- a/gorm.go +++ b/gorm.go @@ -16,14 +16,12 @@ type GormClientFun func() *GormClient // string 表名 type GormClientTableFun func() (*GormClient, string) -type ConfigGormClient struct { +type GormClientConfig struct { Dns string // 地址 LogStatus bool // 日志 - 状态 LogPath string // 日志 - 路径 LogSlow int64 // 日志 - 慢SQL阈值 LogLevel string // 日志 - 级别 - LogNotFoundError bool // 日志 - 忽略ErrRecordNotFound(记录未找到)错误 - LogColorful bool // 日志 - 禁用彩色打印 ConnSetMaxIdle int // 连接 - 设置空闲连接池中连接的最大数量 ConnSetMaxOpen int // 连接 - 设置打开数据库连接的最大数量 ConnSetConnMaxLifetime int64 // 连接 - 设置了连接可复用的最大时间 @@ -33,7 +31,7 @@ type ConfigGormClient struct { // https://gorm.io/ type GormClient struct { Db *gorm.DB // 驱动 - config *ConfigGormClient // 配置 + config *GormClientConfig // 配置 } type writer struct{} diff --git a/gorm_mysql.go b/gorm_mysql.go index f795c97..d8e47be 100644 --- a/gorm_mysql.go +++ b/gorm_mysql.go @@ -10,7 +10,7 @@ import ( "time" ) -func NewGormMysqlClient(config *ConfigGormClient) (*GormClient, error) { +func NewGormMysqlClient(config *GormClientConfig) (*GormClient, error) { var err error c := &GormClient{config: config} @@ -41,10 +41,10 @@ func NewGormMysqlClient(config *ConfigGormClient) (*GormClient, error) { Logger: logger.New( writer{}, logger.Config{ - SlowThreshold: slowThreshold, // 慢SQL阈值 - LogLevel: logLevel, // 日志级别 - IgnoreRecordNotFoundError: c.config.LogNotFoundError, // 忽略ErrRecordNotFound(记录未找到)错误 - Colorful: c.config.LogColorful, // 禁用彩色打印 + SlowThreshold: slowThreshold, // 慢SQL阈值 + LogLevel: logLevel, // 日志级别 + IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误 + Colorful: true, // 禁用彩色打印 }, ), NowFunc: func() time.Time { diff --git a/gorm_postgresql.go b/gorm_postgresql.go index a15149a..1fb3a49 100644 --- a/gorm_postgresql.go +++ b/gorm_postgresql.go @@ -10,7 +10,7 @@ import ( "time" ) -func NewGormPostgresClient(config *ConfigGormClient) (*GormClient, error) { +func NewGormPostgresClient(config *GormClientConfig) (*GormClient, error) { var err error c := &GormClient{} @@ -42,10 +42,10 @@ func NewGormPostgresClient(config *ConfigGormClient) (*GormClient, error) { Logger: logger.New( writer{}, logger.Config{ - SlowThreshold: slowThreshold, // 慢SQL阈值 - LogLevel: logLevel, // 日志级别 - IgnoreRecordNotFoundError: c.config.LogNotFoundError, // 忽略ErrRecordNotFound(记录未找到)错误 - Colorful: c.config.LogColorful, // 禁用彩色打印 + SlowThreshold: slowThreshold, // 慢SQL阈值 + LogLevel: logLevel, // 日志级别 + IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误 + Colorful: true, // 禁用彩色打印 }, ), NowFunc: func() time.Time { @@ -89,7 +89,7 @@ func NewGormPostgresClient(config *ConfigGormClient) (*GormClient, error) { return c, nil } -func NewGormPostgresqlClient(config *ConfigGormClient) (*GormClient, error) { +func NewGormPostgresqlClient(config *GormClientConfig) (*GormClient, error) { var err error c := &GormClient{} @@ -121,10 +121,10 @@ func NewGormPostgresqlClient(config *ConfigGormClient) (*GormClient, error) { Logger: logger.New( writer{}, logger.Config{ - SlowThreshold: slowThreshold, // 慢SQL阈值 - LogLevel: logLevel, // 日志级别 - IgnoreRecordNotFoundError: c.config.LogNotFoundError, // 忽略ErrRecordNotFound(记录未找到)错误 - Colorful: c.config.LogColorful, // 禁用彩色打印 + SlowThreshold: slowThreshold, // 慢SQL阈值 + LogLevel: logLevel, // 日志级别 + IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误 + Colorful: true, // 禁用彩色打印 }, ), NowFunc: func() time.Time { diff --git a/mongo_collection_curd.go b/mongo_collection_curd.go index 3309494..f56b786 100644 --- a/mongo_collection_curd.go +++ b/mongo_collection_curd.go @@ -26,6 +26,11 @@ func (cc *MongoCollectionOptions) DeleteMany(ctx context.Context, filter interfa return cc.dbCollection.DeleteMany(ctx, filter, opts...) } +// ReplaceOne 替换一个文档 +func (cc *MongoCollectionOptions) ReplaceOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error) { + return cc.dbCollection.ReplaceOne(ctx, filter, update, opts...) +} + // UpdateOne 更新一个文档 func (cc *MongoCollectionOptions) UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) { return cc.dbCollection.UpdateOne(ctx, filter, update, opts...) @@ -45,3 +50,8 @@ func (cc *MongoCollectionOptions) FindOne(ctx context.Context, filter interface{ func (cc *MongoCollectionOptions) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) { return cc.dbCollection.Find(ctx, filter, opts...) } + +// Aggregate 统计分析 +func (cc *MongoCollectionOptions) Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error) { + return cc.dbCollection.Aggregate(ctx, pipeline, opts...) +} diff --git a/mongo_session_collection_curd.go b/mongo_session_collection_curd.go index d9fb224..ac4e4a5 100644 --- a/mongo_session_collection_curd.go +++ b/mongo_session_collection_curd.go @@ -25,6 +25,11 @@ func (csc *MongoSessionCollectionOptions) DeleteMany(filter interface{}, opts .. return csc.dbCollection.DeleteMany(csc.sessionContext, filter, opts...) } +// ReplaceOne 替换一个文档 +func (csc *MongoSessionCollectionOptions) ReplaceOne(filter interface{}, update interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error) { + return csc.dbCollection.ReplaceOne(csc.sessionContext, filter, update, opts...) +} + // UpdateOne 更新一个文档 func (csc *MongoSessionCollectionOptions) UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) { return csc.dbCollection.UpdateOne(csc.sessionContext, filter, update, opts...) @@ -44,3 +49,8 @@ func (csc *MongoSessionCollectionOptions) FindOne(filter interface{}, opts ...*o func (csc *MongoSessionCollectionOptions) Find(filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) { return csc.dbCollection.Find(csc.sessionContext, filter, opts...) } + +// Aggregate 统计分析 +func (csc *MongoSessionCollectionOptions) Aggregate(pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error) { + return csc.dbCollection.Aggregate(csc.sessionContext, pipeline, opts...) +} diff --git a/redis.go b/redis.go index 41196db..1a2bd02 100644 --- a/redis.go +++ b/redis.go @@ -11,7 +11,7 @@ import ( // RedisClientFun *RedisClient 驱动 type RedisClientFun func() *RedisClient -type ConfigRedisClient struct { +type RedisClientConfig struct { Addr string // 地址 Password string // 密码 DB int // 数据库 @@ -23,10 +23,10 @@ type ConfigRedisClient struct { // https://redis.uptrace.dev/ type RedisClient struct { Db *redis.Client // 驱动 - config *ConfigRedisClient // 配置 + config *RedisClientConfig // 配置 } -func NewRedisClient(config *ConfigRedisClient) (*RedisClient, error) { +func NewRedisClient(config *RedisClientConfig) (*RedisClient, error) { c := &RedisClient{} c.config = config diff --git a/xorm.go b/xorm.go index 343dba1..718e557 100644 --- a/xorm.go +++ b/xorm.go @@ -4,13 +4,13 @@ import ( "xorm.io/xorm" ) -type ConfigXormClient struct { +type XormClientConfigXorm struct { Dns string // 地址 } // XormClient // https://xorm.io/ type XormClient struct { - Db *xorm.Engine // 驱动 - config *ConfigXormClient // 配置 + Db *xorm.Engine // 驱动 + config *XormClientConfigXorm // 配置 } diff --git a/xorm_mysql.go b/xorm_mysql.go index 7cc1359..a1f4861 100644 --- a/xorm_mysql.go +++ b/xorm_mysql.go @@ -7,7 +7,7 @@ import ( "xorm.io/xorm" ) -func NewXormMysqlClient(config *ConfigXormClient) (*XormClient, error) { +func NewXormMysqlClient(config *XormClientConfigXorm) (*XormClient, error) { var err error c := &XormClient{config: config} diff --git a/xorm_postgres.go b/xorm_postgres.go index 2995903..13ad09b 100644 --- a/xorm_postgres.go +++ b/xorm_postgres.go @@ -7,7 +7,7 @@ import ( "xorm.io/xorm" ) -func NewXormPostgresClient(config *ConfigXormClient) (*XormClient, error) { +func NewXormPostgresClient(config *XormClientConfigXorm) (*XormClient, error) { var err error c := &XormClient{config: config}