Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

```text
/
├── src/Cosmos.Gremlin/ – Azure Cosmos DB for Apache Gremlin on top of FSharp.Gremlin (FSharp.Azure.Cosmos.Gremlin)
│ └── CosmosGremlinConnection.fs – connection settings of an account in Azure or of the emulator
├── src/Cosmos/ – main library (FSharp.Azure.Cosmos)
│ ├── SubStatusCodes.fs – documented Cosmos DB sub-status codes
│ ├── Cosmos.fs – core types and container extensions
Expand All @@ -25,10 +27,19 @@
│ ├── CosmosResponse.fs – response type wrappers
│ ├── TaskSeq.fs – TaskSeq integration
│ └── UniqueKey.fs – unique key helpers
├── src/Gremlin/ – Gremlin traversals over F# records, without a provider (FSharp.Gremlin)
│ ├── Attributes.fs – `[<Label>]` and `[<PartitionKey>]` for the records of a graph
│ ├── VertexProperty.fs – a vertex property with its meta-properties
│ ├── GraphModelException.fs – the refusal of a record that a graph cannot store
│ ├── ElementModel.fs – how a graph stores a record: label, id, partition key, properties and their shapes
│ ├── SerializerProfile.fs – the options of System.Text.Json and FSharp.SystemTextJson that the model reads
│ └── GraphModel.fs – `GraphModel`: builds and keeps the model of each record type
├── src/Shared/ – source files that every F# project under src and tests compiles
│ └── ValueCollections.fs – `Seq`, `List` and `Array` functions that return `voption` and struct tuples
├── tests/Cosmos.Gremlin.Tests/ – MSTest tests of FSharp.Azure.Cosmos.Gremlin
├── tests/Cosmos.Tests/ – MSTest integration test project
├── tests/Cosmos.Tests.Infrastructure/ – shared test fixtures, emulator settings and assertion helpers
├── tests/Gremlin.Tests/ – MSTest unit tests of FSharp.Gremlin, no emulator needed
├── build/ – FAKE build scripts
└── docsSrc/ – FSharp.Formatting documentation source
```
Expand Down
12 changes: 12 additions & 0 deletions .github/instructions/test-layout.instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,16 @@ applyTo: "tests/**/*.fs"
| `Json/` | `JsonConverter`, `TimeSpan` |
| root | `IcedTasksProbe`, `QuerySemantics`, `ResponseMessage`, `Validation`, `ValueCollections` (one file for each category) |

## Groups of `tests/Gremlin.Tests`

| Folder | Test categories |
|---|---|
| `GraphModel/` | `GraphModel`, `SerializerProfile` |

## Groups of `tests/Cosmos.Gremlin.Tests`

| Folder | Test categories |
|---|---|
| root | `Connection` (one file) |

Each name in the second column is the name of a category attribute without the `TestCategoryAttribute` suffix.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageVersion Include="FSharp.Control.Reactive" Version="6.1.2" />
<PackageVersion Include="FSharp.Control.TaskSeq" Version="1.0.0" />
<PackageVersion Include="FSharp.Core" Version="10.0.100" />
<PackageVersion Include="FSharp.SystemTextJson" Version="1.4.36" />
<PackageVersion Include="FsToolkit.ErrorHandling" Version="5.0.0" />
<PackageVersion Include="IcedTasks" Version="0.11.10-beta001" />
</ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion FSharp.Azure.Cosmos.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
"solution": {
"path": "FSharp.Azure.Cosmos.slnx",
"projects": [
"src\\Cosmos.Gremlin\\FSharp.Azure.Cosmos.Gremlin.fsproj",
"src\\Cosmos\\FSharp.Azure.Cosmos.fsproj",
"src\\Gremlin\\FSharp.Gremlin.fsproj",
"tests\\Cosmos.Gremlin.Tests\\FSharp.Azure.Cosmos.Gremlin.Tests.fsproj",
"tests\\Cosmos.Tests.Infrastructure\\FSharp.Azure.Cosmos.Tests.Infrastructure.fsproj",
"tests\\Cosmos.Tests\\FSharp.Azure.Cosmos.Tests.fsproj"
"tests\\Cosmos.Tests\\FSharp.Azure.Cosmos.Tests.fsproj",
"tests\\Gremlin.Tests\\FSharp.Gremlin.Tests.fsproj"
]
}
}
4 changes: 4 additions & 0 deletions FSharp.Azure.Cosmos.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
<File Path="README.md" />
</Folder>
<Project Path="build/build.fsproj" />
<Project Path="src/Cosmos.Gremlin/FSharp.Azure.Cosmos.Gremlin.fsproj" />
<Project Path="src/Cosmos/FSharp.Azure.Cosmos.fsproj" />
<Project Path="src/Gremlin/FSharp.Gremlin.fsproj" />
<Project Path="tests/Cosmos.Gremlin.Tests/FSharp.Azure.Cosmos.Gremlin.Tests.fsproj" />
<Project Path="tests/Cosmos.Tests.Infrastructure/FSharp.Azure.Cosmos.Tests.Infrastructure.fsproj" />
<Project Path="tests/Cosmos.Tests/FSharp.Azure.Cosmos.Tests.fsproj" />
<Project Path="tests/Gremlin.Tests/FSharp.Gremlin.Tests.fsproj" />
</Solution>
23 changes: 23 additions & 0 deletions src/Cosmos.Gremlin/AssemblyInfo.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Auto-Generated by FAKE; do not edit
namespace System
open System.Reflection

