- update mongo

master v1.0.42
李光春 2 years ago
parent 284e13594a
commit dbcb880465

@ -1,3 +1,3 @@
package dorm package dorm
const Version = "1.0.41" const Version = "1.0.42"

@ -10,43 +10,42 @@ import (
type MongoSessionOptions struct { type MongoSessionOptions struct {
db *mongo.Client // 驱动 db *mongo.Client // 驱动
configDatabaseName string // 库名 configDatabaseName string // 库名
startSession mongo.Session // 开始会话 session mongo.Session // 会话
Session mongo.SessionContext // 会话 sessionContext mongo.SessionContext // 会话上下文
} }
// Begin 开始事务,会同时创建开始会话需要在退出时关闭会话 // Begin 开始事务,会同时创建开始会话需要在退出时关闭会话
func (c *MongoClient) Begin() (ms *MongoSessionOptions, err error) { func (c *MongoClient) Begin(ctx context.Context) (ms *MongoSessionOptions, err error) {
var ctx = context.Background()
ms.db = c.db ms.db = c.db
ms.configDatabaseName = c.configDatabaseName ms.configDatabaseName = c.configDatabaseName
// 开始会话 // 开始会话
ms.startSession, err = ms.db.StartSession() ms.session, err = ms.db.StartSession()
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("开始会话失败:%v", err)) return nil, errors.New(fmt.Sprintf("开始会话失败:%v", err))
} }
// 会话上下文 // 会话上下文
ms.Session = mongo.NewSessionContext(ctx, ms.startSession) ms.sessionContext = mongo.NewSessionContext(ctx, ms.session)
// 会话开启事务 // 会话开启事务
err = ms.startSession.StartTransaction() err = ms.session.StartTransaction()
return ms, err return ms, err
} }
// Close 关闭会话 // Close 关闭会话
func (cs *MongoSessionOptions) Close(ctx context.Context) { func (cs *MongoSessionOptions) Close(ctx context.Context) {
cs.startSession.EndSession(ctx) cs.session.EndSession(ctx)
} }
// Rollback 回滚事务 // Rollback 回滚事务
func (cs *MongoSessionOptions) Rollback(ctx context.Context) error { func (cs *MongoSessionOptions) Rollback(ctx context.Context) error {
return cs.startSession.AbortTransaction(ctx) return cs.session.AbortTransaction(ctx)
} }
// Commit 提交事务 // Commit 提交事务
func (cs *MongoSessionOptions) Commit(ctx context.Context) error { func (cs *MongoSessionOptions) Commit(ctx context.Context) error {
return cs.startSession.CommitTransaction(ctx) return cs.session.CommitTransaction(ctx)
} }

@ -8,7 +8,8 @@ import (
type MongoSessionCollectionOptions struct { type MongoSessionCollectionOptions struct {
db *mongo.Client // 驱动 db *mongo.Client // 驱动
configDatabaseName string // 库名 configDatabaseName string // 库名
session mongo.SessionContext // 会话 session mongo.Session // 会话
sessionContext mongo.SessionContext // 会话上下文
dbCollection *mongo.Collection // 集合 dbCollection *mongo.Collection // 集合
} }
@ -18,6 +19,7 @@ func (csd *MongoSessionDatabaseOptions) Collection(name string, opts ...*options
db: csd.db, // 驱动 db: csd.db, // 驱动
configDatabaseName: csd.configDatabaseName, // 库名 configDatabaseName: csd.configDatabaseName, // 库名
session: csd.session, // 会话 session: csd.session, // 会话
sessionContext: csd.sessionContext, // 会话上下文
dbCollection: csd.dbDatabase.Collection(name, opts...), // 集合 dbCollection: csd.dbDatabase.Collection(name, opts...), // 集合
} }
} }

