网络科技

    今日:555| 主题:244643
收藏本版
互联网、科技极客的综合动态。

[其他] Using UUIDs in Yesod

[复制链接]
最溫柔最妖娆 发表于 2016-10-7 16:31:22
81 3

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
This is a post by    Jezen Thomas, a freelance software developer spending most of his time writing software with Ruby and JavaScript. It was originally published on his blog. Follow him    on Twitter.  
  Using UUIDs in Yesod

  I’ve recently been tinkering with a small web application written in Haskell with the Yesod framework. I decided I would like to use Universally Unique Identifiers for one my models, as the identifier for the model would be exposed to the user.
  There are other reasons to use UUIDs, such as    decentralising ID generation, but simply not giving hints as to the size of one of my database tables is enough of a reason for me.  
  Related:Are you a front-end developer or a web designer?    You’ll love this tool  
  Researching how to do this in Yesod proved difficult. I found    this postby Michael Xavier, and as he describes:    “all I found were vague, closed tickets, and old irrelevant blog posts on the Yesod site that mentioned UUIDs but didn’t give any good examples.”  
  Michael proceeds by writing a guide to using UUIDs in his post, but he uses    Control.Lens, which strikes me as a little advanced for the task at hand. Furthermore, I couldn’t actually get his example to work.  
            Using UUIDs in Yesod #haskell #yesod      
  Luckily, I managed to make it work using far less machinery, and here’s how, using a    Blogmodel as an example:  
  First we need to add our    uuidfield and a uniqueness constraint on that field so we know our UUIDs will be unique and lookups will be fast. The fields defined in our model will correspond with fields in our applicative form.  
  1. -- config/models

  2. Blog
  3.   title Text
  4.   content Text
  5.   user UserId
  6.   createdAt UTCTime
  7.   uuid Text default=uuid_generate_v4()
  8.   UniqueUuid uuid
复制代码
Then in our applicative form, we import two modules that give us the UUID functions we need, and extract a value in either a monadic or applicative style.
  1. -- Handler/Home.hs

  2. import Data.UUID
  3. import Data.UUID.V4

  4. form :: UserId -> Form Blog
  5. form userId = renderDivs $ Blog
  6.   <$> areq textField "Title" Nothing
  7.   <*> areq textareaField "Contents" Nothing
  8.   <*> pure userId
  9.   <*> lift (liftIO getCurrentTime)
  10.   -- either use the following line (monadic style)
  11.   <*> lift (liftIO $ do { key <- nextRandom; return $ toText key })
  12.   -- ...or use this style (applicative style)
  13.   <*> lift (fmap toText $ liftIO nextRandom)
复制代码
           Building small web applications written in #Haskell with Yesod #webdev      
  I asked a helpful Haskeller in an IRC channel what the difference between the two approaches was, and this was their reply:
  Somehow I personally like the Monad version more… But it is syntactically too expensive, so in this case I would probably go with the    fmapversion… but if I would have to do more processing on the UUID, I would definitely go the Monad version… but IO is a Monad type class so it has to implement also Functor type class… You can think that using an    fmapis more “low level”.  
  Are there downsides to my approach? Well, I’m not making use of Postgresql’s UUID type, instead using a simple    Text. I’m not sure yet how to do that with the Persistent library, but it’s probably not a problem worth solving at this point.  
  Happy Haskelling.
友荐云推荐




上一篇:漂亮的垂直导航样式
下一篇:740亿美元!今年风投没降温,投资额或超2015年
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

笑蓝 发表于 2016-10-8 16:48:21
和我比温柔那是找抽.
回复 支持 反对

使用道具 举报

LUN 发表于 2016-10-9 11:43:10
在神经的人群里呆久了,我发现我正常了。
回复 支持 反对

使用道具 举报

盼丹 发表于 2016-11-16 10:00:55
看完回帖,THX
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表