[<assembly: AssemblyTitleAttribute("FSharp.Azure.Cosmos.Gremlin")>]
[<assembly: AssemblyProductAttribute("FSharp.Azure.Cosmos")>]
[<assembly: AssemblyVersionAttribute("1.2.0")>]
[<assembly: AssemblyMetadataAttribute("ReleaseDate","2026-10-01T00:00:00.0000000+02:00")>]
[<assembly: AssemblyFileVersionAttribute("1.2.0")>]
[<assembly: AssemblyInformationalVersionAttribute("1.2.0")>]
[<assembly: AssemblyMetadataAttribute("ReleaseChannel","release")>]
[<assembly: AssemblyMetadataAttribute("GitHash","210cf041a0ce44fcc5a5df369550a77a1f75d436")>]
do ()

module internal AssemblyVersionInformation =
let [<Literal>] AssemblyTitle = "FSharp.Azure.Cosmos.Gremlin"
let [<Literal>] AssemblyProduct = "FSharp.Azure.Cosmos"
let [<Literal>] AssemblyVersion = "1.2.0"
let [<Literal>] AssemblyMetadata_ReleaseDate = "2026-10-01T00:00:00.0000000+02:00"
let [<Literal>] AssemblyFileVersion = "1.2.0"
let [<Literal>] AssemblyInformationalVersion = "1.2.0"
let [<Literal>] AssemblyMetadata_ReleaseChannel = "release"
let [<Literal>] AssemblyMetadata_GitHash = "210cf041a0ce44fcc5a5df369550a77a1f75d436"
160 changes: 160 additions & 0 deletions src/Cosmos.Gremlin/CosmosGremlinConnection.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
namespace FSharp.Azure.Cosmos.Gremlin

open System
open System.Text.RegularExpressions

/// <summary>
/// The settings that connect a client to a graph of Azure Cosmos DB for Apache Gremlin: the Gremlin endpoint, the
/// database, the graph and the key of the account.
/// </summary>
/// <remarks>
/// <see cref="T:Microsoft.FSharp.Core.StructuredFormatDisplayAttribute"/> makes <c>ToString</c>, the format <c>%A</c>
/// and an interpolated string of the record give
/// <see cref="P:FSharp.Azure.Cosmos.Gremlin.CosmosGremlinConnection.Description"/>, which does not show
/// <see cref="P:FSharp.Azure.Cosmos.Gremlin.CosmosGremlinConnection.Key"/>. A structured logger that reads the public
/// properties of the record writes the key. Thus write
/// <see cref="P:FSharp.Azure.Cosmos.Gremlin.CosmosGremlinConnection.Description"/> to a log, not the record.
/// </remarks>
[<StructuredFormatDisplay("{Description}")>]
type CosmosGremlinConnection = {
/// <summary>
/// The host of the Gremlin endpoint, for example <c>myaccount.gremlin.cosmos.azure.com</c>.
/// </summary>
Host : string
/// The port of the Gremlin endpoint.
Port : int
/// <summary>
/// <see langword="true"/> to connect with TLS (<c>wss</c>), <see langword="false"/> to connect without TLS
/// (<c>ws</c>).
/// </summary>
UseSsl : bool
/// The id of the database.
Database : string
/// The id of the graph, which is a container of the database.
Graph : string
/// The primary or the secondary key of the account.
Key : string
} with

/// <summary>
/// The user name of the authentication: <c>/dbs/&lt;database&gt;/colls/&lt;graph&gt;</c>.
/// </summary>
member this.UserName = $"/dbs/{this.Database}/colls/{this.Graph}"

