[web-devel] (Yesod) PersistEntity Key types

Michael Snoyman michael at snoyman.com
Sun Apr 3 16:35:05 CEST 2011


As a short term solution, I think you're right. However, I think we might
want to consider a slightly more sophisticated approach... I say consider
specifically, because I'm really not certain that what I'm saying is a good
idea.

I think there's basically three problems with the approach of using a
PersistValue inside a PersistKey:

1) It will probably hurt performance, since we'll need to do more checking.
2) The code becomes a bit more fragile. By adding this sum type to the mix,
we're adding necessity for a number of checks that can fail.
3) There's nothing stopping you from inserting a value into one database
(say, SQLite), getting a key, and then looking up in MongoDB. (Not that this
is a flaw in the current approach as well.)

So here's the idea: each database backend will have an associated type for
its key datatype. Then, instead of having:

    data Key entity = Int64

we'll have

    data Key entity backend = BackendKey backend

(ignoring all the newtype wrappers). I think this should solve both the
issue you raise about MongoDB, and the three points I mention above.
However, I'm still concerned that it might lead to difficult-to-follow code.
There's really only one way to find out, but I just wanted to bounce the
idea around before diving in.

Michael

On Sun, Apr 3, 2011 at 4:45 PM, Rick Richardson
<rick.richardson at gmail.com>wrote:

> I think I have a simple solution.  I haven't looked at all of the places
> that  *PersistKey is used, but it appears that we can simply make persistKey
> a PersistValue, and get rid of the toPersistKey and fromPersistKey
> functions.
>
> I am only half-way through my coffee this morning, but it seems like this
> could solve all of our problems.  Am I off in left field here?
>
>
>
> On Sat, Apr 2, 2011 at 6:57 PM, Rick Richardson <rick.richardson at gmail.com
> > wrote:
>
>> I ran into another snag getting the tests running for MongoDB backend.
>>  The PersistEntity class, and, it appears, the TH is hardcoded to set the
>> key type as Int64.
>>
>> This is a problem since the key type in MongoDB is a 12 byte blob.
>>
>> The other problem is that it looks like PersistBackends are pretty much
>> hardcoded to that type as well.  I guess the correct approach would be to
>> allow the designation in mkPersist and then ensure that the PersistBackends
>> can parametrically deal with differing  Key types.  Or, in the case of
>> MongoDB, hardcode it to a 12 byte blob, because other key types (ObjectId's)
>> rarely make sense.
>>
>>
>> Thoughts?
>>
>>
>>
>
> _______________________________________________
> web-devel mailing list
> web-devel at haskell.org
> http://www.haskell.org/mailman/listinfo/web-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/web-devel/attachments/20110403/bdd95b0a/attachment.htm>


More information about the web-devel mailing list