Go SDK client library for interacting with Azure Cosmos DB SQL API
—
Container configuration includes indexing policies, unique keys, conflict resolution policies, TTL settings, and analytical store configuration.
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
type IndexingMode stringDefines 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"
)func IndexingModeValues() []IndexingModeReturns a list of available indexing modes.
func (c IndexingMode) ToPtr() *IndexingModeReturns a pointer to the IndexingMode.
type IncludedPath struct {
// Path to be included.
Path string `json:"path"`
}Represents a JSON path to be included in indexing.
type ExcludedPath struct {
// Path to be excluded.
Path string `json:"path"`
}Represents a JSON path to be excluded from indexing.
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.
type CompositeIndexOrder stringOrdering values for composite indexes.
Constants:
const (
// Ascending sort order for composite paths.
CompositeIndexAscending CompositeIndexOrder = "ascending"
// Descending sort order for composite paths.
CompositeIndexDescending CompositeIndexOrder = "descending"
)func CompositeIndexOrderValues() []CompositeIndexOrderReturns a list of available composite index orders.
func (c CompositeIndexOrder) ToPtr() *CompositeIndexOrderReturns a pointer to the CompositeIndexOrder.
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.
type SpatialType stringDefines 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"
)func SpatialTypeValues() []SpatialTypeReturns a list of available spatial types.
func (c SpatialType) ToPtr() *SpatialTypeReturns a pointer to the SpatialType.
type IndexingDirective stringSpecifies 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"
)func IndexingDirectives() []IndexingDirectiveReturns a list of available indexing directives.
func (c IndexingDirective) ToPtr() *IndexingDirectiveReturns a pointer to the IndexingDirective.
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
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.
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
type ConflictResolutionMode stringDefines 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"
)func ConflictResolutionModeValues() []ConflictResolutionModeReturns a list of available conflict resolution modes.
func (c ConflictResolutionMode) ToPtr() *ConflictResolutionModeReturns a pointer to the ConflictResolutionMode.
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
}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,
}indexingPolicy := &azcosmos.IndexingPolicy{
Automatic: true,
IndexingMode: azcosmos.IndexingModeConsistent,
IncludedPaths: []azcosmos.IncludedPath{
{Path: "/*"}, // Index all properties
},
ExcludedPaths: []azcosmos.ExcludedPath{
{Path: "/largeData/*"}, // Exclude large data subtree
},
}indexingPolicy := &azcosmos.IndexingPolicy{
Automatic: false,
IndexingMode: azcosmos.IndexingModeNone,
}
containerProperties := azcosmos.ContainerProperties{
ID: "rawData",
PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/id"}},
IndexingPolicy: indexingPolicy,
}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},
},
},
}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,
},
},
},
}uniqueKeyPolicy := &azcosmos.UniqueKeyPolicy{
UniqueKeys: []azcosmos.UniqueKey{
{Paths: []string{"/email"}},
},
}
containerProperties := azcosmos.ContainerProperties{
ID: "users",
PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/userId"}},
UniqueKeyPolicy: uniqueKeyPolicy,
}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,
}// 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// 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
}// Set TTL to -1 to disable automatic expiration
minusOne := int32(-1)
containerProperties := azcosmos.ContainerProperties{
ID: "permanent",
PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/id"}},
DefaultTimeToLive: &minusOne,
}// Enable analytical store with 7-day TTL
analyticalTTL := int32(604800)
containerProperties := azcosmos.ContainerProperties{
ID: "analytics",
PartitionKeyDefinition: azcosmos.PartitionKeyDefinition{Paths: []string{"/category"}},
AnalyticalStoreTimeToLiveInSeconds: &analyticalTTL,
}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,
}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,
}// 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)
}Install with Tessl CLI
npx tessl i tessl/golang-github-com-azure-azure-sdk-for-go-sdk-data-azcosmos