/// <summary>
/// The settings without <see cref="P:FSharp.Azure.Cosmos.Gremlin.CosmosGremlinConnection.Key"/>, for example
/// <c>wss://myaccount.gremlin.cosmos.azure.com:443 /dbs/db1/colls/graph1</c>.
/// </summary>
member this.Description =
let scheme = if this.UseSsl then "wss" else "ws"
$"{scheme}://{this.Host}:{this.Port} {this.UserName}"

/// <summary>
/// Creates a <see cref="T:FSharp.Azure.Cosmos.Gremlin.CosmosGremlinConnection"/> for an account in Azure or for the
/// emulator.
/// </summary>
[<RequireQualifiedAccess>]
module CosmosGremlinConnection =

/// The well-known key of the Azure Cosmos DB Emulator.
[<Literal>]
let EmulatorKey =
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="

/// The port of the Gremlin endpoint of an account in Azure.
[<Literal>]
let AzurePort = 443

/// <summary>
/// The default port of the Gremlin endpoint of the Windows emulator. The emulator opens it when it starts with
/// <c>/EnableGremlinEndpoint</c>.
/// </summary>
[<Literal>]
let EmulatorPort = 8901

// The rule of Azure for the name of a Cosmos DB account: 3 to 44 lowercase letters, numbers and hyphens, starting
// with a letter or a number. \z instead of $, because $ also matches before a final line feed.
let private accountName = Regex (@"\A[a-z0-9][a-z0-9-]{2,43}\z", RegexOptions.CultureInvariant)

// Cosmos DB does not allow these characters in the id of a database or a container. A slash would also change the
// path of the user name.
let private forbiddenIdCharacters = [| '/'; '\\'; '?'; '#' |]

[<Literal>]
let private MaxIdLength = 255

let private ensureId (parameterName : string) (id : string) =
if String.IsNullOrWhiteSpace id then
invalidArg parameterName "The id must not be empty."

if id.IndexOfAny forbiddenIdCharacters >= 0 then
invalidArg parameterName $"The id '{id}' must not contain '/', '\\', '?' or '#'."

// Data Explorer refuses an id that ends in a space (ValidCosmosDbIdDescription in src/Utils/ValidationUtils.ts of
// Azure/cosmos-explorer). The id is not trimmed, because then the settings would name another resource.
if id.EndsWith (" ", StringComparison.Ordinal) then
invalidArg parameterName $"The id '{id}' must not end with a space."

if id.Length > MaxIdLength then
invalidArg parameterName $"The id must have {MaxIdLength} characters or less, but it has {id.Length}."

/// <summary>
/// Creates the settings of the graph <paramref name="graph"/> in the database <paramref name="database"/> of the
/// account <paramref name="account"/> in Azure: the host <c>&lt;account&gt;.gremlin.cosmos.azure.com</c>, the port
/// <see cref="AzurePort"/> and TLS.
/// </summary>
/// <param name="account">The name of the account, without the domain, for example <c>myaccount</c>.</param>
/// <param name="database">The id of the database.</param>
/// <param name="graph">The id of the graph.</param>
/// <param name="key">The primary or the secondary key of the account.</param>
/// <exception cref="ArgumentException">
/// <paramref name="account"/> is not a name of a Cosmos DB account, <paramref name="database"/> or
/// <paramref name="graph"/> is not an id of a resource, or <paramref name="key"/> is empty.
/// </exception>
let azure (account : string) (database : string) (graph : string) (key : string) : CosmosGremlinConnection =
// A caller in C# can give null, and Regex.IsMatch throws for null
if
String.IsNullOrEmpty account
|| not (accountName.IsMatch account)
then
invalidArg
(nameof account)
$"'{account}' is not the name of a Cosmos DB account: 3 to 44 lowercase letters, numbers and hyphens, starting with a letter or a number."

ensureId (nameof database) database
ensureId (nameof graph) graph

if String.IsNullOrWhiteSpace key then
invalidArg (nameof key) "The key must not be empty."

{
Host = $"{account}.gremlin.cosmos.azure.com"
Port = AzurePort
UseSsl = true
Database = database
Graph = graph
Key = key
}

/// <summary>
/// Creates the settings of the graph <paramref name="graph"/> in the database <paramref name="database"/> of the
/// local emulator: the host <c>localhost</c>, the port <see cref="EmulatorPort"/> without TLS, and
/// <see cref="EmulatorKey"/>. The Gremlin endpoint of the emulator does not accept TLS.
/// </summary>
/// <param name="database">The id of the database.</param>
/// <param name="graph">The id of the graph.</param>
/// <exception cref="ArgumentException">
/// <paramref name="database"/> or <paramref name="graph"/> is not an id of a resource.
/// </exception>
let emulator (database : string) (graph : string) : CosmosGremlinConnection =
ensureId (nameof database) database
ensureId (nameof graph) graph

