emongo

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 20 Imported by: 2

README

emongo 组件使用指南

简介

mongo-driver 进行了轻量封装,并提供了以下功能:

  • 规范了标准配置格式,提供了统一的 Load().Build() 方法。
  • 支持自定义拦截器
  • 提供了默认的 Debug 拦截器,开启 Debug 后可输出 Request、Response 至终端。
  • 提供了默认的 Metric 拦截器,开启后可采集 Prometheus 指标数据

快速上手

使用样例可参考 examples

Documentation

Overview

Copyright 2018, OpenCensus Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const PackageName = "component.emongo"

Variables

This section is empty.

Functions

func DefaultConfig

func DefaultConfig() *config

DefaultConfig 返回默认配置

func InterceptorChain

func InterceptorChain(interceptors ...Interceptor) func(oldProcess processFn) processFn

Types

type Client

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

func Connect

func Connect(ctx context.Context, opts ...*options.ClientOptions) (wc *Client, err error)

func NewClient

func NewClient(opts ...*options.ClientOptions) (*Client, error)

func (*Client) Client

func (wc *Client) Client() *mongo.Client

func (*Client) Connect

func (wc *Client) Connect(ctx context.Context) error

func (*Client) Database

func (wc *Client) Database(name string, opts ...*options.DatabaseOptions) *Database

func (*Client) Disconnect

func (wc *Client) Disconnect(ctx context.Context) error

func (*Client) ListDatabaseNames

func (wc *Client) ListDatabaseNames(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) (
	dbs []string, err error)

func (*Client) ListDatabases

func (wc *Client) ListDatabases(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) (
	dbr mongo.ListDatabasesResult, err error)

func (*Client) NewClientEncryption

func (wc *Client) NewClientEncryption(opts ...*options.ClientEncryptionOptions) (*ClientEncryption, error)

func (*Client) Ping

func (wc *Client) Ping(ctx context.Context, rp *readpref.ReadPref) error

func (*Client) StartSession

func (wc *Client) StartSession(opts ...*options.SessionOptions) (ss mongo.Session, err error)

func (*Client) UseSession

func (wc *Client) UseSession(ctx context.Context, fn func(mongo.SessionContext) error) error

func (*Client) UseSessionWithOptions

func (wc *Client) UseSessionWithOptions(ctx context.Context, opts *options.SessionOptions, fn func(mongo.SessionContext) error) error

type ClientEncryption

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

func (*ClientEncryption) Close

func (wce *ClientEncryption) Close(ctx context.Context) error

func (*ClientEncryption) CreateDataKey

func (wce *ClientEncryption) CreateDataKey(ctx context.Context, kmsProvider string, opts ...*options.DataKeyOptions) (
	id primitive.Binary, err error)

func (*ClientEncryption) Decrypt

func (wce *ClientEncryption) Decrypt(ctx context.Context, val primitive.Binary) (value bson.RawValue, err error)

func (*ClientEncryption) Encrypt

func (wce *ClientEncryption) Encrypt(ctx context.Context, val bson.RawValue, opts ...*options.EncryptOptions) (
	value primitive.Binary, err error)

type Collection

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

func (*Collection) Aggregate

func (wc *Collection) Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (res *mongo.Cursor, err error)

func (*Collection) BulkWrite

func (wc *Collection) BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (
	res *mongo.BulkWriteResult, err error)

func (*Collection) Clone

func (wc *Collection) Clone(opts ...*options.CollectionOptions) (res *mongo.Collection, err error)

func (*Collection) Collection

func (wc *Collection) Collection() *mongo.Collection

func (*Collection) CountDocuments

func (wc *Collection) CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (res int64, err error)

func (*Collection) Database

func (wc *Collection) Database() *mongo.Database

func (*Collection) DeleteMany

func (wc *Collection) DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (
	res *mongo.DeleteResult, err error)

func (*Collection) DeleteOne

func (wc *Collection) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (res *mongo.DeleteResult, err error)

func (*Collection) Distinct

func (wc *Collection) Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) (res []interface{}, err error)

func (*Collection) Drop

func (wc *Collection) Drop(ctx context.Context) error

func (*Collection) EstimatedDocumentCount

func (wc *Collection) EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (res int64, err error)

func (*Collection) Find

func (wc *Collection) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (res *mongo.Cursor, err error)

func (*Collection) FindOne

