features

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

README

mongo-features

PkgGoDev Go Report Card

The official mongo driver is great - but it's a bit more low level than the mgo community driver, so we need to check whether certain mongo features can be used.

Enter mongo (feat. features).

tl;dr

Refer: _examples/tldr

package main

import (
	"context"
	"fmt"

	"github.com/matthewhartstonge/mongo-features"
	"go.mongodb.org/mongo-driver/mongo"
)

func main() {
	ctx := context.Background()
	client, err := mongo.Connect(ctx)
	if err != nil {
		panic(err)
	}

	featureSet := features.New(client)
	fmt.Printf("I am running on mongo major version: %d\n", featureSet.MongoVersion.Major())
	fmt.Printf("I am running on mongo minor version: %d\n", featureSet.MongoVersion.Minor())
	fmt.Printf("I am running on mongo version: %s\n", featureSet.MongoVersion.String())
	fmt.Printf("I can perform server sessions: %t\n", featureSet.HasSessions)
	fmt.Printf("I can perform multi-document acid transactions: %t\n", featureSet.HasTransactions)
}

Made for Plug'n'Play

When creating your own mongo datastore API, you can plug this bad boy into your structs:

Refer: _examples/plugnplay

package main

import (
	"context"
	"fmt"

	feat "github.com/matthewhartstonge/mongo-features"
	"go.mongodb.org/mongo-driver/mongo"
)

type Store struct {
	db *mongo.Database

	*feat.Features
}

type thing struct {
	Name string
}

func (s *Store) CreateThing(ctx context.Context, thing1 thing) {
	if s.HasSessions {
		sess, err := s.db.Client().StartSession()
		if err != nil {
			panic(err)
		}
		ctx = mongo.NewSessionContext(ctx, sess)
		defer sess.EndSession(ctx)
	}

	// like, totally put your transact-able actions in here...
}

func New() *Store {
	ctx := context.Background()
	client, err := mongo.Connect(ctx)
	if err != nil {
		panic(err)
	}

	testDb := client.Database("test")
	return &Store{
		db:       testDb,
		Features: feat.New(client),
	}
}

func main() {
	store := New()
	fmt.Printf("My datastore is running on mongo major version: %d\n", store.MongoVersion.Major())
	fmt.Printf("My datastore is running on mongo minor version: %d\n", store.MongoVersion.Minor())
	fmt.Printf("My datastore is running on mongo version: %s\n", store.MongoVersion.String())
	fmt.Printf("My datastore can perform server sessions: %t\n", store.HasSessions)
	fmt.Printf("My datastore can perform multi-document acid transactions: %t\n", store.HasTransactions)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Features

type Features struct {
	// HasSessions returns whether the mongo connected to supports the use of
	// server sessions via the mongo driver `mongo.NewSession`.
	//
	// Server sessions are only supported on a mongo replica/sharded set enabled
	// via the --replSet switch on `mongod`.
	//
	// Refer: https://docs.mongodb.com/manual/reference/server-sessions/
	HasSessions bool

	// HasTransactions returns whether the mongo connected to supports Distributed
	// Transactions/Multi-Document Transactions.
	//
	// Refer: https://docs.mongodb.com/manual/core/transactions/
	HasTransactions bool

	// MongoVersion returns the semver version of mongo connected to.
	MongoVersion *semver.Version
}

Features provides a selection of boolean switches for detection of mongodb features.

func New

func New(client *mongo.Client) *Features

New expects a client with a valid connection to mongoDB.

Jump to

Keyboard shortcuts

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