or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
golangpkg:golang/github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos@v1.4.1

docs

batch.mdclient.mdconfiguration.mdcontainer.mddatabase.mdindex.mditems.mdpartition-keys.mdpatch.mdquerying.mdthroughput.mdtypes.md
tile.json

tessl/golang-github-com-azure-azure-sdk-for-go-sdk-data-azcosmos

tessl install tessl/golang-github-com-azure-azure-sdk-for-go-sdk-data-azcosmos@1.4.1

Go SDK client library for interacting with Azure Cosmos DB SQL API

configuration.mddocs/

Container Configuration

Container configuration includes indexing policies, unique keys, conflict resolution policies, TTL settings, and analytical store configuration.

Indexing Configuration

IndexingPolicy

type IndexingPolicy struct {
    // Automatic defines if the indexing policy is automatic or manual.
    Automatic bool `json:"automatic"`
    // IndexingMode for the container.
    IndexingMode IndexingMode `json:"indexingMode,omitempty"`
    // Paths to be indexed.
    IncludedPaths []IncludedPath `json:"includedPaths,omitempty"`
    // Paths to be excluded.
    ExcludedPaths []ExcludedPath `json:"excludedPaths,omitempty"`
    // Spatial indexes.
    SpatialIndexes []SpatialIndex `json:"spatialIndexes,omitempty"`
    // Composite indexes.
    CompositeIndexes [][]CompositeIndex `json:"compositeIndexes,omitempty"`
}

Represents an indexing policy for a container. For more information see https://docs.microsoft.com/azure/cosmos-db/index-policy

IndexingMode

type IndexingMode string

Defines the supported indexing modes.

Constants:

const (
    // IndexingModeConsistent - Index is updated synchronously with a create, update or delete operation.
    IndexingModeConsistent IndexingMode = "Consistent"
    // IndexingModeNone - No index is provided.
    IndexingModeNone IndexingMode = "None"
)

IndexingModeValues

func IndexingModeValues() []IndexingMode

Returns a list of available indexing modes.

ToPtr

func (c IndexingMode) ToPtr() *IndexingMode

Returns a pointer to the IndexingMode.

IncludedPath

type IncludedPath struct {
    // Path to be included.
    Path string `json:"path"`
}

Represents a JSON path to be included in indexing.

ExcludedPath

type ExcludedPath struct {
    // Path to be excluded.
    Path string `json:"path"`
}

Represents a JSON path to be excluded from indexing.

CompositeIndex

type CompositeIndex struct {
    // Path for the index.
    Path string `json:"path"`
    // Order represents the order of the composite index.
    // For example if you want to run the query "SELECT * FROM c ORDER BY c.age asc, c.height desc",
    // then you need to make the order for "/age" "ascending" and the order for "/height" "descending".
    Order CompositeIndexOrder `json:"order"`
}

Used when queries have an ORDER BY clause with two or more properties.

CompositeIndexOrder

type CompositeIndexOrder string

Ordering values for composite indexes.

Constants:

const (
    // Ascending sort order for composite paths.
    CompositeIndexAscending CompositeIndexOrder = "ascending"
    // Descending sort order for composite paths.
    CompositeIndexDescending CompositeIndexOrder = "descending"
)

CompositeIndexOrderValues

func CompositeIndexOrderValues() []CompositeIndexOrder

Returns a list of available composite index orders.

ToPtr

func (c CompositeIndexOrder) ToPtr() *CompositeIndexOrder

Returns a pointer to the CompositeIndexOrder.

SpatialIndex

type SpatialIndex struct {
    // Path for the index.
    Path string `json:"path"`
    // SpatialType of the spatial index.
    SpatialTypes []SpatialType `json:"types"`
}

Represents a spatial index for geospatial data.

SpatialType

type SpatialType string

Defines supported spatial index types.

Constants:

