项目作者: matthewhartstonge

项目描述 :
Extend mongo-go-driver with feature detection!
高级语言: Go
项目地址: git://github.com/matthewhartstonge/mongo-features.git
创建时间: 2020-09-01T01:11:33Z
项目社区:https://github.com/matthewhartstonge/mongo-features

开源协议:Apache License 2.0

下载


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).

Session detection requires the consumer to have clusterMonitor mongodb permissions.
Sessions should work on a single node, but there is an in progress fix for this.

tl;dr

Refer: _examples/tldr

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/matthewhartstonge/mongo-features"
  6. "go.mongodb.org/mongo-driver/mongo"
  7. )
  8. func main() {
  9. ctx := context.Background()
  10. client, err := mongo.Connect(ctx)
  11. if err != nil {
  12. panic(err)
  13. }
  14. featureSet := features.New(client)
  15. fmt.Printf("I am running on mongo major version: %d\n", featureSet.MongoVersion.Major())
  16. fmt.Printf("I am running on mongo minor version: %d\n", featureSet.MongoVersion.Minor())
  17. fmt.Printf("I am running on mongo version: %s\n", featureSet.MongoVersion.String())
  18. fmt.Printf("I can perform server sessions: %t\n", featureSet.HasSessions)
  19. fmt.Printf("I can perform multi-document acid transactions: %t\n", featureSet.HasTransactions)
  20. }

Made for Plug’n’Play

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

Refer: _examples/plugnplay

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. feat "github.com/matthewhartstonge/mongo-features"
  6. "go.mongodb.org/mongo-driver/mongo"
  7. )
  8. type Store struct {
  9. db *mongo.Database
  10. *feat.Features
  11. }
  12. type thing struct {
  13. Name string
  14. }
  15. func (s *Store) CreateThing(ctx context.Context, thing1 thing) {
  16. if s.HasSessions {
  17. sess, err := s.db.Client().StartSession()
  18. if err != nil {
  19. panic(err)
  20. }
  21. ctx = mongo.NewSessionContext(ctx, sess)
  22. defer sess.EndSession(ctx)
  23. }
  24. // like, totally put your transact-able actions in here...
  25. }
  26. func New() *Store {
  27. ctx := context.Background()
  28. client, err := mongo.Connect(ctx)
  29. if err != nil {
  30. panic(err)
  31. }
  32. testDb := client.Database("test")
  33. return &Store{
  34. db: testDb,
  35. Features: feat.New(client),
  36. }
  37. }
  38. func main() {
  39. store := New()
  40. fmt.Printf("My datastore is running on mongo major version: %d\n", store.MongoVersion.Major())
  41. fmt.Printf("My datastore is running on mongo minor version: %d\n", store.MongoVersion.Minor())
  42. fmt.Printf("My datastore is running on mongo version: %s\n", store.MongoVersion.String())
  43. fmt.Printf("My datastore can perform server sessions: %t\n", store.HasSessions)
  44. fmt.Printf("My datastore can perform multi-document acid transactions: %t\n", store.HasTransactions)
  45. }