yesodweb / yesodweb/persistent

[Feature request] Support custom FromJSON and ToJSON for entity keys?

Open
#795 5 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

Hello.

I use Yesod + Persistent to build my application, and I'm planning to use hashids for primary key representations, since I've thought it would be nice if the traditional integer ids would be hidden from users.

I also tried to make entity keys (such as Key User) an instance of FromJSON and ToJSON, which encodes to / decodes from hashid representations (i.e. String constructor of aeson's Value type), but I realized that Database.Persist.TH made them instances of FromJSON and ToJSON type, whose values are treated as Number.

I read the haddock document, but I couldn't find the way to provide custom instance declaration for them.
(I've found mpsEntityJSON, which allow us to work with Entity a, but what I've wanted is the one for Key a.)

Thanks.

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 Database.Persist.TH Haddock and the generated FromJSON and ToJSON instances for entity keys. Compare that behavior with mpsEntityJSON and determine how custom JSON handling for Key a could be exposed. Done means users can provide hashid-style String encoding and decoding for entity keys without the default Number representation.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
database
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.