database

package module
v2.0.0-...-6e51b18 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: GPL-3.0 Imports: 12 Imported by: 0

README

PaySuper MongoDB Driver

Build Status codecov go report

Installation

Use go get.

go get gopkg.in/paysuper/paysuper-database-mongo.v2

Then import the validator package into your own code.

import "gopkg.in/paysuper/paysuper-database-mongo.v2"

Usage

import (
    "context"
    "go.mongodb.org/mongo-driver/bson"
    database "gopkg.in/paysuper/paysuper-database-mongo.v2"
    "log"
)

func main() {
    opts := []database.Option{
        database.Dsn("mongodb://localhost:27017/example_db"),
    }
    mongodb, err := database.NewDatabase(opts...)
    if err != nil {
        log.Fatal("MongoDB connection failed")
    }

    docs := []interface{}{
        &Example{
            String: "value1",
            Int:    1,
            Float:  11.11,
        },
        &Example{
            String: "value2",
            Int:    2,
            Float:  22.22,
        },
        &Example{
            String: "value3",
            Int:    2,
            Float:  33.33,
        },
    }
    
    _, err := mongodb.Collection(collectionName).InsertMany(context.Background(), docs)
    
    if err != nil {
        log.Fatal("Data insert failed")
    }
}

More examples available in examples directory

Environment variables:

Name Required Default Description
MONGO_DIAL_TIMEOUT - 10 MongoBD dial timeout in seconds
MONGO_DSN true - MongoBD DSN connection string

Contributing

We feel that a welcoming community is important and we ask that you follow PaySuper's Open Source Code of Conduct in all interactions with the community.

PaySuper welcomes contributions from anyone and everyone. Please refer to each project's style and contribution guidelines for submitting patches and additions. In general, we follow the "fork-and-pull" Git workflow.

The master branch of this repository contains the latest stable release of this component.

Documentation

Index

Constants

View Source
const (
	CodeDuplicateKeyErrorCollection = 11000
)

Variables

View Source
var (
	ErrorSessionNotInit   = errors.New("database session not init")
	ErrorDatabaseNotFound = errors.New("database name not found in DSN connection string")
)
View Source
var (
	ErrorProtocolNotFound = errors.New("protocol not found")
	ErrorHostsNotFound    = errors.New("hosts not found")
)

Functions

func GetObjectIDCounter

func GetObjectIDCounter(objectID primitive.ObjectID) int64

func IsDuplicate

func IsDuplicate(err error) bool

func ToSortOption

func ToSortOption(fields []string) interface{}

Types

type Auth

type Auth struct {
	User     string
	Password string
}

type Collection

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

func (*Collection) Aggregate

func (m *Collection) Aggregate(
	ctx context.Context,
	pipeline interface{},
	opts ...*options.AggregateOptions,
) (CursorInterface, error)

func (*Collection) BulkWrite

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

func (*Collection) CountDocuments

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

func (*Collection) DeleteMany

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

func (*Collection) DeleteOne

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

func (*Collection) Distinct

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

func (*Collection) Find

func (m *Collection) Find(
	ctx context.Context,
	filter interface{},
	opts ...*options.FindOptions,
) (CursorInterface, error)

func (*Collection) FindOne

func (m *Collection) FindOne(
	ctx context.Context,
	filter interface{},
	opts ...*options.FindOneOptions,
) SingleResultInterface

func (*Collection) FindOneAndDelete

func (m *Collection) FindOneAndDelete(
	ctx context.Context,
	filter interface{},
	opts ...*options.FindOneAndDeleteOptions,
) SingleResultInterface

func (*Collection) FindOneAndReplace

func (m *Collection) FindOneAndReplace(
	ctx context.Context,
	filter interface{},
	replacement interface{},
	opts ...*options.FindOneAndReplaceOptions,
) SingleResultInterface

func (*Collection) FindOneAndUpdate

