Documentation
¶
Index ¶
- Constants
- Variables
- func NewClient(address string, opts ...*options.ClientOptions) (client *mongo.Client, err error)
- type BulkWrite
- type Config
- type DB
- func (db *DB) AutoMigrator(dst ...interface{}) error
- func (db *DB) BulkWrite(model interface{}) *BulkWrite
- func (db *DB) Close() (err error)
- func (db *DB) Collection(model interface{}) (tx *DB, coll *mongo.Collection)
- func (db *DB) Count(count *int64, conds ...interface{}) (tx *DB)
- func (db *DB) Create(value interface{}) (tx *DB)
- func (db *DB) Database(dbname string) *DB
- func (db *DB) Delete(conds ...interface{}) (tx *DB)
- func (db *DB) Errorf(msg interface{}, args ...interface{}) error
- func (db *DB) Find(dest interface{}, conds ...interface{}) (tx *DB)
- func (db *DB) Get(key string) (val interface{}, ok bool)
- func (db *DB) Limit(limit int) (tx *DB)
- func (db *DB) Merge(i interface{}) error
- func (db *DB) Model(value interface{}) (tx *DB)
- func (db *DB) Multiple() (tx *DB)
- func (db *DB) Offset(offset int) (tx *DB)
- func (db *DB) Omit(columns ...string) (tx *DB)
- func (db *DB) Order(key string, value int) (tx *DB)
- func (db *DB) Page(page, size int) (tx *DB)
- func (db *DB) Select(columns ...string) (tx *DB)
- func (db *DB) Session(session *Session) *DB
- func (db *DB) Set(key string, value interface{}) *DB
- func (db *DB) SetColumn(data map[string]interface{}) error
- func (db *DB) Start(dbname string, address interface{}) (err error)
- func (db *DB) Table(name string) (tx *DB)
- func (db *DB) Update(values interface{}, conds ...interface{}) (tx *DB)
- func (db *DB) Use(plugin Plugin) error
- func (db *DB) Where(query interface{}, args ...interface{}) (tx *DB)
- func (db *DB) WithContext(ctx context.Context) *DB
- type ExecuteHandle
- type Paging
- type Plugin
- type Session
- type Statement
Constants ¶
const ( MongoTagName = "BuildUpdate" MongoPrimaryKey = "_id" MongoSetOnInsert = "$setOnInsert" )
Variables ¶
var ( ErrInvalidConfig = errors.New("invalid config") // ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback` ErrInvalidTransaction = errors.New("invalid transaction") // ErrNotImplemented not implemented ErrNotImplemented = errors.New("not implemented") // ErrMissingWhereClause missing where clause ErrMissingWhereClause = errors.New("WHERE conditions required") // ErrUnsupportedRelation unsupported relations ErrUnsupportedRelation = errors.New("unsupported relations") // ErrPrimaryKeyRequired primary keys required ErrPrimaryKeyRequired = errors.New("primary key required") // ErrModelValueRequired model value required ErrModelValueRequired = errors.New("model value required") // ErrInvalidData unsupported data ErrInvalidData = errors.New("unsupported data") // ErrUnsupportedDriver unsupported driver ErrUnsupportedDriver = errors.New("unsupported driver") // ErrRegistered registered ErrRegistered = errors.New("registered") // ErrInvalidField invalid field ErrInvalidField = errors.New("invalid field") // ErrEmptySlice empty slice found ErrEmptySlice = errors.New("empty slice found") // ErrDryRunModeUnsupported dry run mode unsupported ErrDryRunModeUnsupported = errors.New("dry run mode unsupported") // ErrInvalidDB invalid db ErrInvalidDB = errors.New("invalid db") // ErrInvalidValue invalid value ErrInvalidValue = errors.New("invalid value, should be pointer to struct or slice") ErrInvalidModel = errors.New("invalid model, should be pointer to struct or struct") // ErrInvalidValueOfLength invalid values do not match length ErrInvalidValueOfLength = errors.New("invalid association values, length doesn't match") ErrSelectOnOmitsExist = errors.New("select on omits exist") ErrOmitOnSelectsExist = errors.New("omit on selects exist") )
var Schema = schema.New()
Functions ¶
func NewClient ¶
NewClient
uri实例 mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[dbname][?options]]
mongodb:// 前缀,代表这是一个Connection String
username:password@ 如果启用了鉴权,需要指定用户密码
hostX:portX 多个 mongos 的地址列表
/dbname 鉴权时,用户帐号所属的数据库
?options 指定额外的连接选项
read preference
1)primary : 主节点,默认模式,读操作只在主节点,如果主节点不可用,报错或者抛出异常。 2)primaryPreferred:首选主节点,大多情况下读操作在主节点,如果主节点不可用,如故障转移,读操作在从节点。 3)secondary:从节点,读操作只在从节点, 如果从节点不可用,报错或者抛出异常。 4)secondaryPreferred:首选从节点,大多情况下读操作在从节点,特殊情况(如单主节点架构)读操作在主节点。 5)nearest:最邻近节点,读操作在最邻近的成员,可能是主节点或者从节点。
Types ¶
type Config ¶
type Config struct {
Logger logger.Interface
Plugins map[string]Plugin
// contains filtered or unexported fields
}
Config GORM config
func (*Config) AfterInitialize ¶
type DB ¶
type DB struct {
*Config
Error error
Statement *Statement
RowsAffected int64 //操作影响的条数
// contains filtered or unexported fields
}
DB GORM DB definition
func (*DB) AutoMigrator ¶
AutoMigrator returns migrator Sparse
func (*DB) Collection ¶
func (db *DB) Collection(model interface{}) (tx *DB, coll *mongo.Collection)
func (*DB) Delete ¶
Delete 删除记录 db.Delete(&User{Id:1,name:"myname"}) 匹配 _id=1 db.Model(&User).Delete(1) 匹配 _id=1 db.Model(&User).Delete([]int{1,2,3}) 匹配 _id IN (1,2,3) db.Model(&User).Delete("name = ?","myname") 匹配 name=myname
func (*DB) Model ¶
Model specify the model you would like to run db operations
// update all users's name to `hello`
db.Model(&User{}).Update("name", "hello")
// if user's primary key is non-blank, will use it as condition, then will only update the user's name to `hello`
db.Model(&user).Update("name", "hello")
func (*DB) Offset ¶
Offset specify the number of records to skip before starting to return the records
func (*DB) SetColumn ¶
SetColumn set column's value
stmt.SetColumn("Name", "jinzhu") // Hooks Method
func (*DB) Table ¶
Table specify the table you would like to run db operations 使用TABLE 时select,order,Omit 中必须使用数据库字段名称
type ExecuteHandle ¶
type Paging ¶
type Paging struct {
// contains filtered or unexported fields
}
分页
type Session ¶
type Session struct {
DBName string
//DryRun bool
//PrepareStmt bool
NewDB bool
SkipHooks bool
//SkipDefaultTransaction bool
//DisableNestedTransaction bool
//AllowGlobalUpdate bool
//FullSaveAssociations bool
//QueryFields bool
Context context.Context
Logger logger.Interface
NowTime func() time.Time
}
Session session config when create session with Session() method
type Statement ¶
type Statement struct {
*DB
Dest interface{}
Table string
Model interface{}
ReflectValue reflect.Value
//ReflectModel reflect.Value
Omits []string // omit columns
Selects []string // selected columns
Schema *schema.Schema
Context context.Context
Clause *clause.Query
Paging *Paging
// contains filtered or unexported fields
}
Statement statement