cosmosdb

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// StorageKeyTrimPaddingLen is the length of the padding when key is trimed.
	StorageKeyTrimPaddingLen = 17
	// The resource group name storage key length.
	ResourceGroupNameMaxStorageKeyLen = 64
	// The resource identifier storage key limit.
	ResourceIdMaxStorageKeyLen = 157
)
View Source
const (
	// PartitionKeyName is the property used for partitioning.
	PartitionKeyName = "/partitionKey"
)

Variables

View Source
var (
	ErrInvalidKey = errors.New("key includes invalid character")
)

Functions

func CombineStorageKeys

func CombineStorageKeys(keys ...string) (string, error)

CombineStorageKeys combines multiple storage keys into one, returning an error if any of the keys contain the key delimiter.

func EscapedStorageKey

func EscapedStorageKey(key string) string

EscapedStorageKey escapes a string so that it can be used as a storage key.

func GenerateCosmosDBKey

func GenerateCosmosDBKey(id resources.ID) (string, error)

GenerateCosmosDBKey takes in an ID object and returns a string and an error if the resource group or resource type and name fail to normalize.

func GetPartitionKey

func GetPartitionKey(id resources.ID) (string, error)

GetPartitionKey returns a partition key based on the given ID, normalizing the subscription ID and normalizing the plane namespace if the ID is UCP-qualified. Examples: /planes/radius/local/... - Partition Key: radius/local subscriptions/{guid}/... - Partition Key: {guid}

func NormalizeLetterOrDigitToUpper

func NormalizeLetterOrDigitToUpper(s string) string

NormalizeLetterOrDigitToUpper takes in a string and returns a new string with all letters and digits converted to uppercase.

func NormalizeStorageKey

func NormalizeStorageKey(storageKey string, maxLength int) (string, error)

NormalizeStorageKey takes a storage key string and a maximum length and returns a normalized string with the maximum length, or an error if the maximum length is exceeded.

func NormalizeSubscriptionID

func NormalizeSubscriptionID(subscriptionID string) string

NormalizeSubscriptionID normalizes subscription id.

func TrimStorageKey

func TrimStorageKey(storageKey string, maxLength int) (string, error)

TrimStorageKey checks if the storage key is too short, contains invalid characters, or exceeds the maximum length, and returns a trimmed version of the key or an error if any of these conditions are met.

Types

type ConnectionOptions

type ConnectionOptions struct {
	// Url represents the url of cosmosdb endpoint.
	Url string
	// DatabaseName represents the database name to connect.
	DatabaseName string
	// CollectionName represents the collection name in DataBaseName
	CollectionName string
	// DefaultQueryItemCount represents the maximum number of items for query.
	DefaultQueryItemCount int
	// CollectionThroughput represents shared throughput database share the throughput (RU/s) allocated to that database.
	CollectionThroughput int

	// MasterKey is the key string for CosmosDB connection.
	MasterKey string
}

ConnectionOptions represents connection info to connect CosmosDB

type CosmosDBStorageClient

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

CosmosDBStorageClient implements CosmosDB stroage client.

func NewCosmosDBStorageClient

func NewCosmosDBStorageClient(options *ConnectionOptions) (*CosmosDBStorageClient, error)

NewCosmosDBStorageClient creates a new CosmosDBStorageClient instance using the provided ConnectionOptions and returns it, or an error if the ConnectionOptions are invalid.

func (*CosmosDBStorageClient) Delete

func (c *CosmosDBStorageClient) Delete(ctx context.Context, id string, opts ...store.DeleteOptions) error

Delete parses the given ID, gets the partition key, generates the CosmosDB key, and deletes the document from the CosmosDB collection. It returns an error if the document is not found.

func (*CosmosDBStorageClient) Get

Get retrieves an object using CosmosDBStorageClient using the provided ID and optional GetOptions. It returns an error if the object is not found or if an error occurs while retrieving the object.

func (*CosmosDBStorageClient) Init

Init checks if the database and collection exist, and if not, creates them. It returns an error if either of the checks or creations fail.

func (*CosmosDBStorageClient) Query

Query builds and executes a CosmosDB query based on the provided store.Query and returns the results.

func (*CosmosDBStorageClient) Save

Save saves an object to the CosmosDB storage, returning an error if one occurs. If an ETag is provided, an error is returned if the ETag does not match the existing ETag.

type ResourceEntity

type ResourceEntity struct {
	// CosmosDB system-related properties.
	// ID represents the primary key.
	ID string `json:"id"`
	// ETag represents an etag required for optimistic concurrency control.
	ETag string `json:"_etag"`
	// Self represents the unique addressable URI for the resource.
	Self string `json:"_self"`
	// Timestamp represents the last updated timestamp of the resource.
	UpdatedTime int `json:"_ts"`

	// ResourceID represents fully qualified resource id.
	ResourceID string `json:"resourceId"`
	// RootScope represents root scope such as subscription id.
	RootScope string `json:"rootScope"`
	// ResourceGroup represents fully qualified resource scope.
	ResourceGroup string `json:"resourceGroup"`
	// PartitionKey represents the key used for partitioning.
	PartitionKey string `json:"partitionKey"`
	// Entity represents the resource metadata.
	Entity any `json:"entity"`
}

ResourceEntity represents the default envelope model to store resource metadata.

Jump to

Keyboard shortcuts

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