const (
    // Represents a point.
    SpatialTypePoint SpatialType = "Point"
    // Represents a polygon.
    SpatialTypePolygon SpatialType = "Polygon"
    // Represents a line string.
    SpatialTypeLineString SpatialType = "LineString"
    // Represents a multi polygon.
    SpatialTypeMultiPolygon SpatialType = "MultiPolygon"
)

SpatialTypeValues

func SpatialTypeValues() []SpatialType

Returns a list of available spatial types.

ToPtr

func (c SpatialType) ToPtr() *SpatialType

Returns a pointer to the SpatialType.

IndexingDirective

type IndexingDirective string

Specifies whether a resource is to be indexed.

Constants:

const (
    // Use any pre-defined/pre-configured defaults.
    IndexingDirectiveDefault IndexingDirective = "Default"
    // Index the resource.
    IndexingDirectiveInclude IndexingDirective = "Include"
    // Do not index the resource.
    IndexingDirectiveExclude IndexingDirective = "Exclude"
)

IndexingDirectives

func IndexingDirectives() []IndexingDirective

Returns a list of available indexing directives.

ToPtr

func (c IndexingDirective) ToPtr() *IndexingDirective

Returns a pointer to the IndexingDirective.

Unique Keys

UniqueKeyPolicy

type UniqueKeyPolicy struct {
    // UniqueKeys defines the list of unique keys.
    UniqueKeys []UniqueKey `json:"uniqueKeys"`
}

Represents a unique key policy for a container. For more information see https://docs.microsoft.com/azure/cosmos-db/unique-keys

UniqueKey

type UniqueKey struct {
    // Paths define a sets of paths which must be unique for each document.
    Paths []string `json:"paths"`
}

Represents a unique key for a container. Multiple paths in a single UniqueKey form a composite unique constraint.

Conflict Resolution

ConflictResolutionPolicy

type ConflictResolutionPolicy struct {
    // Conflict resolution mode. By default, the conflict resolution mode is LastWriteWins.
    Mode ConflictResolutionMode `json:"mode"`
    // The path which is present in each item in the container to be used on LastWriteWins conflict resolution.
    // It must be an integer value.
    ResolutionPath string `json:"conflictResolutionPath,omitempty"`
    // The stored procedure path on Custom conflict.
    // The path should be the full path to the procedure
    ResolutionProcedure string `json:"conflictResolutionProcedure,omitempty"`
}

Represents a conflict resolution policy for a container. For more information see https://docs.microsoft.com/azure/cosmos-db/unique-keys

ConflictResolutionMode

type ConflictResolutionMode string

Defines the conflict resolution mode.

Constants:

const (
    // Conflict resolution that uses the highest value of the conflicting documents property values.
    ConflictResolutionModeLastWriteWins ConflictResolutionMode = "LastWriterWins"
    // Custom conflict resolution mode that requires the definition of a stored procedure.
    ConflictResolutionModeCustom ConflictResolutionMode = "Custom"
)

ConflictResolutionModeValues

func ConflictResolutionModeValues() []ConflictResolutionMode

Returns a list of available conflict resolution modes.

ToPtr

func (c ConflictResolutionMode) ToPtr() *ConflictResolutionMode

Returns a pointer to the ConflictResolutionMode.

Usage Examples

Default Indexing Policy

import "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"

// Default: Index all properties automatically
containerProperties := azcosmos.ContainerProperties{
    ID: "items",
    PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{
        Paths: []string{"/category"},
    },
    // IndexingPolicy is nil, defaults to indexing all properties
}

Custom Indexing Policy

indexingPolicy := &azcosmos.IndexingPolicy{
    Automatic:    true,
    IndexingMode: azcosmos.IndexingModeConsistent,
    IncludedPaths: []azcosmos.IncludedPath{
        {Path: "/name/?"},
        {Path: "/price/?"},
        {Path: "/category/?"},
    },
    ExcludedPaths: []azcosmos.ExcludedPath{
        {Path: "/description/*"},
        {Path: "/metadata/*"},
    },
}

