yesod-filter: Automatic filter generator for Yesod

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Please see the README on GitHub at https://github.com/iij-ii/yesod-filter#readme


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.0, 0.1.0.1, 0.1.0.2
Change log ChangeLog.md
Dependencies base (>=4.7 && <5), persistent, template-haskell, text, time, yesod-core, yesod-persistent [details]
License BSD-3-Clause
Copyright IIJ Innovation Institute Inc.
Author Kenzo Yotsuya
Maintainer kyotsuya@iij-ii.co.jp
Category Web
Home page https://github.com/iij-ii/yesod-filter#readme
Source repo head: git clone https://github.com/iij-ii/yesod-filter
Uploaded by KenzoYotsuya at 2020-10-06T04:00:52Z

Modules

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for yesod-filter-0.1.0.0

[back to package description]

yesod-filter

Build Status

yesod-filter is a library that automatically generates Filter and SelectOpt from URL query string. yesod-filter is inspired by django-filter.

Usage

Example

{-
# Suppose the model is defined as follows:
Pet json
    name Text
    age Int
    deriving Eq
    deriving Show

# And the route is defined as follows:
/pets PetsR GET
-}

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell   #-}

module Handler.Pet where

import           Import
import           Yesod.Filter.TH

-- Define the query options to be available.
$(mkFilterGenerator "Pet" defaultOptions
    { filtering = defaultFiltering
        { filterDefs =
            [ FilterDef "name" defaultFilterParams
            , FilterDef "age"  defaultFilterParams
            ]
        }
    , sorting = defaultSorting
        { sortFields = ["name", "age"]
        , defaultOrdering = ORDERBY "id" ASC
        }
    }
 )

getPetsR :: Handler Value
getPetsR = do
    -- The list of Filter and SelectOpts are automatically converted from query parameters.
    filters' <- $(mkFilters)
    selectOpts <- $(mkSelectOpts)
    pets <- runDB $ selectList filters' selectOpts
    returnJson pets

The above handler definition generates the following endpoint.

# Without query strings
$ curl -s "http://localhost:3000/pets" | jq .
[
  {
    "age": 5,
    "name": "John",
    "id": 1
  },
  {
    "age": 3,
    "name": "Charlie",
    "id": 2
  },
  {
    "age": 10,
    "name": "Jack",
    "id": 3
  }
]

# Filter: WHERE AGE >= 3
$ curl -s "http://localhost:3000/pets?age__gt=3" | jq .
[
  {
    "age": 5,
    "name": "John",
    "id": 1
  },
  {
    "age": 10,
    "name": "Jack",
    "id": 3
  }
]

# SelectOpt: ORDER BY name
$ curl -s "http://localhost:3000/pets?sort=name" | jq .
[
  {
    "age": 3,
    "name": "Charlie",
    "id": 2
  },
  {
    "age": 10,
    "name": "Jack",
    "id": 3
  },
  {
    "age": 5,
    "name": "John",
    "id": 1
  }
]

LICENCE

Copyright (c) IIJ Innovation Institute Inc.

Licensed under The 3-Clause BSD License.