Latest Socket ResearchMalicious Chrome Extension Performs Hidden Affiliate Hijacking.Details
Socket
Book a DemoInstallSign in
Socket

trpc.group/trpc-go/trpc-database/mongodb

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trpc.group/trpc-go/trpc-database/mongodb

Go Modules
Version
v1.0.0
Version published
Created
Source

English | 中文

tRPC-Go mongodb plugin

BK Pipelines Status

Base on community mongo, used with trpc.

mongodb client

client:                                            # Backend configuration for client calls.
  service:                                         # Configuration for the backend.
    - name: trpc.mongodb.xxx.xxx         
      target: mongodb://user:passwd@vip:port       # mongodb standard uri:mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
      timeout: 800                                 # The maximum processing time of the current request.
    - name: trpc.mongodb.xxx.xxx1         
      target: mongodb+polaris://user:passwd@polaris_name  # mongodb+polaris means that the host in the mongodb uri will perform Polaris analysis.
      timeout: 800                                        # The maximum processing time of the current request.
package main

import (
	"context"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"trpc.group/trpc-go/trpc-database/mongodb"
	"trpc.group/trpc-go/trpc-go/log"
)

// BattleFlow is battle information.
type BattleInfo struct {
	Id    string `bson:"_id,omitempty"`
	Ctime uint32 `bson:"ctime,omitempty" json:"ctime,omitempty"`
}

func (s *server) SayHello(ctx context.Context, req *pb.ReqBody, rsp *pb.RspBody) (err error) {
	proxy := mongodb.NewClientProxy("trpc.mongodb.xxx.xxx") // Your custom service name,used for monitoring, reporting and mapping configuration.

	// mongodb insert
	_, err = proxy.InsertOne(sc, "database", "table", bson.M{"_id": "key2", "value": "v2"})

	// mongodb ReplaceOne
	opts := options.Replace().SetUpsert(true)
	filter := bson.D{{"_id", "key1"}}
	_, err := proxy.ReplaceOne(ctx, "database", "table", filter, &BattleInfo{}, opts)
	if err != nil {
		log.Errorf("err=%v, data=%v", err, *battleInfo)
		return err
	}

	// mongodb FindOne
	rst := proxy.FindOne(ctx, "database", "table", bson.D{{"_id", "key1"}})
	battleInfo = &BattleInfo{}
	err = rst.Decode(battleInfo)
	if err != nil {
		return nil, err
	}

	// mongodb transaction
	err = proxy.Transaction(ctx, func(sc mongo.SessionContext) error {
		// The same proxy instance needs to be used during transaction execution.
		_, tErr := proxy.InsertOne(sc, "database", "table", bson.M{"_id": "key1", "value": "v1"})
		if tErr != nil {
			return tErr
		}
		_, tErr = proxy.InsertOne(sc, "database", "table", bson.M{"_id": "key2", "value": "v2"})
		if tErr != nil {
			return tErr
		}
		return nil
	}, nil)

	// mongodb RunCommand
	cmdDB := bson.D{}
	cmdDB = append(cmdDB, bson.E{Key: "enableSharding", Value: "dbName"})
	err = proxy.RunCommand(ctx, "admin", cmdDB).Err()
	if err != nil {
		return nil, err
	}

	cmdColl := bson.D{}
	cmdColl = append(cmdColl, bson.E{Key: "shardCollection", Value: "dbName.collectionName"})
	cmdColl = append(cmdColl, bson.E{Key: "key", Value: bson.D{{"openId", "hashed"}}})
	cmdColl = append(cmdColl, bson.E{Key: "unique", Value: false})
	cmdColl = append(cmdColl, bson.E{Key: "numInitialChunks", Value: 10})
	err = proxy.RunCommand(ctx, "admin", cmdColl).Err()
	if err != nil {
		return nil, err
	}
	// Business logic.
}

Frequently Asked Questions (FAQs)

  • Q1: How to configure ClientOptions:
  • A1: When creating a Transport, you can use WithOptionInterceptor to configure ClientOptions. You can refer to options_test.go for more information.

FAQs

Package last updated on 23 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts