yesodweb / yesodweb/persistent

Support for parametrized type entity

Open
#942 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
486
Forks
306
PR merge metrics
No merged PRs in 30d

Description

It would be nice to be able declare parametrized entities using the entity dsl.

The use case for parametrized types (or type a la carte or Barbies) are numerous one is to allow validation.
A Type like

data Customer f = Customer 
   { firstname :: f Text
   , surname :: f Text
  }

allows for example to use Customer (Either Text) for validation, convert it to Customer Identity to save it.

Another use would be to allow to replace entity reference to the entity itself (using type family), so we could have something like

data Order cust = Order
{ date :: Day
, amount :: Double
, customer :: cust
}
You can then use either Order CustomerKey, Order Customer or Order (Entity Customer) etc ...

There are mainly two problems, one is to instanciate the different Persistent classes and the other is how alter the existing template syntax withouth complicationg the existing one.
Obviously not all version of Order cust can be a PersistEntity, but Order CustomerKey corresponds to what you can at the moment do using Persistent, so what I'm thinking is instead of doing something like this

Order
 date Day
 amount Double
customer CustomerId

allows

Order cust=CustomerId
  date Day
  amount Double
  customer cust

This would mean create Order with the parameter cust but do as it if was Order CustomerId, ie create the table as if customer was of type CustomerId (as in the current scenario) and create all instances for Customer CustomerId.

Of course, it would be nice to use functor, type families etc and also generates instance for more that one examples.

An alternative (which might be already possible) would be to declare Order cust manually and have a easy way to create required instances. For example it could be that we specify in the entity syntax file, don't create the data type leave for me (in the same way that you can specify the sql table we could do)

Order datatype="Order CustomerId"
   date Day
   amount Double
   customer customerId

This will have the advantage to work with type family, but the inconvenient that Order CustomerId needs to indeed have customer of the right type.

Another way would be to provide TH functions to help create tables and instances from an existing type.

I will be happy to give it a go (as it's something I've been missing) but obviously, I prefer to do something that people agree with and come up with a nice solution.

Contributor guide

Open the contributing guide

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.

Research direction

Start by reading the entity DSL and the code that generates Persistent data types and instances; the issue does not name specific files or tests. Compare the proposed entity parameter syntax with the alternative of manually declaring the datatype and using TH helpers. Done would require an agreed design that supports parameterized declarations and generates the required instances without complicating existing syntax.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.