func (m *Collection) FindOneAndUpdate(
	ctx context.Context,
	filter interface{},
	update interface{},
	opts ...*options.FindOneAndUpdateOptions,
) SingleResultInterface

func (*Collection) Indexes

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

func (*Collection) InsertMany

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

func (*Collection) InsertOne

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

func (*Collection) ReplaceOne

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

func (*Collection) UpdateMany

func (m *Collection) UpdateMany(
	ctx context.Context,
	filter interface{},
	update interface{},
	opts ...*options.UpdateOptions,
) (*mongo.UpdateResult, error)

func (*Collection) UpdateOne

func (m *Collection) UpdateOne(
	ctx context.Context,
	filter interface{},
	update interface{},
	opts ...*options.UpdateOptions,
) (*mongo.UpdateResult, error)

type CollectionInterface

type CollectionInterface interface {
	Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (CursorInterface, error)
	CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
	DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
	DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
	Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)
	Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (CursorInterface, error)
	FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) SingleResultInterface
	FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) SingleResultInterface
	FindOneAndReplace(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) SingleResultInterface
	FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) SingleResultInterface
	InsertMany(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
	InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
	ReplaceOne(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
	UpdateMany(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
	Indexes() mongo.IndexView
}

type Cursor

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

func (*Cursor) All

func (m *Cursor) All(ctx context.Context, results interface{}) error

func (*Cursor) Close

func (m *Cursor) Close(ctx context.Context) error

func (*Cursor) Decode

func (m *Cursor) Decode(val interface{}) error

func (*Cursor) Err

func (m *Cursor) Err() error

func (*Cursor) ID

func (m *Cursor) ID() int64

func (*Cursor) Next

func (m *Cursor) Next(ctx context.Context) bool

func (*Cursor) TryNext

func (m *Cursor) TryNext(ctx context.Context) bool

type CursorInterface

type CursorInterface interface {
	All(ctx context.Context, results interface{}) error
	Close(ctx context.Context) error
	Decode(val interface{}) error
	Err() error
	ID() int64
	Next(ctx context.Context) bool
	TryNext(ctx context.Context) bool
}

type DSN

type DSN struct {
	Dsn      string
	Protocol string
	*Auth
	Hosts    []*Host
	Database string
	Options  map[string]string
}

func NewDSN

func NewDSN(dsn string) (*DSN, error)

type Host

type Host struct {
	Host string
	Port string
}

type Option

type Option func(*Options)

func Context

func Context(ctx context.Context) Option

func Dsn

func Dsn(dsn string) Option

func Mode

func Mode(mode string) Option

type Options

type Options struct {
	Dsn     string `envconfig:"MONGO_DSN" default:"mongodb://localhost:27017/test"`
	Mode    string `envconfig:"MONGO_MODE" default:"primary"`
	Context context.Context
}

func (Options) String

func (c Options) String() (s string)

type SingleResult

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

func (*SingleResult) Decode

func (m *SingleResult) Decode(v interface{}) error

func (*SingleResult) DecodeBytes

func (m *SingleResult) DecodeBytes() (bson.Raw, error)

func (*SingleResult) Err

func (m *SingleResult) Err() error

type SingleResultInterface

type SingleResultInterface interface {
	Decode(v interface{}) error
	DecodeBytes() (bson.Raw, error)
	Err() error
}

type Source

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

func (*Source) Close

func (s *Source) Close() error

func (*Source) Collection

func (s *Source) Collection(name string) CollectionInterface

func (*Source) Drop

func (s *Source) Drop() error

func (*Source) Open

func (s *Source) Open(conn *Options) error

func (*Source) Ping

func (s *Source) Ping(ctx context.Context) error

type SourceInterface

type SourceInterface interface {
	Close() error
	Ping(ctx context.Context) error
	Drop() error
	Collection(name string) CollectionInterface
}

func NewDatabase

func NewDatabase(options ...Option) (SourceInterface, error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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