@ -7,40 +7,40 @@ import (
// InsertOne 插入一个文档 // InsertOne 插入一个文档
func (csc *MongoSessionCollectionOptions) InsertOne(document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) { func (csc *MongoSessionCollectionOptions) InsertOne(document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
return csc.dbCollection.InsertOne(csc.session, document, opts...) return csc.dbCollection.InsertOne(csc.sessionContext, document, opts...)
} }
// InsertMany 插入多个文档 // InsertMany 插入多个文档
func (csc *MongoSessionCollectionOptions) InsertMany(document []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) { func (csc *MongoSessionCollectionOptions) InsertMany(document []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) {
return csc.dbCollection.InsertMany(csc.session, document, opts...) return csc.dbCollection.InsertMany(csc.sessionContext, document, opts...)
} }
// DeleteOne 删除一个文档 // DeleteOne 删除一个文档
func (csc *MongoSessionCollectionOptions) DeleteOne(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) { func (csc *MongoSessionCollectionOptions) DeleteOne(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
return csc.dbCollection.DeleteOne(csc.session, filter, opts...) return csc.dbCollection.DeleteOne(csc.sessionContext, filter, opts...)
} }
// DeleteMany 删除多个文档 // DeleteMany 删除多个文档
func (csc *MongoSessionCollectionOptions) DeleteMany(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) { func (csc *MongoSessionCollectionOptions) DeleteMany(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
return csc.dbCollection.DeleteMany(csc.session, filter, opts...) return csc.dbCollection.DeleteMany(csc.sessionContext, filter, opts...)
} }
// UpdateOne 更新一个文档 // UpdateOne 更新一个文档
func (csc *MongoSessionCollectionOptions) UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) { func (csc *MongoSessionCollectionOptions) UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
return csc.dbCollection.UpdateOne(csc.session, filter, update, opts...) return csc.dbCollection.UpdateOne(csc.sessionContext, filter, update, opts...)
} }
// UpdateMany 更新多个文档 // UpdateMany 更新多个文档
func (csc *MongoSessionCollectionOptions) UpdateMany(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) { func (csc *MongoSessionCollectionOptions) UpdateMany(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
return csc.dbCollection.UpdateMany(csc.session, filter, update, opts...) return csc.dbCollection.UpdateMany(csc.sessionContext, filter, update, opts...)
} }
// FindOne 查询一个文档 // FindOne 查询一个文档
func (csc *MongoSessionCollectionOptions) FindOne(filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult { func (csc *MongoSessionCollectionOptions) FindOne(filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult {
return csc.dbCollection.FindOne(csc.session, filter, opts...) return csc.dbCollection.FindOne(csc.sessionContext, filter, opts...)
} }
// Find 查询多个文档 // Find 查询多个文档
func (csc *MongoSessionCollectionOptions) Find(filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) { func (csc *MongoSessionCollectionOptions) Find(filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) {
return csc.dbCollection.Find(csc.session, filter, opts...) return csc.dbCollection.Find(csc.sessionContext, filter, opts...)
} }

@ -9,7 +9,8 @@ import (
type MongoSessionDatabaseOptions struct { type MongoSessionDatabaseOptions struct {
db *mongo.Client // 驱动 db *mongo.Client // 驱动
configDatabaseName string // 库名 configDatabaseName string // 库名
session mongo.SessionContext // 会话 session mongo.Session // 会话
sessionContext mongo.SessionContext // 会话上下文
dbDatabase *mongo.Database // 数据库 dbDatabase *mongo.Database // 数据库
} }
@ -18,7 +19,8 @@ func (cs *MongoSessionOptions) Database(name string, opts ...*options.DatabaseOp
return &MongoSessionDatabaseOptions{ return &MongoSessionDatabaseOptions{
db: cs.db, // 驱动 db: cs.db, // 驱动
configDatabaseName: cs.configDatabaseName, // 库名 configDatabaseName: cs.configDatabaseName, // 库名
session: cs.Session, // 会话 session: cs.session, // 会话
sessionContext: cs.sessionContext, // 会话上下文
dbDatabase: cs.db.Database(name, opts...), // 数据库 dbDatabase: cs.db.Database(name, opts...), // 数据库
} }
} }

@ -0,0 +1,16 @@
package dorm
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
)
// GetSession 获取会话
func (cs *MongoSessionOptions) GetSession(ctx context.Context) mongo.Session {
return cs.session
}
// GetSessionContext 获取会话上下文
func (cs *MongoSessionOptions) GetSessionContext(ctx context.Context) mongo.SessionContext {
return cs.sessionContext
}
Loading…
Cancel
Save