Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrNoDocuments occurs when the operation // that created the SingleResult did not return // any document. ErrNoDocument = mongo.ErrNoDocuments // ErrDuplicatedKey occurs when there is a // unique key constraint violation. ErrDuplicatedKey = errors.New("mongo: duplicated key") )
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn struct { Username string Password string Host string Port uint /* leave blank if you already add port in host */ DB string /* database name */ Pem string /* optional */ AdminAuth bool ClientOptionsHandler func(*options.ClientOptions) }
Conn is an implementation of the Connection interface for connecting mongo db through the host and port.
func (Conn) SetupClientOptions ¶ added in v1.3.1
func (c Conn) SetupClientOptions(opt *options.ClientOptions)
type ConnSrv ¶ added in v1.4.0
type ConnSrv struct { Username string Password string Host string DB string /* database name */ Pem string /* optional */ AdminAuth bool ClientOptionsHandler func(*options.ClientOptions) }
ConnSrv is an implementation of the Connection interface for connecting mongo db through the SRV.
func (ConnSrv) SetupClientOptions ¶ added in v1.4.0
func (c ConnSrv) SetupClientOptions(opt *options.ClientOptions)
type Connection ¶
type Connection interface { DSN(cfg *tls.Config) (dns string, isPem bool) Database() string SetupClientOptions(*options.ClientOptions) }
Connection provides a definition of connect configuration.
type DB ¶
type DB interface { // RawClient returns the raw mongo client diver. RawClient() *mongo.Client // RawDatabase returns the raw mongo database diver. RawDatabase() *mongo.Database // Collection gets a handle for a collection with the given name configured with the given CollectionOptions. // // col := db.Collection("col_name") // // // insert // col.Insert(&obj).Exec(ctx) // // // find // col.Find().Equal("name", "yanun").First().Exec(ctx, &result) // // // update // upsert := true // col.Update().Set("name", "changed").Exec(ctx, upsert) // col.UpdateWith(&obj).Exec(ctx, upsert) // // // delete // col.Delete().Greater("age", 20).Exec(ctx) Collection(name string, opts ...*options.CollectionOptions) builder // Disconnect closes sockets to the topology referenced by this Client. It will // shut down any monitoring goroutines, close the idle connection pool, and will // wait until all the in use connections have been returned to the connection // pool and closed before returning. If the context expires via cancellation, // deadline, or timeout before the in use connections have returned, the in use // connections will be closed, resulting in the failure of any in flight read // or write operations. If this method returns with no errors, all connections // associated with this Client have been closed. Disconnect(ctx context.Context) error }
DB provides a definition of db behavior.
func NewDB ¶
func NewDB(ctx context.Context, c Connection) (DB, error)
NewDB initializes a sutando.DB from providing connection configuration.
// connect through host and port. db, err := sutando.NewDB(ctx, sutando.Conn{ Username: "example", Password: "example", Host: "example", Port: 27017, DB: "example", AdminAuth: true, Pem: "", ClientOptionsHandler: func(opts *options.ClientOptions) { opts.SetConnectTimeout(5 * time.Second) opts.SetTimeout(15 * time.Second) }, }) // connect through SRV. db, err := sutando.NewDB(ctx, sutando.ConnSrv{ Username: "example", Password: "example", Host: "example.mongo.net", DB: "example", AdminAuth: true, Pem: "", ClientOptionsHandler: func(opts *options.ClientOptions) { opts.SetConnectTimeout(5 * time.Second) opts.SetTimeout(15 * time.Second) }, })
func NewDBFromMongo ¶ added in v1.0.3
NewDBFromMongo initializes a sutando.DB by using the mongoDB connection from an existed mongo-driver.
var client *mongo.Client ... database := "example" db := sutando.NewDBFromMongo(ctx, client, database)
Source Files
¶
Click to show internal directories.
Click to hide internal directories.