This project is a wrapper around the Cosmos DB v4 .NET SDK to make it a bit more friendly to the F# language.
Install via NuGet:
dotnet add package FSharp.CosmosDb
Or using Paket:
dotnet paket add FSharp.CosmosDb
All operations will return an AsyncSeq
via FSharp.Control.AsyncSeq that contains the data fetched, data inserted or data updated.
open FSharp.CosmosDb
let connStr = "..."
let insertUsers data =
connStr
|> Cosmos.fromConnectionString
|> Cosmos.database "UserDb"
|> Cosmos.container "UserContainer"
|> Cosmos.insertMany<User> data
|> Cosmos.execAsync
open FSharp.CosmosDb
let connStr = "..."
let insertUsers data =
connStr
|> Cosmos.fromConnectionString
|> Cosmos.database "UserDb"
|> Cosmos.container "UserContainer"
|> Cosmos.upsertMany<User> data
|> Cosmos.execAsync
open FSharp.CosmosDb
let connStr = "..."
let updateUser id partitionKey =
connStr
|> Cosmos.fromConnectionString
|> Cosmos.database "UserDb"
|> Cosmos.container "UserContainer"
|> Cosmos.update<User> id partitionKey (fun user -> { user with IsRegistered = true })
|> Cosmos.execAsync
open FSharp.CosmosDb
let host = "https://..."
let key = "..."
let findUsers() =
host
|> Cosmos.host
|> Cosmos.connect key
|> Cosmos.database "UserDb"
|> Cosmos.container "UserContainer"
|> Cosmos.query "SELECT u.FirstName, u.LastName FROM u WHERE u.LastName = @name"
|> Cosmos.parameters [ "@name", box "Powell" ]
|> Cosmos.execAsync<User>
[<EntryPoint>]
let main argv =
async {
let users = findUsers()
do! users
|> AsyncSeq.iter (fun u -> printfn "%s %s" u.FirstName u.LastName)
return 0
} |> Async.RunSynchronously
open FSharp.CosmosDb
let connStr = "..."
let updateUser id partitionKey =
connStr
|> Cosmos.fromConnectionString
|> Cosmos.database "UserDb"
|> Cosmos.container "UserContainer"
|> Cosmos.deleteItem id partitionKey
|> Cosmos.execAsync
open FSharp.CosmosDb
let connStr = "..."
connStr
|> Cosmos.container "ContainerName"
|> Cosmos.deleteContainer
|> Cosmos.execAsync
|> Async.Ignore
Also part of this repo is a F# Analyzer for use from the CLI or in Ionide.
@
for parameter name
Connection information can be provided as either environment variables or using an appsettings.json
/appsettings.Development.json
file.
The analyzer will look for the following environment variables:
FSHARP_COSMOS_CONNSTR
-> A full connection string to Cosmos DBFSHARP_COSMOS_HOST
& FSHARP_COSMOS_KEY
-> The URI endpoint and access keyThe FSHARP_COSMOS_CONNSTR
will take precedence if both sets of environment variables are provided
The analyzer will look for a file matching appsettings.json
or appsettings.Development.json
in either the workspace root of the VS Code instance or relative to the file being parsed. The file is expected to have the following JSON structure in it:
{
"CosmosConnection": {
"ConnectionString": "",
"Host": "",
"Key": ""
}
}
If CosmosConnection.ConnectionString
exists, it will be used, otherwise it will use the CosmosConnection.Host
and CosmosConnection.Key
to connect.
paket add FSharp.CosmosDb.Analyzer --group Analyzers
Add the following settings (globally or in the workspace):
{
"FSharp.enableAnalyzers": true,
"FSharp.analyzersPath": ["./packages/analyzers"]
}