Discriminated Unions for Static Data

Open
#392 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
fsharp, sql
Domain
databases

Research direction

The issue names no files or tests; start by reading the existing Individuals feature and the query/type-provider paths it relies on. Compare those entry points with the manual ContentTypeId projection shown here; done means discriminated-union foreign-key IDs can be used directly without that mapping.

Written by the indexing model from the issue text.

Description

enhancement

Another question, but are there any plans for allowing discriminated unions to be used as the type for foreign key IDs to static data? I know entity framework supports this with enums but it would be handy to be able to use discriminated unions for obvious reasons:

type ContentTypeId =
  | Book
  | Magazine
  | Newspaper

...

let printData item =
  match item.ContentTypeId with
  | Book -> printfn "Found a book with id: %d" item.Id
  | Magazine -> printfn "Found a magazine with id: %d" item.Id
  | Newspaper -> printfn "Found a newspaper with id: %d" item.Id

It's possible to do this now by mapping the ID when projecting results:

let getContentTypeId = function
  | 1 -> Book
  | 2 -> Magazine
  | 3 -> Newpaper

...

query {
  for item in ctx.Dbo.Media do
  select
    { contentTypeId = getContentTypeId item.contentTypeId;
    }
}

This can get tedious and is pretty brittle, and an auto-generated solution would be preferable if possible. The infrastructure for the Individuals feature would probably be reusable to accomplish this?

Dominant language
F#
Stars
627
Forks
147
Avg merge
2h 2m
Merged PRs (30d)
1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from fsprojects/SQLProvider

All issues in fsprojects/SQLProvider

Similar issues

More Databases issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.