func (wc *Collection) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (res *mongo.SingleResult)

func (*Collection) FindOneAndDelete

func (wc *Collection) FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) (res *mongo.SingleResult)

func (*Collection) FindOneAndReplace

func (wc *Collection) FindOneAndReplace(ctx context.Context, filter, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) (res *mongo.SingleResult)

func (*Collection) FindOneAndUpdate

func (wc *Collection) FindOneAndUpdate(ctx context.Context, filter, update interface{}, opts ...*options.FindOneAndUpdateOptions) (res *mongo.SingleResult)

func (*Collection) Indexes

func (wc *Collection) Indexes() mongo.IndexView

func (*Collection) InsertMany

func (wc *Collection) InsertMany(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) (res *mongo.InsertManyResult, err error)

func (*Collection) InsertOne

func (wc *Collection) InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (res *mongo.InsertOneResult, err error)

func (*Collection) Name

func (wc *Collection) Name() string

func (*Collection) ReplaceOne

func (wc *Collection) ReplaceOne(ctx context.Context, filter, replacement interface{}, opts ...*options.ReplaceOptions) (res *mongo.UpdateResult, err error)

func (*Collection) UpdateMany

func (wc *Collection) UpdateMany(ctx context.Context, filter, replacement interface{}, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)

func (*Collection) UpdateOne

func (wc *Collection) UpdateOne(ctx context.Context, filter, replacement interface{}, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)

func (*Collection) Watch

func (wc *Collection) Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (res *mongo.ChangeStream, err error)

type Component

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

Component client (cmdable and config)

func (*Component) Client

func (c *Component) Client() *Client

Client returns emongo Client

type Container

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

func DefaultContainer

func DefaultContainer() *Container

DefaultContainer 返回默认Container

func Load

func Load(key string) *Container

Load 载入配置,初始化Container

func (*Container) Build

func (c *Container) Build(options ...Option) *Component

Build 构建Container

type Database

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

func (*Database) Client

func (wd *Database) Client() *Client

func (*Database) Collection

func (wd *Database) Collection(name string, opts ...*options.CollectionOptions) *Collection

func (*Database) Database

func (wd *Database) Database() *mongo.Database

func (*Database) Drop

func (wd *Database) Drop(ctx context.Context) error

func (*Database) ListCollections

func (wd *Database) ListCollections(ctx context.Context, filter interface{}, opts ...*options.ListCollectionsOptions) (
	cur *mongo.Cursor, err error)

func (*Database) Name

func (wd *Database) Name() string

func (*Database) ReadConcern

func (wd *Database) ReadConcern() *readconcern.ReadConcern

func (*Database) ReadPreference

func (wd *Database) ReadPreference() *readpref.ReadPref

func (*Database) RunCommand

func (wd *Database) RunCommand(ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) (res *mongo.SingleResult)

func (*Database) WriteConcern

func (wd *Database) WriteConcern() (res *writeconcern.WriteConcern)

type Interceptor

type Interceptor func(oldProcessFn processFn) (newProcessFn processFn)

type Option

type Option func(c *Container)

func WithDSN

func WithDSN(dsn string) Option

WithDSN 注入DSN配置

func WithDebug added in v0.1.5

func WithDebug(debug bool) Option

WithDebug 注入Debug配置

func WithInterceptor

func WithInterceptor(interceptors ...Interceptor) Option

WithInterceptor 注入拦截器

type Session

type Session struct {
	mongo.Session
	// contains filtered or unexported fields
}

func (*Session) AbortTransaction

func (ws *Session) AbortTransaction(ctx context.Context) error

func (*Session) AdvanceClusterTime

func (ws *Session) AdvanceClusterTime(br bson.Raw) error

func (*Session) AdvanceOperationTime

func (ws *Session) AdvanceOperationTime(pt *primitive.Timestamp) error

func (*Session) ClusterTime

func (ws *Session) ClusterTime() (raw bson.Raw)

func (*Session) CommitTransaction

func (ws *Session) CommitTransaction(ctx context.Context) error

func (*Session) EndSession

func (ws *Session) EndSession(ctx context.Context)

func (*Session) OperationTime

func (ws *Session) OperationTime() (ts *primitive.Timestamp)

func (*Session) StartTransaction

func (ws *Session) StartTransaction(topts ...*options.TransactionOptions) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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