containerProperties := azcosmos.ContainerProperties{
    ID:                     "items",
    PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/category"}},
    IndexingPolicy:         indexingPolicy,
}

Indexing with Wildcards

indexingPolicy := &azcosmos.IndexingPolicy{
    Automatic:    true,
    IndexingMode: azcosmos.IndexingModeConsistent,
    IncludedPaths: []azcosmos.IncludedPath{
        {Path: "/*"}, // Index all properties
    },
    ExcludedPaths: []azcosmos.ExcludedPath{
        {Path: "/largeData/*"}, // Exclude large data subtree
    },
}

No Indexing Policy

indexingPolicy := &azcosmos.IndexingPolicy{
    Automatic:    false,
    IndexingMode: azcosmos.IndexingModeNone,
}

containerProperties := azcosmos.ContainerProperties{
    ID:                     "rawData",
    PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/id"}},
    IndexingPolicy:         indexingPolicy,
}

Composite Index for ORDER BY

indexingPolicy := &azcosmos.IndexingPolicy{
    Automatic:    true,
    IndexingMode: azcosmos.IndexingModeConsistent,
    IncludedPaths: []azcosmos.IncludedPath{
        {Path: "/*"},
    },
    CompositeIndexes: [][]azcosmos.CompositeIndex{
        // For: ORDER BY c.category ASC, c.price DESC
        {
            {Path: "/category", Order: azcosmos.CompositeIndexAscending},
            {Path: "/price", Order: azcosmos.CompositeIndexDescending},
        },
        // For: ORDER BY c.name ASC, c.lastModified DESC
        {
            {Path: "/name", Order: azcosmos.CompositeIndexAscending},
            {Path: "/lastModified", Order: azcosmos.CompositeIndexDescending},
        },
    },
}

Spatial Index for Geospatial Queries

indexingPolicy := &azcosmos.IndexingPolicy{
    Automatic:    true,
    IndexingMode: azcosmos.IndexingModeConsistent,
    IncludedPaths: []azcosmos.IncludedPath{
        {Path: "/*"},
    },
    SpatialIndexes: []azcosmos.SpatialIndex{
        {
            Path:         "/location/*",
            SpatialTypes: []azcosmos.SpatialType{azcosmos.SpatialTypePoint},
        },
        {
            Path: "/region/*",
            SpatialTypes: []azcosmos.SpatialType{
                azcosmos.SpatialTypePolygon,
                azcosmos.SpatialTypeMultiPolygon,
            },
        },
    },
}

Unique Key Constraint (Single Field)

uniqueKeyPolicy := &azcosmos.UniqueKeyPolicy{
    UniqueKeys: []azcosmos.UniqueKey{
        {Paths: []string{"/email"}},
    },
}

containerProperties := azcosmos.ContainerProperties{
    ID:                     "users",
    PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/userId"}},
    UniqueKeyPolicy:        uniqueKeyPolicy,
}

Unique Key Constraint (Composite)

uniqueKeyPolicy := &azcosmos.UniqueKeyPolicy{
    UniqueKeys: []azcosmos.UniqueKey{
        // Email must be unique
        {Paths: []string{"/email"}},
        // First name + last name combination must be unique
        {Paths: []string{"/firstName", "/lastName"}},
    },
}

containerProperties := azcosmos.ContainerProperties{
    ID:                     "users",
    PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/userId"}},
    UniqueKeyPolicy:        uniqueKeyPolicy,
}

Time-to-Live (TTL) Configuration

// Set default TTL to 24 hours (86400 seconds)
defaultTTL := int32(86400)

containerProperties := azcosmos.ContainerProperties{
    ID:                     "sessions",
    PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/sessionId"}},
    DefaultTimeToLive:      &defaultTTL,
}

// Items will automatically expire after 24 hours
// Individual items can override with their own "ttl" property

