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)
  • CosmosDB Graph databases (Microsoft.DocumentDb/databaseAccounts/gremlinDatabases)
  • CosmosDB Graph containers (Microsoft.DocumentDb/databaseAccounts/gremlinDatabases/graphs)

There is currently support for document databases (the so-called “SQL API”) and Gremlin graphs. Support for 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.
AccountkindSets 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.
gremlin_graphMarks the container as graph (must be used with an account of kind DatabaseKind.Gremlin)

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))
    //kind DatabaseKind.Gremlin //Create a gremlin enabled account
    add_containers [
        cosmosContainer {
            name "myContainer"
            partition_key [ "/id" ] CosmosDb.Hash
            add_index "/path" [ CosmosDb.Number, CosmosDb.Hash ]
            exclude_path "/excluded/*"
            //gremlin_graph //Mark this container to be a graph
        }
    ]
}