cosmosdb

package
v0.0.0-...-d9f4117 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Example (CosmosDBOperations)
var groupName = config.GenerateGroupName("CosmosDB")
config.SetGroupName(groupName)

ctx := context.Background()
defer resources.Cleanup(ctx)

_, err := resources.CreateGroup(ctx, config.GroupName())
if err != nil {
	util.LogAndPanic(err)
}

_, err = CreateDatabaseAccount(ctx, accountName)
if err != nil {
	util.LogAndPanic(fmt.Errorf("cannot create database account: %+v", err))
}
util.PrintAndLog("database account created")

keys, err := ListKeys(ctx, accountName)
if err != nil {
	util.LogAndPanic(fmt.Errorf("cannot list keys: %+v", err))
}
util.PrintAndLog("keys listed")

host := fmt.Sprintf("%s.documents.azure.com", accountName)
collection := "Packages"

session, err := mongodb.NewMongoDBClientWithCredentials(accountName, *keys.PrimaryMasterKey, host)
if err != nil {
	util.LogAndPanic(fmt.Errorf("cannot get mongoDB session: %+v", err))
}
util.PrintAndLog("got mongoDB session")

GetCollection(session, accountName, collection)
util.PrintAndLog("got collection")

err = InsertDocument(
	session,
	accountName,
	collection,
	map[string]interface{}{
		"fullname":      "react",
		"description":   "A framework for building native apps with React.",
		"forksCount":    11392,
		"StarsCount":    48794,
		"LastUpdatedBy": "shergin",
	})
if err != nil {
	util.LogAndPanic(fmt.Errorf("cannot insert document: %v", err))
}
util.PrintAndLog("inserted document")

doc, err := GetDocument(
	session,
	accountName,
	collection,
	bson.M{"fullname": "react"})
if err != nil {
	util.LogAndPanic(fmt.Errorf("cannot get document: %v", err))
}
util.PrintAndLog("got document")
util.PrintAndLog(fmt.Sprintf("document description: %s", doc["description"]))

err = UpdateDocument(
	session,
	accountName,
	collection,
	doc["_id"].(bson.ObjectId),
	bson.M{
		"$set": bson.M{
			"fullname": "react-native",
		},
	})
if err != nil {
	util.LogAndPanic(fmt.Errorf("cannot update document: %v", err))
}
util.PrintAndLog("update document")

err = DeleteDocument(session, accountName, collection, doc["_id"].(bson.ObjectId))
if err != nil {
	util.LogAndPanic(fmt.Errorf("cannot delete document: %v", err))
}
util.PrintAndLog("delete document")
Output:

database account created
keys listed
got mongoDB session
got collection
inserted document
got document
document description: A framework for building native apps with React.
update document
delete document

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDatabaseAccount

func CreateDatabaseAccount(ctx context.Context, accountName string) (dba documentdb.DatabaseAccount, err error)

CreateDatabaseAccount creates or updates an Azure Cosmos DB database account.

func DeleteDocument

func DeleteDocument(session *mgo.Session, database, collectionName string, id bson.ObjectId) error

DeleteDocument deletes the mongoDB document with the specified ID

func GetCollection

func GetCollection(session *mgo.Session, database, collectionName string) *mgo.Collection

GetCollection gets a mongoDB collection

func GetDocument

func GetDocument(session *mgo.Session, database, collectionName string, query bson.M) (result map[string]interface{}, err error)

GetDocument gets a mongoDB document in the specified collection

func InsertDocument

func InsertDocument(session *mgo.Session, database, collectionName string, document map[string]interface{}) error

InsertDocument inserts a mongoDB document in the specified collection

func ListKeys

ListKeys gets the keys for a Azure Cosmos DB database account.

func UpdateDocument

func UpdateDocument(session *mgo.Session, database, collectionName string, id bson.ObjectId, change bson.M) error

UpdateDocument updates the mongoDB document with the specified ID

Types

This section is empty.

Jump to

Keyboard shortcuts

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