{
Host = "localhost"
Port = EmulatorPort
UseSsl = false
Database = database
Graph = graph
Key = EmulatorKey
}
45 changes: 45 additions & 0 deletions src/Cosmos.Gremlin/FSharp.Azure.Cosmos.Gremlin.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">

<!--
Azure Cosmos DB for Apache Gremlin on top of FSharp.Gremlin. It holds the connection settings, and the next steps
add the dialect of Cosmos DB, its status attributes and the results of the errors that a caller handles. The
library is built step by step. Until a query runs end to end, no package is made of it.
-->
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<AssemblyName>$(AssemblyBaseName).Gremlin</AssemblyName>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Deterministic>true</Deterministic>
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Label="NuGet">
<PackageId>FSharp.Azure.Cosmos.Gremlin</PackageId>
<Title>FSharp.Azure.Cosmos.Gremlin</Title>
<Summary>Typed Gremlin traversals over immutable F# records for Azure Cosmos DB for Apache Gremlin</Summary>
<Description>Connects FSharp.Gremlin to Azure Cosmos DB for Apache Gremlin</Description>
<PackageTags>F#;FSharp;Cosmos;CosmosDB;Cosmos DB;Gremlin;Graph;TinkerPop</PackageTags>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
<Optimize>true</Optimize>
<Tailcalls>true</Tailcalls>
</PropertyGroup>

<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="CosmosGremlinConnection.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Gremlin\FSharp.Gremlin.fsproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions src/Gremlin/AssemblyInfo.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Auto-Generated by FAKE; do not edit
namespace System
open System.Reflection

[<assembly: AssemblyTitleAttribute("FSharp.Gremlin")>]
[<assembly: AssemblyProductAttribute("FSharp.Azure.Cosmos")>]
[<assembly: AssemblyVersionAttribute("1.2.0")>]
[<assembly: AssemblyMetadataAttribute("ReleaseDate","2026-10-01T00:00:00.0000000+02:00")>]
[<assembly: AssemblyFileVersionAttribute("1.2.0")>]
[<assembly: AssemblyInformationalVersionAttribute("1.2.0")>]
[<assembly: AssemblyMetadataAttribute("ReleaseChannel","release")>]
[<assembly: AssemblyMetadataAttribute("GitHash","210cf041a0ce44fcc5a5df369550a77a1f75d436")>]
do ()

module internal AssemblyVersionInformation =
let [<Literal>] AssemblyTitle = "FSharp.Gremlin"
let [<Literal>] AssemblyProduct = "FSharp.Azure.Cosmos"
let [<Literal>] AssemblyVersion = "1.2.0"
let [<Literal>] AssemblyMetadata_ReleaseDate = "2026-10-01T00:00:00.0000000+02:00"
let [<Literal>] AssemblyFileVersion = "1.2.0"
let [<Literal>] AssemblyInformationalVersion = "1.2.0"
let [<Literal>] AssemblyMetadata_ReleaseChannel = "release"
let [<Literal>] AssemblyMetadata_GitHash = "210cf041a0ce44fcc5a5df369550a77a1f75d436"
29 changes: 29 additions & 0 deletions src/Gremlin/Attributes.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace FSharp.Gremlin

open System

/// <summary>
/// Sets the label of the vertices or the edges of a record type.
/// </summary>
/// <remarks>
/// Without this attribute, the label is the name of the type. ExRam.Gremlinq writes the same label. Thus a graph that
/// ExRam.Gremlinq wrote keeps its labels.
/// </remarks>
[<Sealed; AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Struct, AllowMultiple = false)>]
type LabelAttribute (label : string) =
inherit Attribute ()

/// The label of the vertices or the edges of the type.
member _.Label = label

/// <summary>
/// Marks the field of a vertex record that holds the partition key of the vertex.
/// </summary>
/// <remarks>
/// A graph of Azure Cosmos DB has one partition key property, which its container defines. The client writes that
/// property of a new vertex from the marked field. A record without this attribute uses the field whose name in the
/// graph is the name of the partition key property.
/// </remarks>
[<Sealed; AttributeUsage(AttributeTargets.Property, AllowMultiple = false)>]
type PartitionKeyAttribute () =
inherit Attribute ()
Loading
Loading