Dzoukr / Dapper.FSharp

Lightweight F# extension for StackOverflow Dapper with support for MSSQL, MySQL, PostgreSQL, and SQLite
MIT License
365 stars 35 forks source link

Record attribute to use capitalized word for record key on F# but lowercase for database #97

Closed 64J0 closed 9 months ago

64J0 commented 9 months ago

Description

Hello, hope you're good.

I noticed from the docs that we must create the F# record representing the table using for the keys the same values that we used when creating the columns at the database.

It uses reflection to get record properties. So yes, there is one (the only) simple rule: All property names must match columns in the table.

Is there a way (maybe using some attribute) to change this?

I'd like to use capitalized words for the record keys on F#, but when dealing with the database, I'd like it to be converted to lowercase.


Example:

CREATE TABLE [dbo].[persons](
    [id] [uniqueidentifier] NOT NULL,
    [name] [nvarchar](max) NOT NULL,
    [position] [int] NOT NULL,
    [date_of_birth] [datetime] NULL)

Then,

type Person = {
    Id : Guid
    Name : string
    Position : int
    DateOfBirth : DateTime option
}
Dzoukr commented 9 months ago

Hi @64J0,

I hope everything is good on your side too.

Well... this is one of the essential requirements from the v1 and my opinion on this is stable. Adding support for Attributes is tempting, but it could open a river of "just one more attribute to make it more fine-tunable" requests which may completely change its simplicity.

I fully understand your struggle here, but since the data layer should be sort of a standalone (isolated) part, I would just live with the fact that DB records start with lowercase (which could help spot them easily in code). After all, you will map them to Domain/Business layer records anyway, so it's something one can survive.

64J0 commented 9 months ago

Got it, thanks for the reply @Dzoukr.

I'm moving forward with your suggestion.