Cosmos DB

Overview

The CosmosDb package containers two builders, used to create databases and containers.

  • CosmosDB Account (Microsoft.DocumentDb/databaseAccounts)
  • CosmosDB SQL (Microsoft.DocumentDB/databaseAccounts/sqlDatabases)
  • CosmosDB MongoDB (Microsoft.DocumentDB/databaseAccounts/mongodbDatabases)
  • CosmosDB SQL Container (Microsoft.DocumentDb/databaseAccounts/sqlDatabases/containers)

There is currently only support for document databases (the so-called “SQL API”), with support for Gremlin, Table and Cassandra data models planned.

Cosmos DB Builder

The CosmosDB builder abstracts the idea of an account and database into one. If you wish to “re-use” an already-created Cosmos DB account, use the link_to_account keyword - no account will be created and the database will be attached to the existing one.

Applies ToKeywordPurpose
DatabasenameSets the name of the database.
Databaselink_to_accountInstructs Farmer to link this database to an existing Cosmos DB account rather than creating a new one.
DatabasethroughputSets the throughput with either “provisioned throughput” or “serverless”.
Databaseadd_containersAdds a list of containers to the database.
Accountaccount_nameSets the name of the CosmosDB account.
Accountapi (not yet implemented)Sets the API and data model to use – currently defaults to “Core (SQL)".
Accountenable_public_network_accessEnables public network access for the account.
Accountdisable_public_network_accessDisables public network access for the account.
Accountconsistency_policySets the consistency policy of the database.
Accountfailover_policySets the failover policy of the database.
Accountfree_tierRegisters this server with the free pricing tier, if supported and allowed by Azure.

Cosmos Container Builder

The container builder allows you to create and configure a specific container that is attached to a cosmos database.

KeywordPurpose
nameSets the name of the container.
partition_keySets the partition key of the container.
add_indexAdds an index to the container.
exclude_pathExcludes a path from the container index.

Example

open Farmer
open Farmer.Builders

let myCosmosDb = cosmosDb {
    name "isaacsappdb"
    account_name "isaacscosmosdb"
    throughput 400<CosmosDb.RU> // or throughput Serverless
    failover_policy CosmosDb.NoFailover
    consistency_policy (CosmosDb.BoundedStaleness(500, 1000))
    add_containers [
        cosmosContainer {
            name "myContainer"
            partition_key [ "/id" ] CosmosDb.Hash
            add_index "/path" [ CosmosDb.Number, CosmosDb.Hash ]
            exclude_path "/excluded/*"
        }
    ]
}