JasperFx / marten

.NET Transactional Document DB and Event Store on PostgreSQL
https://martendb.io
MIT License
2.74k stars 428 forks source link

Add support for Querying F# discriminated union properties #3286

Open nkosi23 opened 1 week ago

nkosi23 commented 1 week ago

This issue is to track the work on this feature discussed on discord that I am currently implementing. The goal is to make it possible to query based on the case of an F# discriminated union property. For example given the following types:

type City = { Name: string; ZipCode: string }

type Country = { Name: string; Code: string; }

type Regions =
    | Country of Country
    | City of City
    | Continent of Country list

type Survey = { Id: Guid; Topic: string; Target: Regions }

Which are persisted to the following json:

{
    "Id: "99bc3539-9928-4abe-9ea9-1bd86de7eff6",
    "Topic": "Music preferences",
    "Target" : {
        "Case": "City",
        "Fields": {
            "Item": {
              "Name": "Charlotte",
              "Code": "28500"
            }
        }
    }
}

The goal is to add a Query API of the form .WhereDiscriminatedUnionHas<TDocument, TUnion, TCase>(Func<TDocument, TUnion> lambda, Func<TCase, bool>) like the below:

Query<Survey>().WhereDiscriminatedUnionHas<Survey, Regions, Country>(x => x.Target, x => x.Code == "US")
Query<Survey>().WhereDiscriminatedUnionIs<Survey, Country, Regions>(x => x.Target)

Items to deliver

Todo