TTL with Per-Item Override

// Set default TTL to 1 hour
defaultTTL := int32(3600)

containerProperties := azcosmos.ContainerProperties{
    ID:                     "cache",
    PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/key"}},
    DefaultTimeToLive:      &defaultTTL,
}

// Item with custom TTL
item := map[string]interface{}{
    "id":   "item1",
    "key":  "cache-key",
    "data": "value",
    "ttl":  7200, // Override: expire after 2 hours instead of 1
}

Disable TTL

// Set TTL to -1 to disable automatic expiration
minusOne := int32(-1)

containerProperties := azcosmos.ContainerProperties{
    ID:                     "permanent",
    PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/id"}},
    DefaultTimeToLive:      &minusOne,
}

Analytical Store Configuration

// Enable analytical store with 7-day TTL
analyticalTTL := int32(604800)

containerProperties := azcosmos.ContainerProperties{
    ID:                                 "analytics",
    PartitionKeyDefinition:             azcosmos.PartitionKeyDefinition{Paths: []string{"/category"}},
    AnalyticalStoreTimeToLiveInSeconds: &analyticalTTL,
}

Last Write Wins Conflict Resolution

conflictPolicy := &azcosmos.ConflictResolutionPolicy{
    Mode:           azcosmos.ConflictResolutionModeLastWriteWins,
    ResolutionPath: "/_ts", // Use timestamp field for conflict resolution
}

containerProperties := azcosmos.ContainerProperties{
    ID:                       "multiRegion",
    PartitionKeyDefinition:   azcosmos.PartitionKeyDefinition{Paths: []string{"/id"}},
    ConflictResolutionPolicy: conflictPolicy,
}

Custom Conflict Resolution

conflictPolicy := &azcosmos.ConflictResolutionPolicy{
    Mode:                azcosmos.ConflictResolutionModeCustom,
    ResolutionProcedure: "dbs/mydb/colls/mycoll/sprocs/resolveConflict",
}

containerProperties := azcosmos.ContainerProperties{
    ID:                       "multiRegion",
    PartitionKeyDefinition:   azcosmos.PartitionKeyDefinition{Paths: []string{"/id"}},
    ConflictResolutionPolicy: conflictPolicy,
}

Complete Container Configuration

// Comprehensive container configuration example
defaultTTL := int32(2592000) // 30 days
analyticalTTL := int32(2592000)

containerProperties := azcosmos.ContainerProperties{
    ID: "products",
    PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{
        Paths:   []string{"/category", "/subCategory"},
        Version: 2,
    },
    DefaultTimeToLive:                  &defaultTTL,
    AnalyticalStoreTimeToLiveInSeconds: &analyticalTTL,
    IndexingPolicy: &azcosmos.IndexingPolicy{
        Automatic:    true,
        IndexingMode: azcosmos.IndexingModeConsistent,
        IncludedPaths: []azcosmos.IncludedPath{
            {Path: "/*"},
        },
        ExcludedPaths: []azcosmos.ExcludedPath{
            {Path: "/largeDescription/*"},
        },
        CompositeIndexes: [][]azcosmos.CompositeIndex{
            {
                {Path: "/category", Order: azcosmos.CompositeIndexAscending},
                {Path: "/price", Order: azcosmos.CompositeIndexDescending},
            },
        },
    },
    UniqueKeyPolicy: &azcosmos.UniqueKeyPolicy{
        UniqueKeys: []azcosmos.UniqueKey{
            {Paths: []string{"/sku"}},
        },
    },
    ConflictResolutionPolicy: &azcosmos.ConflictResolutionPolicy{
        Mode:           azcosmos.ConflictResolutionModeLastWriteWins,
        ResolutionPath: "/_ts",
    },
}

database, _ := client.NewDatabase("store")
response, err := database.CreateContainer(context.Background(), containerProperties, nil)
if err != nil {
    panic(err)
}